#include #include #include #include #include #define ALLOCATOR_SIZE 16*1024*sizeof(char) int main(void) { allocator_t systemallocator; void *block_from_malloc; void *p1, *p2, *p3; 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); 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); /* 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); return 0; }