浏览代码

Major code reorganization; introduction of raw blocks

Ioannis Koutras 12 年之前
父节点
当前提交
62b394c0ba
共有 56 个文件被更改,包括 1377 次插入1906 次删除
  1. 1 1
      CMakeLists.txt
  2. 10 22
      DefineOptions.cmake
  3. 2 2
      dmm_config.h.in
  4. 1 1
      examples/CMakeLists.txt
  5. 12 12
      examples/larson.c
  6. 55 0
      include/dmmlib/block_header.h
  7. 3 3
      include/dmmlib/custom_realloc.h
  8. 1 4
      include/dmmlib/dmmlib.h
  9. 0 163
      include/dmmlib/heap.h
  10. 93 0
      include/dmmlib/pool.h
  11. 71 0
      include/dmmlib/raw_block.h
  12. 5 6
      include/dmmlib/single_allocator.h
  13. 5 0
      private-include/debug.h
  14. 31 78
      private-include/block_header.h
  15. 3 3
      private-include/coalesce.h
  16. 35 0
      private-include/freelist/freelist_malloc.h
  17. 4 4
      private-include/linked_lists/add_block.h
  18. 0 0
      private-include/freelist/linked_lists/add_block_in_order.h
  19. 0 0
      private-include/freelist/linked_lists/address_order.h
  20. 0 0
      private-include/freelist/linked_lists/fifo_order.h
  21. 7 22
      private-include/linked_lists/lifo_order.h
  22. 71 0
      private-include/freelist/linked_lists/linked_lists.h
  23. 9 22
      private-include/linked_lists/search_algorithms.h
  24. 0 0
      private-include/freelist/linked_lists/size_order.h
  25. 3 3
      private-include/split.h
  26. 0 131
      private-include/linked_lists/linked_lists.h
  27. 2 8
      private-include/other.h
  28. 40 0
      private-include/request_memory.h
  29. 2 2
      private-include/sys_alloc.h
  30. 37 24
      src/CMakeLists.txt
  31. 0 144
      src/block_header.c
  32. 0 143
      src/coalesce.c
  33. 0 160
      src/custom_free.c
  34. 0 125
      src/custom_malloc.c
  35. 42 0
      src/debug.c
  36. 130 0
      src/freelist/block_header_funcs.c
  37. 113 0
      src/freelist/coalesce.c
  38. 156 0
      src/freelist/freelist_malloc.c
  39. 0 0
      src/freelist/linked_lists/add_block_in_order.c
  40. 0 0
      src/freelist/linked_lists/address_order.c
  41. 0 0
      src/freelist/linked_lists/fifo_order.c
  42. 8 11
      private-include/posix_lock.h
  43. 79 0
      src/freelist/linked_lists/linked_lists.c
  44. 25 70
      src/linked_lists/search_algorithms.c
  45. 0 0
      src/freelist/linked_lists/size_order.c
  46. 110 0
      src/freelist/split.c
  47. 0 170
      src/initialize_allocator.c
  48. 0 147
      src/linked_lists/linked_lists.c
  49. 2 4
      src/other.c
  50. 39 0
      src/raw_block.c
  51. 49 0
      src/request_memory_mmap_linux.c
  52. 21 20
      src/linked_lists/lifo_order.c
  53. 17 17
      src/posix_lock.c
  54. 83 56
      src/single_allocator.c
  55. 0 161
      src/split.c
  56. 0 167
      src/sys_alloc.c

+ 1 - 1
CMakeLists.txt

@@ -11,7 +11,7 @@ macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out of source buil
 
 if(CMAKE_COMPILER_IS_GNUCC AND LINUXTEST)
   add_definitions(-std=c99)
