Parcourir la source

Simplified the block header structure and functions.

Ioannis Koutras il y a 13 ans
Parent
commit
0ada8dc4ad
13 fichiers modifiés avec 107 ajouts et 217 suppressions
  1. 0 60
      LeaHeader.c
  2. 0 47
      LeaHeader.h
  3. 2 2
      Makefile
  4. 27 0
      block_header.c
  5. 23 0
      block_header.h
  6. 7 12
      custom_free.c
  7. 19 22
      custom_malloc.c
  8. 3 2
      dmm_init.c
  9. 7 18
      heap.h
  10. 1 1
      other.c
  11. 10 47
      sys_alloc.c
  12. 1 2
      sys_alloc.h
  13. 7 4
      test.c

+ 0 - 60
LeaHeader.c

@@ -1,60 +0,0 @@
-#include "LeaHeader.h"
-
-//returns a pointer to the header of the block	
-leaHdr *getHeader(const void * ptr) {
-	return (leaHdr *) ((char *) ptr - HDR_SIZE);
-}
-
-size_t getSize (void *ptr) {
-	return (getHeader(ptr)->size >> 1);
-}
-
-size_t getPrevSize (void *ptr) {
-	size_t sz = getHeader(ptr)->previous_size;
-	return sz >> 1;
-}
-
-void *getNext(void *ptr) {
-	return (void *)((char *) ptr + HDR_SIZE + getSize(ptr));
-}
-
-void setSize(void *ptr, size_t sz) {
-	size_t size = getSize(ptr);
-	size = ((size & 1) | (sz << 1));
-	getHeader(ptr)->size = size;
-}
-
-void setPrevSize (void *ptr, size_t sz) {
-	size_t previous_size = getHeader(ptr)->previous_size;
-	previous_size = (previous_size & 1) | (sz << 1);
-	getHeader(ptr)->previous_size = previous_size;
-}
-
-void markPrevFree (char * ptr) {
-	getHeader(ptr)->previous_size &= (size_t) (~ 1);
-}
-
-void markPrevInUse (void * ptr) {
-	getHeader(ptr)->previous_size |= 1;
-}
-
-size_t isPrevFree (void * ptr) {
-	size_t free = ~(getHeader(ptr)->previous_size & 1);
-	return (free & 1) ;
-}
-
-void markInUse (void * ptr) {
-	markPrevInUse(getNext(ptr));
-}
-
-void markFree (char * ptr) {
-	markPrevFree(getNext(ptr));
-}
-	
-size_t isFree (char * ptr) {
-	return isPrevFree(getNext(ptr));
-}
-
-void set_requested_size (void *ptr, size_t size) {
-	getHeader(ptr)->requested_size = size;
-}

+ 0 - 47
LeaHeader.h

@@ -1,47 +0,0 @@
-#ifndef RA_LEAHEADER_H
-#define RA_LEAHEADER_H
-
-#include <stddef.h>
-
-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;
-typedef struct LeaHeader* leaHdr_ptr;
-
-//const unsigned int HDR_SIZE = sizeof(leaHdr);
-#define HDR_SIZE sizeof(leaHdr)
-
-
-//returns a pointer to the header of the block	
-leaHdr * getHeader(const void *ptr);
-
-void *getNext(void *ptr);
-
-size_t getSize(void *ptr);
-
-size_t getPrevSize (void *ptr);
-
-void setSize(void *ptr, size_t sz);
-
-void setPrevSize (void *ptr, size_t sz);
-
-void markPrevFree (char * ptr);
-
-void markPrevInUse (void *ptr);
-
-size_t isPrevFree (void *ptr);
-
-void markInUse (void *ptr);
-
-void markFree (char * ptr);
-
-size_t isFree (char * ptr);
-
-void set_requested_size(void *ptr, size_t size);
-
-#endif /*RA_LEAHEADER_H*/

+ 2 - 2
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 custom_malloc.o custom_free.o
+OBJ = posix_lock.o other.o block_header.o sys_alloc.o dmm_init.o custom_malloc.o custom_free.o
 
 %.o: %.c
 	$(CC) -c -o $@ $< $(CFLAGS)
