Ioannis Koutras 14 éve
commit
d279e2a024
15 módosított fájl, 2209 hozzáadás és 0 törlés
  1. 6 0
      .hgignore
  2. 1716 0
      Doxyfile
  3. 68 0
      LeaHeader.c
  4. 53 0
      LeaHeader.h
  5. 15 0
      Makefile
  6. 77 0
      dmm_init.c
  7. 8 0
      dmm_init.h
  8. 63 0
      heap.h
  9. 35 0
      other.c
  10. 12 0
      other.h
  11. 20 0
      posix_lock.c
  12. 12 0
      posix_lock.h
  13. 63 0
      sys_alloc.c
  14. 6 0
      sys_alloc.h
  15. 55 0
      test.c

+ 6 - 0
.hgignore

@@ -0,0 +1,6 @@
+syntax: glob
+
+*.o
+test
+html/*
+latex/*

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 1716 - 0
Doxyfile


+ 68 - 0
LeaHeader.c

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

+ 53 - 0
LeaHeader.h

@@ -0,0 +1,53 @@
+#ifndef RA_LEAHEADER_H
+#define RA_LEAHEADER_H
+
+struct LeaHeader {
+	unsigned int size;
+	unsigned int prevSize;
+};
+
+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 char * ptr);
+
+
+unsigned int getSize (const char * ptr);
+
+
+unsigned int getPrevSize (const char * ptr);
+
+
+void setSize (char * ptr, const unsigned int sz);
+
+
+void setPrevSize (char * ptr, const unsigned int sz);
+
+
+//returns a pointer (that points) to the next block
+char * getNext (const char * ptr);
+
+
+char * getPrev(const char * ptr);
+
+void markPrevFree (char * ptr);
+
+void markPrevInUse (char * ptr);
+
+unsigned int isPrevFree (char * ptr);
+
+
+void markInUse (char * ptr);
+
+
+void markFree (char * ptr);
+	
+
+unsigned int isFree (char * ptr);
+
+#endif /*RA_LEAHEADER_H*/

+ 15 - 0
Makefile

@@ -0,0 +1,15 @@
+CC=gcc
+
+WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
+	          -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
+	          -Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
+	          -Wuninitialized -Wconversion -Wstrict-prototypes
+CFLAGS := -g -std=c99 $(WARNINGS)
+
+OBJ = posix_lock.o other.o LeaHeader.o sys_alloc.o dmm_init.o test.o
+
+%.o: %.c
+	$(CC) -c -o $@ $< $(CFLAGS)
+
+test: $(OBJ)
+	gcc -pthread -o $@ $^ $(CFLAGS)

+ 77 - 0
dmm_init.c

@@ -0,0 +1,77 @@
+#include <unistd.h>
+#include <pthread.h>
+#include "dmm_init.h"
+
+allocator_t *dmm_init(void) {
+	int i;
+	allocator_t *main_allocator;
+	heap_t *current_heap;
+	MAPTABLE_NODE *maptablenode;
+
+	main_allocator = (allocator_t *) sbrk(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].rov_ptr = NULL;
+		main_allocator->heaps[i].num_objects = 0;
+		main_allocator->heaps[i].dmm_stats.num_malloc = 0;
+		main_allocator->heaps[i].dmm_stats.num_free = 0;
+		pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
+	}
+
+	// Custom number of fixed lists and their initialization
+	// 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap)
+	// 2 last ones with 64 and 256 (2 fixed lists per heap)
+	current_heap = &main_allocator->heaps[0];
+	maptablenode = (MAPTABLE_NODE *) sbrk(12*(sizeof(MAPTABLE_NODE)));
+	maptablenode->size = 32;
+	maptablenode->fixed_list_head = NULL;
+	maptablenode->next = maptablenode+1;
+	current_heap->maptable_head = maptablenode;
+	(maptablenode+1)->size = 64;
+	(maptablenode+1)->fixed_list_head = NULL;
+	(maptablenode+1)->next = maptablenode+2;
+	(maptablenode+2)->size = 128;
+	(maptablenode+2)->fixed_list_head = NULL;
+	(maptablenode+2)->next = maptablenode+3;
+	(maptablenode+3)->size = 256;
+	(maptablenode+3)->fixed_list_head = NULL;
+	(maptablenode+3)->next = NULL;
+	current_heap = &main_allocator->heaps[1];
+	maptablenode += 4;
+	maptablenode->size = 32;
+	maptablenode->fixed_list_head = NULL;
+	maptablenode->next = maptablenode+1;
+	current_heap->maptable_head = maptablenode;
+	(maptablenode+1)->size = 64;
+	(maptablenode+1)->fixed_list_head = NULL;
+	(maptablenode+1)->next = maptablenode+2;
+	(maptablenode+2)->size = 128;
+	(maptablenode+2)->fixed_list_head = NULL;
+	(maptablenode+2)->next = maptablenode+3;
+	(maptablenode+3)->size = 256;
+	(maptablenode+3)->fixed_list_head = NULL;
+	(maptablenode+3)->next = NULL;
+	current_heap = &main_allocator->heaps[2];
+	maptablenode += 4;
+	maptablenode->size = 64;
+	maptablenode->fixed_list_head = NULL;
+	maptablenode->next = maptablenode+1;
+	current_heap->maptable_head = maptablenode;
+	(maptablenode+1)->size = 256;
+	(maptablenode+1)->fixed_list_head = NULL;
+	(maptablenode+1)->next = NULL;
+	current_heap = &main_allocator->heaps[3];
+	maptablenode += 2;
+	maptablenode->size = 64;
+	maptablenode->fixed_list_head = NULL;
+	maptablenode->next = maptablenode+1;
+	current_heap->maptable_head = maptablenode;
+	(maptablenode+1)->size = 256;
+	(maptablenode+1)->fixed_list_head = NULL;
+	(maptablenode+1)->next = NULL;
+
+	return main_allocator;
+}
+

