12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <dmmlib/initialize_allocator.h>
- #include <dmmlib/dmmlib.h>
- #include <dmm_config.h>
- #define ALLOCATOR_SIZE 16*1024*sizeof(char)
- #define cmalloc(size) custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) size)
- #define cfree(ptr) custom_ahfree(&systemallocator, &systemallocator.heaps[0], ptr)
- 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 is a block can be reused if free'd before. */
- printf("Test 1: Check is a block can be reused if free'd before.\n");
- p1 = cmalloc(1024);
- cfree(p1);
- /* Check if the block is taken back */
- p2 = cmalloc(1024);
- assert( p2 == p1 );
- cfree(p2);
- printf("Test 1: Success.\n");
- /* Test 2: Check if best fit on LIFO works */
- printf("Test 2: Check if best fit on LIFO works.\n");
- /* Allocate two memory blocks of 1024 and 432 bytes */
- p1 = cmalloc(1024);
- p2 = cmalloc(432);
- /* Make them free */
- cfree(p2);
- cfree(p1);
- /* Pray that the following malloc is going to use the second block of the
- * free list.
- */
- p3 = cmalloc(432);
- assert( p3 == p2 );
- cfree(p3);
- printf("Test 2: Success.\n");
- /* Test 3: Check if splitting works
- *
- * Note: Splitting is checked for fixed-sized splitting or the
- * initial value of variable-sized splitting.
- */
- #ifdef MIN_SPLITTING_SIZE
- printf("Test 3: Check if splitting works.\n");
- /* Reset everything */
- initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
- if(1000 - MIN_SPLITTING_SIZE > 0 && MIN_SPLITTING_SIZE - 50 > 0) {
- p1 = cmalloc(1000);
- cfree(p1);
- /* This would go to the first block and split it */
- p1 = cmalloc(1000 - MIN_SPLITTING_SIZE - 50); /* 50 to compansate safely the
- header size */
- /* This goes to the third block */
- p2 = cmalloc(432);
- /* Check if the split free block is used */
- p3 = cmalloc(MIN_SPLITTING_SIZE - 50); /* 50 to compansate safely the
- header size */
- assert( p3 > p1 ); /* Small sanity check */
- assert( p3 < p2 );
- printf("Test 3: Success.\n");
- } else {
- printf("Please check maximum splitting size and try to fit it between 50 and 1000 bytes.\n");
- }
- #endif /* MIN_SPLITTING_SIZE */
- free(block_from_malloc);
- return 0;
- }
|