Преглед изворни кода

Debugging printf's (lame) and changed the size of requested blocks in test.c to match them with Larson's benchmark.

Ioannis Koutras пре 14 година
родитељ
комит
af5da76194
3 измењених фајлова са 18 додато и 4 уклоњено
  1. 3 0
      custom_free.c
  2. 11 0
      custom_malloc.c
  3. 4 4
      test.c

+ 3 - 0
custom_free.c

@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include "custom_free.h"
 #include "other.h"
 #include "posix_lock.h"
@@ -25,9 +26,11 @@ void custom_free(heap_t* heap, void *ptr) {
 			set_next(ptr, current_maptable_node->fixed_list_head);
 			current_maptable_node->fixed_list_head = ptr;
 		}
+		printf("Block of size %d was free'd to a fixed list.\n", size);
 	} else { // put it in the free list
 		set_next(ptr, heap->free_list_head);
 		heap->free_list_head = ptr;
+		printf("Block of size %d was free'd to free list.\n", size);
 	}
 
 	posix_unlock(heap);

+ 11 - 0
custom_malloc.c

@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include "custom_malloc.h"
 #include "posix_lock.h"
 #include "other.h"
@@ -11,13 +12,17 @@ void * custom_malloc(heap_t* heap, size_t size) {
 	void *current_block, *previous_block;
 
 	ptr = NULL;
+	previous_block = NULL;
 
 	posix_lock(heap);
 
+	printf("Searching for a free block of size %d.\n", size);
+
 	fixed_list_id = map_size_to_list(heap, size);
 	
 	// If a fixed list is found, do a first fit
 	if(fixed_list_id != -1) {
+		printf("Searching on fixed lists...\n");
 		current_maptable_node = heap->maptable_head;
 		// traverse through the maptable node list
 		if(fixed_list_id != 0) {
@@ -29,6 +34,8 @@ void * custom_malloc(heap_t* heap, size_t size) {
 			ptr = current_maptable_node->fixed_list_head;
 			current_maptable_node->fixed_list_head = get_next(ptr);
 			set_requested_size(ptr, size);
+			set_next(ptr, heap->used_blocks_head);
+		 	heap->used_blocks_head = ptr;
 		}
 	}
 
@@ -36,6 +43,7 @@ void * custom_malloc(heap_t* heap, size_t size) {
 		found = 0;
 
 		// first fit from free list
+		printf("Searching on the free list...\n", size);
 		for(current_block = heap->free_list_head; current_block != NULL; current_block = get_next(current_block)) {
 			if(get_size(current_block) >= size) {
 				ptr = current_block;
@@ -48,13 +56,16 @@ void * custom_malloc(heap_t* heap, size_t size) {
 					heap->free_list_head = get_next(ptr);
 				}
 				posix_unlock(heap);
+				printf("Block found in the free list.\n");
 				return ptr;
 			}
 			previous_block = current_block;
 		}
 
 		if(!found) {
+			printf("No free available chunk was found.\n");
 			ptr = sys_alloc(heap, size);
+			printf("Chuck was given by the OS.\n");
 		}
 
 	}

+ 4 - 4
test.c

@@ -15,10 +15,10 @@ int main(void) {
 	heap_id = map_thread_heap();
 	printf("This thread accesses heap %d\n", heap_id);
 	myheap = &myallocator->heaps[heap_id];
-	p1 = custom_malloc(myheap, (size_t) 3000);
-	p2 = custom_malloc(myheap, (size_t) 3000);
-	p3 = custom_malloc(myheap, (size_t) 3000);
+	p1 = custom_malloc(myheap, (size_t) 1024);
 	custom_free(myheap, p1);
-	custom_free(myheap, p3);
+	p2 = custom_malloc(myheap, (size_t) 2855);
 	custom_free(myheap, p2);
+	p3 = custom_malloc(myheap, (size_t) 3018);
+	custom_free(myheap, p3);
 }