Quellcode durchsuchen

Introduce is_free() and get_dlnext() to allow coalescing the current and next block.

Ioannis Koutras vor 13 Jahren
Ursprung
Commit
781f8072c3
2 geänderte Dateien mit 36 neuen und 1 gelöschten Zeilen
  1. 19 0
      private-include/block_header.h
  2. 17 1
      src/block_header.c

+ 19 - 0
private-include/block_header.h

@@ -172,6 +172,11 @@ heap_t * get_owner(void *ptr);
 #endif /* WITH_OWNERSHIP */
 
 /**
+ * Check if a memory block is free
+ */
+bool is_free(void *ptr);
+
+/**
  * Check if previous block (in the memory space) belongs to a free list
  */
 bool is_previous_free(void *ptr);
@@ -195,7 +200,21 @@ size_t get_previous_size_availability(void *ptr);
  * Get the previous memory block on data layout level
  *
  * \param ptr 	The pointer to the data part of the current memory block.
+ *
+ * \return 	The pointer to the data part of the previous memory block on
+ * data layout level. 
  */
 void * get_dlprevious(void *ptr);
 
+/**
+ * Get the next memory block on data layout level if there is one
+ *
+ * \param allocator 	The pointer to the allocator who manages the block.
+ * \param ptr 		The pointer to the data part of the current memory block.
+ *
+ * \return The pointer to the next block.
+ * \retval NULL There is no next block.
+ */
+void * get_dlnext(allocator_t *allocator, void *ptr);
+
 #endif /* BLOCK_HEADER_H */

+ 17 - 1
src/block_header.c

@@ -100,8 +100,16 @@ heap_t * get_owner(void *ptr) {
 
 #endif /* WITH_OWNERSHIP */
 
+bool is_free(void *ptr) {
+    if(get_header(ptr)->size & (size_t) 1) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
 bool is_previous_free(void *ptr) {
-    if (get_header(ptr)->previous_size & (size_t) 1) {
+    if(get_header(ptr)->previous_size & (size_t) 1) {
         return false;
     } else {
         return true;
@@ -124,3 +132,11 @@ void * get_dlprevious(void *ptr) {
     return (void *) ((char *) ptr - HEADER_SIZE - get_previous_size(ptr));
 }
 
+void * get_dlnext(allocator_t *allocator, void *ptr) {
+    if(allocator->border_ptr != ptr) {
+        return (void *) ((char) ptr + get_size(ptr));
+    } else {
+        return NULL;
+    }
+}
+