+ 8 - 0
dmm_init.h

@@ -0,0 +1,8 @@
+#ifndef DMM_INIT_H
+#define DMM_INIT_H
+
+#include "heap.h"
+
+allocator_t *dmm_init(void);
+
+#endif /* DMM_INIT_H */

+ 63 - 0
heap.h

@@ -0,0 +1,63 @@
+#ifndef HEAP_H
+#define HEAP_H
+
+#include <stdint.h>
+#include <pthread.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 */
+	struct maptable_node_s *next; /**< pointer to the next node of the maptable */
+} MAPTABLE_NODE;
+
+typedef struct dmmstats_s {
+	uint32_t max_mem_allocated; /**< maximum total memory allocated */
+	uint32_t max_mem_requested; /**< maximum total memory requested */
+	uint32_t mem_allocated; /**< total memory currently allocated */
+	uint32_t mem_requested; /**< total memory currently requested */
+	uint32_t live_objects; /**< number of live objects */
+	uint32_t read_mem_accesses; /**< number of read accesses */
+	uint32_t write_mem_accesses; /**< number of write accesses */
+	uint32_t num_malloc; /**< number of malloc()'s served */
+	uint32_t num_free; /**< number of free()'s served */
+} dmmstats_t;
+
+typedef struct dmmknobs_s {
+	uint32_t maxCoalesceSize;
+	uint32_t minSplitSize;
+	float empty_threshold;
+	uint32_t percentage;
+	char frag_state; //FIXME It was in the old code to refresh the frag check
+} dmmknobs_t;
+
+typedef struct heap_s {
+	MAPTABLE_NODE *maptable_head;
+	NODE *free_list_head;
+	NODE *rov_ptr;
+	uint64_t num_objects;
+	dmmstats_t dmm_stats;
+	dmmknobs_t dmm_knobs;
+	pthread_mutex_t mutex;
+} heap_t;
+
+typedef struct allocator_s {
+	heap_t heaps[NUM_HEAPS];
+	char *border_ptr;
+} allocator_t;
+
+#endif /* HEAP_H */

+ 35 - 0
other.c

@@ -0,0 +1,35 @@
+#include "other.h"
+#include <pthread.h>
+
+unsigned int req_padding(unsigned int size) {
+	if(size <= 32)
+		return 32;
+	if(size <= 64)
+		return 64;
+	if(size <= 128)
+		return 128;
+	if(size <= 256)
+		return 256;
+	return size;
+}
+
+int map_size_to_list(heap_t *heap, unsigned int sz) {
+	int i;
+	MAPTABLE_NODE *node;
+	i = 0;
+	node = heap->maptable_head;
+	while(node) {
+		if(node->size == sz) {
+			return i;
+		}
+		i++;
+		node = node->next;
+	}
+	return -1;
+}
+
+// Random assignment
+int map_thread_heap(void) {
+	return (int) (((unsigned long) pthread_self() >> 10) % NUM_HEAPS);
+}
+

+ 12 - 0
other.h

