Ver código fonte

Added functions to find previous memory blocks in terms of data layout. Initial maptable nodes require now a memory block. Simplified initialize_allocator.

Ioannis Koutras 13 anos atrás
pai
commit
eda81eee4e

+ 42 - 20
private-include/block_header.h

@@ -2,7 +2,8 @@
  * \file 	block_header.h
  * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
  * \date 	September, 2011
- * \brief 	Block header structure and functions, and memory block functions.
+ *  
+ * \brief Block header structure and functions, and memory block functions.
  */
 
 #ifndef BLOCK_HEADER_H
@@ -11,25 +12,26 @@
 #include <stddef.h>
 #include <stdbool.h>
 
-/**
- * \brief The header structure of every memory block inside a heap.
- */
+/** The header structure of every memory block inside a heap. */
 typedef struct block_header_s {
-	size_t size; /**< \brief The size of the data part. */
-	size_t requested_size; /**< \brief The requested size of the data part */
-	size_t previous_size; /**< \brief The size of the data part of the
-				previous block in the memory space */
-	void *next; /**< \brief The next memory block in the list that the
-		      current block belongs to */
+	size_t size; /**< The LSB represents the availability of the block (1
+		       for used, 0 for free), the rest the size of the data
+		       part. */
+	size_t requested_size; /**< The requested size of the data part */
+	size_t previous_size; /**< The LSB represents the availability of the
+				previous block, the rest the size of the data
+				part of the previous block in the memory space */
+	void *next; /**< The next memory block in the list that the current
+		      block belongs to */
 } block_header_t;
 
 /**
- * \brief The size of the header in number of bytes
+ * The size of the header in number of bytes
  */
 #define HEADER_SIZE sizeof(block_header_t)
 
 /**
- * \brief Get the next memory block.
+ * Get the next memory block.
  *
  * \param ptr The pointer to the data part of the current memory block.
  *
@@ -39,7 +41,7 @@ typedef struct block_header_s {
 void * get_next(void *ptr);
 
 /**
- * \brief Get the size of the memory block's data
+ * Get the size of the memory block's data
  *
  * \param ptr The pointer to the data part of the current memory block.
  *
@@ -48,7 +50,17 @@ void * get_next(void *ptr);
 size_t get_size(void *ptr);
 
 /**
- * \brief Set the size of the memory block's data
+ * Get all information of the memory block header's size record
+ *
+ * \param ptr The pointer to the data part of the current memory block.
+ *
+ * \return The availability and the size of the data part of the current memory
+ * block.
+ */
+size_t get_size_availability(void *ptr);
+
+/**
+ * Set the size of the memory block's data
  *
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The size of the data part of the current memory block.
@@ -56,7 +68,7 @@ size_t get_size(void *ptr);
 void set_size(void *ptr, size_t size);
 
 /**
- * \brief Set the requested size of memory block's data
+ * Set the requested size of memory block's data
  *
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The requested size for the data part of the current memory
@@ -65,7 +77,17 @@ void set_size(void *ptr, size_t size);
 void set_requested_size(void *ptr, size_t size);
 
 /**
- * \brief Set the next memory block of a block
+ * Set the availability and the size of the previous memory block's data
+ *
+ * \param ptr 		The pointer to the data part of the current memory block.
+ * \param previous_size The size for the data part of the previous memory 
+ * block on data layout level.
+ *
+ */
+void set_previous_size(void *ptr, size_t previous_size);
+
+/**
+ * Set the next memory block of a block
  *
  * \param block 	The pointer to the data part of the current memory
  * block.
@@ -74,26 +96,26 @@ void set_requested_size(void *ptr, size_t size);
 void set_next(void *block, void *next_block);
 
 /**
- * \brief Check if previous block (in the memory space) belongs to a free list
+ * Check if previous block (in the memory space) belongs to a free list
  */
 bool is_previous_free(void *ptr);
 
 /**
- * \brief Get the size of the previous block (in the memory space) 
+ * Get the size of the previous block (in the memory space) 
  *
  * \param ptr 	The pointer to the data part of the current memory block.
  */
 size_t get_previous_size(void *ptr);
 
 /**
- * \brief Get the previous memory block (in the memory space)
+ * Get the previous memory block (in the memory space)
  *
  * \param ptr 	The pointer to the data part of the current memory block.
  */
 void * get_previous(void *ptr);
 
 /**
- * \brief Removes a memory block from a singly linked list of memory blocks.
+ * Removes a memory block from a singly linked list of memory blocks.
  *
  * \param *block The block to be removed.
  * \param *starting_node The starting memory block of the list.

+ 0 - 4
private-include/sys_alloc.h

@@ -1,10 +1,6 @@
 #ifndef SYS_ALLOC_H
 #define SYS_ALLOC_H
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-void *sys_alloc(heap_t *heap, size_t size);
-#else
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size);
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
 
 #endif /* SYS_ALLOC_H */

