Explorar el Código

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

Ioannis Koutras hace 14 años
padre
commit
af5da76194
Se han modificado 3 ficheros con 18 adiciones y 4 borrados
  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 "custom_free.h"
 #include "other.h"
 #include "other.h"
 #include "posix_lock.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);
 			set_next(ptr, current_maptable_node->fixed_list_head);
 			current_maptable_node->fixed_list_head = ptr;
 			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
 	} else { // put it in the free list
 		set_next(ptr, heap->free_list_head);
 		set_next(ptr, heap->free_list_head);
 		heap->free_list_head = ptr;
 		heap->free_list_head = ptr;
+		printf("Block of size %d was free'd to free list.\n", size);
 	}
 	}
 
 
 	posix_unlock(heap);
 	posix_unlock(heap);

+ 11 - 0
custom_malloc.c

@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include "custom_malloc.h"
 #include "custom_malloc.h"
 #include "posix_lock.h"
 #include "posix_lock.h"
 #include "other.h"
 #include "other.h"
@@ -11,13 +12,17 @@ void * custom_malloc(heap_t* heap, size_t size) {
 	void *current_block, *previous_block;
 	void *current_block, *previous_block;
 
 
 	ptr = NULL;
 	ptr = NULL;
+	previous_block = NULL;
 
 
 	posix_lock(heap);
 	posix_lock(heap);
 
 
+	printf("Searching for a free block of size %d.\n", size);
+
 	fixed_list_id = map_size_to_list(heap, size);
 	fixed_list_id = map_size_to_list(heap, size);
 	
 	
 	// If a fixed list is found, do a first fit
 	// If a fixed list is found, do a first fit
 	if(fixed_list_id != -1) {
 	if(fixed_list_id != -1) {
+		printf("Searching on fixed lists...\n");
 		current_maptable_node = heap->maptable_head;
 		current_maptable_node = heap->maptable_head;
 		// traverse through the maptable node list
 		// traverse through the maptable node list
 		if(fixed_list_id != 0) {
 		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;
 			ptr = current_maptable_node->fixed_list_head;
 			current_maptable_node->fixed_list_head = get_next(ptr);
 			current_maptable_node->fixed_list_head = get_next(ptr);
 			set_requested_size(ptr, size);
 			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;
 		found = 0;
 
 
 		// first fit from free list
 		// 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)) {
 		for(current_block = heap->free_list_head; current_block != NULL; current_block = get_next(current_block)) {
 			if(get_size(current_block) >= size) {
 			if(get_size(current_block) >= size) {
 				ptr = current_block;
 				ptr = current_block;
@@ -48,13 +56,16 @@ void * custom_malloc(heap_t* heap, size_t size) {
 					heap->free_list_head = get_next(ptr);
 					heap->free_list_head = get_next(ptr);
 				}
 				}
 				posix_unlock(heap);
 				posix_unlock(heap);
+				printf("Block found in the free list.\n");
 				return ptr;
 				return ptr;
 			}
 			}
 			previous_block = current_block;
 			previous_block = current_block;
 		}
 		}
 
 
 		if(!found) {
 		if(!found) {
+			printf("No free available chunk was found.\n");
 			ptr = sys_alloc(heap, size);
 			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();
 	heap_id = map_thread_heap();
 	printf("This thread accesses heap %d\n", heap_id);
 	printf("This thread accesses heap %d\n", heap_id);
 	myheap = &myallocator->heaps[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, p1);
-	custom_free(myheap, p3);
+	p2 = custom_malloc(myheap, (size_t) 2855);
 	custom_free(myheap, p2);
 	custom_free(myheap, p2);
+	p3 = custom_malloc(myheap, (size_t) 3018);
+	custom_free(myheap, p3);
 }
 }