ソースを参照

Created function to search fixed lists.

Ioannis Koutras 13 年 前
コミット
3636ba9139
共有4 個のファイルを変更した58 個の追加19 個の削除を含む
  1. 26 0
      private-include/sll/search_on_fixed.h
  2. 1 0
      src/CMakeLists.txt
  3. 3 19
      src/custom_malloc.c
  4. 28 0
      src/sll/search_on_fixed.c

+ 26 - 0
private-include/sll/search_on_fixed.h

@@ -0,0 +1,26 @@
+/**
+ * \file 	search_on_fixed.h
+ * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date 	September, 2011
+ *  
+ * \brief Exact-fit search on fixed lists
+ */
+
+#ifndef SEARCH_ON_FIXED_H
+#define SEARCH_ON_FIXED_H
+
+#include <dmmlib/heap.h>
+
+/**
+ * Perform an exact-fit search on fixed lists for a block of a certain size
+ *
+ * \param heap The heap whose fixed lists should be accessed.
+ * \param requested_size The desired size of the block.
+ *
+ * \return The pointer of the data part of the matched memory block.
+ * \retval NULL No block was found.
+ */
+void * search_on_fixed(heap_t * heap, size_t requested_size);
+
+#endif /* SEARCH_ON_FIXED_H */
+

+ 1 - 0
src/CMakeLists.txt

@@ -30,6 +30,7 @@ endif (WITH_STATIC_LIB)
 
 set(dmmlib_SRCS
   block_header.c
+  sll/search_on_fixed.c
   custom_free.c
   custom_malloc.c
   other.c

+ 3 - 19
src/custom_malloc.c

@@ -8,14 +8,14 @@
 #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
 #include <dmmlib/initialize_allocator.h>
 #include "other.h"
+#include "sll/search_on_fixed.h"
 #include "sys_alloc.h"
 #include "block_header.h"
 #include "dmm_adaptor.h"
 
 void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     void *ptr;
-    int heap_id, fixed_list_id, i;
-    maptable_node_t *current_maptable_node;
+    int heap_id;
     void *current_block, *previous_block;
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
     size_t new_size;
@@ -46,23 +46,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 */
-    if(fixed_list_id != -1) {
-        current_maptable_node = heap->maptable_head;
-        /* traverse through the maptable node list */
-        if(fixed_list_id != 0) {
-            for(i = 1; i < fixed_list_id; i++) {
-                current_maptable_node = current_maptable_node->next;
-            }
-        }
-        if(current_maptable_node->fixed_list_head != NULL) {
-            ptr = current_maptable_node->fixed_list_head;
-            current_maptable_node->fixed_list_head = get_next(ptr);
-        }
-    }
+    ptr = search_on_fixed(heap, req_padding(size));
 
     if(ptr == NULL) {
 

+ 28 - 0
src/sll/search_on_fixed.c

@@ -0,0 +1,28 @@
+#include "sll/search_on_fixed.h"
+#include "block_header.h"
+
+/**
+ * \details The maptable of the heap is being traversed sequentially while
+ * searching for a size equal of the requested size. If one is found, then we
+ * return the head of this list and set the next block as the new head.
+ */
+void * search_on_fixed(heap_t * heap, size_t requested_size) {
+    maptable_node_t *node;
+    void *ptr;
+
+    node = heap->maptable_head;
+    ptr = NULL;
+
+    while(node) {
+        if(node->size == requested_size) {
+            ptr = node->fixed_list_head;
+            if(ptr != NULL) {
+                node->fixed_list_head = get_next(ptr);
+            }
+            break;
+        }
+        node = node->next;
+    }
+
+    return ptr;
+}