Parcourir la source

Introduced custom_realloc().

Ioannis Koutras il y a 13 ans
Parent
commit
7f7881f6eb
5 fichiers modifiés avec 168 ajouts et 0 suppressions
  1. 1 0
      CMakeLists.txt
  2. 2 0
      DefineOptions.cmake
  3. 56 0
      include/dmmlib/custom_realloc.h
  4. 7 0
      src/CMakeLists.txt
  5. 102 0
      src/custom_realloc.c

+ 1 - 0
CMakeLists.txt

@@ -64,5 +64,6 @@ if(MIN_SPLITTING_SIZE)
   endif(WITH_SPLITTING STREQUAL "fixed")
 endif(MIN_SPLITTING_SIZE)
 message(STATUS "Adaptivity: " ${WITH_ADAPTIVITY})
+message(STATUS "Support for realloc(): " ${WITH_REALLOC})
 message(STATUS "********************************************")
 

+ 2 - 0
DefineOptions.cmake

@@ -1,4 +1,5 @@
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" ON)
+option(WITH_REALLOC "Build with realloc" OFF)
 option(WITH_EXAMPLES "Build with examples" OFF)
 option(WITH_MEMORY_SPACE_AWARENESS "Build with memory space awareness" OFF)
 option(WITH_FIXED_LISTS "Build with predefined lists of fixed-sized blocks" ON)
@@ -62,6 +63,7 @@ if (LINUXTEST)
   set(MIN_SPLITTING_SIZE 300)
   set(WITH_SHARED_LIB ON)
   set(WITH_DOC ON)
+  set(WITH_REALLOC ON)
   set(BLOCKS_ORGANIZATION "dll")
 endif (LINUXTEST)
 

+ 56 - 0
include/dmmlib/custom_realloc.h

@@ -0,0 +1,56 @@
+/*
+ *   Copyright 2011 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    custom_realloc.h
+ * \author  Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date    November, 2011
+ * \brief   Function prototype of realloc for the dmmlib allocator.
+ */
+
+#ifndef CUSTOM_REALLOC_H
+#define CUSTOM_REALLOC_H
+#include "dmm_config.h"
+#include <dmmlib/dmmlib.h>
+#include <dmmlib/heap.h>
+
+/**
+ * Try to change the size of an allocation
+ *
+ * \param allocator 	The pointer to the allocator who manages the block.
+ * \param heap 		The pointer to the heap who manages the block.
+ * \param ptr 		The pointer to the data part of the original memory block.
+ * \param size 		The new desired size of the block.
+ *
+ * \return 		The pointer to the data part of the memory block which
+ * has the new, desired size.
+ */ 
+void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size);
+
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+/**
+ * Try to change the size of an allocation
+ * \param   ptr	    The pointer to the data part of the original memory block.
+ * \param   size    The new desired size of the block.
+ *
+ * \return  The pointer to the data part of the memory block which
+ * has the new, desired size.
+ */
+void * custom_realloc(void *ptr, size_t size);
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+#endif /* CUSTOM_REALLOC_H */
+

+ 7 - 0
src/CMakeLists.txt

@@ -68,6 +68,13 @@ if (WITH_STATS)
   )
 endif (WITH_STATS)
 
+if (WITH_REALLOC)
+  set(dmmlib_SRCS
+    ${dmmlib_SRCS}
+    custom_realloc.c
+  )
+endif (WITH_REALLOC)
+
 if (WITH_ADAPTIVITY)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}

+ 102 - 0
src/custom_realloc.c

@@ -0,0 +1,102 @@
+/*
+ *   Copyright 2011 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    custom_realloc.c
+ * \author  Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date    November, 2011
+ *
+ * \brief   Implementation of a function to change the size of an allocated
+ * block.
+ */
+
+#include <dmmlib/custom_realloc.h>
+#include <dmmlib/dmmlib.h>
+#include <dmmlib/initialize_allocator.h>
+#include "block_header.h"
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+#include "other.h"
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+/**
+ * Copy a memory area
+ */
+static void * custom_memcpy(void* dest, const void* src, size_t n) {
+    char *dst8, *src8;
+    
+    dst8 = (char *) dest;
+    src8 = (char *) src;
+    
+    while(n--) {
+        *dst8++ = *src8++;
+    }
+
+    return dest;
+}
+
+/**
+ * \details The realloc() function tries to change the size of the allocation
+ * pointed to by ptr to size, and returns ptr. realloc() creates a new
+ * allocation, copies as much of the old data pointed to by ptr as will fit to
+ * the new allocation, frees the old allocation and returns a pointer to the
+ * allocated memory. If ptr is NULL, realloc() is identical to a call to
+ * malloc() for size bytes.
+ */
+void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size) {
+    void *new_ptr;
+    size_t old_size;
+
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+    int heap_id;
+
+    /* Go to the system allocator if none was given */
+    if(allocator == NULL) {
+        allocator = &systemallocator;
+        if(allocator->initialized != true) {
+            initialize_allocator(allocator);
+        }
+    }
+
+    if(heap == NULL) {
+        heap_id = map_thread_heap();
+        heap = &allocator->heaps[heap_id];
+    }
+
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+    new_ptr = custom_ahmalloc(allocator, heap, size);
+
+    if(ptr != NULL) {
+        old_size = get_size(ptr);
+        if(old_size < size) {
+            new_ptr = custom_memcpy(new_ptr, ptr, old_size);
+        } else {
+            new_ptr = custom_memcpy(new_ptr, ptr, size);
+        }
+        custom_ahfree(allocator, heap, ptr);
+    }
+
+    return new_ptr;
+
+}
+
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+void * custom_realloc(void *ptr, size_t size) {
+    return custom_ahrealloc(NULL, NULL, ptr, size);
+}
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+