浏览代码

Completed first fit.

Ioannis Koutras 13 年之前
父节点
当前提交
0dfb6538c0
共有 1 个文件被更改,包括 44 次插入9 次删除
  1. 44 9
      test.c

+ 44 - 9
test.c

@@ -6,26 +6,34 @@
 #include "dmm_init.h"
 
 /*
-void *first_fit_search(unsigned int size, NODE *starting_node,
-		char is_fixed_global) {
+void *first_fit_search(size_t size, NODE *starting_node);
+
+void *first_fit_search(size_t size, NODE *starting_node) {
 	NODE *node;
+	int found;
+	void *ptr;
 
 	node = starting_node;
+	found = 0;
 
-	while(node) {
+	if(node != NULL && 
 
+	while(node && !found) {
+		if(getSize(node->data) >= size) {
+			ptr = node
+		}
 	}
 
 }
-
 */
 
 void *custom_malloc(heap_t* heap, size_t size);
 
 void *custom_malloc(heap_t* heap, size_t size) {
-	void *ptr;
-	int fixed_list_id, i;
+	NODE *ptr;
+	int fixed_list_id, i, found;
 	MAPTABLE_NODE *current_maptable_node;
+	NODE *current_node;
 
 	ptr = NULL;
 
@@ -44,17 +52,42 @@ void *custom_malloc(heap_t* heap, size_t size) {
 		}
 		ptr = current_maptable_node->fixed_list_head;
 		current_maptable_node->fixed_list_head = ((NODE *) ptr)->next;
-		set_requested_size(ptr, size);
-		markInUse(ptr);
+		set_requested_size(ptr->data, size);
+		markInUse(ptr->data);
 	}
 
 	if(ptr == NULL) {
+		found = 0;
+
 		// first fit from free list
+		current_node = heap->free_list_head;
+
+		if(current_node != NULL && getSize(current_node->data) >= size) {
+			ptr = current_node;
+			heap->free_list_head = ((NODE *) ptr)->next;
+			set_requested_size(ptr->data, size);
+			markInUse(ptr->data);
+		}
+		while(current_node && !found) {
+			if(getSize(current_node->next->data) >= size) {
+				ptr = current_node->next;
+				current_node->next = ptr->next;
+				set_requested_size(ptr->data, size);
+				markInUse(ptr->data);
+				found = 1;
+			}
+			current_node = current_node->next;
+		}
+
+		if(!found) {
+			return sys_alloc(size);
+		}
+
 	}
 
 	posix_unlock(heap);
 
-	return ptr;
+	return ptr->data;
 }
 
 
@@ -62,9 +95,11 @@ int main(void) {
 	allocator_t *myallocator;
 	heap_t *myheap;
 	int heap_id;
+	void *p;
 
 	myallocator = dmm_init();
 	heap_id = map_thread_heap();
 	printf("This thread accesses heap %d\n", heap_id);
 	myheap = &myallocator->heaps[heap_id];
+	p = custom_malloc(myheap, 32);
 }