소스 검색

More specialized tests on space-aware allocators.

Ioannis Koutras 13 년 전
부모
커밋
0533faeb5e
1개의 변경된 파일50개의 추가작업 그리고 12개의 파일을 삭제
  1. 50 12
      examples/test_for_memory_space_aware.c

+ 50 - 12
examples/test_for_memory_space_aware.c

@@ -3,9 +3,13 @@
 #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;
@@ -17,25 +21,59 @@ int main(void) {
 
     /* 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);
+    /* 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 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1000);
+    p2 = cmalloc(1024);
     assert( p2 == p1 );
-    custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
+    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 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
-    p2 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 432);
+    p1 = cmalloc(1024);
+    p2 = cmalloc(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);
+    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 );
-    custom_ahfree(&systemallocator, &systemallocator.heaps[0], p3);
+    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);