Explorar o código

Use macros for singly-linked lists as defined in /usr/include/sys/queue.h

Ioannis Koutras %!s(int64=12) %!d(string=hai) anos
pai
achega
279df2491f

+ 6 - 4
include/dmmlib/allocator.h

@@ -37,13 +37,15 @@
 
 /** The allocator structure of dmmlib. */
 typedef struct allocator_s {
-    raw_block_header_t *raw_block_head; /**< The head of the raw blocks list. */
+    /** The head of the raw blocks list. */
+    struct rb_head_s rb_head;
 #ifdef WITH_DEBUG
-    raw_block_header_t *big_blocks_head; /**< The head of the big blocks list. */
+/** The head of the big blocks list. */
+    struct rb_head_s bb_head; 
 #endif /* WITH_DEBUG */
 #ifdef HAVE_LOCKS
-    pthread_mutex_t creation_mutex; /**< Mutex to allow the creation of new raw
-                                      blocks. */
+/** Mutex to allow the creation of new raw blocks. */
+    pthread_mutex_t creation_mutex;
 #endif /* HAVE_LOCKS */
 #ifdef WITH_ALLOCATOR_STATS
     dmmstats_t dmm_stats; /**< Global statistics. */

+ 108 - 0
include/dmmlib/lists.h

@@ -0,0 +1,108 @@
+/*
+ *   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   lists.h
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   October 2012
+ * @brief  Macros for singly-linked lists
+ *
+ * These macros were copied from /usr/include/sys/queue.h
+ *
+ */
+
+#ifndef _DMMLIB_LISTS_H_
+#define _DMMLIB_LISTS_H_
+
+/*
+ * Singly-linked List definitions.
+ */
+
+/** The head of a singly-linked list */
+#define	SLIST_HEAD(name, type)						\
+struct name {								\
+	struct type *slh_first;	/* first element */			\
+}
+
+/** Initial value of the head of a singly-linked list */
+#define	SLIST_HEAD_INITIALIZER(head)					\
+	{ NULL }
+
+/** Entry of a singly linked list */
+#define	SLIST_ENTRY(type)						\
+struct {								\
+	struct type *sle_next;	/* next element */			\
+}
+
+/*
+ * Singly-linked List functions.
+ */
+
+/** Initializes the head of a singly-linked list */
+#define	SLIST_INIT(head) do {						\
+	(head)->slh_first = NULL;					\
+} while (/*CONSTCOND*/0)
+
+/** Inserts an element after a specific one in a singly-linked list */
+#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
+	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
+	(slistelm)->field.sle_next = (elm);				\
+} while (/*CONSTCOND*/0)
+
+/** Inserts an element as the head of a singly-linked list */
+#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
+	(elm)->field.sle_next = (head)->slh_first;			\
+	(head)->slh_first = (elm);					\
+} while (/*CONSTCOND*/0)
+
+/** Removes the head element of a singly-linked list */
+#define	SLIST_REMOVE_HEAD(head, field) do {				\
+	(head)->slh_first = (head)->slh_first->field.sle_next;		\
+} while (/*CONSTCOND*/0)
+
+/** Removes an element from a singly-linked list */
+#define	SLIST_REMOVE(head, elm, type, field) do {			\
+	if ((head)->slh_first == (elm)) {				\
+		SLIST_REMOVE_HEAD((head), field);			\
+	}								\
+	else {								\
+		struct type *curelm = (head)->slh_first;		\
+		while(curelm->field.sle_next != (elm))			\
+			curelm = curelm->field.sle_next;		\
+		curelm->field.sle_next =				\
+		    curelm->field.sle_next->field.sle_next;		\
+	}								\
+} while (/*CONSTCOND*/0)
+
+/** Iterates through a singly-linked list */
+#define	SLIST_FOREACH(var, head, field)					\
+	for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
+
+/*
+ * Singly-linked List access methods.
+ */
+
+/** Empties a singly-linked list */
+#define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
+
+/** Gets the head of a singly-linked list */
+#define	SLIST_FIRST(head)	((head)->slh_first)
+
+/** Gets the next element of a specific element from a singly-linked list */
+#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
+
+#endif /* _DMMLIB_LISTS_H_ */

+ 11 - 6
include/dmmlib/raw_block.h

@@ -28,14 +28,16 @@
 
 #include <stddef.h>
 
-#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 */
 
