ソースを参照

Added heap ownership information in memory blocks, coalescing currently uses it.

Ioannis Koutras 13 年 前
コミット
b671dfb720
共有5 個のファイルを変更した53 個の追加10 個の削除を含む
  1. 5 1
      DefineOptions.cmake
  2. 1 0
      dmm_config.h.in
  3. 24 0
      private-include/block_header.h
  4. 12 0
      src/block_header.c
  5. 11 9
      src/custom_free.c

+ 5 - 1
DefineOptions.cmake

@@ -6,11 +6,13 @@ option(WITH_STATIC_LIB "Build a static library" OFF)
 option(WITH_SHARED_LIB "Build a shared library" OFF)
 option(WITH_DOC "Build with documentation" OFF)
 
-set(NUM_HEAPS '0')
+set(NUM_HEAPS '1')
 
 set(WITH_COALESCING "never" "Build with coalescing support")
 set(WITH_SPLITTING "never" "Build with splitting support")
 
+option(WITH_OWNERSHIP "Build with ownership information in blocks" OFF)
+
 option(P2012 "Build for P2012 runtime" OFF)
 option(LINUXTEST "Build a case for Linux" ON) 
 
@@ -34,8 +36,10 @@ if (LINUXTEST)
 endif (LINUXTEST)
 
 if(WITH_COALESCING STREQUAL "fixed")
+  set(WITH_OWNERSHIP ON)
   set(COALESCING_FIXED ON)
 elseif(WITH_COALESCING STREQUAL "variable")
+  set(WITH_OWNERSHIP ON)
   set(WITH_KNOBS ON)
   set(COALESCING_VARIABLE ON)
 endif(WITH_COALESCING STREQUAL "fixed")

+ 1 - 0
dmm_config.h.in

@@ -21,6 +21,7 @@
 
 #cmakedefine WITH_KNOBS
 
+#cmakedefine WITH_OWNERSHIP
 
 #define MIN_FRAG_THRESHOLD 0.05
 

+ 24 - 0
private-include/block_header.h

@@ -11,6 +11,7 @@
 
 #include <stddef.h>
 #include <stdbool.h>
+#include <dmmlib/heap.h>
 
 /** The header structure of every memory block inside a heap. */
 typedef struct block_header_s {
@@ -23,6 +24,9 @@ typedef struct block_header_s {
 				part of the previous block in the memory space */
 	void *next; /**< The next memory block in the list that the current
 		      block belongs to */
+#ifdef WITH_OWNERSHIP
+	heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
+#endif /* WITH_OWNERSHIP */
 } block_header_t;
 
 /**
@@ -118,6 +122,26 @@ void set_previous_size_availability(void *ptr, size_t previous_size_availability
  */
 void set_next(void *block, void *next_block);
 
+#ifdef WITH_OWNERSHIP
+
+/**
+ * Set the heap owner of a memory block
+ *
+ * \param ptr 		The pointer to the data part of the memory block.
+ * \param heap_owner 	The pointer to the heap owner.
+ */
+void set_owner(void *ptr, heap_t *heap_owner);
+
+/**
+ * Get the heap owner of a memory block
+ *
+ * \param ptr 	The pointer to the data part of the memory block.
+ * \return 	The pointer to the heap owner.
+ */
+heap_t * get_owner(void *ptr);
+
+#endif /* WITH_OWNERSHIP */
+
 /**
  * Check if previous block (in the memory space) belongs to a free list
  */

+ 12 - 0
src/block_header.c

@@ -47,6 +47,18 @@ void set_next(void *ptr, void *next_block) {
     get_header(ptr)->next = next_block;
 }
 
+#ifdef WITH_OWNERSHIP
+
+void set_owner(void *ptr, heap_t *heap_owner) {
+    get_header(ptr)->heap_owner = heap_owner;
+}
+
+heap_t * get_owner(void *ptr) {
+    return get_header(ptr)->heap_owner;
+}
+
+#endif /* WITH_OWNERSHIP */
+
 bool is_previous_free(void *ptr) {
     if (get_header(ptr)->previous_size & (size_t) 1) {
         return false;

+ 11 - 9
src/custom_free.c

@@ -41,21 +41,23 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
 
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     if(is_previous_free(ptr)) {
+        if(get_owner(ptr) == get_owner(get_previous(ptr))) {
 
 #ifdef COALESCING_FIXED
-        max_coal_size = MAX_COALESCE_SIZE;
+            max_coal_size = MAX_COALESCE_SIZE;
 #endif /* COALESCING_FIXED */
 #ifdef COALESCING_VARIABLE
-        max_coal_size = heap->dmm_knobs.max_coalesce_size;
+            max_coal_size = heap->dmm_knobs.max_coalesce_size;
 #endif /* COALESCING_VARIABLE */
 
-        if(get_previous_size(ptr) + get_size(ptr) + HEADER_SIZE <=
-                max_coal_size) {
-            ptr = coalesce(ptr, heap, allocator);
-            coalesced = true;
-        } else {
-            mark_free(ptr);
-            coalesced = false;
+            if(get_previous_size(ptr) + get_size(ptr) + HEADER_SIZE <=
+                    max_coal_size) {
+                ptr = coalesce(ptr, heap, allocator);
+                coalesced = true;
+            } else {
+                mark_free(ptr);
+                coalesced = false;
+            }
         }
     } else {
         mark_free(ptr);