Переглянути джерело

refactor locking mechanisms; more char * to uintptr_t conversions

Ioannis Koutras 12 роки тому
батько
коміт
8356eff3b0

+ 1 - 0
include/dmmlib/raw_block.h

@@ -31,6 +31,7 @@
 #ifdef WITH_RAWBLOCK_STATS
 #include <dmmlib/dmmstats.h>
 #endif /* WITH_RAWBLOCK_STATS */
+
 #ifdef HAVE_LOCKS
 #include <pthread.h> /* FIXME To be removed once mutex is removed. */
 #endif /* HAVE_LOCKS */

+ 60 - 0
private-include/locks.h

@@ -0,0 +1,60 @@
+/*
+ *   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   locks.h
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ * @brief  Locking functions
+ */
+
+#include "dmm_config.h"
+
+#ifndef LOCKS_H
+#define LOCKS_H
+
+#ifdef HAVE_LOCKS
+
+#include <pthread.h>
+
+/** Locks the systemallocator object */
+#define lock_global() pthread_mutex_lock(&systemallocator.creation_mutex)
+/** Unlocks the systemallocator object */
+#define unlock_global() pthread_mutex_unlock(&systemallocator.creation_mutex)
+/** Locks a specific raw block */
+#define lock_raw_block(rb) pthread_mutex_lock(&rb->mutex)
+/** Initialized a raw block lock */
+#define init_raw_block_lock(rb) pthread_mutex_init(&rb->mutex, NULL);
+/** Unlocks a specific raw block */
+#define unlock_raw_block(rb) pthread_mutex_unlock(&rb->mutex)
+
+#else /* HAVE_LOCKS */
+
+/** Does nothing. */
+#define lock_global()
+/** Does nothing. */
+#define unlock_global()
+/** Does nothing. */
+#define lock_raw_block(rb)
+/** Does nothing. */
+#define init_raw_block_lock(rb)
+/** Does nothing. */
+#define unlock_raw_block(rb)
+
+#endif /* HAVE_LOCKS */
+
+#endif /* LOCKS_H */

+ 5 - 8
src/bitmap/bitmap_realloc.c

@@ -26,9 +26,7 @@
 #include "dmmlib/dmmlib.h"
 #include "memmove.h"
 
-#ifdef HAVE_LOCKS
-#include <pthread.h>
-#endif /* HAVE_LOCKS */
+#include "locks.h"
 
 /**
  * Reallocates a memory block from a bitmap-organized raw block
@@ -58,13 +56,12 @@ void * bitmap_realloc(bitmap_rb_t *raw_block, void * ptr,
 
 #ifdef HAVE_LOCKS
     raw_block_header_t *rb;
-    rb = (raw_block_header_t *)((char *)raw_block - sizeof(raw_block_header_t));
-    pthread_mutex_lock(&rb->mutex);
+    rb = (raw_block_header_t *)
+        ((uintptr_t) raw_block - sizeof(raw_block_header_t));
 #endif /* HAVE_LOCKS */
+    lock_raw_block(rb);
     bitmap_free(raw_block, ptr);
-#ifdef HAVE_LOCKS
-    pthread_mutex_unlock(&rb->mutex);
-#endif /* HAVE_LOCKS */
+    unlock_raw_block(rb);
 
     return ret;
 }

+ 6 - 12
src/free.c

@@ -28,6 +28,8 @@
 #include <inttypes.h>
 #include <stdbool.h>
 