-  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wuninitialized -Wconversion -Wstrict-prototypes")  
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wconversion -Wstrict-prototypes")  
 endif(CMAKE_COMPILER_IS_GNUCC AND LINUXTEST)
 
 configure_file (

+ 10 - 22
DefineOptions.cmake

@@ -18,11 +18,7 @@ option(SORT_POLICY "Choose the block sorting policy, options are: lifo, fifo, si
 
 set(REQUEST_SIZE_INFO ON)
 
-set(NUM_HEAPS 1)
-
-if (NUM_HEAPS GREATER 1)
-  set(MULTIPLE_HEAPS ON)
-endif (NUM_HEAPS GREATER 1)
+set(NUM_POOLS 1)
 
 set(WITH_SYSTEM_CALLS "none")
 
@@ -46,7 +42,7 @@ if (P2012)
   set(HAVE_LOCKS OFF)
   set(WITH_SYSTEM_CALLS "none")
   set(WITH_STATIC_LIB ON)
-  set(NUM_HEAPS 1)
+  set(NUM_POOLS 1)
   set(LINUXTEST OFF)
   set(WITH_FIXED_LISTS OFF)
   set(WITH_COALESCING "never")
@@ -59,7 +55,7 @@ if (LEON3)
   set(HAVE_LOCKS OFF)
   set(WITH_SYSTEM_CALLS "none")
   set(WITH_STATIC_LIB ON)
-  set(NUM_HEAPS 1)
+  set(NUM_POOLS 1)
   set(LINUXTEST OFF)
   set(WITH_FIXED_LISTS OFF)
   set(COUNT_HOPS ON)
@@ -70,14 +66,14 @@ endif (LEON3)
 
 if (LINUXTEST)
   set(WITH_SYSTEM_CALLS "mmap")
-  set(SYS_ALLOC_SIZE 1024000000)
+  set(SYS_ALLOC_SIZE 60000)
   set(WITH_FIXED_LISTS OFF)
-  set(SORT_POLICY "fifo")
+  set(SORT_POLICY "lifo")
   set(SEARCH_POLICY "first")
   set(FIT_PERCENTAGE 0.6f)
   set(HAVE_LOCKS ON)
   set(WITH_EXAMPLES ON)
-  set(WITH_COALESCING "fixed")
+  set(WITH_COALESCING "never")
   set(MAX_COALESCE_SIZE 60000)
   set(WITH_SPLITTING "never")
   set(MIN_SPLITTING_SIZE 300)
@@ -89,6 +85,10 @@ if (LINUXTEST)
   set(WITH_DOC ON)
 endif (LINUXTEST)
 
+if (NUM_POOLS GREATER 1)
+  set(MULTIPLE_POOLS ON)
+endif (NUM_POOLS GREATER 1)
+
 if(COALESCE_AFTER_SPLIT)
   if(WITH_SPLITTING STREQUAL "never")
          message(FATAL_ERROR "You have to set WITH_SPLITTING to fixed or variable if you want to coalesce after split.")
@@ -114,13 +114,6 @@ elseif(SORT_POLICY STREQUAL "address")
   set(ADDRESS_SORT_POLICY ON)
 endif(SORT_POLICY STREQUAL "lifo")
 
-if(WITH_SYSTEM_CALLS STREQUAL "none")
-  set(NO_SYSTEM_CALLS ON)
-elseif(WITH_SYSTEM_CALLS STREQUAL "sbrk")
-  set(WITH_SBRK ON)
-elseif(WITH_SYSTEM_CALLS STREQUAL "mmap")
-  set(WITH_MMAP ON)
-endif(WITH_SYSTEM_CALLS STREQUAL "none")
 
 if(SEARCH_POLICY STREQUAL "best")
   set(BEST_FIT ON)
@@ -141,11 +134,6 @@ if(WITH_COALESCING STREQUAL "fixed")
   if(NOT DEFINED MAX_COALESCE_SIZE)
     message(FATAL_ERROR "You have to set MAX_COALESCE_SIZE by using -DMAX_COALESCE_SIZE={size}.")
   endif(NOT DEFINED MAX_COALESCE_SIZE)
-  if (MULTIPLE_HEAPS)
-    # Coalescing on multiple heaps requires heap ownership information per
-    # block
-    set(WITH_OWNERSHIP ON)
-  endif (MULTIPLE_HEAPS)
   set(COALESCING_FIXED ON)
 elseif(WITH_COALESCING STREQUAL "variable")
   if(NOT DEFINED MAX_COALESCE_SIZE)

+ 2 - 2
dmm_config.h.in

@@ -14,8 +14,8 @@
 #cmakedefine SYS_ALLOC_SIZE @SYS_ALLOC_SIZE@
 #endif /* NO_SYSTEM_CALLS */
 
-/** The number of the heaps. */
-#cmakedefine NUM_HEAPS @NUM_HEAPS@
+/** The number of the pools. */
+#cmakedefine NUM_POOLS @NUM_POOLS@
 
 #cmakedefine WITH_FIXED_LISTS
 

+ 1 - 1
examples/CMakeLists.txt

@@ -9,7 +9,7 @@ set(CMAKE_BUILD_TYPE Debug)
 if (HAVE_LOCKS)
   find_package (Threads)
   add_executable(larson larson.c)
-  target_link_libraries(larson ${DMMLIB_SHARED_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
+  target_link_libraries(larson ${CMAKE_THREAD_LIBS_INIT} ${DMMLIB_SHARED_LIBRARY})
 endif (HAVE_LOCKS)
 
 if (NO_SYSTEM_CALLS)

+ 12 - 12
examples/larson.c

@@ -25,9 +25,9 @@
 #include <string.h>
 #include <assert.h>
 #include <unistd.h>
-#include <stdlib.h>
+/* #include <stdlib.h> */
 #include <dmmlib/dmmlib.h>
-#include <dmmlib/print_stats.h>
+/* #include <dmmlib/print_stats.h> */
 #include "lran2.h"
 
 #define MAX_THREADS     100
@@ -104,7 +104,7 @@ static void warmup(char **blkp, int num_chunks) {
 
     for(cblks = 0; cblks < num_chunks; cblks++) {
         blk_size = min_size + lran2(&rgen) % (max_size - min_size);
-        blkp[cblks] = (char *) malloc((size_t) blk_size);
+        blkp[cblks] = (char *) dmmlib_malloc((size_t) blk_size);
         blksize[cblks] = blk_size;
         assert(blkp[cblks] != NULL);
     }
@@ -119,10 +119,10 @@ static void warmup(char **blkp, int num_chunks) {
 
     for(cblks=0; cblks < 4 * num_chunks; cblks++) {
         victim = lran2(&rgen) % num_chunks;
-        free(blkp[victim]);
+        dmmlib_free(blkp[victim]);
 
         blk_size = min_size + lran2(&rgen) % (max_size - min_size);
-        blkp[victim] = (char *) malloc((size_t) blk_size);
+        blkp[victim] = (char *) dmmlib_malloc((size_t) blk_size);
         blksize[victim] = blk_size;
         assert(blkp[victim] != NULL);
     }
@@ -145,11 +145,11 @@ static void * exercise_heap( void *pinput) {
     /* allocate NumBlocks chunks of random size */
     for(cblks=0; cblks < pdea->NumBlocks; cblks++) {
         victim = lran2(&pdea->rgen)%pdea->asize;
-        free(pdea->array[victim]);
+        dmmlib_free(pdea->array[victim]);
         pdea->cFrees++;
 
         blk_size = pdea->min_size+lran2(&pdea->rgen)%range;
-        pdea->array[victim] = (char *) malloc((size_t) blk_size);
+        pdea->array[victim] = (char *) dmmlib_malloc((size_t) blk_size);
 
         pdea->blksize[victim] = blk_size;
         assert(pdea->array[victim] != NULL);
@@ -271,8 +271,8 @@ static void runthreads(long sleep_cnt, int min_threads, int max_threads, int chp
         //reqd_space = (0.5*(min_size+max_size)*num_threads*chperthread) ;
         //used_space = CountReservedSpace() - init_space;
         // FIXME Currently only one heap is used in the example
-        used_space = get_allocated_space(&systemallocator.heaps[0]);
-        reqd_space = get_used_space(&systemallocator.heaps[0]);
+        /* used_space = get_allocated_space(&systemallocator.heaps[0]); */
+        /* reqd_space = get_used_space(&systemallocator.heaps[0]); */
         //used_space = 0;
         printf(" Used space: %zu\n Requested space: %zu\n", used_space, reqd_space);
 
@@ -280,7 +280,7 @@ static void runthreads(long sleep_cnt, int min_threads, int max_threads, int chp
         printf("%6.3f", duration  ) ;
         printf("%6.3f", rate_n/rate_1 );
         printf("%8.0f", sum_allocs/duration);
-        printf(" %6.3f %.3f", (double)(used_space/(1024*1024)), (used_space/reqd_space));
+        /* printf(" %6.3f %.3f", (double)(used_space/(1024*1024)), (used_space/reqd_space)); */
         printf("\n") ;
 
         Sleep(5000L) ; // wait 5 sec for old threads to die
@@ -312,8 +312,8 @@ int main(void) {
 
     printf("threads (min, max):   ") ; 
     //scanf("%d %d", &min_threads, &max_threads) ;
-    min_threads = 1;
-    max_threads = 4;
+    min_threads = 2;
+    max_threads = 2;
     printf("%d %d\n", min_threads, max_threads);
 
     pthread_setconcurrency(max_threads);

+ 55 - 0
include/dmmlib/block_header.h

@@ -0,0 +1,55 @@
+/*
+ *   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 	block_header.h
+ * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date 	September, 2011
+ *  
+ * \brief Block header structure and functions, and memory block functions.
+ */
+
+#ifndef BLOCK_HEADER_H
+#define BLOCK_HEADER_H
+
+#include <stddef.h>
+#include <stdbool.h>
+
+#include "dmm_config.h"
+
+/** The header structure of every memory block inside a heap. */
+typedef struct block_header_s {
+    size_t size; /**< The LSB represents the availability of the block (1 for
+                   used, 0 for free), the rest the size of the data part. */
+#ifdef REQUEST_SIZE_INFO
+    size_t requested_size; /**< The requested size of the data part */
+#endif /* REQUEST_SIZE_INFO */
+    size_t previous_size; /**< The LSB represents the availability of the
+                            previous block, the rest the size of the data
+                            part of the previous block in the memory space */
+    struct block_header_s *next; /**< The pointer to the next memory block in the list */
+#ifdef BLOCKS_IN_DLL
+    struct block_header_s *previous; /**< The pointer to the previous node in the list */
+#endif /* BLOCKS_IN_DLL */
+} block_header_t;
+
+/**
+ * The size of the header in number of bytes
+ */
+#define HEADER_SIZE sizeof(block_header_t)
+
+#endif /* BLOCK_HEADER_H */

+ 3 - 3
include/dmmlib/custom_realloc.h

@@ -25,19 +25,19 @@
 #define CUSTOM_REALLOC_H
 #include "dmm_config.h"
 #include <dmmlib/dmmlib.h>
-#include <dmmlib/heap.h>
+#include <dmmlib/pool.h>
 
 /**
  * Try to change the size of an allocation on a specific allocator and heap
  *
  * \param allocator 	The pointer to the allocator who manages the block.
- * \param heap 		The pointer to the heap who manages the block.
+ * \param raw_block     The pointer to the raw block 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);
+void * custom_ahrealloc(allocator_t *allocator, raw_block_header_t *raw_block, void *ptr, size_t size);
 
 #endif /* CUSTOM_REALLOC_H */

+ 1 - 4
include/dmmlib/dmmlib.h

@@ -18,14 +18,11 @@
 #ifndef DMMLIB_H
 #define DMMLIB_H
 #include "dmm_config.h"
-#include <dmmlib/heap.h>
+#include <dmmlib/pool.h>
 #include <dmmlib/custom_realloc.h>
 
 #ifdef WITH_SINGLE_ALLOCATOR
 #include <dmmlib/single_allocator.h>
 #endif /* WITH_SINGLE_ALLOCATOR */
 
-void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size);
-void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr);
-
 #endif /* DMMLIB_H */

+ 0 - 163
include/dmmlib/heap.h

@@ -1,163 +0,0 @@
-/*
- *   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 	heap.h
- * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
- * \date 	September, 2011
- * 	Basic structures needed for the dmmlib allocator.
- */
-
-#ifndef HEAP_H
-#define HEAP_H
-
-#include "dmm_config.h"
-
-#include <stdbool.h>
-#ifndef LEON3
-#include <stdint.h>
-#include <stddef.h> /* For size_t support */
-#else
-#include <sys/types.h>
-#endif
-
-#ifdef HAVE_LOCKS
-#include <pthread.h>
-#endif /* HAVE_LOCKS */
-
-#ifdef LEON3
-typedef __uint32_t uint32_t ;
-#endif /* LEON3 */
-
-#ifdef WITH_KNOBS
-/**
- * A structure for knobs states (currently 5)
- * 
- * FIXME Have to check them
- *
- * */
-typedef uint8_t knob_state_t;
-#endif /* WITH_KNOBS */
-
-/** A structure to represent a maptable node.
- *  This is a single-linked list holoding blocks of specific size, thus acting
- *  as a fixed list within the allocator.
- */
-typedef struct maptable_node_s {
-	unsigned int size; /**< The size of the blocks of the fixed list. */
-	void *fixed_list_head; /**< Pointer to the head node of the fixed list. */
-#ifdef FIFO_SORT_POLICY
-	void *fixed_list_tail; /**< Pointer to the tail node of the fixed list. */
-#endif /* FIFO_SORT_POLICY */
-	struct maptable_node_s *next; /**< Pointer to the next node of the
-					maptable. */
-} maptable_node_t;
-
-#ifdef WITH_STATS
-/** Statistics data structure. */
-typedef struct dmmstats_s {
-#ifdef REQUEST_SIZE_INFO
-	size_t mem_requested; /**< Total memory currently requested. */
-#endif /* REQUEST_SIZE_INFO */
-	size_t mem_used; /**< Total memory actively used (holds allocated
-			   blocks). */
-	size_t mem_allocated; /**< Total managed memory (heap size, malloc and
-				free blocks). */
-	uint32_t live_objects; /**< Number of the currently used blocks. */
-#ifdef COUNT_ACCESSES
-	uint32_t read_mem_accesses; /**< Number of read accesses. */
-	uint32_t write_mem_accesses; /**< Number of write accesses. */
-#endif /* COUNT_ACCESSES */
-	uint32_t num_malloc; /**< Number of malloc()'s served. */
-	uint32_t num_free; /**< Number of free()'s served. */
-#ifdef COUNT_HOPS
-	size_t total_hops; /**< Number of hops done for a DMM request. */
-#endif /* COUNT_HOPS */
-} dmmstats_t;
-#endif /* WITH_STATS */
-
-#ifdef WITH_KNOBS
-/** A structure to represent tunable parameters of a heap */
-typedef struct dmmknobs_s {
-	float frag_threshold; /**< Fragmentation threshold to enable coalescing
-				or not. */ 
-	uint32_t mem_threshold; /**< Memory size threshold. */
-#ifdef GOOD_FIT
-	float fit_percentage; /**< Fit percentage for good-fit searches. */
-#endif  /* GOOD_FIT */
-#ifdef HEAP_VAR_FIT
-	/** Current search policy on the allocator's free lists. */
-	void *(*search_policy)(heap_t * heap, size_t requested_size);
-#endif /* HEAP_VAR_FIT */
-#ifdef COALESCING_VARIABLE
-	size_t max_coalesce_size; /**< Maximum coalesce size; -1 if coalescing
-				     is not supported */
-#endif /* COALESCING_VARIABLE */
-#ifdef SPLITTING_VARIABLE
-	size_t min_split_size; /**< Minimum split size. */
-#endif /* SPLITTING_VARIABLE */
-	/* FIXME Need to find explanation */
-	float empty_threshold; /**< Empty Threshold */
-	uint32_t percentage; /**< Percentage value */
-	knob_state_t frag_state; /**< The current state of fragmentation. */
-	knob_state_t foot_state; /**< The current state of footprint. */
-} dmmknobs_t;
-#endif /* WITH_KNOBS */
-
-/** A structure to store heap information. */
-typedef struct heap_s {
-#ifdef WITH_FIXED_LISTS
-	maptable_node_t *maptable_head; /**< The head of the maptable list. */
-#endif /* WITH_FIXED_LISTS */
-	void *free_list_head; /**< The head of the free list. */
-#ifdef FIFO_SORT_POLICY
-	void *free_list_tail; /**< The tail of the free list. */
-#endif /* FIFO_SORT_POLICY */
-#ifdef FUTURE_FEATURES
-	void *used_blocks_head; /**< The head of the used blocks list. */
-	void *rov_ptr; /**< Roving pointer. */
-#endif /* FUTURE_FEATURES */
-#ifndef LEON3
-	uint64_t num_objects; /**< Number of objects in the heap. */
-#else
-	unsigned long num_objects; /**< Number of objects in the heap. */
-#endif /* LEON3 */
-#ifdef WITH_STATS
-	dmmstats_t dmm_stats; /**< Statistics of the heap. */
-#endif /* WITH_STATS */
-#ifdef WITH_KNOBS
-	dmmknobs_t dmm_knobs; /**< Tunable parameters of the heap. */
-#endif /* WITH_KNOBS */
-#ifdef HAVE_LOCKS
-	pthread_mutex_t mutex;/**< Mutex when POSIX Threads are used. */
-#endif /* HAVE_LOCKS */
-} heap_t;
-
-/** The allocator structure of dmmlib. */
-typedef struct allocator_s {
-	heap_t heaps[NUM_HEAPS]; /**< The heaps that the allocator manages. */
-	bool initialized; /**< Initialization flag of the allocator. */
-	void *border_ptr; /**< Border pointer of the allocator. */
-	size_t remaining_size; /**< The size of the remaining free space which
-				 is handled by the allocator. */
-#ifdef ALLOC_VAR_FIT
-	/** Current search policy on the allocator's free lists. */
-	void *(*search_policy)(heap_t * heap, size_t requested_size);
-#endif /* ALLOC_VAR_FIT */
-} allocator_t;
-
-#endif /* HEAP_H */

+ 93 - 0
include/dmmlib/pool.h

@@ -0,0 +1,93 @@
+/*
+ *   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 	pool.h
+ * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date 	June, 2012
+ * 	Basic structures needed for the dmmlib allocator.
+ */
+
+#ifndef POOL_H
+#define POOL_H
+
+#include "dmm_config.h"
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stddef.h> /* For size_t support */
+
+#ifdef HAVE_LOCKS
+#include <pthread.h>
+#endif /* HAVE_LOCKS */
+
+#include <dmmlib/raw_block.h>
+
+#ifdef LEON3
+typedef __uint32_t uint32_t ;
+#endif /* LEON3 */
+
+#ifdef WITH_KNOBS
+/**
+ * A structure for knobs states (currently 5)
+ * 
+ * FIXME Have to check them
+ *
+ * */
+typedef uint8_t knob_state_t;
+#endif /* WITH_KNOBS */
+
+#ifdef WITH_KNOBS
+/** A structure to represent tunable parameters of a heap */
+typedef struct dmmknobs_s {
+	float frag_threshold; /**< Fragmentation threshold to enable coalescing
+				or not. */ 
+	uint32_t mem_threshold; /**< Memory size threshold. */
+#ifdef GOOD_FIT
+	float fit_percentage; /**< Fit percentage for good-fit searches. */
+#endif  /* GOOD_FIT */
+#ifdef POOL_VAR_FIT
+	/** Current search policy on the allocator's free lists. */
+	void *(*search_policy)(heap_t * heap, size_t requested_size);
+#endif /* HEAP_VAR_FIT */
+#ifdef COALESCING_VARIABLE
+	size_t max_coalesce_size; /**< Maximum coalesce size; -1 if coalescing
+				     is not supported */
+#endif /* COALESCING_VARIABLE */
+#ifdef SPLITTING_VARIABLE
+	size_t min_split_size; /**< Minimum split size. */
+#endif /* SPLITTING_VARIABLE */
+	/* FIXME Need to find explanation */
+	float empty_threshold; /**< Empty Threshold */
+	uint32_t percentage; /**< Percentage value */
+	knob_state_t frag_state; /**< The current state of fragmentation. */
+	knob_state_t foot_state; /**< The current state of footprint. */
+} dmmknobs_t;
+#endif /* WITH_KNOBS */
+
+/** The allocator structure of dmmlib. */
+typedef struct allocator_s {
+    raw_block_header_t *raw_block_head; /**< The head of the raw blocks list. */
+    pthread_mutex_t creation_mutex; /**< Mutex to allow the creation of new
+                                      pools. */
+#ifdef ALLOC_VAR_FIT
+	/** Current search policy on the allocator's free lists. */
+	void *(*search_policy)(heap_t * heap, size_t requested_size);
+#endif /* ALLOC_VAR_FIT */
+} allocator_t;
+
+#endif /* HEAP_H */

+ 71 - 0
include/dmmlib/raw_block.h

@@ -0,0 +1,71 @@
+/*
+ *   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        raw_block.h
+ * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date 	August, 2012
+ *  
+ * \brief       Raw block structure and functions.
+ */
+
+#ifndef RAW_BLOCK_H
+#define RAW_BLOCK_H
+
+#include <stddef.h> /* for size_t */
+#include <inttypes.h>
+#include <pthread.h> /* FIXME To be removed once mutex is removed. */
+#include <dmmlib/block_header.h>
+
+#ifdef WITH_STATS
+/** Statistics data structure. */
+typedef struct dmmstats_s {
+#ifdef REQUEST_SIZE_INFO
+    size_t total_mem_requested; /**< Total memory actively used by the
+                                  application. */
+#endif /* REQUEST_SIZE_INFO */
+    size_t total_mem_allocated; /**< Total memory allocated by the 
+                                  application. */
+    uint32_t live_objects; /**< Number of the currently used blocks. */
+    uint32_t num_malloc; /**< Number of malloc()'s served. */
+    uint32_t num_free; /**< Number of free()'s served. */
+} dmmstats_t;
+#endif /* WITH_STATS */
+
+/** The header structure of every raw block inside a heap. */
+typedef struct raw_block_header_s {
+    struct raw_block_header_s *next_raw_block; /**< Pointer to the next raw
+                                                 block. */
+    size_t size; /**< Total available size of the memory block. */
+    block_header_t *border_ptr; /**< Pointer to the memory block allocated last. */
+    size_t remaining_size; /**< Remaining, never allocated before, size. */
+    block_header_t *free_list_head; /**< Head of the free list of memory blocks. */
+#ifdef FIFO_SORT_POLICY
+    block_header_t *free_list_tail; /**< The tail of the free list. */
+#endif /* FIFO_SORT_POLICY */
+#ifdef WITH_STATS
+    dmmstats_t dmm_stats; /**< Statistics of the heap. */
+#endif /* WITH_STATS */
+#ifdef HAVE_LOCKS
+    pthread_mutex_t mutex;/**< Mutex when POSIX Threads are used. */
+#endif /* HAVE_LOCKS */
+} raw_block_header_t;
+
+/** Create a new raw block. */
+raw_block_header_t *create_new_raw_block(size_t raw_block_size);
+
+#endif /* RAW_BLOCK_H */

+ 5 - 6
include/dmmlib/single_allocator.h

@@ -25,23 +25,23 @@
 #ifndef SINGLE_ALLOCATOR_H
 #define SINGLE_ALLOCATOR_H
 
-#include <dmmlib/heap.h>
+#include <dmmlib/pool.h>
 
 /* In case stdlib.h is used, there is no need to redeclare
  * malloc() and free()
  */
-#ifndef __malloc_and_calloc_defined
+
 /**
  * Allocates size bytes by using the system's allocator.
  * \param   size    Requested allocation size in bytes.
  */
-void * malloc(size_t size);
+void *dmmlib_malloc(size_t size);
 
 /**
  * De-allocates the memory space pointed to by ptr.
  * \param   ptr     The pointer to memory to free.
  */
-void free(void *ptr);
+void dmmlib_free(void *ptr);
 
 /**
  * Try to change the size of an allocation
@@ -51,9 +51,8 @@ void free(void *ptr);
  * \return  The pointer to the data part of the memory block which
  * has the new, desired size.
  */
-void * realloc(void *ptr, size_t size);
+void * dmmlib_realloc(void *ptr, size_t size);
 
-#endif /* __malloc_and_calloc_defined */
 
 /** Global variable storing allocator's settings */
 allocator_t systemallocator;

+ 5 - 0
private-include/debug.h

@@ -0,0 +1,5 @@
+#include <dmmlib/pool.h>
+#include "dmm_config.h"
+
+void get_raw_blocks(allocator_t *allocator);
+void get_memory_blocks(raw_block_header_t *raw_block);

+ 31 - 78
private-include/block_header.h

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2011 Institute of Communication and Computer Systems (ICCS) 
+ *   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.
@@ -16,46 +16,17 @@
  */
 
 /**
- * \file 	block_header.h
+ * \file 	block_header_funcs.h
  * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
- * \date 	September, 2011
  *  
- * \brief Block header structure and functions, and memory block functions.
+ * \brief Memory block functions.
  */
 
-#ifndef BLOCK_HEADER_H
-#define BLOCK_HEADER_H
+#ifndef BLOCK_HEADER_FUNCS_H
+#define BLOCK_HEADER_FUNCS_H
 
-#include <stddef.h>
-#include <stdbool.h>
-#include <dmmlib/heap.h>
-
-/* TODO Add an ifndef guard in case we have other block organizations */
-#include "linked_lists/linked_lists.h"
-
-/** The header structure of every memory block inside a heap. */
-typedef struct __attribute__((__aligned__(32))) block_header_s {
-    size_t size; /**< The LSB represents the availability of the block (1
-                   for used, 0 for free), the rest the size of the data
-                   part. */
-#ifdef REQUEST_SIZE_INFO
-    size_t requested_size; /**< The requested size of the data part */
-#endif /* REQUEST_SIZE_INFO */
-    size_t previous_size; /**< The LSB represents the availability of the
-                            previous block, the rest the size of the data
-                            part of the previous block in the memory space */
-    /* TODO Add an ifndef guard in case we have other block organizations */
-    list_node_header_t pointers; /**< The necessary pointers for block
-                                   organization. */
-#ifdef WITH_OWNERSHIP
-    heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
-#endif /* WITH_OWNERSHIP */
-} block_header_t;
-
-/**
- * The size of the header in number of bytes
- */
-#define HEADER_SIZE sizeof(block_header_t)
+#include <dmmlib/block_header.h>
+#include <dmmlib/raw_block.h>
 
 /**
  * \brief Get the address of the block header of a memory block.
@@ -67,11 +38,11 @@ block_header_t * get_header(void *ptr);
 /**
  * Get the size of the memory block's data
  *
- * \param ptr The pointer to the data part of the current memory block.
+ * \param ptr The pointer to the current memory block.
  *
  * \return The size of the data part of the current memory block.
  */
-size_t get_size(void *ptr);
+size_t get_size(block_header_t *ptr);
 
 #ifdef REQUEST_SIZE_INFO
 
@@ -82,7 +53,7 @@ size_t get_size(void *ptr);
  * \param size 	The requested size for the data part of the current memory
  * block.
  */
-void set_requested_size(void *ptr, size_t size);
+void set_requested_size(block_header_t *ptr, size_t size);
 
 /**
  * Get the requested size of the memory block's data
@@ -92,7 +63,7 @@ void set_requested_size(void *ptr, size_t size);
  * \return The size of the data that was initialy requested for this memory
  * block.
  */
-size_t get_requested_size(void *ptr);
+size_t get_requested_size(block_header_t *ptr);
 #endif /* REQUEST_SIZE_INFO */
 
 /**
@@ -103,43 +74,43 @@ size_t get_requested_size(void *ptr);
  * \return The availability and the size of the data part of the current memory
  * block.
  */
-size_t get_size_availability(void *ptr);
+size_t get_size_availability(block_header_t *ptr);
 
 /**
  * Set the size of the memory block's data and mark it free
  *
- * \param allocator The pointer to the allocator who manages the block.
+ * \param raw_block The pointer to the raw block which includes the block.
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The size of the data part of the current memory block.
  */
-void set_size_and_free(allocator_t *allocator, void *ptr, size_t size);
+void set_size_and_free(raw_block_header_t *raw_block, block_header_t *ptr, size_t size);
 
 /**
  * Set the size of the memory block's data and mark it used
  *
- * \param allocator The pointer to the allocator who manages the block.
+ * \param raw_block The pointer to the raw block which includes the block.
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The size of the data part of the current memory block.
  */
-void set_size_and_used(allocator_t *allocator, void *ptr, size_t size);
+void set_size_and_used(raw_block_header_t *raw_block, block_header_t *ptr, size_t size);
 
 /**
  * Mark the memory block as used, as well as the previous_size element of the
  * next block if there is one.
  *
- * \param allocator The pointer to the allocator who manages the block.
+ * \param raw_block The pointer to the raw block which includes the block.
  * \param ptr 	The pointer to the data part of the memory block.
  */
-void mark_used(allocator_t *allocator, void *ptr);
+void mark_used(raw_block_header_t *raw_block, block_header_t *ptr);
 
 /**
  * Mark the memory block as free, as well as the previous_size element of the
  * next block if there is one.
  *
- * \param allocator The pointer to the allocator who manages the block.
+ * \param raw_block The pointer to the raw block which includes the block.
  * \param ptr 	The pointer to the data part of the memory block.
  */
-void mark_free(allocator_t *allocator, void *ptr);
+void mark_free(raw_block_header_t *raw_block, block_header_t *ptr);
 
 /**
  * Set the availability and the size of the previous memory block
@@ -148,44 +119,24 @@ void mark_free(allocator_t *allocator, void *ptr);
  * \param previous_size_availability The size for the data part of the previous
  * memory block on data layout level.
  */
-void set_previous_size_availability(void *ptr, size_t previous_size_availability);
-
-#ifdef WITH_OWNERSHIP
-
-/**
- * Set the heap owner of a memory block
- *
- * \param ptr 		The pointer to the data part of the memory block.
- * \param heap_owner 	The pointer to the heap owner.
- */
-void set_owner(void *ptr, heap_t *heap_owner);
-
-/**
- * Get the heap owner of a memory block
- *
- * \param ptr 	The pointer to the data part of the memory block.
- * \return 	The pointer to the heap owner.
- */
-heap_t * get_owner(void *ptr);
-
-#endif /* WITH_OWNERSHIP */
+void set_previous_size_availability(block_header_t *ptr, size_t previous_size_availability);
 
 /**
  * Check if a memory block is free
  */
-bool is_free(void *ptr);
+bool is_free(block_header_t *ptr);
 
 /**
  * Check if previous block (in the memory space) belongs to a free list
  */
-bool is_previous_free(void *ptr);
+bool is_previous_free(block_header_t *ptr);
 
 /**
  * Get the size of the previous block (in the memory space) 
  *
  * \param ptr 	The pointer to the data part of the current memory block.
  */
-size_t get_previous_size(void *ptr);
+size_t get_previous_size(block_header_t *ptr);
 
 /**
  * Get the size and the availability of the previous block (in the memory
@@ -193,7 +144,7 @@ size_t get_previous_size(void *ptr);
  *
  * \param ptr 	The pointer to the data part of the previous memory block.
  */
-size_t get_previous_size_availability(void *ptr);
+size_t get_previous_size_availability(block_header_t *ptr);
 
 /**
  * Get the previous memory block on data layout level
@@ -203,17 +154,19 @@ size_t get_previous_size_availability(void *ptr);
  * \return 	The pointer to the data part of the previous memory block on
  * data layout level. 
  */
-void * get_dlprevious(void *ptr);
+block_header_t * get_dlprevious(block_header_t *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 raw_block     The pointer to the raw block which includes the block.
  * \param ptr 		The pointer to the data part of the current memory block.
  *
  * \return The pointer to the data part of the next block.
  * \retval NULL There is no next block.
  */
-void * get_dlnext(allocator_t *allocator, void *ptr);
+block_header_t * get_dlnext(raw_block_header_t *raw_block, block_header_t *ptr);
+
+
 
-#endif /* BLOCK_HEADER_H */
+#endif /* BLOCK_HEADER_FUNCS_H */

+ 3 - 3
private-include/coalesce.h

@@ -18,16 +18,16 @@
 #ifndef COALESCE_H
 #define COALESCE_H
 
-#include <dmmlib/heap.h>
+#include <dmmlib/raw_block.h>
+#include <dmmlib/block_header.h>
 
 /**
  * Tries to merges a memory block with its previous one and/or the next one.
- * Returns true if it is not required to add the coalesced block to any pool.
  *
  * @param ptr The memory block to be checked.
  * @param heap The heap of the memory block.
  */
-bool coalesce(allocator_t **allocator, heap_t *heap, void **ptr);
+void coalesce(raw_block_header_t *raw_block, block_header_t *ptr);
 
 #endif /* COALESCE_H */
 

+ 35 - 0
private-include/freelist/freelist_malloc.h

@@ -0,0 +1,35 @@
+/*
+ *   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_malloc.h
+ * \author  Ioannis Koutras (joko@microlab.ntua.gr)
+ *  
+ * \brief   Memory allocation for free-list based raw blocks
+ */
+
+#ifndef FREELIST_MALLOC_H
+#define FREELIST_MALLOC_H
+
+#include <dmmlib/raw_block.h>
+#include <dmmlib/block_header.h>
+
+void * freelist_malloc(raw_block_header_t *raw_block, size_t size);
+
+void freelist_free(raw_block_header_t *raw_block, void *ptr);
+
+#endif /* FREELIST_MALLOC_H */

+ 4 - 4
private-include/linked_lists/add_block.h

@@ -29,19 +29,19 @@
 #include "dmm_config.h"
 
 #ifdef LIFO_SORT_POLICY
-#include "linked_lists/lifo_order.h"
+#include "freelist/linked_lists/lifo_order.h"
 #endif /* LIFO_SORT_POLICY */
 
 #ifdef FIFO_SORT_POLICY
-#include "linked_lists/fifo_order.h"
+#include "freelist/linked_lists/fifo_order.h"
 #endif /* LIFO_SORT_POLICY */
 
 #ifdef SIZE_SORT_POLICY
-#include "linked_lists/size_order.h"
+#include "freelist/linked_lists/size_order.h"
 #endif /* LIFO_SORT_POLICY */
 
 #ifdef ADDRESS_SORT_POLICY
-#include "linked_lists/address_order.h"
+#include "freelist/linked_lists/address_order.h"
 #endif /* LIFO_SORT_POLICY */
 
 #endif /* ADD_BLOCK_H */

private-include/linked_lists/add_block_in_order.h → private-include/freelist/linked_lists/add_block_in_order.h


private-include/linked_lists/address_order.h → private-include/freelist/linked_lists/address_order.h


private-include/linked_lists/fifo_order.h → private-include/freelist/linked_lists/fifo_order.h


+ 7 - 22
private-include/linked_lists/lifo_order.h

@@ -26,38 +26,23 @@
 #ifndef LIFO_ORDER_H
 #define LIFO_ORDER_H
 
-#ifdef COUNT_HOPS
-#include <dmmlib/heap.h>
-#endif /* COUNT_HOPS */
 #include "dmm_config.h"
+#include <dmmlib/raw_block.h>
+#include <dmmlib/block_header.h>
 
 #ifdef LIFO_SORT_POLICY
-#ifdef COUNT_HOPS
-#define add_block(heap, block, list) add_block_lifo_order(heap, block, list)
-#else /* COUNT_HOPS */
-#define add_block(block, list) add_block_lifo_order(block, list)
-#endif /* COUNT_HOPS */
+#define add_block(raw_block, block) add_block_lifo_order(raw_block, block)
 #endif /* LIFO_SORT_POLICY */
 
 /**
  * Adds a block in a LIFO-ordered list.
  *
- * @param block The pointer of the data part of the block to be added.
- * @param starting_node The pointer to the starting memory block of the list.
+ * @param raw_block The pointer to the raw block.
+ * @param block The pointer of the block to be added.
  */
-#ifdef COUNT_HOPS
-/*
- * @param heap A pointer to the heap which manages the block.
- */
-void add_block_lifo_order(
-        heap_t *heap,
-        void **block,
-        void **starting_node);
-#else /* COUNT_HOPS */
 void add_block_lifo_order(
-        void **block,
-        void **starting_node);
-#endif /* COUNT_HOPS */
+        raw_block_header_t *raw_block,
+        block_header_t *block);
 
 #endif /* LIFO_ORDER_H */
 

+ 71 - 0
private-include/freelist/linked_lists/linked_lists.h

@@ -0,0 +1,71 @@
+/*
+ *   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 	linked_lists.h
+ * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
+ * \date 	September, 2011
+ *  
+ * \brief Linked list node header structure and functions.
+ */
+
+#ifndef LINKED_LISTS_H
+#define LINKED_LISTS_H
+
+#include "dmm_config.h"
+#ifndef LEON3
+#include <stdint.h>
+#else
+#include <sys/types.h>
+#endif /* LEON3 */
+
+#include <dmmlib/block_header.h>
+#include <dmmlib/raw_block.h>
+
+#ifdef BLOCKS_IN_DLL
+
+/**
+ * Set the previous memory block of a block in a linked list.
+ *
+ * \param block             The pointer to the data part of the current memory
+ * block.
+ * \param previous_block    The pointer to the data part of the previous memory
+ * block.
+ */
+void set_previous(void *previous, void *previous_block);
+
+/**
+ * Get the previous memory block in a linked list.
+ *
+ * \param ptr The pointer to the data part of the current memory block.
+ *
+ * \return The pointer of the data part of the previous (in list) memory block.
+ * \retval NULL There is no previous memory block in the list.
+ */
+void * get_previous(void *ptr);
+
+#endif /* BLOCKS_IN_DLL */
+
+/**
+ * Removes a memory block from a linked list of memory blocks.
+ *
+ * \param block A pointer to the block to be removed.
+ * \param raw_block A pointer to the raw block.
+ */
+void remove_block(block_header_t *block, raw_block_header_t *raw_block);
+
+#endif /* LINKED_LISTS_H */

+ 9 - 22
private-include/linked_lists/search_algorithms.h

@@ -26,69 +26,56 @@
 #ifndef LINKED_LISTS_SEARCH_ALGORITHMS_H
 #define LINKED_LISTS_SEARCH_ALGORITHMS_H
 
-#include <dmmlib/heap.h>
+#include <dmmlib/raw_block.h>
 #include <dmm_config.h>
 
-#ifdef WITH_FIXED_LISTS
-/**
- * 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 /* WITH_FIXED_LISTS */
-
 /**
  * Perform a best-fit search on free lists for a block of a certain size
  *
- * \param heap The heap whose fixed lists should be accessed.
+ * \param raw_block The raw block whose fixed list 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 * best_fit_on_freelist(heap_t *heap, size_t requested_size);
+block_header_t * best_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size);
 
 #ifdef GOOD_FIT
 
 /**
  * Perform a good-fit search on the free list
  *
- * \param heap The heap whose free list should be accessed.
+ * \param raw_block The raw block whose fixed list should be accessed.
  * \param requested_size The desired size of the block.
  *
  * \return The pointer to the data part of the matched memory block.
  * \retval NULL No block was found.
  */
-void * good_fit_on_freelist(heap_t *heap, size_t requested_size);
+block_header_t * good_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size);
 
 #endif /* GOOD_FIT */
 
 /**
  * Perform an exact-fit search on free lists for a block of a certain size
  *
- * \param heap The heap whose fixed lists should be accessed.
+ * \param raw_block The raw block whose fixed list 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 * exact_fit_on_freelist(heap_t *heap, size_t requested_size);
+block_header_t * exact_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size);
 
 /**
  * Perform a first-fit search on free lists for a block of a certain size
  *
- * \param heap The heap whose fixed lists should be accessed.
+ * \param raw_block The raw block whose fixed list 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 * first_fit_on_freelist(heap_t *heap, size_t requested_size);
+block_header_t * first_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size);
 
 #endif /* LINKED_LISTS_SEARCH_ALGORITHMS_H */
 

private-include/linked_lists/size_order.h → private-include/freelist/linked_lists/size_order.h


+ 3 - 3
private-include/split.h

@@ -18,7 +18,8 @@
 #ifndef SPLIT_H
 #define SPLIT_H
 
-#include <dmmlib/heap.h>
+#include <dmmlib/raw_block.h>
+#include <dmmlib/block_header.h>
 #include "dmm_config.h"
 
 /**
@@ -30,8 +31,7 @@
  * @param ptr The memory block to be split.
  * @param req_size The size which the first block will use.
  */ 
-void split(allocator_t *allocator, heap_t *heap, void *ptr,
-		size_t req_size);
+void split(raw_block_header_t *raw_block, block_header_t *ptr, size_t req_size);
 
 #endif /* SPLIT_H */
 

+ 0 - 131
private-include/linked_lists/linked_lists.h

@@ -1,131 +0,0 @@
-/*
- *   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 	linked_lists.h
- * \author 	Ioannis Koutras (joko@microlab.ntua.gr)
- * \date 	September, 2011
- *  
- * \brief Linked list node header structure and functions.
- */
-
-#ifndef LINKED_LISTS_H
-#define LINKED_LISTS_H
-
-#include "dmm_config.h"
-#ifndef LEON3
-#include <stdint.h>
-#else
-#include <sys/types.h>
-#endif /* LEON3 */
-#include <dmmlib/heap.h>
-
-/** The type of the pointer to a list node */
-#ifndef LEON3
-typedef uintptr_t list_node_ptr;
-#else
-typedef void* list_node_ptr;
-#endif /* LEON3 */
-
-/** The header structure of a linked list node */
-typedef struct list_node_header_s {
-    list_node_ptr next; /**< The pointer to the next node in the list */
-#ifdef BLOCKS_IN_DLL
-    list_node_ptr previous; /**< The pointer to the previous node in the list */
-#endif /* BLOCKS_IN_DLL */
-} list_node_header_t;
-
-/**
- * Set the next memory block of a block in a linked list.
- *
- * \param block 	The pointer to the data part of the current memory
- * block.
- * \param next_block 	The pointer to the data part of the next memory block.
- */
-void set_next(void *block, void *next_block);
-
-/**
- * Get the next memory block in a linked list.
- *
- * \param ptr The pointer to the data part of the current memory block.
- *
- * \return The pointer of the data part of the next (in list) memory block.
- * \retval NULL There is no next memory block in the list.
- */
-void * get_next(void *ptr);
-
-#ifdef BLOCKS_IN_DLL
-
-/**
- * Set the previous memory block of a block in a linked list.
- *
- * \param block             The pointer to the data part of the current memory
- * block.
- * \param previous_block    The pointer to the data part of the previous memory
- * block.
- */
-void set_previous(void *previous, void *previous_block);
-
-/**
- * Get the previous memory block in a linked list.
- *
- * \param ptr The pointer to the data part of the current memory block.
- *
- * \return The pointer of the data part of the previous (in list) memory block.
- * \retval NULL There is no previous memory block in the list.
- */
-void * get_previous(void *ptr);
-
-#endif /* BLOCKS_IN_DLL */
-
-/**
- * Push a memory block to the head of a linked list.
- * 
- * \param **block A pointer to the block to be put.
- * \param **starting_node A pointer to the starting memory block of the list.
- */
-void push_block(void **block, void **starting_node);
-
-
-#ifdef COUNT_HOPS
-/**
- * Removes a memory block from a linked list of memory blocks.
- *
- * \param *heap A pointer to the heap structure which contains the block.
- * \param **block A pointer to the block to be removed.
- * \param **starting_node A pointer to the starting memory block of the list.
- */
-void remove_block(heap_t *heap, void **block, void **starting_node);
-#else
-/**
- * Removes a memory block from a linked list of memory blocks.
- *
- * \param **block A pointer to the block to be removed.
- * \param **starting_node A pointer to the starting memory block of the list.
- */
-void remove_block(void **block, void **starting_node);
-#endif /* COUNT_HOPS */
-
-/**
- * Removes a memory block from any of the linked lists of free memory blocks.
- *
- * \param **block A pointer to the block to be removed.
- * \param *heap A pointer to the heap which manages the block.
- */
-void remove_block_from_lists(void **block, heap_t *heap);
-
-#endif /* LINKED_LISTS_H */

+ 2 - 8
private-include/other.h

@@ -19,16 +19,10 @@
 #define OTHER_H
 
 #include "dmm_config.h"
-#include <dmmlib/heap.h>
+#include <dmmlib/pool.h>
 
 size_t req_padding(size_t size);
 
-#ifdef WITH_FIXED_LISTS
-int map_size_to_list(heap_t *heap, size_t sz);
-#endif /* WITH_FIXED_LISTS */
-
-#ifndef NO_SYSTEM_CALLS
-int map_thread_heap(void);
-#endif /* NO_SYSTEM_CALLS */
+int map_thread_pool(void);
 
 #endif /* OTHER_H */

+ 40 - 0
private-include/request_memory.h

@@ -0,0 +1,40 @@
+/*
+ *   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   request_memory.h
+ * \author Ioannis Koutras
+ * \date   August, 2012
+ * \brief  Function prototype to request additional memory space.
+ */
+
+#ifndef REQUEST_MEMORY_H
+#define REQUEST_MEMORY_H
+
+#include <stddef.h>
+
+/**
+ * Requests memory address space from the OS.
+ *
+ * @param size The memory space to be requested in bytes.
+ *
+ * @return A pointer to the memory space.
+ * @retval NULL The OS could not return any memory space.
+ */
+void * request_memory(size_t size);
+
+#endif /* REQUEST_MEMORY_H */

+ 2 - 2
private-include/sys_alloc.h

@@ -19,8 +19,8 @@
 #define SYS_ALLOC_H
 
 #include "dmm_config.h"
-#include <dmmlib/heap.h>
+#include <dmmlib/pool.h>
 
-void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size);
+void *sys_alloc(pool_t *pool, size_t size);
 
 #endif /* SYS_ALLOC_H */

+ 37 - 24
src/CMakeLists.txt

@@ -29,80 +29,93 @@ if (WITH_STATIC_LIB)
 endif (WITH_STATIC_LIB)
 
 set(dmmlib_SRCS
-  block_header.c
-  linked_lists/linked_lists.c
-  linked_lists/search_algorithms.c
-  custom_free.c
-  custom_malloc.c
+  freelist/block_header_funcs.c
+  freelist/linked_lists/linked_lists.c
+  freelist/linked_lists/search_algorithms.c
+  freelist/freelist_malloc.c
+  raw_block.c
   other.c
-  initialize_allocator.c
-  sys_alloc.c
+  debug.c
 )
 
-if (WITH_SINGLE_ALLOCATOR)
+if(WITH_SYSTEM_CALLS STREQUAL "none")
+  set(NO_SYSTEM_CALLS ON)
+elseif(WITH_SYSTEM_CALLS STREQUAL "sbrk")
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    single_allocator.c
+    request_memory_sbrk.c
   )
-endif (WITH_SINGLE_ALLOCATOR)
+elseif(WITH_SYSTEM_CALLS STREQUAL "mmap")
+  if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+    set(dmmlib_SRCS
+      ${dmmlib_SRCS}
+      request_memory_mmap_linux.c
+    )
+  elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+    set(dmmlib_SRCS
+      ${dmmlib_SRCS}
+      request_memory_mmap_osx.c
+    )
+  endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+
+endif(WITH_SYSTEM_CALLS STREQUAL "none")
 
-if (HAVE_LOCKS)
-  find_package (Threads)
+if (WITH_SINGLE_ALLOCATOR)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    posix_lock.c
+    single_allocator.c
   )
-endif (HAVE_LOCKS)
+endif (WITH_SINGLE_ALLOCATOR)
 
 if(SORT_POLICY STREQUAL "lifo")
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    linked_lists/lifo_order.c
+    freelist/linked_lists/lifo_order.c
   )
 elseif(SORT_POLICY STREQUAL "fifo")
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    linked_lists/fifo_order.c
+    freelist/linked_lists/fifo_order.c
   )
 elseif(SORT_POLICY STREQUAL "size")
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    linked_lists/add_block_in_order.c
-    linked_lists/size_order.c
+    freelist/linked_lists/add_block_in_order.c
+    freelist/linked_lists/size_order.c
   )
 elseif(SORT_POLICY STREQUAL "address")
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    linked_lists/add_block_in_order.c
-    linked_lists/address_order.c
+    freelist/linked_lists/add_block_in_order.c
+    freelist/linked_lists/address_order.c
   )
 endif(SORT_POLICY STREQUAL "lifo")
 
 if (COALESCING_FIXED OR COALESCING_VARIABLE)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    coalesce.c
+    freelist/coalesce.c
   )
 endif (COALESCING_FIXED OR COALESCING_VARIABLE)
 
 if (SPLITTING_FIXED OR SPLITTING_VARIABLE)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    split.c