+ 8 - 0
src/block_header.c

@@ -16,6 +16,10 @@ void * get_next(void *ptr) {
 }
 
 size_t get_size(void *ptr) {
+    return get_header(ptr)->size >> 1;
+}
+
+size_t get_size_availability(void *ptr) {
     return get_header(ptr)->size;
 }
 
@@ -35,6 +39,10 @@ bool is_previous_free(void *ptr) {
     return (bool) (get_header(ptr)->previous_size & 1);
 }
 
+void set_previous_size(void *ptr, size_t previous_size) {
+    get_header(ptr)->previous_size = previous_size;
+}
+
 size_t get_previous_size(void *ptr) {
     return get_header(ptr)->previous_size >> 1;
 }

+ 0 - 4
src/custom_malloc.c

@@ -77,11 +77,7 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     }
 
     if(ptr == NULL) {
-#ifdef WITH_MEMORY_SPACE_AWARENESS
         ptr = sys_alloc(allocator, heap, size);
-#else
-        ptr = sys_alloc(heap, size);
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
     }
 
     set_requested_size(ptr, size);

+ 11 - 11
src/initialize_allocator.c

@@ -6,6 +6,7 @@
 #endif /* HAVE_LOCKS */
 #include <dmmlib/initialize_allocator.h>
 #include "dmm_config.h"
+#include "sys_alloc.h"
 
 #ifdef WITH_MEMORY_SPACE_AWARENESS
 void initialize_allocator(allocator_t *allocator, void *starting_address, size_t size) {
@@ -40,23 +41,19 @@ void initialize_allocator(allocator_t *allocator) {
 #endif /* HAVE_LOCKS */
     }
 
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+    allocator->border_ptr = NULL;
+#else
+    allocator->border_ptr = starting_address;
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
     // 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)
     // 2 * 4 + 2 * 2 = 12 maptable nodes
     current_heap = &allocator->heaps[0];
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    maptablenode = (maptable_node_t *) sbrk((int)(12*(sizeof(maptable_node_t))));
-#else
-    if(12*(sizeof(maptable_node_t)) < size) {
-        maptablenode = (maptable_node_t *) starting_address;
-        allocator->border_ptr = starting_address + 12*(sizeof(maptable_node_t));
-        allocator->remaining_size = size - 12*(sizeof(maptable_node_t));
-    } else {
-        // Houston, we have a problem
-    }
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+    maptablenode = (maptable_node_t *) sys_alloc(allocator, &allocator->heaps[0], 12*(sizeof(maptable_node_t)));
 
     maptablenode->size = 32;
     maptablenode->fixed_list_head = NULL;
@@ -105,6 +102,9 @@ void initialize_allocator(allocator_t *allocator) {
     (maptablenode+1)->fixed_list_head = NULL;
     (maptablenode+1)->next = NULL;
 
+#ifndef WITH_MEMORY_SPACE_AWARENESS
     allocator->initialized = true;
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+    
 }
 

+ 17 - 6
src/sys_alloc.c

@@ -11,13 +11,9 @@
 #include "sys_alloc.h"
 #include "block_header.h"
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-void *sys_alloc(heap_t *heap, size_t size) {
-#else
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
 
-    size_t allocation_size;
+    size_t allocation_size, previous_size;
     void *ptr;
 
 #ifdef HAVE_LOCKS
@@ -26,25 +22,40 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
     allocation_size = req_padding(size) + HEADER_SIZE;
 
+    if(allocator->border_ptr != NULL) {
+        previous_size = get_size_availability(allocator->border_ptr);
+    } else {
+        previous_size = 0;
+    }
+
 #ifndef WITH_MEMORY_SPACE_AWARENESS
     ptr = sbrk((int) allocation_size);
     if(ptr == (void *) -1) {
         printf("sbrk problem for size of: %zu\n", allocation_size);
         printf( "Error on sbrk: %s\n", strerror( errno ) );
+    }   
+    if(allocator->border_ptr != NULL &&
+            ptr != (void *) ((char *) allocator->border_ptr + previous_size)) {
+        printf("sbrk() does not return sequential space.\n");
     }
 #else
     if(allocator->remaining_size >= allocation_size) {
         ptr = allocator->border_ptr;
-        allocator->border_ptr = (char *) allocator->border_ptr + allocation_size;
         allocator->remaining_size -= allocation_size;
     } else {
         printf("No more free space.\n");
     }
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
 
+    allocator->border_ptr = (char *) allocator->border_ptr + allocation_size;
+
     ptr = (void *) ((char *) ptr + HEADER_SIZE);
 
+    /* Set some values for block header */
     set_size(ptr, req_padding(size));
+    set_previous_size(ptr, previous_size);
+
+    /* Update stats */
     heap->dmm_stats.mem_allocated += req_padding(size);
     heap->dmm_stats.mem_requested += size;