Browse Source

Started custom_free.

Ioannis Koutras 13 years ago
parent
commit
afd8dacbf1
3 changed files with 60 additions and 16 deletions
  1. 1 0
      LeaHeader.h
  2. 8 3
      heap.h
  3. 51 13
      test.c

+ 1 - 0
LeaHeader.h

@@ -7,6 +7,7 @@ struct LeaHeader {
 	size_t size;
 	size_t previous_size;
 	size_t requested_size; /**< originally requested size by the application */
+	struct LeaHeader *next;
 };
 
 typedef struct LeaHeader  leaHdr;

+ 8 - 3
heap.h

@@ -4,6 +4,8 @@
 #include <stdint.h>
 #include <pthread.h>
 
+#include "LeaHeader.h"
+
 #define NUM_HEAPS 4
 
 /**
@@ -21,7 +23,8 @@ typedef struct node_s {
  */
 typedef struct maptable_node_s {
 	unsigned int size; /**< the size of the blocks of the fixed list */
-	NODE *fixed_list_head; /**< pointer to the head node of the fixed list */
+	//NODE *fixed_list_head; /**< pointer to the head node of the fixed list */
+	leaHdr *fixed_list_head; /**< pointer to the head node of the fixed list */
 	struct maptable_node_s *next; /**< pointer to the next node of the maptable */
 } MAPTABLE_NODE;
 
@@ -47,8 +50,10 @@ typedef struct dmmknobs_s {
 
 typedef struct heap_s {
 	MAPTABLE_NODE *maptable_head;
-	NODE *free_list_head;
-	NODE *rov_ptr;
+	//NODE *free_list_head;
+	leaHdr *free_list_head;
+	//NODE *rov_ptr;
+	leaHdr *rov_ptr;
 	uint64_t num_objects;
 	dmmstats_t dmm_stats;
 	dmmknobs_t dmm_knobs;

+ 51 - 13
test.c

@@ -30,10 +30,10 @@ void *first_fit_search(size_t size, NODE *starting_node) {
 void *custom_malloc(heap_t* heap, size_t size);
 
 void *custom_malloc(heap_t* heap, size_t size) {
-	NODE *ptr;
+	leaHdr *ptr;
 	int fixed_list_id, i, found;
 	MAPTABLE_NODE *current_maptable_node;
-	NODE *current_node;
+	leaHdr *current_node;
 
 	ptr = NULL;
 
@@ -51,9 +51,9 @@ 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->data, size);
-		markInUse(ptr->data);
+		current_maptable_node->fixed_list_head = ptr->next;
+		set_requested_size(ptr, size);
+		markInUse(ptr);
 	}
 
 	if(ptr == NULL) {
@@ -62,18 +62,18 @@ void *custom_malloc(heap_t* heap, size_t size) {
 		// first fit from free list
 		current_node = heap->free_list_head;
 
-		if(current_node != NULL && getSize(current_node->data) >= size) {
+		if(current_node != NULL && getSize(current_node) >= size) {
 			ptr = current_node;
-			heap->free_list_head = ((NODE *) ptr)->next;
-			set_requested_size(ptr->data, size);
-			markInUse(ptr->data);
+			heap->free_list_head = ptr->next;
+			set_requested_size(ptr, size);
+			markInUse(ptr);
 		}
 		while(current_node && !found) {
-			if(getSize(current_node->next->data) >= size) {
+			if(getSize(current_node->next) >= size) {
 				ptr = current_node->next;
 				current_node->next = ptr->next;
-				set_requested_size(ptr->data, size);
-				markInUse(ptr->data);
+				set_requested_size(ptr, size);
+				markInUse(ptr);
 				found = 1;
 			}
 			current_node = current_node->next;
@@ -87,9 +87,46 @@ void *custom_malloc(heap_t* heap, size_t size) {
 
 	posix_unlock(heap);
 
-	return ptr->data;
+	return ptr;
 }
 
+void custom_free(heap_t* heap, void *ptr);
+
+void custom_free(heap_t* heap, void *ptr) {
+	size_t size;
+	int fixed_list_id, i;
+	MAPTABLE_NODE *current_maptable_node;
+	leaHdr *test;
+	
+
+	size = getSize((char *) ptr);
+	fixed_list_id = map_size_to_list(heap, size);
+	
+	posix_lock(heap);
+
+	if(fixed_list_id != -1) {
+		current_maptable_node = heap->maptable_head;		
+		if(fixed_list_id == 0) {
+			test = getHeader(ptr);
+			test->next = current_maptable_node->fixed_list_head;
+			//getHeader((char *) ptr)->next = current_maptable_node->fixed_list_head;
+			current_maptable_node->fixed_list_head = ptr;
+		} else {
+			for(i = 1; i < fixed_list_id; i++) {
+				current_maptable_node = current_maptable_node->next;
+			}
+			getHeader(ptr)->next = current_maptable_node->fixed_list_head;
+			current_maptable_node->fixed_list_head = ptr;
+		}
+	} else { // put it in the free list
+		getHeader(ptr)->next = heap->free_list_head;
+		heap->free_list_head = getHeader(ptr);
+	}
+
+	markFree((char *) ptr);
+
+	posix_unlock(heap);
+}
 
 int main(void) {
 	allocator_t *myallocator;
@@ -102,4 +139,5 @@ int main(void) {
 	printf("This thread accesses heap %d\n", heap_id);
 	myheap = &myallocator->heaps[heap_id];
 	p = custom_malloc(myheap, 32);
+	custom_free(myheap, p);
 }