+    freelist/split.c
   )
 endif (SPLITTING_FIXED OR SPLITTING_VARIABLE)
 
 if (WITH_STATS)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    print_stats.c
+#    print_stats.c
   )
 endif (WITH_STATS)
 
 if (WITH_REALLOC)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}
-    custom_realloc.c
+    #    custom_realloc.c
   )
 endif (WITH_REALLOC)
 

+ 0 - 144
src/block_header.c

@@ -1,144 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include "block_header.h"
-
-block_header_t * get_header(void *ptr) {
-    return (block_header_t *) ((char *) ptr - HEADER_SIZE);
-}
-
-size_t get_size(void *ptr) {
-    return get_header(ptr)->size >> 1;
-}
-
-#ifdef REQUEST_SIZE_INFO
-void set_requested_size(void *ptr, size_t size) {
-    get_header(ptr)->requested_size = size;
-}
-
-size_t get_requested_size(void *ptr) {
-    return get_header(ptr)->requested_size;
-}
-#endif /* REQUEST_SIZE_INFO */
-
-size_t get_size_availability(void *ptr) {
-    return get_header(ptr)->size;
-}
-
-void set_size_and_free(allocator_t *allocator, void *ptr, size_t size) {
-    block_header_t *next_block_header;
-
-    get_header(ptr)->size = size << 1;
-
-    if(allocator->border_ptr != ptr) {
-        next_block_header = (block_header_t *) ((char *) ptr + size);
-        next_block_header->previous_size = size << 1;
-    }
-}
-
-void set_size_and_used(allocator_t *allocator, void *ptr, size_t size) {
-    block_header_t *header, *next_block_header;
-
-    header = get_header(ptr);
-    header->size = size << 1;
-    header->size |= 1;
-
-    if(allocator->border_ptr != ptr) {
-        next_block_header = (block_header_t *) ((char *) ptr + size);
-        next_block_header->previous_size = size << 1;
-        next_block_header->previous_size |= 1;
-    }
-}
-
-void mark_used(allocator_t *allocator, void *ptr) {
-    block_header_t *next_block_header;
-
-    get_header(ptr)->size |= 1;
-
-    if(allocator->border_ptr != ptr) {
-        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
-        next_block_header->previous_size |= 1;
-    }
-}
-
-void mark_free(allocator_t *allocator, void *ptr) {
-    block_header_t *next_block_header;
-    
-    get_header(ptr)->size &= (~ ((size_t) 0x1));
-
-    if(allocator->border_ptr != ptr) {
-        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
-        next_block_header->previous_size &= (~ ((size_t) 0x1));
-    }    
-}
-
-#ifdef WITH_OWNERSHIP
-
-void set_owner(void *ptr, heap_t *heap_owner) {
-    get_header(ptr)->heap_owner = heap_owner;
-}
-
-heap_t * get_owner(void *ptr) {
-    return get_header(ptr)->heap_owner;
-}
-
-#endif /* WITH_OWNERSHIP */
-
-bool is_free(void *ptr) {
-    if(ptr == NULL) {
-        return false;
-    }
-
-    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) {
-        return false;
-    } else {
-        return true;
-    }
-}
-
-void set_previous_size_availability(void *ptr, size_t previous_size_availability) {
-    get_header(ptr)->previous_size = previous_size_availability;
-}
-
-size_t get_previous_size(void *ptr) {
-    return get_header(ptr)->previous_size >> 1;
-}
-
-size_t get_previous_size_availability(void *ptr) {
-    return get_header(ptr)->previous_size;
-}
-
-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) + HEADER_SIZE);
-    } else {
-        return NULL;
-    }
-}
-