@@ -0,0 +1,12 @@
+#ifndef OTHER_H
+#define OTHER_H
+
+#include "heap.h"
+
+unsigned int req_padding(unsigned int sz);
+
+int map_size_to_list(heap_t *heap, unsigned int sz);
+
+int map_thread_heap(void);
+
+#endif /* OTHER_H */

+ 20 - 0
posix_lock.c

@@ -0,0 +1,20 @@
+#include <pthread.h>
+#include "posix_lock.h"
+
+pthread_mutex_t sbrk_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void sbrk_lock(void) {
+	pthread_mutex_lock(&sbrk_mutex);
+}
+
+void sbrk_unlock(void) {
+	pthread_mutex_unlock(&sbrk_mutex);
+}
+
+void posix_lock(heap_t *heap) {
+	pthread_mutex_lock(&heap->mutex);
+}
+
+void posix_unlock(heap_t *heap) {
+	pthread_mutex_unlock(&heap->mutex);
+}

+ 12 - 0
posix_lock.h

@@ -0,0 +1,12 @@
+#ifndef POSIX_LOCK_H
+#define POSIX_LOCK_H
+
+#include "heap.h"
+
+void sbrk_lock(void);
+void sbrk_unlock(void);
+
+void posix_lock(heap_t *heap);
+void posix_unlock(heap_t *heap);
+
+#endif /* POSIX_LOCK_H */

+ 63 - 0
sys_alloc.c

@@ -0,0 +1,63 @@
+#include <unistd.h>
+#include <stdio.h>
+#include "posix_lock.h"
+#include "other.h"
+#include "LeaHeader.h"
+
+static char * borderPtr = NULL;
+
+void setHeaders (char * ptr,unsigned int sz) {
+	sz = req_padding(sz);
+	setSize(ptr,sz);
+	markInUse(ptr);
+	setPrevSize(getNext(ptr),sz);
+	setSize(getNext(ptr),0);//border block
+	markInUse(getNext(ptr));//border block
+}
+
+void *sys_alloc(unsigned int size) {
+	unsigned int allocation_size;
+	void *ptr;
+
+	sbrk_lock();
+	allocation_size = req_padding(size) + HDR_SIZE;
+
+	if (borderPtr == NULL) {
+
+		ptr = sbrk(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(ptr + HDR_SIZE,0);
+		markPrevInUse(ptr + HDR_SIZE);
+		borderPtr  = ptr;
+
+	} else {
+
+		ptr =  sbrk(allocation_size);
+
+		if ((ptr == (char *) -1)){
+			printf("sbrk Fail: out of Memory\n");
+			return NULL;
+		}
+		if((ptr != borderPtr + 2*HDR_SIZE)){
+			printf("sbrk Fail: Non-contiguous Memory\n");
+			return NULL;
+		}
+	}
+
+
+	ptr = borderPtr + HDR_SIZE;
+	borderPtr += allocation_size;
+	setHeaders(ptr,size);
+
+	sbrk_unlock();
+	return (void *) ptr;
+
+}
+

+ 6 - 0
sys_alloc.h

@@ -0,0 +1,6 @@
+#ifndef SYS_ALLOC_H
+#define SYS_ALLOC_H
+
+void *sys_alloc(unsigned int size);
+
+#endif /* SYS_ALLOC_H */

+ 55 - 0
test.c

@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include "heap.h"
+#include "other.h"
+#include "posix_lock.h"
+#include "dmm_init.h"
+
+void *first_fit_search(unsigned int size, NODE *starting_node,
+		char is_fixed_global) {
+	NODE *node;
+
+	node = starting_node;
+
+	while(node) {
+
+	}
+
+
+}
+
+void *custom_malloc(heap_t* heap, unsigned int size);
+
+void *custom_malloc(heap_t* heap, unsigned int size) {
+	void *ptr;
+	int fixed_list_id;
+
+	ptr = NULL;
+
+	posix_lock(heap);
+
+	fixed_list_id = map_size_to_list(heap, size);
+	
+	if(fixed_list_id != -1) {
+		// first fit from fixed list
+	}
+
+	if(ptr == NULL) {
+		// first fit from free list
+	}
+
+	posix_unlock(heap);
+
+	return ptr;
+}
+
+
+int main(void) {
+	allocator_t *myallocator;
+	heap_t *myheap;
+	int heap_id;
+
+	myallocator = dmm_init();
+	heap_id = map_thread_heap();
+	printf("This thread accesses heap %d\n", heap_id);
+	myheap = &myallocator->heaps[heap_id];
+}