test_for_memory_space_aware.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <dmmlib/initialize_allocator.h>
  5. #include <dmmlib/dmmlib.h>
  6. #include <dmmlib/config.h>
  7. #define ALLOCATOR_SIZE 2*MAX_COAL_SIZE*sizeof(char)
  8. #define cmalloc(size) custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) size)
  9. #define cfree(ptr) custom_ahfree(&systemallocator, &systemallocator.heaps[0], ptr)
  10. #ifdef WITH_REALLOC
  11. #define crealloc(ptr, size) custom_ahrealloc(&systemallocator, &systemallocator.heaps[0], ptr, size)
  12. #endif /* WITH_REALLOC */
  13. int main(void) {
  14. allocator_t systemallocator;
  15. void *block_from_malloc;
  16. void *p1, *p2, *p3;
  17. block_from_malloc = malloc(ALLOCATOR_SIZE);
  18. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  19. /* Testing a best-fit, w/o fixed lists allocator */
  20. /* Test 1: Check is a block can be reused if free'd before. */
  21. printf("Test 1: Check is a block can be reused if free'd before.\n");
  22. p1 = cmalloc(1024);
  23. cfree(p1);
  24. /* Check if the block is taken back */
  25. p2 = cmalloc(1024);
  26. assert( p2 == p1 );
  27. cfree(p2);
  28. printf("Test 1: Success.\n");
  29. /* Test 2: Check if best fit on LIFO works */
  30. printf("Test 2: Check if best fit on LIFO works.\n");
  31. /* Allocate two memory blocks of 1024 and 432 bytes */
  32. p1 = cmalloc(MAX_COAL_SIZE);
  33. p2 = cmalloc(432);
  34. /* Make them free */
  35. cfree(p2);
  36. cfree(p1);
  37. /* Pray that the following malloc is going to use the second block of the
  38. * free list.
  39. */
  40. p3 = cmalloc(432);
  41. assert( p3 == p2 );
  42. cfree(p3);
  43. printf("Test 2: Success.\n");
  44. /* Test 3: Check if splitting works
  45. *
  46. * Note: Splitting is checked for fixed-sized splitting or the
  47. * initial value of variable-sized splitting.
  48. */
  49. #ifdef MIN_SPLIT_SIZE
  50. printf("Test 3: Check if splitting works.\n");
  51. /* Reset everything */
  52. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  53. if(1000 - MIN_SPLIT_SIZE > 0 && MIN_SPLIT_SIZE - 50 > 0) {
  54. p1 = cmalloc(1000);
  55. cfree(p1);
  56. /* This would go to the first block and split it */
  57. p1 = cmalloc(1000 - MIN_SPLIT_SIZE - 50); /* 50 to compansate safely the
  58. header size */
  59. /* This goes to the third block */
  60. p2 = cmalloc(432);
  61. /* Check if the split free block is used */
  62. p3 = cmalloc(MIN_SPLIT_SIZE - 50); /* 50 to compansate safely the
  63. header size */
  64. assert( p3 > p1 ); /* Small sanity check */
  65. assert( p3 < p2 );
  66. printf("Test 3: Success.\n");
  67. } else {
  68. printf("Please check maximum splitting size and try to fit it between 50 and 1000 bytes.\n");
  69. }
  70. #endif /* MIN_SPLIT_SIZE */
  71. #ifdef WITH_REALLOC
  72. /* Testing realloc() */
  73. printf("Testing realloc()...\n");
  74. /* Reset everything */
  75. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  76. /* realloc() acts as malloc() if given pointer is null */
  77. p1 = crealloc(NULL, 1000);
  78. assert( p1 != NULL );
  79. /* The new size is smaller than the original */
  80. p2 = crealloc(p1, 50);
  81. assert( p2 == p1 );
  82. /* Original size smaller than newer, non-free next block */
  83. /* Reset everything */
  84. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  85. p1 = cmalloc(100);
  86. p2 = cmalloc(100);
  87. p3 = crealloc(p1, 1000);
  88. assert( p3 > p2 );
  89. /* Original size smaller than newer, free next block */
  90. /* Reset everything */
  91. initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
  92. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  93. p1 = cmalloc(MIN_SPLIT_SIZE);
  94. p2 = cmalloc(2*MIN_SPLIT_SIZE);
  95. cfree(p2);
  96. p3 = crealloc(p1, 2*MIN_SPLIT_SIZE);
  97. p2 = cmalloc(MIN_SPLIT_SIZE);
  98. /* FIXME 24 is the current size of the block header */
  99. assert( (char *) p2 == (char *) p1 + 24 + 2 * MIN_SPLIT_SIZE);
  100. #else /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  101. p1 = cmalloc(100);
  102. p2 = cmalloc(100);
  103. cfree(p2);
  104. p3 = crealloc(p1, 200);
  105. #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  106. assert( p3 == p1 );
  107. printf("Passed.\n");
  108. #endif /* WITH_REALLOC */
  109. free(block_from_malloc);
  110. return 0;
  111. }