Browse Source

Split dmmlib.c to four files

Ioannis Koutras 12 years ago
parent
commit
e24063eb29
5 changed files with 226 additions and 130 deletions
  1. 4 1
      src/CMakeLists.txt
  2. 46 0
      src/calloc.c
  3. 96 0
      src/free.c
  4. 2 129
      src/dmmlib.c
  5. 78 0
      src/realloc.c

+ 4 - 1
src/CMakeLists.txt

@@ -29,7 +29,10 @@ if (WITH_STATIC_LIB)
 endif (WITH_STATIC_LIB)
 
 set(dmmlib_SRCS
-  dmmlib.c
+  malloc.c
+  free.c
+  realloc.c
+  calloc.c
   raw_block.c
   release_memory.c
 )

+ 46 - 0
src/calloc.c

@@ -0,0 +1,46 @@
+/*
+ *   Copyright 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   calloc.c
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ *
+ * @brief  Implementations of calloc() call.
+ */
+
+#include "dmmlib/dmmlib.h"
+
+#include <string.h> /* for memset() */
+
+void * calloc(size_t nmemb, size_t size) {
+    void *result;
+
+    if(nmemb == 0 || size == 0) {
+        return NULL;
+    }
+
+    size_t total_size = nmemb * size;
+
+    result = malloc(total_size);
+
+    if(result != NULL) {
+        memset(result, 0, total_size);
+    }
+
+    return result;
+}

+ 96 - 0
src/free.c

@@ -0,0 +1,96 @@
+/*
+ *   Copyright 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   free.c
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ *
+ * @brief  Implementation free() call.
+ */
+
+#include "dmmlib/dmmlib.h"
+
+#include <stdbool.h>
+#ifdef BITMAP_RB_ONLY
+#include "bitmap/bitmap.h"
+#include "bitmap/bitmap_rb.h"
+#endif /* BITMAP_RB_ONLY */
+#include "release_memory.h"
+
+#include "trace.h"
+
+void free(void *ptr) {
+    raw_block_header_t *current_raw_block;
+    bool found;
+
+    if(ptr == NULL) {
+        return;
+    }
+
+    found = false;
+
+    current_raw_block = systemallocator.raw_block_head;
+    while(current_raw_block) {
+        if((char *)ptr - (char *)(current_raw_block) -
+                sizeof(raw_block_header_t) < current_raw_block->size) {
+            found = true;
+            break;
+        }
+        current_raw_block = current_raw_block->next_raw_block;
+    }
+
+    if(found == true) {
+#ifdef BITMAP_RB_ONLY
+        bitmap_rb_t *bitmap_rb;
+        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
+                sizeof(raw_block_header_t));
+#ifdef HAVE_LOCKS
+        pthread_mutex_lock(&current_raw_block->mutex);
+#endif /* HAVE_LOCKS */
+        dmmlib_free(bitmap_rb, ptr);
+#ifdef HAVE_LOCKS
+        pthread_mutex_unlock(&current_raw_block->mutex);
+#endif /* HAVE_LOCKS */
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        dmmlib_free(current_raw_block, ptr);
+#endif /* FL_RB_ONLY */
+    } else { // It has to be a BIGBLOCK, just munmap it
+        current_raw_block = (raw_block_header_t *)((char *)ptr -
+                sizeof(raw_block_header_t));
+        TRACE_1("dmmlib - free - free'ing %zu bytes from raw block %p\n",
+                current_raw_block->size, (void *)current_raw_block);
+
+#ifdef WITH_ALLOCATOR_STATS
+        systemallocator.dmm_stats.total_mem_allocated -=
+            current_raw_block->size;
+        TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+                systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+        systemallocator.dmm_stats.total_mem_requested -=
+            current_raw_block->size - sizeof(raw_block_header_t);
+        TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+                systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+        systemallocator.dmm_stats.live_objects--;
+        systemallocator.dmm_stats.num_free++;
+#endif /* WITH_ALLOCATOR_STATS */
+
+        release_memory(current_raw_block);
+    }
+}

+ 2 - 129
src/dmmlib.c

@@ -16,11 +16,11 @@
  */
 
 /**
- * @file   dmmlib.c
+ * @file   malloc.c
  * @author Ioannis Koutras (joko@microlab.ntua.gr)
  * @date   September 2012
  *
- * @brief  Implementations of malloc(), free() and calloc() calls.
+ * @brief  Implementation of malloc() call.
  */
 
 #include "dmmlib/dmmlib.h"
@@ -32,9 +32,6 @@
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
 #endif /* BITMAP_RB_ONLY */
-#include "release_memory.h"
-#include <stdbool.h>
-#include <string.h> /* for memset() */
 
 #include "trace.h"
 
@@ -148,127 +145,3 @@ void * malloc(size_t size) {
     
     return ptr;
 }