@@ -14,6 +14,6 @@ test: $(OBJ) test.o
 	$(CC) -pthread -o $@ $^ $(CFLAGS)
 
 clean:
-	-@$(RM) $(wildcard $(OBJ))
+	-@$(RM) $(wildcard $(OBJ)) test.o test
 
 .PHONY: all clean test

+ 27 - 0
block_header.c

@@ -0,0 +1,27 @@
+#include "block_header.h"
+
+block_header_t * get_header(void *ptr);
+
+block_header_t * get_header(void *ptr) {
+	return (block_header_t *) ((char *) ptr - HEADER_SIZE);
+}
+
+void * get_next(void *ptr) {
+	return get_header(ptr)->next;
+}
+
+size_t get_size(void *ptr) {
+	return get_header(ptr)->size;
+}
+
+void set_size(void *ptr, size_t size) {
+	get_header(ptr)->size = size;
+}
+
+void set_requested_size(void *ptr, size_t size) {
+	get_header(ptr)->requested_size = size;
+}
+
+void set_next(void *ptr, void *next_block) {
+	get_header(ptr)->next = next_block;
+}

+ 23 - 0
block_header.h

@@ -0,0 +1,23 @@
+#ifndef BLOCK_HEADER_H
+#define BLOCK_HEADER_H
+
+#include <stddef.h>
+
+typedef struct block_header_s {
+	size_t size;
+	size_t requested_size;
+	void *next;
+} block_header_t;
+
+#define HEADER_SIZE sizeof(block_header_t)
+
+void * get_next(void *ptr);
+
+size_t get_size(void *ptr);
+
+void set_size(void *ptr, size_t size);
+void set_requested_size(void *ptr, size_t size);
+
+void set_next(void *block, void *next_block);
+
+#endif /* BLOCK_HEADER_H */

+ 7 - 12
custom_free.c

@@ -1,15 +1,14 @@
 #include "custom_free.h"
 #include "other.h"
 #include "posix_lock.h"
+#include "block_header.h"
 
 void custom_free(heap_t* heap, void *ptr) {
 	size_t size;
 	int fixed_list_id, i;
-	MAPTABLE_NODE *current_maptable_node;
-	leaHdr *test;
-	
+	maptable_node_t *current_maptable_node;
 
-	size = getSize((char *) ptr);
+	size = get_size(ptr);
 	fixed_list_id = map_size_to_list(heap, size);
 	
 	posix_lock(heap);
@@ -17,24 +16,20 @@ void custom_free(heap_t* heap, void *ptr) {
 	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;
+			set_next(ptr, 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;
+			set_next(ptr, 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);
+		set_next(ptr, heap->free_list_head);
+		heap->free_list_head = ptr;
 	}
 
-	markFree((char *) ptr);
-
 	posix_unlock(heap);
 }
 

+ 19 - 22
custom_malloc.c

@@ -2,12 +2,13 @@
 #include "posix_lock.h"
 #include "other.h"
 #include "sys_alloc.h"
+#include "block_header.h"
 
 void * custom_malloc(heap_t* heap, size_t size) {
-	leaHdr *ptr;
+	void *ptr;
 	int fixed_list_id, i, found;
-	MAPTABLE_NODE *current_maptable_node;
-	leaHdr *current_node;
+	maptable_node_t *current_maptable_node;
+	void *current_block, *previous_block;
 
 	ptr = NULL;
 
@@ -26,9 +27,8 @@ void * custom_malloc(heap_t* heap, size_t size) {
 		}
 		if(current_maptable_node->fixed_list_head != NULL) {
 			ptr = current_maptable_node->fixed_list_head;
-			current_maptable_node->fixed_list_head = ptr->next;
+			current_maptable_node->fixed_list_head = get_next(ptr);
 			set_requested_size(ptr, size);
-			markInUse(ptr);
 		}
 	}
 
@@ -36,28 +36,25 @@ void * custom_malloc(heap_t* heap, size_t size) {
 		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;
+		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;
 				set_requested_size(ptr, size);
-				markInUse(ptr);
-				found = 1;
+				set_next(ptr, heap->used_blocks_head);
+				heap->used_blocks_head = ptr;
+				if(current_block != heap->free_list_head) {
+					set_next(previous_block, get_next(ptr));
+				} else {
+					heap->free_list_head = get_next(ptr);
+				}
+				posix_unlock(heap);
+				return ptr;
 			}
-			current_node = current_node->next;
+			previous_block = current_block;
 		}
 
 		if(!found) {
-			posix_unlock(heap);
-			return sys_alloc(size);
+			ptr = sys_alloc(heap, size);
 		}
 
 	}

