Browse Source

Initial realloc support for memory blocks on bitmap-organised raw blocks

Currently a new malloc() takes place, data is moved and the previous memory
location is free'd.
Ioannis Koutras 13 years ago
parent
commit
af04640fb3
4 changed files with 90 additions and 0 deletions
  1. 3 0
      private-include/bitmap/bitmap.h
  2. 1 0
      src/CMakeLists.txt
  3. 54 0
      src/bitmap/bitmap_realloc.c
  4. 32 0
      src/dmmlib.c

+ 3 - 0
private-include/bitmap/bitmap.h

@@ -36,4 +36,7 @@ void * bitmap_malloc(raw_block_header_t *raw_block, size_t size);
 
 
 void bitmap_free(raw_block_header_t *raw_block, void *ptr);
 void bitmap_free(raw_block_header_t *raw_block, void *ptr);
 
 
+void * bitmap_realloc(raw_block_header_t *raw_block, void * ptr,
+        size_t req_size);
+
 #endif /* BITMAP_H */
 #endif /* BITMAP_H */

+ 1 - 0
src/CMakeLists.txt

@@ -91,6 +91,7 @@ elseif(RAW_BLOCKS_TYPE STREQUAL "bitmap")
     bitmap/bitmap_other.c
     bitmap/bitmap_other.c
     bitmap/bitmap_malloc.c
     bitmap/bitmap_malloc.c
     bitmap/bitmap_free.c
     bitmap/bitmap_free.c
+    bitmap/bitmap_realloc.c
     )
     )
 
 
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")

+ 54 - 0
src/bitmap/bitmap_realloc.c

@@ -0,0 +1,54 @@
+/*
+ *   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   bitmap_realloc.c
+ * @author Ilias Pliotas, Ioannis Koutras
+ * @date   September, 2012
+ * @brief  realloc() implementation for bitmap-organized raw blocks
+ */
+
+#include "bitmap/bitmap.h"
+#include "bitmap/bitmap_rb.h"
+#include "dmmlib/dmmlib.h"
+
+#include <string.h>
+
+/**
+ * Reallocates a memory block from a bitmap-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 * bitmap_realloc(raw_block_header_t *raw_block, void * ptr,
+        size_t req_size) { 
+    void *ret;
+    bitmap_rb_t *rb_header;
+    chunk_header_t *chunk_header;
+
+    rb_header = (bitmap_rb_t *)((char *)raw_block + sizeof(raw_block_header_t));
+    chunk_header = (chunk_header_t *)((char *)ptr - CHUNK_HDR_SIZE);
+
+    ret = malloc(req_size);
+    ret = memmove(ret, ptr,
+            chunk_header->num_of_cells * rb_header->bytes_per_cell);
+    bitmap_free(raw_block, ptr);
+    return ret;
+}

+ 32 - 0
src/dmmlib.c

@@ -115,6 +115,38 @@ void free(void *ptr) {
     }
     }
 }
 }
 
 
+void * realloc(void *ptr, size_t size) {
+    raw_block_header_t *current_raw_block;
+    bool found;
+
+    if(ptr == NULL) {
+        return malloc(size);
+    }
+
+    if(size == 0) {
+        free(ptr);
+        return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
+    }
+
+    found = false;
+
+    current_raw_block = systemallocator.raw_block_head;
+    while(current_raw_block) {
+        if((char *)ptr - (char *)(current_raw_block) -
+                sizeof(raw_block_header_t) < current_raw_block->size) {
+            found = true;
+            break;
+        }
+        current_raw_block = current_raw_block->next_raw_block;
+    }
+
+    if(found == true) {
+        return bitmap_realloc(current_raw_block, ptr, size);
+    } else {
+        return NULL;
+    }
+}
+
 void * calloc(size_t nmemb, size_t size) {
 void * calloc(size_t nmemb, size_t size) {
     size_t i;
     size_t i;
     char *buf[nmemb];
     char *buf[nmemb];