Browse Source

Test for realloc() cases.

Ioannis Koutras 13 years ago
parent
commit
96fc3acca3
1 changed files with 49 additions and 2 deletions
  1. 49 2
      examples/test_for_memory_space_aware.c

+ 49 - 2
examples/test_for_memory_space_aware.c

@@ -5,10 +5,13 @@
 #include <dmmlib/dmmlib.h>
 #include <dmm_config.h>
 
-#define ALLOCATOR_SIZE 16*1024*sizeof(char)
+#define ALLOCATOR_SIZE 2*MAX_COALESCE_SIZE*sizeof(char)
 
 #define cmalloc(size) custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) size)
 #define cfree(ptr) custom_ahfree(&systemallocator, &systemallocator.heaps[0], ptr)
+#ifdef WITH_REALLOC
+#define crealloc(ptr, size) custom_ahrealloc(&systemallocator, &systemallocator.heaps[0], ptr, size)
+#endif /* WITH_REALLOC */
 
 int main(void) {
 
@@ -34,7 +37,7 @@ int main(void) {
     /* 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);
+    p1 = cmalloc(MAX_COALESCE_SIZE);
     p2 = cmalloc(432);
     /* Make them free */
     cfree(p2);
@@ -75,6 +78,50 @@ int main(void) {
     }
 #endif /* MIN_SPLITTING_SIZE */
 
+#ifdef WITH_REALLOC
+    /* Testing realloc() */
+    printf("Testing realloc()...\n");
+    /* Reset everything */
+    initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
+
+    /* realloc() acts as malloc() if given pointer is null */
+    p1 = crealloc(NULL, 1000);
+    assert( p1 != NULL );
+
+    /* The new size is smaller than the original */
+    p2 = crealloc(p1, 50);
+    assert( p2 == p1 );
+
+    /* Original size smaller than newer, non-free next block */
+    /* Reset everything */
+    initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
+    p1 = cmalloc(100);
+    p2 = cmalloc(100);
+    p3 = crealloc(p1, 1000);
+    assert( p3 > p2 );
+
+    /* Original size smaller than newer, free next block */
+    /* Reset everything */
+    initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
+#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
+    p1 = cmalloc(MIN_SPLITTING_SIZE);
+    p2 = cmalloc(2*MIN_SPLITTING_SIZE);
+    cfree(p2);
+    p3 = crealloc(p1, 2*MIN_SPLITTING_SIZE);
+    p2 = cmalloc(MIN_SPLITTING_SIZE);
+    /* FIXME 24 is the current size of the block header */
+    assert( (char *) p2 == (char *) p1 + 24 + 2 * MIN_SPLITTING_SIZE);
+#else /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+    p1 = cmalloc(100);
+    p2 = cmalloc(100);
+    cfree(p2);
+    p3 = crealloc(p1, 200);
+#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+    assert( p3 == p1 );
+
+    printf("Passed.\n");
+#endif /* WITH_REALLOC */
+
     free(block_from_malloc);
 
     return 0;