test_for_memory_space_aware.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <dmmlib/initialize_allocator.h>
  5. #include <dmmlib/dmmlib.h>
  6. #define ALLOCATOR_SIZE 16*1024*sizeof(char)
  7. int main(void) {
  8. allocator_t systemallocator;
  9. void *block_from_malloc;
  10. void *p1, *p2, *p3;
  11. block_from_malloc = malloc(ALLOCATOR_SIZE);
  12. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  13. /* Testing a best-fit, w/o fixed lists allocator */
  14. /* Test 1: Check if the block is taken back */
  15. p1 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
  16. custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
  17. /* Check if the block is taken back */
  18. p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1000);
  19. assert( p2 == p1 );
  20. custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
  21. /* Test 2: Check if best fit on LIFO works */
  22. /* Allocate two memory blocks of 1024 and 432 bytes */
  23. p1 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
  24. p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 432);
  25. /* Make them free */
  26. custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
  27. custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
  28. /* Pray that the 423-byte one is going to be used by a 300-byte malloc */
  29. p3 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 300);
  30. assert( p3 == p2 );
  31. custom_ahfree(&systemallocator, &systemallocator.heaps[0], p3);
  32. free(block_from_malloc);
  33. return 0;
  34. }