瀏覽代碼

Initial code for split.

Ioannis Koutras 13 年之前
父節點
當前提交
57a68f4982
共有 6 個文件被更改,包括 84 次插入3 次删除
  1. 20 3
      private-include/block_header.h
  2. 3 0
      private-include/split.h
  3. 1 0
      src/CMakeLists.txt
  4. 4 0
      src/block_header.c
  5. 3 0
      src/custom_malloc.c
  6. 53 0
      src/split.c

+ 20 - 3
private-include/block_header.h

@@ -50,6 +50,16 @@ void * get_next(void *ptr);
 size_t get_size(void *ptr);
 
 /**
+ * Get the requested size of the memory block's data
+ *
+ * \param ptr The pointer to the data part of the current memory block.
+ *
+ * \return The size of the data that was initialy requested for this memory
+ * block.
+ */
+size_t get_requested_size(void *ptr);
+
+/**
  * Get all information of the memory block header's size record
  *
  * \param ptr The pointer to the data part of the current memory block.
@@ -91,12 +101,11 @@ void mark_used(void *ptr);
 void mark_free(void *ptr);
 
 /**
- * Set the availability and the size of the previous memory block's data
+ * Set the availability and the size of the previous memory block
  *
- * \param ptr 		The pointer to the data part of the current memory block.
+ * \param ptr 		The pointer to the data part of the previous memory block.
  * \param previous_size_availability The size for the data part of the previous
  * memory block on data layout level.
- *
  */
 void set_previous_size_availability(void *ptr, size_t previous_size_availability);
 
@@ -121,6 +130,14 @@ bool is_previous_free(void *ptr);
  */
 size_t get_previous_size(void *ptr);
 
+/**
+ * Get the size and the availability of the previous block (in the memory
+ * space) 
+ *
+ * \param ptr 	The pointer to the data part of the previous memory block.
+ * \param previous_size_availability The size for the data part of the previous
+ * memory block on data layout level.
+ */
 size_t get_previous_size_availability(void *ptr);
 
 /**

+ 3 - 0
private-include/split.h

@@ -0,0 +1,3 @@
+#include <dmmlib/heap.h>
+
+void split(allocator_t *allocator, heap_t *heap, void *ptr);

+ 1 - 0
src/CMakeLists.txt

@@ -36,6 +36,7 @@ set(dmmlib_SRCS
   other.c
   initialize_allocator.c
   sys_alloc.c
+  split.c
 )
 
 if (HAVE_LOCKS)

+ 4 - 0
src/block_header.c

@@ -19,6 +19,10 @@ size_t get_size(void *ptr) {
     return get_header(ptr)->size >> 1;
 }
 
+size_t get_requested_size(void *ptr) {
+    return get_header(ptr)->requested_size;
+}
+
 size_t get_size_availability(void *ptr) {
     return get_header(ptr)->size;
 }

+ 3 - 0
src/custom_malloc.c

@@ -41,6 +41,7 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     posix_lock(heap);
 #endif /* HAVE_LOCKS */
 
+    /* Perform exact fit to fixed lists */
     fixed_list_id = map_size_to_list(heap, req_padding(size));
 
     /* If a fixed list is found, do a first fit */
@@ -82,6 +83,8 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         set_requested_size(ptr, size);
         mark_used(ptr);
 
+        /* FIXME split to be put here */
+
         /* Update the used blocks list */
         set_next(ptr, heap->used_blocks_head);
         heap->used_blocks_head = ptr;

+ 53 - 0
src/split.c

@@ -0,0 +1,53 @@
+#include "block_header.h"
+#include "other.h"
+#include "split.h"
+
+void split(allocator_t *allocator, heap_t *heap, void *ptr) {
+    void *new_block;
+    size_t new_size;
+    int fixed_list_id, i;
+    maptable_node_t *current_maptable_node;
+    
+    new_size = get_size(ptr) - get_requested_size(ptr) - HEADER_SIZE;
+
+    /* FIXME This check should be done one level above */
+    if(new_size > 0) {
+        new_block = (void *) ((char *) ptr + get_requested_size(ptr) + 
+                HEADER_SIZE);
+
+        set_size(ptr, get_requested_size(ptr));
+        /* set_size() resets the availability of the block */
+        mark_used(ptr);
+
+        set_size(new_block, new_size);
+        set_previous_size_availability(new_block, get_size_availability(ptr));
+
+        /* FIXME code from custom_free, some refactoring maybe?
+         * Be careful, custom_free also does coalescing, we don't need that
+         */
+
+        /* Check if the block could be put in a fixed list */
+        fixed_list_id = map_size_to_list(heap, new_size);
+
+        if(fixed_list_id != -1) {
+            current_maptable_node = heap->maptable_head;		
+            if(fixed_list_id != 0) {
+                for(i = 1; i < fixed_list_id; i++) {
+                    current_maptable_node = current_maptable_node->next;
+                }
+            }
+            set_next(new_block, current_maptable_node->fixed_list_head);
+            current_maptable_node->fixed_list_head = new_block;
+        } else { /* put it in the free list */
+            set_next(new_block, heap->free_list_head);
+            heap->free_list_head = new_block;
+        }
+        
+        mark_free(ptr);
+
+        if(allocator->border_ptr == ptr) {
+            allocator->border_ptr = new_block;
+        }
+
+    }
+}