+ 3 - 2
dmm_init.c

@@ -7,13 +7,14 @@ allocator_t * dmm_init(void) {
 	int i;
 	allocator_t *main_allocator;
 	heap_t *current_heap;
-	MAPTABLE_NODE *maptablenode;
+	maptable_node_t *maptablenode;
 
 	main_allocator = (allocator_t *) sbrk((int) sizeof(allocator_t));
 
 	for(i = 0; i < NUM_HEAPS; i++) {
 		main_allocator->heaps[i].maptable_head = NULL;
 		main_allocator->heaps[i].free_list_head = NULL;
+		main_allocator->heaps[i].used_blocks_head = NULL;
 		main_allocator->heaps[i].rov_ptr = NULL;
 		main_allocator->heaps[i].num_objects = 0;
 		main_allocator->heaps[i].dmm_stats.num_malloc = 0;
@@ -26,7 +27,7 @@ allocator_t * dmm_init(void) {
 	// 2 last ones with 64 and 256 (2 fixed lists per heap)
 	// 2 * 4 + 2 * 2 = 12 maptable nodes
 	current_heap = &main_allocator->heaps[0];
-	maptablenode = (MAPTABLE_NODE *) sbrk((int)(12*(sizeof(MAPTABLE_NODE))));
+	maptablenode = (maptable_node_t *) sbrk((int)(12*(sizeof(maptable_node_t))));
 
 	maptablenode->size = 32;
 	maptablenode->fixed_list_head = NULL;

+ 7 - 18
heap.h

@@ -4,29 +4,20 @@
 #include <stdint.h>
 #include <pthread.h>
 
-#include "LeaHeader.h"
+#include "block_header.h"
 
 #define NUM_HEAPS 4
 
 /**
- * A structure to represent a singly linked list node
- */
-typedef struct node_s {
-	void *data; /**< pointer to the actual data */
-	struct node_s *next; /**< pointer to the next item of the list */
-} NODE;
-
-/**
  * A structure to represent a maptable node
  *
  * Heaps contain 
  */
 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 */
-	leaHdr *fixed_list_head; /**< pointer to the head node of the fixed list */
+	void *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;
+} maptable_node_t;
 
 typedef struct dmmstats_s {
 	uint32_t max_mem_allocated; /**< maximum total memory allocated */
@@ -49,11 +40,10 @@ typedef struct dmmknobs_s {
 } dmmknobs_t;
 
 typedef struct heap_s {
-	MAPTABLE_NODE *maptable_head;
-	//NODE *free_list_head;
-	leaHdr *free_list_head;
-	//NODE *rov_ptr;
-	leaHdr *rov_ptr;
+	maptable_node_t *maptable_head;
+	void *free_list_head;
+	void *used_blocks_head;
+	void *rov_ptr;
 	uint64_t num_objects;
 	dmmstats_t dmm_stats;
 	dmmknobs_t dmm_knobs;
@@ -62,7 +52,6 @@ typedef struct heap_s {
 
 typedef struct allocator_s {
 	heap_t heaps[NUM_HEAPS];
-	char *border_ptr;
 } allocator_t;
 
 #endif /* HEAP_H */

+ 1 - 1
other.c

@@ -15,7 +15,7 @@ size_t req_padding(size_t size) {
 
 int map_size_to_list(heap_t *heap, size_t sz) {
 	int i;
-	MAPTABLE_NODE *node;
+	maptable_node_t *node;
 	i = 0;
 	node = heap->maptable_head;
 	while(node) {

+ 10 - 47
sys_alloc.c

@@ -1,65 +1,28 @@
-#define _BSD_SOURCE
 #include <unistd.h>
 #include <stdio.h>
 #include "posix_lock.h"
 #include "other.h"
-#include "LeaHeader.h"
 #include "sys_alloc.h"
+#include "block_header.h"
+#include "heap.h"
 
-static void *borderPtr = NULL;
-
-void setHeaders(void *ptr, size_t sz) {
-	sz = req_padding(sz);
-	setSize(ptr,sz);
-	markInUse(ptr);
-	setPrevSize(getNext(ptr), sz);
-	setSize(getNext(ptr), (size_t) 0); //border block
-	markInUse(getNext(ptr)); //border block
-}
-
-void *sys_alloc(size_t size) {
+void *sys_alloc(heap_t *heap, size_t size) {
 	size_t allocation_size;
 	void *ptr;
 
 	sbrk_lock();
-	allocation_size = req_padding(size) + HDR_SIZE;
-
-	if (borderPtr == NULL) {
-
-		ptr = sbrk((int) (allocation_size + 2*HDR_SIZE)); //extra space for coalescing support
-
-		if (ptr == (void *) -1){
-			printf("sbrk Fail\n");
-			return NULL;
-		}
-
-		//printf("sbrk = %p\n",ptr);
-
-		setPrevSize((char *) ptr + HDR_SIZE, (size_t) 0);
-		markPrevInUse((char *) ptr + HDR_SIZE);
-		borderPtr = ptr;
-
-	} else {
-
-		ptr = sbrk((int) allocation_size);
 
-		if ((ptr == (char *) -1)){
-			printf("sbrk Fail: out of Memory\n");
-			return NULL;
-		}
-		if((ptr != (char *) borderPtr + 2*HDR_SIZE)){
-			printf("sbrk Fail: Non-contiguous Memory\n");
-			return NULL;
-		}
-	}
+	allocation_size = req_padding(size) + HEADER_SIZE;
 
+	ptr = sbrk((int) allocation_size);
 
-	ptr = (char *) borderPtr + HDR_SIZE;
-	borderPtr = (char *) borderPtr + allocation_size;
-	setHeaders(ptr,size);
+	set_size(ptr, req_padding(size));
+	set_requested_size(ptr, size);
+	set_next(ptr, heap->used_blocks_head);	
+	heap->used_blocks_head = ptr;
 
 	sbrk_unlock();
-	return (void *) ptr;
 
+	return  ptr;
 }
 

+ 1 - 2
sys_alloc.h

@@ -1,7 +1,6 @@
 #ifndef SYS_ALLOC_H
 #define SYS_ALLOC_H
 
-void setHeaders (void *ptr, size_t sz);
-void *sys_alloc(size_t size);
+void *sys_alloc(heap_t *heap, size_t size);
 
 #endif /* SYS_ALLOC_H */

+ 7 - 4
test.c

@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include "heap.h"
 #include "other.h"
-#include "LeaHeader.h"
 #include "dmm_init.h"
 #include "custom_malloc.h"
 #include "custom_free.h"
@@ -10,12 +9,16 @@ int main(void) {
 	allocator_t *myallocator;
 	heap_t *myheap;
 	int heap_id;
-	void *p;
+	void *p1, *p2, *p3;
 
 	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, (size_t) 32);
-	custom_free(myheap, p);
+	p1 = custom_malloc(myheap, (size_t) 3000);
+	p2 = custom_malloc(myheap, (size_t) 3000);
+	p3 = custom_malloc(myheap, (size_t) 3000);
+	custom_free(myheap, p1);
+	custom_free(myheap, p3);
+	custom_free(myheap, p2);
 }