-
-void free(void *ptr) {
-    raw_block_header_t *current_raw_block;
-    bool found;
-
-    if(ptr == NULL) {
-        return;
-    }
-
-    found = false;
-
-    current_raw_block = systemallocator.raw_block_head;
-    while(current_raw_block) {
-        if((char *)ptr - (char *)(current_raw_block) -
-                sizeof(raw_block_header_t) < current_raw_block->size) {
-            found = true;
-            break;
-        }
-        current_raw_block = current_raw_block->next_raw_block;
-    }
-
-    if(found == true) {
-#ifdef BITMAP_RB_ONLY
-        bitmap_rb_t *bitmap_rb;
-        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
-                sizeof(raw_block_header_t));
-#ifdef HAVE_LOCKS
-        pthread_mutex_lock(&current_raw_block->mutex);
-#endif /* HAVE_LOCKS */
-        dmmlib_free(bitmap_rb, ptr);
-#ifdef HAVE_LOCKS
-        pthread_mutex_unlock(&current_raw_block->mutex);
-#endif /* HAVE_LOCKS */
-#endif /* BITMAP_RB_ONLY */
-#ifdef FL_RB_ONLY
-        dmmlib_free(current_raw_block, ptr);
-#endif /* FL_RB_ONLY */
-    } else { // It has to be a BIGBLOCK, just munmap it
-        current_raw_block = (raw_block_header_t *)((char *)ptr -
-                sizeof(raw_block_header_t));
-        TRACE_1("dmmlib - free - free'ing %zu bytes from raw block %p\n",
-                current_raw_block->size, (void *)current_raw_block);
-
-#ifdef WITH_ALLOCATOR_STATS
-        systemallocator.dmm_stats.total_mem_allocated -=
-            current_raw_block->size;
-        TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
-                systemallocator.dmm_stats.total_mem_allocated);
-#ifdef REQUEST_SIZE_INFO
-        systemallocator.dmm_stats.total_mem_requested -=
-            current_raw_block->size - sizeof(raw_block_header_t);
-        TRACE_1("dmmlib - global requested memory: %zu bytes\n",
-                systemallocator.dmm_stats.total_mem_requested);
-#endif /* REQUEST_SIZE_INFO */
-        systemallocator.dmm_stats.live_objects--;
-        systemallocator.dmm_stats.num_free++;
-#endif /* WITH_ALLOCATOR_STATS */
-
-        release_memory(current_raw_block);
-    }
-}
-
-void * realloc(void *ptr, size_t size) {
-    raw_block_header_t *current_raw_block;
-    bool found;
-
-    if(ptr == NULL) {
-        return malloc(size);
-    }
-
-    if(size == 0) {
-        free(ptr);
-#ifdef BITMAP_RB_ONLY
-        return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
-#endif /* BITMAP_RB_ONLY */
-#ifdef FL_RB_ONLY
-        return malloc((size_t) 32); // FIXME 32 <- minimum size
-#endif /* FL_RB_ONLY */
-    }
-
-    found = false;
-
-    current_raw_block = systemallocator.raw_block_head;
-    while(current_raw_block) {
-        if((char *)ptr - (char *)(current_raw_block) -
-                sizeof(raw_block_header_t) < current_raw_block->size) {
-            found = true;
-            break;
-        }
-        current_raw_block = current_raw_block->next_raw_block;
-    }
-
-    if(found == true) {
-#ifdef BITMAP_RB_ONLY
-        bitmap_rb_t *bitmap_rb;
-        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
-                sizeof(raw_block_header_t));
-        return bitmap_realloc(bitmap_rb, ptr, size);
-#endif /* BITMAP_RB_ONLY */
-#ifdef FL_RB_ONLY
-        return freelist_realloc(current_raw_block, ptr, size);
-#endif /* FL_RB_ONLY */
-    } else {
-        return NULL;
-    }
-}
-
-void * calloc(size_t nmemb, size_t size) {
-    void *result;
-
-    if(nmemb == 0 || size == 0) {
-        return NULL;
-    }
-
-    size_t total_size = nmemb * size;
-
-    result = malloc(total_size);
-
-    if(result != NULL) {
-        memset(result, 0, total_size);
-    }
-
-    return result;
-}

+ 78 - 0
src/realloc.c

@@ -0,0 +1,78 @@
+/*
+ *   Copyright 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   realloc.c
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ *
+ * @brief  Implementation of realloc() call.
+ */
+
+#include "dmmlib/dmmlib.h"
+
+#include <stdbool.h>
+#ifdef BITMAP_RB_ONLY
+#include "bitmap/bitmap.h"
+#include "bitmap/bitmap_rb.h"
+#endif /* BITMAP_RB_ONLY */
+#include "release_memory.h"
+
+void * realloc(void *ptr, size_t size) {
+    raw_block_header_t *current_raw_block;
+    bool found;
+
+    if(ptr == NULL) {
+        return malloc(size);
+    }
+
+    if(size == 0) {
+        free(ptr);
+#ifdef BITMAP_RB_ONLY
+        return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        return malloc((size_t) 32); // FIXME 32 <- minimum size
+#endif /* FL_RB_ONLY */
+    }
+
+    found = false;
+
+    current_raw_block = systemallocator.raw_block_head;
+    while(current_raw_block) {
+        if((char *)ptr - (char *)(current_raw_block) -
+                sizeof(raw_block_header_t) < current_raw_block->size) {
+            found = true;
+            break;
+        }
+        current_raw_block = current_raw_block->next_raw_block;
+    }
+
+    if(found == true) {
+#ifdef BITMAP_RB_ONLY
+        bitmap_rb_t *bitmap_rb;
+        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
+                sizeof(raw_block_header_t));
+        return bitmap_realloc(bitmap_rb, ptr, size);
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        return freelist_realloc(current_raw_block, ptr, size);
+#endif /* FL_RB_ONLY */
+    } else {
+        return NULL;
+    }
+}