|
@@ -1,5 +1,6 @@
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
+#include <assert.h>
|
|
|
#include <dmmlib/initialize_allocator.h>
|
|
|
#include <dmmlib/dmmlib.h>
|
|
|
|
|
@@ -14,11 +15,26 @@ int main(void) {
|
|
|
block_from_malloc = malloc(ALLOCATOR_SIZE);
|
|
|
initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
|
|
|
|
|
|
+ /* Testing a best-fit, w/o fixed lists allocator */
|
|
|
+
|
|
|
+ /* Test 1: Check if the block is taken back */
|
|
|
p1 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
|
|
|
- p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 512);
|
|
|
custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
|
|
|
+ /* Check if the block is taken back */
|
|
|
+ p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1000);
|
|
|
+ assert( p2 == p1 );
|
|
|
custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
|
|
|
- p3 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 948);
|
|
|
+
|
|
|
+ /* Test 2: Check if best fit on LIFO works */
|
|
|
+ /* Allocate two memory blocks of 1024 and 432 bytes */
|
|
|
+ p1 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
|
|
|
+ p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 432);
|
|
|
+ /* Make them free */
|
|
|
+ custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
|
|
|
+ custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
|
|
|
+ /* Pray that the 423-byte one is going to be used by a 300-byte malloc */
|
|
|
+ p3 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 300);
|
|
|
+ assert( p3 == p2 );
|
|
|
custom_ahfree(&systemallocator, &systemallocator.heaps[0], p3);
|
|
|
|
|
|
free(block_from_malloc);
|