|
@@ -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) {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
- p1 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 1024);
|
|
|
- custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
|
|
|
+
|
|
|
+ printf("Test 1: Check is a block can be reused if free'd before.\n");
|
|
|
+ p1 = cmalloc(1024);
|
|
|
+ cfree(p1);
|
|
|
|
|
|
- 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");
|
|
|
|
|
|
|
|
|
+ printf("Test 2: Check if best fit on LIFO works.\n");
|
|
|
|
|
|
- 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);
|
|
|
|
|
|
- custom_ahfree(&systemallocator, &systemallocator.heaps[0], p2);
|
|
|
- custom_ahfree(&systemallocator, &systemallocator.heaps[0], p1);
|
|
|
-
|
|
|
- p3 = custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) 300);
|
|
|
+ cfree(p2);
|
|
|
+ cfree(p1);
|
|
|
+
|
|
|
+ * free list.
|
|
|
+ */
|
|
|
+ p3 = cmalloc(432);
|
|
|
assert( p3 == p2 );
|
|
|
- custom_ahfree(&systemallocator, &systemallocator.heaps[0], p3);
|
|
|
+ cfree(p3);
|
|
|
+ printf("Test 2: Success.\n");
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * 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");
|
|
|
+
|
|
|
+ initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
|
|
|
+ if(1000 - MIN_SPLITTING_SIZE > 0 && MIN_SPLITTING_SIZE - 50 > 0) {
|
|
|
+ p1 = cmalloc(1000);
|
|
|
+ cfree(p1);
|
|
|
+
|
|
|
+ p1 = cmalloc(1000 - MIN_SPLITTING_SIZE - 50);
|
|
|
+ header size */
|
|
|
+
|
|
|
+ p2 = cmalloc(432);
|
|
|
+
|
|
|
+ p3 = cmalloc(MIN_SPLITTING_SIZE - 50);
|
|
|
+ header size */
|
|
|
+ assert( p3 > p1 );
|
|
|
+ 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
|
|
|
|
|
|
free(block_from_malloc);
|
|
|
|