+#include <dmmlib/lists.h>
+
+#ifdef WITH_RAWBLOCK_STATS
+#include <dmmlib/dmmstats.h>
+#endif /* WITH_RAWBLOCK_STATS */
+
 /** Enumeration of raw block's different types */
 typedef enum rb_type_en {
 #ifdef FL_RB_ONLY
@@ -51,13 +53,16 @@ typedef enum rb_type_en {
 typedef struct raw_block_header_s {
     rb_type type; /**< The type of the raw block. */
     size_t size; /**< Total size of the raw block. */
-    struct raw_block_header_s *next_raw_block; /**< Pointer to the next raw
-                                                 block. */
+    SLIST_ENTRY(raw_block_header_s) pointers; /**< Pointer to the next raw
+                                                block. */
 #ifdef HAVE_LOCKS
     pthread_mutex_t mutex;/**< Mutex. */
 #endif /* HAVE_LOCKS */
 } raw_block_header_t;
 
+/** The head element of a singly-linked list of raw_block_header_t records */
+SLIST_HEAD(rb_head_s, raw_block_header_s);
+
 /** Create a new raw block. */
 raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type);
 

+ 0 - 3
private-include/debug.h

@@ -31,7 +31,4 @@
 
 void get_raw_blocks(allocator_t *allocator);
 
-void add_to_big_rb_list(raw_block_header_t *rb);
-void remove_from_big_rb_list(raw_block_header_t *rb);
-
 #endif /* DEBUG_H */

+ 1 - 1
private-include/other.h

@@ -28,6 +28,6 @@
 
 #include "dmmlib/raw_block.h"
 
-raw_block_header_t * find_raw_block_owner(raw_block_header_t *head, void* ptr);
+raw_block_header_t * find_raw_block_owner(struct rb_head_s head, void* ptr);
 
 #endif /* OTHER_H */

+ 2 - 33
src/debug.c

@@ -37,56 +37,25 @@ 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) {
+    SLIST_FOREACH(current_raw_block, &allocator->rb_head, pointers) {
         counter++;
         TRACE_3("dmmlib - Raw block at %p of size %zu\n",
                 (void *)current_raw_block,
                 current_raw_block->size);
-        current_raw_block = current_raw_block->next_raw_block;
     }
 
     TRACE_3("dmmlib - there are %d raw blocks\n", counter);
 
-    current_raw_block = allocator->big_blocks_head;
     counter = 0;
 
-    while(current_raw_block) {
+    SLIST_FOREACH(current_raw_block, &allocator->bb_head, pointers) {
         counter++;
         TRACE_3("dmmlib - Raw block at %p of size %zu\n",
                 (void *)current_raw_block,
                 current_raw_block->size);
-        current_raw_block = current_raw_block->next_raw_block;
     }
 
     TRACE_3("dmmlib - there are %d big blocks\n", counter);
 }
-
-/** Add a big block to the list */
-void add_to_big_rb_list(raw_block_header_t *rb) {
-    rb->next_raw_block = systemallocator.big_blocks_head;
-    systemallocator.big_blocks_head = rb;
-}
-
-/** Removes a big block from the list */
-void remove_from_big_rb_list(raw_block_header_t *rb) {
-    raw_block_header_t *previous_block = NULL;
-
-    lock_global();
-    if(systemallocator.big_blocks_head == rb) {
-        systemallocator.big_blocks_head = rb->next_raw_block;
-    } else {
-        for(raw_block_header_t *current_block = systemallocator.big_blocks_head;
-            current_block != NULL;
-            current_block = current_block->next_raw_block
-       ) {
-            if(current_block == rb) {
-                previous_block->next_raw_block = current_block->next_raw_block;
-            }
-            previous_block = current_block;
-        }
-    }
-    unlock_global();
-}

+ 3 - 3
src/free.c

@@ -53,8 +53,7 @@ void free(void *ptr) {
 
     TRACE_1("dmmlib - f %p\n", ptr);
 
-    owner_raw_block = find_raw_block_owner(systemallocator.raw_block_head,
-            ptr);
+    owner_raw_block = find_raw_block_owner(systemallocator.rb_head, ptr);
 
     if(owner_raw_block != NULL) {
 
@@ -70,7 +69,8 @@ void free(void *ptr) {
             ((uintptr_t) ptr - sizeof(raw_block_header_t));
 
 #ifdef WITH_DEBUG
-        remove_from_big_rb_list(owner_raw_block);
+        SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
+                raw_block_header_s, pointers);
 #endif /* WITH_DEBUG */
 
 #ifdef WITH_ALLOCATOR_STATS

+ 8 - 7
src/malloc.c

@@ -27,8 +27,9 @@
 
 #include <inttypes.h>
 
-#include "locks.h"
+#include "dmmlib/lists.h"
 
+#include "locks.h"
 #include "default_rb.h"
 
 #ifdef WITH_ALLOCATOR_STATS
@@ -46,7 +47,6 @@ void * malloc(size_t size) {
     DEFAULT_RB_T *encapsulated_rb;
     void *ptr;
 
-    raw_block = systemallocator.raw_block_head;
     ptr = NULL;
 
     if(size == 0) {
@@ -55,7 +55,7 @@ void * malloc(size_t size) {
     
     /* Try to find a raw block available for allocation */
 
-    while(raw_block != NULL) {
+    SLIST_FOREACH(raw_block, &systemallocator.rb_head, pointers) {
         lock_raw_block(raw_block);
         encapsulated_rb = (DEFAULT_RB_T *)
             ((uintptr_t) raw_block + sizeof(raw_block_header_t));
@@ -65,7 +65,6 @@ void * malloc(size_t size) {
         if(ptr != NULL) {
             break;
         }
-        raw_block = raw_block->next_raw_block;
     }
 
     if(ptr == NULL) {
@@ -95,7 +94,8 @@ void * malloc(size_t size) {
             if(ptr != NULL) {
 
 #ifdef WITH_DEBUG
-                add_to_big_rb_list((raw_block_header_t *)ptr);
+                SLIST_INSERT_HEAD(&systemallocator.bb_head,
+                        (raw_block_header_t *) ptr, pointers);
 #endif /* WITH_DEBUG */
 
 #ifdef WITH_ALLOCATOR_STATS
@@ -112,6 +112,8 @@ void * malloc(size_t size) {
                 ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
             }
 
+            unlock_global();
+
         } else {
 
             lock_global();
@@ -119,8 +121,7 @@ void * malloc(size_t size) {
                     DEFAULT_RB_TYPE);
             if(new_raw_block != NULL) {
                 lock_raw_block(new_raw_block);
-                new_raw_block->next_raw_block = systemallocator.raw_block_head;
-                systemallocator.raw_block_head = new_raw_block;
+                SLIST_INSERT_HEAD(&systemallocator.rb_head, new_raw_block, pointers);
                 unlock_global();
 
                 encapsulated_rb = (DEFAULT_RB_T *)

+ 5 - 6
src/other.c

@@ -28,13 +28,12 @@
 #include <inttypes.h>
 
 /** Finds the raw block owner of a certain pointer */
-raw_block_header_t * find_raw_block_owner(raw_block_header_t *head, void* ptr) {
-    raw_block_header_t *owner = NULL;
+raw_block_header_t * find_raw_block_owner(struct rb_head_s head, void* ptr) {
+    raw_block_header_t *current_block, *owner;
 
-    for(raw_block_header_t *current_block = head;
-            current_block != NULL;
-            current_block = current_block->next_raw_block
-       ) {
+    owner = NULL;
+
+    SLIST_FOREACH(current_block, &head, pointers) {
         if(((uintptr_t) current_block < (uintptr_t) ptr) &&
                 ((uintptr_t) ptr < (uintptr_t) current_block +
                  current_block->size)

+ 0 - 1
src/raw_block.c

@@ -46,7 +46,6 @@ raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
     }
 
     ptr->size = raw_block_size;
-    ptr->next_raw_block = NULL;
 
     switch(type) {
 

+ 7 - 4
src/realloc.c

@@ -53,8 +53,7 @@ void * realloc(void *ptr, size_t size) {
         goto done;
     }
 
-    owner_raw_block = find_raw_block_owner(systemallocator.raw_block_head,
-            ptr);
+    owner_raw_block = find_raw_block_owner(systemallocator.rb_head, ptr);
 
     if(owner_raw_block != NULL) {
 
@@ -121,8 +120,12 @@ void * realloc(void *ptr, size_t size) {
                         owner_raw_block->size - sizeof(raw_block_header_t));
 
 #ifdef WITH_DEBUG
-                remove_from_big_rb_list(owner_raw_block);
-                add_to_big_rb_list(new_block);
+                lock_global();
+                SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
+                        raw_block_header_s, pointers);
+                SLIST_INSERT_HEAD(&systemallocator.bb_head,
+                        new_block, pointers);
+                unlock_global();
 #endif /* WITH_DEBUG */
 
                 release_memory(owner_raw_block);