+ 0 - 143
src/coalesce.c

@@ -1,143 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include "coalesce.h"
-#include "block_header.h"
-#include "other.h"
-#include "dmm_config.h"
-
-bool coalesce(allocator_t **allocator, heap_t *heap, void **ptr) {
-    size_t max_coal_size;
-    size_t size;
-    size_t current_next_size;
-    size_t previous_current_size;
-    size_t three_blocks_size;
-    void *previous_block, *next_block;
-#ifdef WITH_FIXED_LISTS
-    int fixed_list_id, i;
-    maptable_node_t *current_maptable_node;
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef COALESCING_FIXED
-    max_coal_size = MAX_COALESCE_SIZE;
-#endif /* COALESCING_FIXED */
-#ifdef COALESCING_VARIABLE
-    max_coal_size = heap->dmm_knobs.max_coalesce_size;
-#endif /* COALESCING_VARIABLE */
-
-    size = get_size(*ptr);
-
-    previous_block = get_dlprevious(*ptr);
-    next_block = get_dlnext(*allocator, *ptr);
-
-    /* Find out the sizes of all possible combinations */
-
-    /* Current + Next */
-    if(next_block != NULL) {
-        if(is_free(next_block) == true) {
-#ifdef WITH_OWNERSHIP
-            if(get_owner(next_block) == get_owner(*ptr)) {
-#endif /* WITH_OWNERSHIP */
-                current_next_size = size + get_size(next_block) + HEADER_SIZE;
-#ifdef WITH_OWNERSHIP
-            } else {
-                current_next_size = (size_t) -1; /* SIZE_MAX */
-            }
-#endif /* WITH_OWNERSHIP */
-        } else {
-            current_next_size = (size_t) -1; /* SIZE_MAX */
-        }
-    } else {
-        current_next_size = (size_t) -1; /* SIZE_MAX */
-    }
-
-    /* Previous + Current */
-    if(is_previous_free(*ptr) == true) {
-#ifdef WITH_OWNERSHIP
-        if(get_owner(*ptr) == get_owner(previous_block)) {
-#endif /* WITH_OWNERSHIP */
-            previous_current_size = size + get_previous_size(*ptr) + HEADER_SIZE;
-#ifdef WITH_OWNERSHIP
-        } else {
-            current_next_size = (size_t) -1; /* SIZE_MAX */
-        }
-#endif /* WITH_OWNERSHIP */
-    } else {
-        previous_current_size = (size_t) -1; /* SIZE_MAX */
-    }
-
-    /* Previous + Current + Next */
-    if(next_block != NULL) {
-        if(is_previous_free(*ptr) && is_free(next_block)) {
-#ifdef WITH_OWNERSHIP
-            if(get_owner(*ptr) == get_owner(previous_block) &&
-                    get_owner(*ptr) == get_owner(next_block)) {
-#endif /* WITH_OWNERSHIP */
-                three_blocks_size = get_previous_size(*ptr) + size +
-                    get_size(next_block) + 2 * HEADER_SIZE;
-#ifdef WITH_OWNERSHIP
-            } else {
-                current_next_size = (size_t) -1; /* SIZE_MAX */
-            }
-#endif /* WITH_OWNERSHIP */
-        }
-    } else {
-        three_blocks_size = (size_t) -1; /* SIZE_MAX */
-    }
-
-    /* Check if Previous + Current + Next is ok */
-    if(three_blocks_size <= max_coal_size) {
-        remove_block_from_lists(&previous_block, heap);
-        /* Remove the next block from any freelist */
-        remove_block_from_lists(&next_block, heap);
-        /* Update border pointer if the next block was the border pointer */
-        if((*allocator)->border_ptr == next_block) {
-            (*allocator)->border_ptr = previous_block;
-        }
-        /* Reset the previous block size */
-        set_size_and_free(*allocator, previous_block, three_blocks_size);
-        return false;
-    }
-
-    /* Check if Previous + Current is ok */
-    if(previous_current_size <= max_coal_size) {
-        /* printf("pcs\n"); */
-        remove_block_from_lists(*ptr, heap);
-        /* Update border pointer if the current block was the border pointer */
-        if((*allocator)->border_ptr == *ptr) {
-            (*allocator)->border_ptr = previous_block;
-        }
-        /* Reset the previous block size */
-        set_size_and_free(*allocator, *ptr, previous_current_size);
-        return false;
-    }
- 
-    /* Check if Current + Next is ok */
-    if(current_next_size <= max_coal_size) {
-        /* printf("cn\n"); */
-        remove_block_from_lists(&next_block, heap);
-        if((*allocator)->border_ptr == next_block) {
-            (*allocator)->border_ptr = *ptr;
-        }
-        set_size_and_free(*allocator, *ptr, current_next_size);
-        return false;
-    }
-
-    /* If everything fails, just mark the block free */
-    mark_free(*allocator, *ptr);
-    return false;
-}

+ 0 - 160
src/custom_free.c