+#include "locks.h"
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
@@ -87,13 +89,9 @@ void free(void *ptr) {
 #endif /* FL_RB_ONLY */
                 ((uintptr_t) current_raw_block + sizeof(raw_block_header_t));
 
-#ifdef HAVE_LOCKS
-        pthread_mutex_lock(&current_raw_block->mutex);
-#endif /* HAVE_LOCKS */
+        lock_raw_block(current_raw_block);
         dmmlib_free(encapsulated_rb, ptr);
-#ifdef HAVE_LOCKS
-        pthread_mutex_unlock(&current_raw_block->mutex);
-#endif /* HAVE_LOCKS */
+        unlock_raw_block(current_raw_block);
 
     } else { // It has to be a BIGBLOCK, just munmap it
 
@@ -112,14 +110,10 @@ void free(void *ptr) {
                 ) {
             if(block_in_list == current_raw_block) {
                 if(systemallocator.big_blocks_head == current_raw_block) {
-#ifdef HAVE_LOCKS
-                pthread_mutex_lock(&systemallocator.creation_mutex);
-#endif /* HAVE_LOCKS */
+                    lock_global();
                     systemallocator.big_blocks_head =
                         current_raw_block->next_raw_block;
-#ifdef HAVE_LOCKS
-                pthread_mutex_unlock(&systemallocator.creation_mutex);
-#endif /* HAVE_LOCKS */
+                    unlock_global();
                 } else {
                     previous_raw_block->next_raw_block =
                         current_raw_block->next_raw_block;

+ 8 - 8
src/freelist/freelist_realloc.c

@@ -28,9 +28,10 @@
 #include <string.h>
 
 #ifdef HAVE_LOCKS
-#include <pthread.h>
-#include "dmmlib/dmmlib.h"
+#include "dmmlib/raw_block.h"
+#include <inttypes.h>
 #endif /* HAVE_LOCKS */
+#include "locks.h"
 
 /**
  * Reallocates a memory block from a freelist-organized raw block
@@ -41,7 +42,7 @@
  * @retval           The address to serve the request.
  * @retval NULL      No available memory space.
  */
-void * freelist_realloc(freelist_rb_t *raw_block, void * ptr,
+void * freelist_realloc(freelist_rb_t *raw_block, void *ptr,
         size_t req_size) { 
     block_header_t *block;
     void *ret;
@@ -58,12 +59,11 @@ void * freelist_realloc(freelist_rb_t *raw_block, void * ptr,
 
 #ifdef HAVE_LOCKS
     raw_block_header_t *rb;
-    rb = (raw_block_header_t *)((char *)raw_block - sizeof(raw_block_header_t));
-    pthread_mutex_lock(&rb->mutex);
+    rb = (raw_block_header_t *)
+        ((uintptr_t) raw_block - sizeof(raw_block_header_t));
 #endif /* HAVE_LOCKS */
+    lock_raw_block(rb);
     freelist_free(raw_block, ptr);
-#ifdef HAVE_LOCKS
-    pthread_mutex_unlock(&rb->mutex);
-#endif /* HAVE_LOCKS */
+    unlock_raw_block(rb);
     return ret;
 }

+ 11 - 22
src/malloc.c

@@ -27,6 +27,8 @@
 
 #include <inttypes.h>
 
+#include "locks.h"
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
@@ -65,9 +67,7 @@ void * malloc(size_t size) {
     /* Try to find a raw block available for allocation */
 
     while(raw_block != NULL) {
-#ifdef HAVE_LOCKS
-        pthread_mutex_lock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
+        lock_raw_block(raw_block);
         encapsulated_rb =
 #ifdef BITMAP_RB_ONLY
             (bitmap_rb_t *)
@@ -77,9 +77,8 @@ void * malloc(size_t size) {
 #endif /* FL_RB_ONLY */
             ((uintptr_t) raw_block + sizeof(raw_block_header_t));
         ptr = dmmlib_malloc(encapsulated_rb, size);
-#ifdef HAVE_LOCKS
-        pthread_mutex_unlock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
+        unlock_raw_block(raw_block);
+
         if(ptr != NULL) {
             break;
         }
@@ -114,15 +113,11 @@ void * malloc(size_t size) {
                 /* When debugging is enabled, the big blocks are also stored on
                  * a list. */
 #ifdef WITH_DEBUG
-#ifdef HAVE_LOCKS
-                pthread_mutex_lock(&systemallocator.creation_mutex);
-#endif /* HAVE_LOCKS */
+                lock_global();
                 ((raw_block_header_t *)ptr)->next_raw_block = 
                     systemallocator.big_blocks_head;
                 systemallocator.big_blocks_head = (raw_block_header_t *)ptr;
-#ifdef HAVE_LOCKS
-                pthread_mutex_unlock(&systemallocator.creation_mutex);
-#endif /* HAVE_LOCKS */
+                unlock_global();
 #endif /* WITH_DEBUG */
 
 #ifdef WITH_ALLOCATOR_STATS
@@ -139,9 +134,7 @@ void * malloc(size_t size) {
 
         } else {
 
-#ifdef HAVE_LOCKS
-            pthread_mutex_lock(&systemallocator.creation_mutex);
-#endif /* HAVE_LOCKS */
+            lock_global();
             new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE,
 #ifdef FL_RB_ONLY
                     FREELIST
@@ -153,11 +146,9 @@ void * malloc(size_t size) {
             if(new_raw_block != NULL) {
                 new_raw_block->next_raw_block = systemallocator.raw_block_head;
                 systemallocator.raw_block_head = new_raw_block;
-#ifdef HAVE_LOCKS
-                pthread_mutex_unlock(&systemallocator.creation_mutex);
-                pthread_mutex_lock(&new_raw_block->mutex);
-#endif /* HAVE_LOCKS */
+                unlock_global();
 
+                lock_raw_block(new_raw_block);
                 encapsulated_rb =
 #ifdef BITMAP_RB_ONLY
                     (bitmap_rb_t *)
@@ -167,9 +158,7 @@ void * malloc(size_t size) {
 #endif /* FL_RB_ONLY */
                     ((uintptr_t) new_raw_block + sizeof(raw_block_header_t));
                 ptr = dmmlib_malloc(encapsulated_rb, size);
-#ifdef HAVE_LOCKS
-                pthread_mutex_unlock(&new_raw_block->mutex);
-#endif /* HAVE_LOCKS */
+                unlock_raw_block(new_raw_block);
             }
         }
     }

+ 11 - 12
src/raw_block.c

@@ -17,9 +17,8 @@
 
 #include "dmmlib/raw_block.h"
 
-#ifdef HAVE_LOCKS
-#include <pthread.h>
-#endif /* HAVE_LOCKS */
+#include <inttypes.h>
+
 #include "request_memory.h"
 #ifdef FL_RB_ONLY
 #include "dmmlib/freelist/freelist_rb.h"
@@ -29,8 +28,10 @@
 #include "bitmap/bitmap_rb.h"
 #endif /* BITMAP_RB_ONLY */
 
+#include "locks.h"
+
 raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
-    void *ptr;
+    raw_block_header_t *ptr;
 #ifdef BITMAP_RB_ONLY
     bitmap_rb_t *bitmap_rb;
     BMAP_EL_TYPE *bitmap_p;
@@ -38,14 +39,14 @@ raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
     size_t remaining_cells;
 #endif /* BITMAP_RB_ONLY */
 
-    ptr = request_memory(raw_block_size);
+    ptr = (raw_block_header_t *)request_memory(raw_block_size);
 
     if(ptr == NULL) {
         return NULL;
     }
 
-    ((raw_block_header_t *)ptr)->size = raw_block_size;
-    ((raw_block_header_t *)ptr)->next_raw_block = NULL;
+    ptr->size = raw_block_size;
+    ptr->next_raw_block = NULL;
 
     switch(type) {
 
@@ -88,7 +89,6 @@ raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
                 bitmap_p++;
             }
 
-
             /* If there are some remaining cells, so an extra bitmap vector
              * element is used, the cells that cannot be used by the
              * application, have to be set as already used.
@@ -107,13 +107,12 @@ raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
 
             break;
 #endif /* BITMAP_RB_ONLY */
+
         case BIGBLOCK:
             break;
     }
 
-#ifdef HAVE_LOCKS
-    pthread_mutex_init(&((raw_block_header_t *)ptr)->mutex, NULL);
-#endif /* HAVE LOCKS */
+    init_raw_block_lock(ptr);
 
-    return (raw_block_header_t *)ptr;
+    return ptr;
 }