Explorar o código

Separated custom_malloc and custom_free from test.c

Ioannis Koutras %!s(int64=13) %!d(string=hai) anos
pai
achega
b969dfd00e
Modificáronse 6 ficheiros con 119 adicións e 128 borrados
  1. 1 1
      Makefile
  2. 40 0
      custom_free.c
  3. 4 0
      custom_free.h
  4. 67 0
      custom_malloc.c
  5. 4 0
      custom_malloc.h
  6. 3 127
      test.c

+ 1 - 1
Makefile

@@ -5,7 +5,7 @@ WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
 	          -Wuninitialized -Wconversion -Wstrict-prototypes
 CFLAGS := -g -O -std=c99 $(WARNINGS)
 
-OBJ = posix_lock.o other.o LeaHeader.o sys_alloc.o dmm_init.o test.o
+OBJ = posix_lock.o other.o LeaHeader.o sys_alloc.o dmm_init.o custom_malloc.o custom_free.o test.o
 
 %.o: %.c
 	$(CC) -c -o $@ $< $(CFLAGS)

+ 40 - 0
custom_free.c

@@ -0,0 +1,40 @@
+#include "custom_free.h"
+#include "other.h"
+#include "posix_lock.h"
+
+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);
+}
+

+ 4 - 0
custom_free.h

@@ -0,0 +1,4 @@
+#include "heap.h"
+
+void custom_free(heap_t* heap, void *ptr);
+

+ 67 - 0
custom_malloc.c

@@ -0,0 +1,67 @@
+#include "custom_malloc.h"
+#include "posix_lock.h"
+#include "other.h"
+#include "sys_alloc.h"
+
+void * custom_malloc(heap_t* heap, size_t size) {
+	leaHdr *ptr;
+	int fixed_list_id, i, found;
+	MAPTABLE_NODE *current_maptable_node;
+	leaHdr *current_node;
+
+	ptr = NULL;
+
+	posix_lock(heap);
+
+	fixed_list_id = map_size_to_list(heap, size);
+	
+	// If a fixed list is found, do a first fit
+	if(fixed_list_id != -1) {
+		current_maptable_node = heap->maptable_head;
+		// traverse through the maptable node list
+		if(fixed_list_id != 0) {
+			for(i = 1; i < fixed_list_id; i++) {
+				current_maptable_node = current_maptable_node->next;
+			}
+		}
+		if(current_maptable_node->fixed_list_head != NULL) {
+			ptr = current_maptable_node->fixed_list_head;
+			current_maptable_node->fixed_list_head = ptr->next;
+			set_requested_size(ptr, size);
+			markInUse(ptr);
+		}
+	}
+
+	if(ptr == NULL) {
+		found = 0;
+
+		// first fit from free list
+		current_node = heap->free_list_head;
+
+		if(current_node != NULL && getSize(current_node) >= size) {
+			ptr = current_node;
+			heap->free_list_head = ptr->next;
+			set_requested_size(ptr, size);
+			markInUse(ptr);
+		}
+		while(current_node && !found) {
+			if(getSize(current_node->next) >= size) {
+				ptr = current_node->next;
+				current_node->next = ptr->next;
+				set_requested_size(ptr, size);
+				markInUse(ptr);
+				found = 1;
+			}
+			current_node = current_node->next;
+		}
+
+		if(!found) {
+			posix_unlock(heap);
+			return sys_alloc(size);
+		}
+
+	}
+
+	posix_unlock(heap);
+	return ptr;
+}

+ 4 - 0
custom_malloc.h

@@ -0,0 +1,4 @@
+#include "heap.h"
+
+void * custom_malloc(heap_t* heap, size_t size);
+

+ 3 - 127
test.c

@@ -1,134 +1,10 @@
 #include <stdio.h>
 #include "heap.h"
 #include "other.h"
-#include "posix_lock.h"
 #include "LeaHeader.h"
 #include "dmm_init.h"
-
-/*
-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;
-
-	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) {
-	leaHdr *ptr;
-	int fixed_list_id, i, found;
-	MAPTABLE_NODE *current_maptable_node;
-	leaHdr *current_node;
-
-	ptr = NULL;
-
-	posix_lock(heap);
-
-	fixed_list_id = map_size_to_list(heap, size);
-	
-	// If a fixed list is found, do a first fit
-	if(fixed_list_id != -1) {
-		current_maptable_node = heap->maptable_head;
-		// traverse through the maptable node list
-		if(fixed_list_id != 0) {
-			for(i = 1; i < fixed_list_id; i++) {
-				current_maptable_node = current_maptable_node->next;
-			}
-		}
-		if(current_maptable_node->fixed_list_head != NULL) {
-			ptr = current_maptable_node->fixed_list_head;
-			current_maptable_node->fixed_list_head = ptr->next;
-			set_requested_size(ptr, size);
-			markInUse(ptr);
-		}
-	}
-
-	if(ptr == NULL) {
-		found = 0;
-
-		// first fit from free list
-		current_node = heap->free_list_head;
-
-		if(current_node != NULL && getSize(current_node) >= size) {
-			ptr = current_node;
-			heap->free_list_head = ptr->next;
-			set_requested_size(ptr, size);
-			markInUse(ptr);
-		}
-		while(current_node && !found) {
-			if(getSize(current_node->next) >= size) {
-				ptr = current_node->next;
-				current_node->next = ptr->next;
-				set_requested_size(ptr, size);
-				markInUse(ptr);
-				found = 1;
-			}
-			current_node = current_node->next;
-		}
-
-		if(!found) {
-			posix_unlock(heap);
-			return sys_alloc(size);
-		}
-
-	}
-
-	posix_unlock(heap);
-	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);
-}
+#include "custom_malloc.h"
+#include "custom_free.h"
 
 int main(void) {
 	allocator_t *myallocator;
@@ -140,6 +16,6 @@ int main(void) {
 	heap_id = map_thread_heap();
 	printf("This thread accesses heap %d\n", heap_id);
 	myheap = &myallocator->heaps[heap_id];
-	p = custom_malloc(myheap, 32);
+	p = custom_malloc(myheap, (size_t) 32);
 	custom_free(myheap, p);
 }