@@ -1,160 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include <stdio.h>
-#include <dmmlib/dmmlib.h>
-#include "other.h"
-#ifdef HAVE_LOCKS
-#include "posix_lock.h"
-#endif /* HAVE_LOCKS */
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-#include "coalesce.h"
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-#include "linked_lists/add_block.h"
-#ifdef WITH_ADAPTIVITY
-#include "dmm_adaptor.h"
-#endif /* WITH_ADAPTIVITY */
-#include "block_header.h"
-
-void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
-    size_t size;
-#ifdef WITH_FIXED_LISTS
-    int fixed_list_id, i;
-    maptable_node_t *current_maptable_node;
-#endif /* WITH_FIXED_LISTS */
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-    bool coalesced;
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-
-    if(ptr == NULL) {
-        return;
-    }
-
-    size = get_size(ptr);
-
-#ifdef HAVE_LOCKS	
-    posix_lock(heap);
-#endif /* HAVE_LOCKS */
-
-#ifdef WITH_STATS
-    /* Update Stats */
-#ifdef REQUEST_SIZE_INFO
-    heap->dmm_stats.mem_requested -= get_requested_size(ptr);
-#endif /* REQUEST_SIZE_INFO  */
-    heap->dmm_stats.mem_used -= HEADER_SIZE + size;
-    heap->dmm_stats.live_objects -= 1;
-    heap->dmm_stats.num_free += 1;
-    /* End of Stats */
-#endif /* WITH_STATS */
-
-#ifdef COUNT_HOPS
-    /* Initialize the hops counter */
-    heap->dmm_stats.total_hops = 0;
-#endif /* COUNT_HOPS */
-
-#ifdef FUTURE_FEATURES
-#ifdef COUNT_HOPS
-    remove_block(heap, &ptr, &heap->used_blocks_head);
-#else
-    remove_block(&ptr, &heap->used_blocks_head);
-#endif /* COUNT_HOPS */
-#endif /* FUTURE_FEATURES */
-
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-    coalesced = coalesce(&allocator, heap, &ptr);
-#else
-    mark_free(allocator, ptr);
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-
-#ifdef WITH_FIXED_LISTS
-    /* Check if the block could be put in a fixed list */
-    fixed_list_id = map_size_to_list(heap, size);
-
-    if(fixed_list_id != -1) {
-        current_maptable_node = heap->maptable_head;		
-        if(fixed_list_id != 0) {
-            for(i = 1; i < fixed_list_id; i++) {
-                current_maptable_node = current_maptable_node->next;
-            }
-        }
-#ifdef COUNT_HOPS
-#ifdef FIFO_SORT_POLICY
-        add_block(heap, &ptr, &current_maptable_node->fixed_list_head, &current_maptable_node->fixed_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(heap, &ptr, &current_maptable_node->fixed_list_head);
-#endif /* FIFO_SORT_POLICY */
-#else /* COUNT_HOPS */
-#ifdef FIFO_SORT_POLICY
-        add_block(&ptr, &current_maptable_node->fixed_list_head, &current_maptable_node->fixed_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(&ptr, &current_maptable_node->fixed_list_head);
-#endif /* FIFO_SORT_POLICY */
-#endif /* COUNT_HOPS */
-    } else { /* put it in the free list */
-#endif /* WITH_FIXED_LISTS */
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-        /* The block should be added to the free list only if it is not
-         * coalesced
-         */
-        if(!coalesced) {
-#ifdef COUNT_HOPS
-#ifdef FIFO_SORT_POLICY
-            add_block(heap, &ptr, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-            add_block(heap, &ptr, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#else /* COUNT_HOPS */
-#ifdef FIFO_SORT_POLICY
-            add_block(&ptr, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-            add_block(&ptr, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#endif /* COUNT_HOPS */
-        }
-#else /* COALESCING_FIXED || COALESCING_VARIABLE */
-#ifdef COUNT_HOPS
-#ifdef FIFO_SORT_POLICY
-        add_block(heap, &ptr, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(heap, &ptr, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#else /* COUNT_HOPS */
-#ifdef FIFO_SORT_POLICY
-        add_block(&ptr, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(&ptr, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#endif /* COUNT_HOPS */
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-#ifdef WITH_FIXED_LISTS
-    }
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef WITH_ADAPTIVITY
-    /* Refresh the state of the heap allocator if a certain number of
-     * free's has been served already
-     */
-    /* TODO Define 100 as a constant */
-    if(heap->dmm_stats.num_free % 100) {
-        free_state_refresh(heap);
-    }
-#endif /* WITH_ADAPTIVITY */
-
-#ifdef HAVE_LOCKS
-    posix_unlock(heap);
-#endif /* HAVE_LOCKS */
-}

+ 0 - 125
src/custom_malloc.c

@@ -1,125 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include <dmmlib/dmmlib.h>
-#include "dmm_config.h"
-#ifdef HAVE_LOCKS
-#include "posix_lock.h"
-#endif /* HAVE_LOCKS */
-#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-#include "split.h"
-#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
-#include "other.h"
-#include "linked_lists/linked_lists.h"
-#include "linked_lists/search_algorithms.h"
-#include "sys_alloc.h"
-#include "block_header.h"
-#ifdef WITH_ADAPTIVITY
-#include "dmm_adaptor.h"
-#endif /* WITH_ADAPTIVITY */
-#include <stdio.h>
-
-#if defined (BEST_FIT)
-#define search_on_free(size) best_fit_on_freelist(heap, size)
-#elif defined (GOOD_FIT)
-#define search_on_free(size) good_fit_on_freelist(heap, size)
-#elif defined (EXACT_FIT)
-#define search_on_free(size) exact_fit_on_freelist(heap, size)
-#elif defined (FIRST_FIT)
-#define search_on_free(size) first_fit_on_freelist(heap, size)
-#endif /* BEST_FIT */
-
-void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
-    void *ptr;
-
-    ptr = NULL;
-    
-#ifdef HAVE_LOCKS
-    posix_lock(heap);
-#endif /* HAVE_LOCKS */
-
-#ifdef COUNT_HOPS
-    /* Initialize the hops counter */
-    heap->dmm_stats.total_hops = 0;
-#endif /* COUNT_HOPS */
-
-#ifdef WITH_FIXED_LISTS
-    ptr = search_on_fixed(heap, req_padding(size));
-
-    if(ptr == NULL) {
-#endif /* WITH_FIXED_LISTS */
-#ifdef ALLOC_VAR_FIT
-        ptr = (*allocator->search_policy)(heap, req_padding(size));
-#elif defined (HEAP_VAR_FIT)
-        ptr = (*heap->dmm_knobs.search_policy)(heap, req_padding(size));
-#else /* ALLOC_VAR_FIT */
-        ptr = search_on_free(size);
-#endif /* ALLOC_VAR_FIT */
-#ifdef WITH_FIXED_LISTS
-    }
-#endif /* WITH_FIXED_LISTS */
-
-    if(ptr != NULL) {
-
-#ifdef REQUEST_SIZE_INFO
-        set_requested_size(ptr, size);
-#endif /* REQUEST_SIZE_INFO */
-
-        /* Try to split */
-#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-        split(allocator, heap, ptr, size);
-#endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
-
-        mark_used(allocator, ptr);
-
-#ifdef FUTURE_FEATURES
-        /* Update the used blocks list */
-        push_block(&ptr, &heap->used_blocks_head);
-#endif /* FUTURE_FEATURES */
-
-#ifdef WITH_STATS
-        /* Update statistics */
-#ifdef REQUEST_SIZE_INFO
-        heap->dmm_stats.mem_requested += size;
-#endif /* REQUEST_SIZE_INFO */
-        heap->dmm_stats.mem_used += HEADER_SIZE + get_size(ptr);
-        heap->dmm_stats.live_objects += 1;
-        heap->dmm_stats.num_malloc += 1;
-#endif /* WITH_STATS */
-
-        /* FIXME To be refactored - END */
-
-    } else {
-        ptr = sys_alloc(allocator, heap, size);
-    }
-
-#ifdef WITH_ADAPTIVITY
-    /* Refresh the state of the heap allocator if a certain number of
-     * malloc's has been served already
-     */
-    /* TODO Define 50 as a constant */
-    if(heap->dmm_stats.num_malloc % 50) {
-        malloc_state_refresh(heap);
-    }
-#endif /* WITH_ADAPTIVITY */
-
-#ifdef HAVE_LOCKS
-    posix_unlock(heap);
-#endif /* HAVE_LOCKS */
-
-    return ptr;
-}

+ 42 - 0
src/debug.c

@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <dmmlib/block_header.h>
+#include "freelist/block_header_funcs.h"
+#include "freelist/linked_lists/search_algorithms.h"
+#include "debug.h"
+
+void get_raw_blocks(allocator_t *allocator) {
+    int counter;
+    raw_block_header_t *current_raw_block;
+
+    current_raw_block = allocator->raw_block_head;
+    counter = 0;
+
+    while(current_raw_block) {
+        counter++;
+        printf("Raw block at %p with remaining size %zu\n",
+                (void *)current_raw_block,
+                current_raw_block->remaining_size);
+        current_raw_block = current_raw_block->next_raw_block;
+    }
+
+    printf("There are %d raw blocks\n", counter);
+
+}
+
+void get_memory_blocks(raw_block_header_t *raw_block) {
+    block_header_t *memory_block;
+    int counter;
+
+    memory_block = raw_block->free_list_head;
+    counter = 0;
+
+    while(memory_block) {
+        counter++;
+        printf("Free memory block at %p with size %zu\n",
+                (void *)memory_block, get_size(memory_block));
+        memory_block = memory_block->next;
+    }
+
+    printf("Raw block at %p has %d memory blocks\n",
+            (void *)raw_block, counter);
+}

+ 130 - 0
src/freelist/block_header_funcs.c

@@ -0,0 +1,130 @@
+/*
+ *   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.
+ *
+ */
+
+#include "freelist/block_header_funcs.h"
+
+block_header_t * get_header(void *ptr) {
+    return (block_header_t *) ((char *) ptr - HEADER_SIZE);
+}
+
+size_t get_size(block_header_t *ptr) {
+    return ptr->size >> 1;
+}
+
+#ifdef REQUEST_SIZE_INFO
+void set_requested_size(block_header_t *ptr, size_t size) {
+    ptr->requested_size = size;
+}
+
+size_t get_requested_size(block_header_t *ptr) {
+    return ptr->requested_size;
+}
+#endif /* REQUEST_SIZE_INFO */
+
+size_t get_size_availability(block_header_t *ptr) {
+    return ptr->size;
+}
+
+void set_size_and_free(raw_block_header_t *raw_block, block_header_t *ptr, size_t size) {
+    block_header_t *next_block_header;
+
+    ptr->size = size << 1;
+
+    if(raw_block->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + size);
+        next_block_header->previous_size = size << 1;
+    }
+}
+
+void set_size_and_used(raw_block_header_t *raw_block, block_header_t *ptr, size_t size) {
+    block_header_t *next_block_header;
+
+    ptr->size = size << 1;
+    ptr->size |= 1;
+
+    if(raw_block->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + size);
+        next_block_header->previous_size = ptr->size;
+    }
+}
+
+void mark_used(raw_block_header_t *raw_block, block_header_t *ptr) {
+    block_header_t *next_block_header;
+
+    ptr->size |= 1;
+
+    if(raw_block->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size |= 1;
+    }
+}
+
+void mark_free(raw_block_header_t *raw_block, block_header_t *ptr) {
+    block_header_t *next_block_header;
+    
+    ptr->size &= (~ ((size_t) 0x1));
+
+    if(raw_block->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size &= (~ ((size_t) 0x1));
+    }    
+}
+
+bool is_free(block_header_t * ptr) {
+    if(ptr == NULL) {
+        return false;
+    }
+
+    if(ptr->size & (size_t) 1) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
+bool is_previous_free(block_header_t *ptr) {
+    if(ptr->previous_size & (size_t) 1) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
+void set_previous_size_availability(block_header_t *ptr, size_t previous_size_availability) {
+    ptr->previous_size = previous_size_availability;
+}
+
+size_t get_previous_size(block_header_t *ptr) {
+    return ptr->previous_size >> 1;
+}
+
+size_t get_previous_size_availability(block_header_t *ptr) {
+    return ptr->previous_size;
+}
+
+block_header_t * get_dlprevious(block_header_t *ptr) {
+    return (block_header_t *) ((char *) ptr - HEADER_SIZE - get_previous_size(ptr));
+}
+
+block_header_t * get_dlnext(raw_block_header_t *raw_block, block_header_t *ptr) {
+    if(raw_block->border_ptr != ptr) {
+        return (block_header_t *) ((char *) ptr + get_size(ptr) + HEADER_SIZE);
+    } else {
+        return (block_header_t *) NULL;
+    }
+}
+

+ 113 - 0
src/freelist/coalesce.c

@@ -0,0 +1,113 @@
+/*
+ *   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.
+ *
+ */
+
+#include "freelist/coalesce.h"
+#include "freelist/linked_lists/add_block.h"
+#include "freelist/linked_lists/linked_lists.h"
+#include "freelist/block_header_funcs.h"
+#include "other.h"
+#include "dmm_config.h"
+
+void coalesce(raw_block_header_t *raw_block, block_header_t *ptr) {
+    size_t max_coal_size;
+    size_t size;
+    size_t current_next_size;
+    size_t previous_current_size;
+    size_t three_blocks_size;
+    block_header_t *previous_block, *next_block;
+
+#ifdef COALESCING_FIXED
+    max_coal_size = MAX_COALESCE_SIZE;
+#endif /* COALESCING_FIXED */
+#ifdef COALESCING_VARIABLE
+    max_coal_size = heap->dmm_knobs.max_coalesce_size;
+#endif /* COALESCING_VARIABLE */
+
+    size = get_size(ptr);
+
+    previous_block = get_dlprevious(ptr);
+    next_block = get_dlnext(raw_block, ptr);
+
+    /* Find out the sizes of all possible combinations */
+
+    /* Current + Next */
+    if(next_block != NULL) {
+        if(is_free(next_block) == true) {
+                current_next_size = size + get_size(next_block) + HEADER_SIZE;
+        } else {
+            current_next_size = (size_t) -1; /* SIZE_MAX */
+        }
+    } else {
+        current_next_size = (size_t) -1; /* SIZE_MAX */
+    }
+
+    /* Previous + Current */
+    if(is_previous_free(ptr) == true) {
+        previous_current_size = size + get_previous_size(ptr) + HEADER_SIZE;
+    } else {
+        previous_current_size = (size_t) -1; /* SIZE_MAX */
+    }
+
+    /* Previous + Current + Next */
+    if(is_previous_free(ptr) == true && is_free(next_block) == true) {
+        three_blocks_size = get_previous_size(ptr) + size +
+            get_size(next_block) + 2 * HEADER_SIZE;
+    } else {
+        three_blocks_size = (size_t) -1; /* SIZE_MAX */
+    }
+
+    /* Check if Previous + Current + Next is ok */
+    if(three_blocks_size <= max_coal_size) {
+        /* Remove the next block from the free list */
+        remove_block(next_block, raw_block);
+        /* Update border pointer if the next block was the border pointer */
+        if(raw_block->border_ptr == next_block) {
+            raw_block->border_ptr = previous_block;
+        }
+        /* Reset the previous block size */
+        set_size_and_free(raw_block, previous_block, three_blocks_size);
+        return;
+    }
+
+    /* Check if Previous + Current is ok */
+    if(previous_current_size <= max_coal_size) {
+        /* Update border pointer if the current block was the border pointer */
+        if(raw_block->border_ptr == ptr) {
+            raw_block->border_ptr = previous_block;
+        }
+        /* Reset the previous block size */
+        set_size_and_free(raw_block, previous_block, previous_current_size);
+        return;
+    }
+ 
+    /* Check if Current + Next is ok */
+    if(current_next_size <= max_coal_size) {
+        remove_block(next_block, raw_block);
+        if(raw_block->border_ptr == next_block) {
+            raw_block->border_ptr = ptr;
+        }
+        set_size_and_free(raw_block, ptr, current_next_size);
+        return;
+    }
+
+    /* If everything fails, just mark the block free and add it to the free
+     * list
+     */
+    mark_free(raw_block, ptr);
+    add_block(raw_block, ptr);
+    return;
+}

+ 156 - 0
src/freelist/freelist_malloc.c

@@ -0,0 +1,156 @@
+/*
+ *   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.
+ *
+ */
+
+#include <dmmlib/dmmlib.h>
+#include "dmm_config.h"
+
+#ifdef HAVE_LOCKS
+#include <pthread.h>
+#endif /* HAVE_LOCKS */
+
+#include "freelist/block_header_funcs.h"
+#include "freelist/linked_lists/linked_lists.h"
+#include "freelist/linked_lists/search_algorithms.h"
+#include "other.h"
+
+#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
+#include "freelist/split.h"
+#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+
+#include "freelist/freelist_malloc.h"
+
+#include <stdio.h>
+
+#if defined (BEST_FIT)
+#define search_on_free(size) best_fit_on_freelist(raw_block, size)
+#elif defined (GOOD_FIT)
+#define search_on_free(size) good_fit_on_freelist(raw_block, size)
+#elif defined (EXACT_FIT)
+#define search_on_free(size) exact_fit_on_freelist(raw_block, size)
+#elif defined (FIRST_FIT)
+#define search_on_free(size) first_fit_on_freelist(raw_block, size)
+#endif /* BEST_FIT */
+
+void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
+    block_header_t *ptr;
+    size_t allocation_size, previous_size, previous_size_availability;
+
+    ptr = NULL;
+
+#ifdef HAVE_LOCKS
+    pthread_mutex_lock(&raw_block->mutex);
+#endif /* HAVE_LOCKS */
+
+    ptr = search_on_free(size);
+
+    if(ptr != NULL) {
+#ifdef REQUEST_SIZE_INFO
+        set_requested_size(ptr, size);
+#endif /* REQUEST_SIZE_INFO */
+
+        /* Try to split */
+#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
+        split(raw_block, ptr, size);
+#endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
+
+        mark_used(raw_block, ptr);
+    } else {
+
+        allocation_size = req_padding(size) + HEADER_SIZE;
+        if(allocation_size < raw_block->remaining_size) {
+            if(raw_block->border_ptr == NULL) {
+                previous_size_availability = 1; // Occupied and of 0 size
+                ptr = (block_header_t *)((char *)raw_block +
+                        sizeof(raw_block_header_t));
+            } else {
+                previous_size = get_size(raw_block->border_ptr);
+                previous_size_availability =
+                    get_size_availability(raw_block->border_ptr);
+                ptr = (block_header_t *)((char *)raw_block->border_ptr +
+                        previous_size);
+            }
+
+            // Update raw block metadata
+            raw_block->remaining_size -= allocation_size;
+            raw_block->border_ptr = ptr;
+
+            // Update block metadata
+            set_size_and_used(raw_block, ptr, req_padding(size));
+            set_previous_size_availability(ptr, previous_size_availability);
+#ifdef REQUEST_SIZE_INFO
+            set_requested_size(ptr, req_padding(size));
+#endif /* REQUEST_SIZE_INFO */
+        }
+    }
+
+#ifdef WITH_STATS
+    if(ptr != NULL) {
+#if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
+        raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
+#endif /* !((SPLITTING_FIXED) || (SPLITTING_VARIABLE)) */
+        raw_block->dmm_stats.live_objects++;
+        raw_block->dmm_stats.num_malloc++;
+#ifdef REQUEST_SIZE_INFO
+        raw_block->dmm_stats.total_mem_requested += size;
+#endif /* REQUEST_SIZE_INFO */
+#endif /* WITH_STATS */
+    }
+
+#ifdef HAVE_LOCKS
+    pthread_mutex_unlock(&raw_block->mutex);
+#endif /* HAVE_LOCKS */
+
+    if(ptr != NULL) {
+        return (void *)((char *)ptr + HEADER_SIZE);
+    } else {
+        return NULL;
+    }
+}
+
+#include "freelist/linked_lists/add_block.h"
+#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
+#include "freelist/coalesce.h"
+#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
+
+void freelist_free(raw_block_header_t *raw_block, void *ptr) {
+    ptr = get_header(ptr);
+
+#ifdef HAVE_LOCKS
+    pthread_mutex_lock(&raw_block->mutex);
+#endif /* HAVE_LOCKS */
+
+#ifdef WITH_STATS
+    raw_block->dmm_stats.total_mem_allocated -= get_size(ptr);
+#endif /* WITH_STATS */
+
+#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
+    coalesce(raw_block, ptr);
+#else
+    mark_free(raw_block, ptr);
+    add_block(raw_block, ptr);
+#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
+
+#ifdef WITH_STATS
+    raw_block->dmm_stats.live_objects--;
+    raw_block->dmm_stats.num_free++;
+#endif /* WITH_STATS */
+
+#ifdef HAVE_LOCKS
+    pthread_mutex_unlock(&raw_block->mutex);
+#endif /* HAVE_LOCKS */
+
+}

src/linked_lists/add_block_in_order.c → src/freelist/linked_lists/add_block_in_order.c


src/linked_lists/address_order.c → src/freelist/linked_lists/address_order.c


src/linked_lists/fifo_order.c → src/freelist/linked_lists/fifo_order.c


+ 8 - 11
private-include/posix_lock.h

@@ -15,15 +15,12 @@
  *
  */
 
-#ifndef POSIX_LOCK_H
-#define POSIX_LOCK_H
+#include "freelist/linked_lists/lifo_order.h"
 
-#include <dmmlib/heap.h>
-
-void sbrk_lock(void);
-void sbrk_unlock(void);
-
-void posix_lock(heap_t *heap);
-void posix_unlock(heap_t *heap);
-
-#endif /* POSIX_LOCK_H */
+void add_block_lifo_order(
+        raw_block_header_t *raw_block,
+        block_header_t *block) {
+    
+    block->next = raw_block->free_list_head;
+    raw_block->free_list_head = block;
+}

+ 79 - 0
src/freelist/linked_lists/linked_lists.c

@@ -0,0 +1,79 @@
+/*
+ *   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.
+ *
+ */
+
+#include "freelist/linked_lists/linked_lists.h"
+
+#ifdef BLOCKS_IN_DLL
+
+void set_previous(block_header_t *ptr, block_header_t *previous_block) {
+    ptr->previous = previous_block;
+}
+
+void * get_previous(void *ptr) {
+    return (void *) ptr->previous;
+}
+
+#endif /* BLOCKS_IN_DLL */
+
+#ifdef BLOCKS_IN_SLL
+void remove_block(block_header_t *block, raw_block_header_t *raw_block) {
+
+    block_header_t *current_node, *previous_node;
+
+    previous_node = NULL;
+
+    /* Traverse a list starting from the starting node until block is found. */
+    for(current_node = raw_block->free_list_head; current_node != NULL;
+            current_node = current_node->next) {
+        if(current_node == block) {
+            if(current_node == raw_block->free_list_head) {
+                raw_block->free_list_head = block->next;
+            } else {
+                previous_node->next = block->next;
+            }
+            break;
+        }
+        previous_node = current_node;
+    }
+}
+#endif /* BLOCKS_IN_SLL */
+
+#ifdef BLOCKS_IN_DLL
+void remove_block(void **block, void **starting_node) {
+    void *previous_block, *next_block;
+
+    /* No need to traverse the list, just check if the block is the starting
+     * node of the list.
+     */
+
+    previous_block = get_previous(*block);
+    next_block = get_next(*block);
+
+    if(*block == *starting_node) {
+        *starting_node = previous_block;
+    }
+
+    if(previous_block != NULL) {
+        set_next(previous_block, next_block);
+    }
+
+    if(next_block != NULL) {
+        set_previous(next_block, previous_block);
+    }
+
+}
+#endif /* BLOCKS_IN_DLL */

+ 25 - 70
src/linked_lists/search_algorithms.c

@@ -16,43 +16,8 @@
  */
 
 #include <stdio.h>
-#include "linked_lists/search_algorithms.h"
-#include "block_header.h"
-
-#ifdef WITH_FIXED_LISTS
-/**
- * \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 (provided that it is not null) 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) {
-#ifdef COUNT_HOPS
-        heap->dmm_stats.total_hops++;
-#endif /* COUNT_HOPS */
-        if(node->size == requested_size) {
-            if(node->fixed_list_head != NULL) {
-                ptr = node->fixed_list_head;
-                node->fixed_list_head = get_next(ptr);
-#ifdef BLOCKS_IN_DLL
-                set_previous(node->fixed_list_head, NULL);
-#endif /* BLOCKS_IN_DLL */
-            }
-            break;
-        }
-        node = node->next;
-    }
-
-    return ptr;
-}
-#endif /* WITH_FIXED_LISTS */
+#include "freelist/linked_lists/search_algorithms.h"
+#include "freelist/block_header_funcs.h"
 
 /**
  * \details In order to remove a block from a singly linked list, we need to
@@ -63,9 +28,9 @@ void * search_on_fixed(heap_t * heap, size_t requested_size) {
  * time a new best candidate is found, so that we stop once a perfect block is
  * found.
  */
-void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
-    void *current_block, *previous_block;
-    void *best_block, *best_previous_block;
+block_header_t * best_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size) {
+    block_header_t *current_block, *previous_block;
+    block_header_t *best_block, *best_previous_block;
     size_t best_size, block_size;
 
     current_block = NULL;
@@ -75,11 +40,8 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
     best_size = (size_t) -1; /* SIZE_MAX */
     block_size = 0;
 
-    for(current_block = heap->free_list_head; current_block != NULL;
-            current_block = get_next(current_block)) {
-#ifdef COUNT_HOPS
-        heap->dmm_stats.total_hops++;
-#endif /* COUNT_HOPS */
+    for(current_block = raw_block->free_list_head; current_block != NULL;
+            current_block = current_block->next) {
         block_size = get_size(current_block);
         if(block_size >= requested_size) {
             if(block_size < best_size) {
@@ -99,15 +61,15 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
     /* Remove the block from the list */
     /* Note: remove_block() is not used because the block is already found */
     if(best_block != NULL) {
-        if(best_block == heap->free_list_head) {
-            heap->free_list_head = get_next(best_block);
+        if(best_block == raw_block->free_list_head) {
+            raw_block->free_list_head = best_block->next;
 #ifdef BLOCKS_IN_DLL
             if(heap->free_list_head != NULL) {
                 set_previous(heap->free_list_head, NULL);
             }
 #endif /* BLOCKS_IN_DLL */
         } else {
-            set_next(best_previous_block, get_next(best_block));
+            best_previous_block->next = best_block->next;
 #ifdef BLOCKS_IN_DLL
             if(get_next(best_block) != NULL) {
                 set_previous(get_next(best_block), best_previous_block);
@@ -121,7 +83,7 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
 
 #ifdef GOOD_FIT
 
-void * good_fit_on_freelist(heap_t *heap, size_t requested_size) {
+void * good_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size) {
     void *current_block, *previous_block;
     void *best_block, *best_previous_block;
     size_t best_size, block_size;
@@ -185,25 +147,22 @@ void * good_fit_on_freelist(heap_t *heap, size_t requested_size) {
  * keep track of the previous block as well: The previous block must point to
  * the current block's next block once the current one is removed.
  */
-void * exact_fit_on_freelist(heap_t *heap, size_t requested_size) {
-    void *current_block, *previous_block, *ptr;
+block_header_t * exact_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size) {
+    block_header_t *current_block, *previous_block, *ptr;
 
     previous_block = NULL;
     ptr = NULL;
 
-    for(current_block = heap->free_list_head; current_block != NULL;
-            current_block = get_next(current_block)) {
-#ifdef COUNT_HOPS
-        heap->dmm_stats.total_hops++;
-#endif /* COUNT_HOPS */
+    for(current_block = raw_block->free_list_head; current_block != NULL;
+            current_block = current_block->next) {
         if(get_size(current_block) == requested_size) {
-            if(current_block == heap->free_list_head) {
-                heap->free_list_head = get_next(current_block);
+            if(current_block == raw_block->free_list_head) {
+                raw_block->free_list_head = current_block->next;
 #ifdef BLOCKS_IN_DLL
                 set_previous(heap->free_list_head, NULL);
 #endif /* BLOCKS_IN_DLL */
             } else {
-                set_next(previous_block, get_next(current_block));
+                previous_block->next = current_block->next;
 #ifdef BLOCKS_IN_DLL
                 set_previous(get_next(current_block), previous_block);
 #endif /* BLOCKS_IN_DLL */
@@ -222,25 +181,22 @@ void * exact_fit_on_freelist(heap_t *heap, size_t requested_size) {
  * keep track of the previous block as well: The previous block must point to
  * the current block's next block once the current one is removed.
  */
-void * first_fit_on_freelist(heap_t *heap, size_t requested_size) {
-    void *current_block, *previous_block, *ptr;
+block_header_t * first_fit_on_freelist(raw_block_header_t *raw_block, size_t requested_size) {
+    block_header_t *current_block, *previous_block, *ptr;
 
     previous_block = NULL;
     ptr = NULL;
 
-    for(current_block = heap->free_list_head; current_block != NULL;
-            current_block = get_next(current_block)) {
-#ifdef COUNT_HOPS
-        heap->dmm_stats.total_hops++;
-#endif /* COUNT_HOPS */
+    for(current_block = raw_block->free_list_head; current_block != NULL;
+            current_block = current_block->next) {
         if(get_size(current_block) >= requested_size) {
-            if(current_block == heap->free_list_head) {
-                heap->free_list_head = get_next(current_block);
+            if(current_block == raw_block->free_list_head) {
+                raw_block->free_list_head = current_block->next;
 #ifdef BLOCKS_IN_DLL
                 set_previous(heap->free_list_head, NULL);
 #endif /* BLOCKS_IN_DLL */
             } else {
-                set_next(previous_block, get_next(current_block));
+                previous_block->next = current_block->next;
 #ifdef BLOCKS_IN_DLL
                 set_previous(get_next(current_block), previous_block);
 #endif /* BLOCKS_IN_DLL */
@@ -253,4 +209,3 @@ void * first_fit_on_freelist(heap_t *heap, size_t requested_size) {
 
     return ptr;
 }
-

src/linked_lists/size_order.c → src/freelist/linked_lists/size_order.c


+ 110 - 0
src/freelist/split.c

@@ -0,0 +1,110 @@
+/*
+ *   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.
+ *
+ */
+
+#include "freelist/block_header_funcs.h"
+#include "other.h"
+#include "freelist/linked_lists/add_block.h"
+#include "freelist/split.h"
+
+
+void split(raw_block_header_t *raw_block, block_header_t *ptr, size_t req_size) {
+    size_t new_size;
+    size_t min_split_size;
+    block_header_t *new_block;
+#ifdef COALESCE_AFTER_SPLIT
+    size_t max_coal_size;
+    block_header_t *next_block;
+    size_t current_next_size;
+
+    next_block = get_dlnext(allocator, ptr);
+#endif /* COALESCE_AFTER_SPLIT */
+
+    /* Check what would be the size of the new block if we split the current
+     * one.
+     * Note: new_size is a size_t, so compare first in order to prevent an
+     * underflow.
+     */
+    if(get_size(ptr) > req_size + HEADER_SIZE) {
+        new_size = get_size(ptr) - req_size - HEADER_SIZE;
+    } else {
+        new_size = 0;
+    }
+
+#ifdef SPLITTING_FIXED
+    min_split_size = MIN_SPLITTING_SIZE;
+#endif /* SPLITTING_FIXED */
+#ifdef SPLITTING_VARIABLE
+    min_split_size = heap->dmm_knobs.min_split_size;
+#endif /* SPLITTING_VARIABLE */
+
+    if(new_size < min_split_size) {
+#ifdef WITH_STATS
+        raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
+#endif /* WITH_STATS */
+        return;
+    }
+
+    new_block = (block_header_t *)((char *)ptr + req_size + HEADER_SIZE);
+
+    if(raw_block->border_ptr == ptr) {
+        raw_block->border_ptr = new_block;
+    }
+
+    /* Resize the previous, to be used block */
+    set_size_and_used(raw_block, ptr, req_size);
+
+#ifdef COALESCE_AFTER_SPLIT
+    /* Try to coalesce with the next block if it is free */
+#ifdef COALESCING_FIXED
+    max_coal_size = MAX_COALESCE_SIZE;
+#endif /* COALESCING_FIXED */
+#ifdef COALESCING_VARIABLE
+    max_coal_size = heap->dmm_knobs.max_coalesce_size;
+#endif /* COALESCING_VARIABLE */
+
+    if(next_block != (memory_block_ptr) NULL) {
+        if(is_free(next_block) == true) {
+            current_next_size = new_size + get_size(next_block) +
+                HEADER_SIZE;
+            if(current_next_size <= max_coal_size) {
+                /* it's ok to coalesce the new split block with the next
+                 * free block
+                 */
+                remove_block_from_lists(&next_block, heap);
+                if(allocator->border_ptr == next_block) {
+                    allocator->border_ptr = ptr;
+                }
+                new_size = current_next_size;
+            }
+        }
+    }
+
+#endif /* COALESCE_AFTER_SPLIT */
+
+    set_size_and_free(raw_block, new_block, new_size);
+
+#ifdef FIFO_SORT_POLICY
+    add_block(&new_block, &heap->free_list_head, &heap->free_list_tail);
+#else /* FIFO_SORT_POLICY */
+    add_block(raw_block, new_block);
+#endif /* FIFO_SORT_POLICY */
+
+#ifdef WITH_STATS
+    raw_block->dmm_stats.total_mem_allocated += req_size;
+#endif /* WITH_STATS */
+
+}

+ 0 - 170
src/initialize_allocator.c

@@ -1,170 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include <dmmlib/initialize_allocator.h>
-#ifdef HAVE_LOCKS
-#include "posix_lock.h"
-#endif /* HAVE_LOCKS */
-#include "sys_alloc.h"
-
-#if defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT)
-#include <string.h> /* for strcmp() */
-#include "linked_lists/search_algorithms.h"
-#endif /* defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT) */
-
-#ifdef NO_SYSTEM_CALLS
-void initialize_allocator(allocator_t *allocator, void *starting_address, 
-        size_t size) {
-#else
-void initialize_allocator(allocator_t *allocator) {
-#endif /* NO_SYSTEM_CALLS */
-    int i;
-#ifdef WITH_FIXED_LISTS
-    heap_t *current_heap;
-    maptable_node_t *maptablenode;
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef HAVE_LOCKS
-    sbrk_lock();
-#endif /* HAVE_LOCKS */
-
-    for(i = 0; i < NUM_HEAPS; i++) {
-#ifdef HEAP_VAR_FIT
-        if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) {
-            allocator->heaps[i].dmm_knobs.search_policy = &best_fit_on_freelist;
-        } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) {
-            allocator->heaps[i].dmm_knobs.search_policy = &good_fit_on_freelist;
-        } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) {
-            allocator->heaps[i].dmm_knobs.search_policy = &exact_fit_on_freelist;
-        } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) {
-            allocator->heaps[i].dmm_knobs.search_policy = &first_fit_on_freelist;
-        }
-#endif /* HEAP_VAR_FIT */
-#ifdef WITH_FIXED_LISTS
-        allocator->heaps[i].maptable_head = NULL;
-#endif /* WITH_FIXED_LISTS */
-        allocator->heaps[i].free_list_head = NULL;
-#ifdef FUTURE_FEATURES
-        allocator->heaps[i].used_blocks_head = NULL;
-        allocator->heaps[i].rov_ptr = NULL;
-#endif /* FUTURE_FEATURES */
-        allocator->heaps[i].num_objects = 0;
-
-#ifdef WITH_STATS
-#ifdef REQUEST_SIZE_INFO
-        allocator->heaps[i].dmm_stats.mem_requested = 0;
-#endif /* REQUEST_SIZE_INFO */
-        allocator->heaps[i].dmm_stats.mem_used = 0;
-        allocator->heaps[i].dmm_stats.mem_allocated = 0;
-        allocator->heaps[i].dmm_stats.live_objects = 0;
-#ifdef COUNT_ACCESSES
-        allocator->heaps[i].dmm_stats.read_mem_accesses = 0;
-        allocator->heaps[i].dmm_stats.write_mem_accesses = 0;
-#endif /* COUNT_ACCESSES */
-        allocator->heaps[i].dmm_stats.num_malloc = 0;
-        allocator->heaps[i].dmm_stats.num_free = 0;
-#endif /* WITH_STATS */
-
-#ifdef WITH_KNOBS
-        /* Knobs initialization */
-
-#ifdef GOOD_FIT
-        allocator->heaps[i].dmm_knobs.fit_percentage = FIT_PERCENTAGE;
-#endif /* GOOD_FIT */
-
-#ifdef COALESCING_VARIABLE
-        allocator->heaps[i].dmm_knobs.max_coalesce_size = MAX_COALESCE_SIZE;
-#endif /* COALESCING_VARIABLE */
-
-#ifdef SPLITTING_VARIABLE
-        allocator->heaps[i].dmm_knobs.min_split_size = MIN_SPLITTING_SIZE;
-#endif /* SPLITTING_VARIABLE */
-
-        /* FIXME Create a constant for the initial value of the next
-         * variables
-         */
-        allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
-        allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
-#endif /* WITH_KNOBS */
-
-#ifdef HAVE_LOCKS
-        pthread_mutex_init(&allocator->heaps[i].mutex, NULL);
-#endif /* HAVE_LOCKS */
-    }
-
-#ifndef NO_SYSTEM_CALLS
-    allocator->border_ptr = NULL;
-    allocator->remaining_size = 0;
-#else /* NO_SYSTEM_CALLS */
-    allocator->border_ptr = starting_address;
-    allocator->remaining_size = size;
-#endif /* NO_SYSTEM_CALLS */
-    allocator->initialized = false;
-
-#ifdef ALLOC_VAR_FIT
-    if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) {
-        allocator->search_policy = &best_fit_on_freelist;
-    } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) {
-        allocator->search_policy = &good_fit_on_freelist;
-    } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) {
-        allocator->search_policy = &exact_fit_on_freelist;
-    } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) {
-        allocator->search_policy = &first_fit_on_freelist;
-    }
-#endif /* ALLOC_VAR_FIT */
-
-#ifdef WITH_FIXED_LISTS
-
-    /* Custom number of fixed lists and their initialization
-     * Fixed lists of 32, 64, 128 and 256 bytes (4 fixed lists per heap)
-     */
-    current_heap = &allocator->heaps[0];
-
-#ifdef HAVE_LOCKS
-    /* sys_alloc() is going to use the sbrk() lock, so it needs to be released.
-     */
-    sbrk_unlock();
-#endif /* HAVE_LOCKS */
-
-    maptablenode = (maptable_node_t *) sys_alloc(allocator, &allocator->heaps[0], 4*(sizeof(maptable_node_t)));
-
-#ifdef HAVE_LOCKS
-    /* Take back the sbrk() lock */
-    sbrk_lock();
-#endif /* HAVE_LOCKS */
-
-    maptablenode->size = 32;
-    maptablenode->fixed_list_head = NULL;
-    maptablenode->next = maptablenode+1;
-    current_heap->maptable_head = maptablenode;
-    (maptablenode+1)->size = 64;
-    (maptablenode+1)->fixed_list_head = NULL;
-    (maptablenode+1)->next = maptablenode+2;
-    (maptablenode+2)->size = 128;
-    (maptablenode+2)->fixed_list_head = NULL;
-    (maptablenode+2)->next = maptablenode+3;
-    (maptablenode+3)->size = 256;
-    (maptablenode+3)->fixed_list_head = NULL;
-    (maptablenode+3)->next = NULL;
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef HAVE_LOCKS
-    sbrk_unlock();
-#endif /* HAVE_LOCKS */
-    
-}
-

+ 0 - 147
src/linked_lists/linked_lists.c

@@ -1,147 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include "linked_lists/linked_lists.h"
-#ifdef WITH_FIXED_LISTS
-#include "other.h"
-#endif /* WITH_FIXED_LISTS */
-#include "block_header.h"
-
-void set_next(void *ptr, void *next_block) {
-    get_header(ptr)->pointers.next = (list_node_ptr) next_block;
-}
-
-void * get_next(void *ptr) {
-    return (void *) get_header(ptr)->pointers.next;
-}
-
-#ifdef BLOCKS_IN_DLL
-
-void set_previous(void *ptr, void *previous_block) {
-    get_header(ptr)->pointers.previous = (list_node_ptr) previous_block;
-}
-
-void * get_previous(void *ptr) {
-    return (void *) get_header(ptr)->pointers.previous;
-}
-
-#endif /* BLOCKS_IN_DLL */
-
-void push_block(void **block, void **starting_node) {
-    if(*starting_node != NULL) {
-#ifdef BLOCKS_IN_DLL
-        set_previous(*starting_node, *block);
-#endif /* BLOCKS_IN_DLL */
-        set_next(*block, *starting_node);
-    } else {
-       set_next(*block, NULL);
-    }
-#ifdef BLOCKS_IN_DLL
-    set_previous(*block, NULL);
-#endif /* BLOCKS_IN_DLL */
-     *starting_node = *block;
-}
-
-#ifdef BLOCKS_IN_SLL
-#ifdef COUNT_HOPS
-void remove_block(heap_t *heap, void **block, void **starting_node) {
-#else
-void remove_block(void **block, void **starting_node) {
-#endif /* COUNT_HOPS */
-
-    void *current_node, *previous_node;
-
-    /* Traverse a list starting from the starting node until block is found. */
-    for(current_node = *starting_node; current_node != NULL; 
-            current_node = get_next(current_node)) {
-#ifdef COUNT_HOPS
-        heap->dmm_stats.total_hops++;
-#endif /* COUNT_HOPS */
-        if(current_node == *block) {
-            if(current_node == *starting_node) {
-                *starting_node = get_next(*block);
-            } else {
-                set_next(previous_node, get_next(*block));
-            }
-            break;
-        }
-        previous_node = current_node;
-    }
-}
-#endif /* BLOCKS_IN_SLL */
-
-#ifdef BLOCKS_IN_DLL
-void remove_block(void **block, void **starting_node) {
-    void *previous_block, *next_block;
-
-    /* No need to traverse the list, just check if the block is the starting
-     * node of the list.
-     */
-
-    previous_block = get_previous(*block);
-    next_block = get_next(*block);
-
-    if(*block == *starting_node) {
-        *starting_node = previous_block;
-    }
-
-    if(previous_block != NULL) {
-        set_next(previous_block, next_block);
-    }
-
-    if(next_block != NULL) {
-        set_previous(next_block, previous_block);
-    }
-
-}
-#endif /* BLOCKS_IN_DLL */
-
-void remove_block_from_lists(void **block, heap_t *heap) {
-#ifdef WITH_FIXED_LISTS
-    int fixed_list_id, i;
-    maptable_node_t *current_maptable_node;
-    size_t size;
-
-    size = get_size(*block);
-
-    /* Check if it is a block of a fixed list */
-    fixed_list_id = map_size_to_list(heap, size);
-    if(fixed_list_id != -1) {
-        /* If it is, find the fixed list and remove the block */
-        current_maptable_node = heap->maptable_head;
-        if(fixed_list_id != 0) {
-            for(i = 1; i < fixed_list_id; i++) {
-                current_maptable_node = current_maptable_node->next;
-            }
-        }
-#ifdef COUNT_HOPS
-        remove_block(heap, block, &current_maptable_node->fixed_list_head);
-#else
-        remove_block(block, &current_maptable_node->fixed_list_head);
-#endif /* COUNT_HOPS */
-    } else {
-#endif /* WITH_FIXED_LISTS */
-#ifdef COUNT_HOPS
-        remove_block(heap, block, &heap->free_list_head);
-#else
-        remove_block(block, &heap->free_list_head);
-#endif /* COUNT_HOPS */
-#ifdef WITH_FIXED_LISTS
-    }
-#endif /* WITH_FIXED_LISTS */
-}
-

+ 2 - 4
src/other.c

@@ -60,9 +60,7 @@ int map_size_to_list(heap_t *heap, size_t sz) {
 
 #endif /* WITH_FIXED_LISTS */
 
-#ifndef NO_SYSTEM_CALLS
 /* Random assignment */
-int map_thread_heap(void) {
-    return (int) (((unsigned long) pthread_self() >> 10) % NUM_HEAPS);
+int map_thread_pool(void) {
+    return (int) (((unsigned long) pthread_self() >> 10) % NUM_POOLS);
 }
-#endif /* NO_SYSTEM_CALLS */

+ 39 - 0
src/raw_block.c

@@ -0,0 +1,39 @@
+/*
+ *   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.
+ *
+ */
+
+#include <pthread.h>
+#include <dmmlib/raw_block.h>
+#include "request_memory.h"
+
+raw_block_header_t *create_new_raw_block(size_t raw_block_size) {
+    void *ptr;
+
+    ptr = request_memory(raw_block_size);
+
+    if(ptr == NULL) {
+        return NULL;
+    }
+
+    ((raw_block_header_t *)ptr)->next_raw_block = NULL;
+    ((raw_block_header_t *)ptr)->size = raw_block_size;
+    ((raw_block_header_t *)ptr)->remaining_size = raw_block_size - sizeof(raw_block_header_t);
+    ((raw_block_header_t *)ptr)->border_ptr = NULL;
+    ((raw_block_header_t *)ptr)->free_list_head = NULL;
+    pthread_mutex_init(&((raw_block_header_t *)ptr)->mutex, NULL);
+
+    return (raw_block_header_t *)ptr;
+}

+ 49 - 0
src/request_memory_mmap_linux.c

@@ -0,0 +1,49 @@
+/*
+ *   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   request_memory_mmap_linux.c
+ * \author Ioannis Koutras
+ * \date   August, 2012
+ * \brief  Request additional memory space via mmap() in Linux.
+ */
+
+#include "request_memory.h"
+#include <sys/stat.h> /* for open() */
+#include <fcntl.h> /* for open() */
+
+#include <sys/mman.h>
+
+static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
+
+void *request_memory(size_t size) {	
+    int fd;
+    void *zone;
+
+    if(dev_zero_fd < 0) {
+        dev_zero_fd = open("/dev/zero", O_RDWR);
+    }
+    fd = dev_zero_fd;
+
+    zone = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+
+    if(zone == MAP_FAILED) {
+        return NULL;
+    } else {
+        return zone;
+    }
+}

+ 21 - 20
src/linked_lists/lifo_order.c

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2011 Institute of Communication and Computer Systems (ICCS) 
+ *   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.
@@ -15,26 +15,27 @@
  *
  */
 
-#include "block_header.h"
-#include "linked_lists/lifo_order.h"
+/**
+ * \file   request_memory_mmap_osx.c
+ * \author Ioannis Koutras
+ * \date   August, 2012
+ * \brief  Request additional memory space via mmap() in OS X.
+ */
+
+#include "request_memory.h"
+#include <sys/mman.h>
+
+#include <mach/vm_statistics.h> /* for VM_MAKE_TAG */
 
-void add_block_lifo_order(
-#ifdef COUNT_HOPS
-        heap_t *heap,
-#endif /* COUNT_HOPS */
-        void **block,
-        void **starting_node) {
-    if(*starting_node != NULL) {
-#ifdef BLOCKS_IN_DLL
-        set_previous(*starting_node, *block);
-#endif /* BLOCKS_IN_DLL */
-        set_next(*block, *starting_node);
+void *request_memory(size_t size) {	
+    void *zone;
+
+    zone = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+            VM_MAKE_TAG(VM_MEMORY_MALLOC), (off_t) 0);
+
+    if(zone == MAP_FAILED) {
+        return NULL;
     } else {
-        set_next(*block, NULL);
+        return zone;
     }
-#ifdef BLOCKS_IN_DLL
-    set_previous(*block, NULL);
-#endif /* BLOCKS_IN_DLL */
-    *starting_node = *block;
 }
-

+ 17 - 17
src/posix_lock.c

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2011 Institute of Communication and Computer Systems (ICCS) 
+ *   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.
@@ -15,24 +15,24 @@
  *
  */
 
-#include "posix_lock.h"
-#include <pthread.h>
-
-pthread_mutex_t sbrk_mutex = PTHREAD_MUTEX_INITIALIZER;
+/**
+ * \file   request_memory_sbrk.c
+ * \author Ioannis Koutras
+ * \date   August, 2012
+ * \brief  Request additional memory space via sbrk().
+ */
 
-void sbrk_lock(void) {
-    pthread_mutex_lock(&sbrk_mutex);
-}
+#include "request_memory.h"
+#include <unistd.h> /* for sbrk() */
 
-void sbrk_unlock(void) {
-    pthread_mutex_unlock(&sbrk_mutex);
-}
+void *request_memory(size_t size) {	
+    void *ptr;
 
-void posix_lock(heap_t *heap) {
-    pthread_mutex_lock(&heap->mutex);
-}
+    ptr = sbrk((int) size);
 
-void posix_unlock(heap_t *heap) {
-    pthread_mutex_unlock(&heap->mutex);
+    if(ptr == (void *) -1) {
+        return NULL;
+    } else {
+        return ptr;
+    }    
 }
-

+ 83 - 56
src/single_allocator.c

@@ -25,74 +25,101 @@
  */
 
 #include <dmmlib/single_allocator.h>
-#include <dmmlib/initialize_allocator.h>
+#include "freelist/freelist_malloc.h"
 #include "other.h"
+#include <dmmlib/block_header.h>
 #include <dmmlib/dmmlib.h>
 
-void * malloc(size_t size) {
-    allocator_t *allocator;
-    heap_t *heap;
-    int heap_id;
-
-    allocator = &systemallocator;
-    /* Space aware allocators have to be already initialized, no need to do
-     * that here. */
-#ifndef NO_SYSTEM_CALLS
-    if(allocator->initialized != true) {
-        initialize_allocator(allocator);
+#include <stdio.h>
+
+void * dmmlib_malloc(size_t size) {
+    raw_block_header_t *raw_block, *new_raw_block;
+    size_t allocation_size;
+    void *ptr;
+
+    raw_block = systemallocator.raw_block_head;
+    ptr = NULL;
+
+    /* Try to find a raw block available for allocation */
+
+    while(raw_block != NULL) {
+        ptr = freelist_malloc(raw_block, size);
+        if(ptr != NULL) {
+            break;
+        }
+        raw_block = raw_block->next_raw_block;
     }
-#endif /* NO_SYSTEM_CALLS */
 
-    /* FIXME Space aware allocators currently use one heap */
-#ifdef NO_SYSTEM_CALLS
-    heap_id = 0;
-#else /* NO_SYSTEM_CALLS */
-    heap_id = map_thread_heap();
-#endif /* NO_SYSTEM_CALLS */
-    heap = &allocator->heaps[heap_id];
+    if(ptr == NULL) {
+        allocation_size = size;
+
+        if(allocation_size < SYS_ALLOC_SIZE) {
+            allocation_size = SYS_ALLOC_SIZE;
+        } else {
+            /* If allocation size is more than the default size, then request
+             * multiples of it.
+             */
+            allocation_size = ((size + HEADER_SIZE) / SYS_ALLOC_SIZE + 1) *
+                SYS_ALLOC_SIZE;
+        }
 
-    return custom_ahmalloc(allocator, heap, size);
+        pthread_mutex_lock(&systemallocator.creation_mutex);
+        new_raw_block = create_new_raw_block(allocation_size);
+        if(new_raw_block != NULL) {
+            new_raw_block->next_raw_block = systemallocator.raw_block_head;
+            systemallocator.raw_block_head = new_raw_block;
+            pthread_mutex_unlock(&systemallocator.creation_mutex);
+            ptr = freelist_malloc(new_raw_block, size);
+        }
+    }
+    
+    return ptr;
 }
 
-void free(void *ptr) {
-    allocator_t *allocator;
-    heap_t *heap;
-    int heap_id;
+void dmmlib_free(void *ptr) {
+    raw_block_header_t *current_raw_block;
+    bool found;
 
-    allocator = &systemallocator;
+    found = false;
 
-    /* FIXME Space aware allocators currently use one heap */
-#ifdef NO_SYSTEM_CALLS
-    heap_id = 0;
-#else /* NO_SYSTEM_CALLS */
-    heap_id = map_thread_heap();
-#endif /* NO_SYSTEM_CALLS */
-    heap = &allocator->heaps[heap_id];
+    current_raw_block = systemallocator.raw_block_head;
+    while(current_raw_block) {
+        if(current_raw_block->border_ptr != NULL) {
+            if(((char *)current_raw_block < (char *)ptr) &&
+                    ((char *)ptr < (char *)(current_raw_block->border_ptr))) {
+                found = true;
+                break;
+            }
+        }
+        current_raw_block = current_raw_block->next_raw_block;
+    }
 
-    custom_ahfree(allocator, heap, ptr);
+    if(found == true) {
+        freelist_free(current_raw_block, ptr);
+    }
 }
 
-void * realloc(void *ptr, size_t size) {
-    allocator_t *allocator;
-    heap_t *heap;
-    int heap_id;
-
-    allocator = &systemallocator;
-    /* Space aware allocators have to be already initialized, no need to do
-     * that here. */
-#ifndef NO_SYSTEM_CALLS
-    if(allocator->initialized != true) {
-        initialize_allocator(allocator);
-    }
-#endif /* NO_SYSTEM_CALLS */
+/* void * realloc(void *ptr, size_t size) { */
+/*     allocator_t *allocator; */
+/*     heap_t *heap; */
+/*     int heap_id; */
 
-    /* FIXME Space aware allocators currently use one heap */
-#ifdef NO_SYSTEM_CALLS
-    heap_id = 0;
-#else /* NO_SYSTEM_CALLS */
-    heap_id = map_thread_heap();
-#endif /* NO_SYSTEM_CALLS */
-    heap = &allocator->heaps[heap_id];
+/*     allocator = &systemallocator; */
+/*     /1* Space aware allocators have to be already initialized, no need to do */
+/*      * that here. *1/ */
+/* #ifndef NO_SYSTEM_CALLS */
+/*     if(allocator->initialized != true) { */
+/*         initialize_allocator(allocator); */
+/*     } */
+/* #endif /1* NO_SYSTEM_CALLS *1/ */
 
-    return custom_ahrealloc(allocator, heap, ptr, size);
-}
+/*     /1* FIXME Space aware allocators currently use one heap *1/ */
+/* #ifdef NO_SYSTEM_CALLS */
+/*     heap_id = 0; */
+/* #else /1* NO_SYSTEM_CALLS *1/ */
+/*     heap_id = map_thread_heap(); */
+/* #endif /1* NO_SYSTEM_CALLS *1/ */
+/*     heap = &allocator->heaps[heap_id]; */
+
+/*     return custom_ahrealloc(allocator, heap, ptr, size); */
+/* } */

+ 0 - 161
src/split.c

@@ -1,161 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include "block_header.h"
-#include "other.h"
-#include "linked_lists/add_block.h"
-#include "split.h"
-
-void split(allocator_t *allocator, heap_t *heap, void *ptr,
-        size_t req_size) {
-    size_t new_size;
-    size_t min_split_size;
-    void *new_block;
-    size_t new_block_size;
-#ifdef WITH_FIXED_LISTS
-    int fixed_list_id, i;
-    maptable_node_t *current_maptable_node;
-#endif /* WITH_FIXED_LISTS */
-#ifdef COALESCE_AFTER_SPLIT
-    size_t max_coal_size;
-    void *next_block;
-    size_t current_next_size;
-
-    next_block = get_dlnext(allocator, ptr);
-#endif /* COALESCE_AFTER_SPLIT */
-
-    /* Check what would be the size of the new block if we split the current
-     * one.
-     * Note: new_size is a size_t, so compare first in order to prevent an
-     * underflow.
-     */
-    if(get_size(ptr) > req_size + HEADER_SIZE) {
-        new_size = get_size(ptr) - req_size - HEADER_SIZE;
-    } else {
-        new_size = 0;
-    }
-
-#ifdef SPLITTING_FIXED
-    min_split_size = MIN_SPLITTING_SIZE;
-#endif /* SPLITTING_FIXED */
-#ifdef SPLITTING_VARIABLE
-    min_split_size = heap->dmm_knobs.min_split_size;
-#endif /* SPLITTING_VARIABLE */
-
-    if(new_size < min_split_size) {
-        return;
-    }
-
-    new_block = (void *)((char *)ptr + req_size + HEADER_SIZE);
-    new_block_size = get_size(ptr) - req_size - HEADER_SIZE;
-
-    if(allocator->border_ptr == ptr) {
-        allocator->border_ptr = new_block;
-    }
-
-    /* Resize the previous, to be used block */
-    set_size_and_used(allocator, ptr, req_size);
-
-#ifdef COALESCE_AFTER_SPLIT
-    /* Try to coalesce with the next block if it is free */
-#ifdef COALESCING_FIXED
-    max_coal_size = MAX_COALESCE_SIZE;
-#endif /* COALESCING_FIXED */
-#ifdef COALESCING_VARIABLE
-    max_coal_size = heap->dmm_knobs.max_coalesce_size;
-#endif /* COALESCING_VARIABLE */
-
-    if(next_block != NULL) {
-        if(is_free(next_block) == true) {
-#ifdef WITH_OWNERSHIP
-            if(get_owner(next_block) == get_owner(ptr)) {
-#endif /* WITH_OWNERSHIP */
-                current_next_size = new_block_size + get_size(next_block) +
-                    HEADER_SIZE;
-                if(current_next_size <= max_coal_size) {
-                    /* it's ok to coalesce the new split block with the next
-                     * free block
-                     */
-                    remove_block_from_lists(&next_block, heap);
-                    if(allocator->border_ptr == next_block) {
-                        allocator->border_ptr = ptr;
-                    }
-                    new_block_size = current_next_size;
-                }
-#ifdef WITH_OWNERSHIP
-            }
-#endif /* WITH_OWNERSHIP */
-        }
-    }
-
-#endif /* COALESCE_AFTER_SPLIT */
-
-    set_size_and_free(allocator, new_block, new_block_size);
-
-#ifdef WITH_FIXED_LISTS
-    /* FIXME code from custom_free, some refactoring maybe?
-     * Be careful, custom_free also does coalescing, we don't need that
-     */
-
-    /* Check if the block could be put in a fixed list */
-    fixed_list_id = map_size_to_list(heap, new_block_size);
-
-    if(fixed_list_id != -1) { /* put it in the right fixed list */
-        current_maptable_node = heap->maptable_head;		
-        if(fixed_list_id != 0) {
-            for(i = 1; i < fixed_list_id; i++) {
-                current_maptable_node = current_maptable_node->next;
-            }
-        }
-#ifdef COUNT_HOPS
-#ifdef FIFO_SORT_POLICY
-        add_block(heap, &new_block, &current_maptable_node->fixed_list_head, &current_maptable_node->fixed_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(heap, &new_block, &current_maptable_node->fixed_list_head);
-#endif /* FIFO_SORT_POLICY */
-#else /* COUNT_HOPS */
-#ifdef FIFO_SORT_POLICY
-        add_block(&new_block, &current_maptable_node->fixed_list_head, &current_maptable_node->fixed_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(&new_block, &current_maptable_node->fixed_list_head);
-#endif /* FIFO_SORT_POLICY */
-#endif /* COUNT_HOPS */
-    } else { /* put it in the free list */
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef COUNT_HOPS
-#ifdef FIFO_SORT_POLICY
-        add_block(heap, &new_block, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(heap, &new_block, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#else /* COUNT_HOPS */
-#ifdef FIFO_SORT_POLICY
-        add_block(&new_block, &heap->free_list_head, &heap->free_list_tail);
-#else /* FIFO_SORT_POLICY */
-        add_block(&new_block, &heap->free_list_head);
-#endif /* FIFO_SORT_POLICY */
-#endif /* COUNT_HOPS */
-#ifdef WITH_FIXED_LISTS
-    }
-#endif /* WITH_FIXED_LISTS */
-
-#ifdef WITH_OWNERSHIP
-    set_owner(new_block, heap);
-#endif /* WITH_OWNERSHIP */
-
-}

+ 0 - 167
src/sys_alloc.c

@@ -1,167 +0,0 @@
-/*
- *   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.
- *
- */
-
-#include "sys_alloc.h"
-
-#ifdef WITH_MMAP
-#include <sys/stat.h> /* for open() */
-#include <fcntl.h> /* for open() */
-
-#include <sys/mman.h>
-#else /* WITH_MMAP */
-#include <unistd.h> /* for sbrk() */
-#endif /* WITH_MMAP */
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#ifdef HAVE_LOCKS
-#include "posix_lock.h"
-#endif /* HAVE_LOCKS */
-#include "other.h"
-#include "block_header.h"
-#ifdef WITH_STATS
-#include <dmmlib/print_stats.h>
-#endif /* WITH_STATS */
-
-#ifdef WITH_MMAP
-static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
-#endif /* WITH_MMAP */
-
-void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
-
-    size_t allocation_size, previous_size, previous_size_availability;
-    void *ptr;
-#ifdef WITH_MMAP
-    int fd;
-#endif /* WITH_MMAP */
-
-#ifdef HAVE_LOCKS
-    sbrk_lock();
-#endif /* HAVE_LOCKS */
-
-    allocation_size = req_padding(size) + HEADER_SIZE;
-
-    /* Step 1: Check if the memory allocator is initialized.
-     * 
-     * Before the first application malloc() call, the allocator uses
-     * sys_alloc() to allocate space for its metadata. So, these are the first
-     * data blocks in the heaps and there are no previous data blocks.
-     */
-    if(allocator->initialized == false) {
-        previous_size = 0;
-        previous_size_availability = 1; /* Occupied and of 0 size */
-        allocator->initialized = true;
-    } else {
-        previous_size = get_size(allocator->border_ptr);
-        previous_size_availability = get_size_availability(allocator->border_ptr);
-    }
-
-    if(allocation_size > allocator->remaining_size) {
-#ifndef NO_SYSTEM_CALLS
-        if(allocation_size < SYS_ALLOC_SIZE) {
-            allocation_size = SYS_ALLOC_SIZE;
-        } else {
-            /* If allocation size is more than the default size, then request
-             * multiples of it.
-             */
-            allocation_size = ((size + HEADER_SIZE) / SYS_ALLOC_SIZE + 1) *
-                SYS_ALLOC_SIZE;
-        }
-#ifdef WITH_MMAP
-        if(dev_zero_fd < 0) {
-            dev_zero_fd = open("/dev/zero", O_RDWR);
-        }
-        fd = dev_zero_fd;
-        ptr = mmap(0, allocation_size, PROT_READ|PROT_WRITE,
-                MAP_SHARED, fd, 0);
-#endif /* WITH_MMAP */
-#ifdef WITH_SBRK
-        ptr = sbrk((int) allocation_size);
-#endif /* WITH_SBRK */
-
-        /* Print a warning for sbrk / mmap in case the user has enabled
-         * coalescing support
-         */
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-        if(allocator->border_ptr != NULL && ptr != (void *) 
-                ((char *) allocator->border_ptr + previous_size)) {
-            printf("sbrk() / mmap() does not return sequential space. You should"
-                    " disable coalescing.\n");
-        }
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */    
-
-        if(ptr == (void *)-1) {
-#endif /* NO_SYSTEM_CALLS */
-            printf("Couldn't allocate more space from the system.\n");
-#ifdef WITH_STATS
-            print_stats(allocator);
-#endif /* WITH_STATS */
-            return NULL;
-#ifndef NO_SYSTEM_CALLS
-        } else {
-            allocator->remaining_size += allocation_size;
-        }
-#endif /* NO_SYSTEM_CALLS */
-    } else {
-        ptr = (void *)((char *)allocator->border_ptr + previous_size);
-        allocator->remaining_size -= allocation_size;
-    }
-
-    /* Go to the data part of the block */
-    ptr = (void *) ((char *) ptr + HEADER_SIZE);
-    
-    /* Set the new border pointer */
-    allocator->border_ptr = ptr;
-
-    /* Set some values for block header */
-    set_size_and_used(allocator, ptr, req_padding(size));
-    set_previous_size_availability(ptr, previous_size_availability);
-
-#ifdef REQUEST_SIZE_INFO
-    set_requested_size(ptr, size);
-#endif /* REQUEST_SIZE_INFO */
-
-#ifdef FUTURE_FEATURES
-    /* Update the used blocks list */
-    push_block(&ptr, &heap->used_blocks_head);
-#endif /* FUTURE_FEATURES */
-
-#ifdef WITH_STATS
-    /* Iraklis' request: Each sys_alloc() call is equivalent to 2 hops. */
-#ifdef COUNT_HOPS
-    heap->dmm_stats.total_hops += 2;
-#endif /* COUNT_HOPS */   
-    /* Update statistics */
-#ifdef REQUEST_SIZE_INFO
-    heap->dmm_stats.mem_requested += size;
-#endif /* REQUEST_SIZE_INFO */
-    heap->dmm_stats.mem_used += HEADER_SIZE + req_padding(size);
-    heap->dmm_stats.mem_allocated += HEADER_SIZE + req_padding(size);
-    heap->dmm_stats.live_objects += 1;
-    heap->dmm_stats.num_malloc += 1;
-#endif /* WITH_STATS */
-
-    /* FIXME To be refactored - END */
-
-#ifdef HAVE_LOCKS
-    sbrk_unlock();
-#endif /* HAVE_LOCKS */
-
-    return  ptr;
-}
-