Ver código fonte

realloc() for freelist-organized raw blocks

Ioannis Koutras 12 anos atrás
pai
commit
16499deb6f

+ 2 - 0
private-include/freelist/freelist.h

@@ -39,4 +39,6 @@ void * freelist_malloc(raw_block_header_t *raw_block, size_t size);
 
 void freelist_free(raw_block_header_t *raw_block, void *ptr);
 
+void * freelist_realloc(raw_block_header_t *raw_block, void *ptr, size_t size);
+
 #endif /* FREELIST_H */

+ 1 - 0
src/CMakeLists.txt

@@ -43,6 +43,7 @@ if(RAW_BLOCKS_TYPE STREQUAL "freelist")
     freelist/linked_lists/search_algorithms.c
     freelist/freelist_malloc.c
     freelist/freelist_free.c
+    freelist/freelist_realloc.c
     )
 
   if(SORT_POLICY STREQUAL "lifo")

+ 10 - 0
src/dmmlib.c

@@ -185,7 +185,12 @@ void * realloc(void *ptr, size_t size) {
 
     if(size == 0) {
         free(ptr);
+#ifdef BITMAP_RB_ONLY
         return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        return malloc((size_t) 32); // FIXME 32 <- minimum size
+#endif /* FL_RB_ONLY */
     }
 
     found = false;
@@ -201,7 +206,12 @@ void * realloc(void *ptr, size_t size) {
     }
 
     if(found == true) {
+#ifdef BITMAP_RB_ONLY
         return bitmap_realloc(current_raw_block, ptr, size);
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        return freelist_realloc(current_raw_block, ptr, size);
+#endif /* FL_RB_ONLY */
     } else {
         return NULL;
     }

+ 52 - 0
src/freelist/freelist_realloc.c

@@ -0,0 +1,52 @@
+/*
+ *   Copyright 2012 Institute of Communication and Computer Systems (ICCS) 
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+/**
+ * @file   freelist_realloc.c
+ * @author Ioannis Koutras
+ * @date   September, 2012
+ * @brief  realloc() implementation for freelist-organized raw blocks
+ */
+
+#include "freelist/freelist.h"
+#include "freelist/block_header.h"
+#include "dmmlib/dmmlib.h"
+
+#include <string.h>
+
+/**
+ * Reallocates a memory block from a freelist-organized raw block
+ *
+ * @param  raw_block The pointer of the raw block.
+ * @param  ptr       The pointer of the memory block to be re-allocated.
+ * @param  req_size  The requested memory size.
+ * @retval           The address to serve the request.
+ * @retval NULL      No available memory space.
+ */
+void * freelist_realloc(raw_block_header_t *raw_block, void * ptr,
+        size_t req_size) { 
+    block_header_t *block;
+    void *ret;
+
+    block = (block_header_t *)((char *) ptr - HEADER_SIZE);
+
+    ret = malloc(req_size);
+    ret = memmove(ret, ptr,
+            block->size);
+    freelist_free(raw_block, ptr);
+    return ret;
+}