Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use mallocators for dictionaries
authorthiery <thiery@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 14 Aug 2006 11:17:40 +0000 (11:17 +0000)
committerthiery <thiery@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 14 Aug 2006 11:17:40 +0000 (11:17 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2714 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/xbt/dict.c
src/xbt/dict_elm.c
src/xbt/dict_private.h
src/xbt/xbt_main.c
src/xbt_modinter.h

index c28171f..6d788f0 100644 (file)
 #include <string.h>
 #include "xbt/ex.h"
 #include "xbt/log.h"
+#include "xbt/mallocator.h"
+#include "xbt_modinter.h"
 #include "dict_private.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict,xbt,
    "Dictionaries provide the same functionnalities than hash tables");
 /*####[ Private prototypes ]#################################################*/
 
+static xbt_mallocator_t dict_mallocator = NULL;
+static void* dict_mallocator_new_f(void);
+static void dict_mallocator_free_f(void* dict);
+static void dict_mallocator_reset_f(void* dict);
+
+
 /*####[ Code ]###############################################################*/
 
 /**
@@ -36,14 +44,23 @@ xbt_dict_t xbt_dict_new(void) {
  * \see xbt_dict_new(), xbt_dict_free()
  */
 xbt_dict_t xbt_dict_new_ext(int hashsize) {
-  int i;
-  xbt_dict_t dict = xbt_new0(s_xbt_dict_t, 1);
+  xbt_dict_t dict;
+
+  if (dict_mallocator == NULL) {
+    /* first run */
+    dict_mallocator = xbt_mallocator_new(256,
+                                        dict_mallocator_new_f,
+                                        dict_mallocator_free_f,
+                                        dict_mallocator_reset_f);
+    dict_elm_mallocator = xbt_mallocator_new(256,
+                                            dict_elm_mallocator_new_f,
+                                            dict_elm_mallocator_free_f,
+                                            dict_elm_mallocator_reset_f);
+  }
+
+  dict = xbt_mallocator_get(dict_mallocator);
   dict->table_size = hashsize;
   dict->table = xbt_new0(xbt_dictelm_t, dict->table_size);
-
-  for (i = 0; i < hashsize; i++) {
-    dict->table[i] = NULL;
-  }
   dict->count = 0;
 
   return dict;
@@ -58,17 +75,22 @@ xbt_dict_t xbt_dict_new_ext(int hashsize) {
 void xbt_dict_free(xbt_dict_t *dict) {
   int i;
   xbt_dictelm_t current, previous;
+  int table_size;
+  xbt_dictelm_t *table;
+
   if (dict != NULL && *dict != NULL) {
-    for (i = 0; i < (*dict)->table_size; i++) {
-      current = (*dict)->table[i];
+    table_size = (*dict)->table_size;
+    table = (*dict)->table;
+    for (i = 0; i < table_size; i++) {
+      current = table[i];
       while (current != NULL) {
        previous = current;
        current = current->next;
        xbt_dictelm_free(previous);
       }
     }
-    xbt_free((*dict)->table);
-    xbt_free(*dict);
+    xbt_free(table);
+    xbt_mallocator_release(dict_mallocator, *dict);
     *dict = NULL;
   }
 }
@@ -397,6 +419,31 @@ void xbt_dict_dump(xbt_dict_t     dict,
   }
 }
 
+/**
+ * Destroy the dict mallocators.
+ * This is an internal XBT function called by xbt_exit().
+ */
+void xbt_dict_exit(void) {
+  if (dict_mallocator != NULL) {
+    xbt_mallocator_free(dict_mallocator);
+    xbt_mallocator_free(dict_elm_mallocator);
+  }
+}
+
+static void* dict_mallocator_new_f(void) {
+  return xbt_new(s_xbt_dict_t, 1);
+}
+
+static void dict_mallocator_free_f(void* dict) {
+  xbt_free(dict);
+}
+
+static void dict_mallocator_reset_f(void* dict) {
+  /* nothing to do because all fields are
+   * initialized in xbt_dict_new
+   */
+}
+
 #ifdef SIMGRID_TEST
 #include "xbt.h"
 #include "xbt/ex.h"
index 529ea65..e08190d 100644 (file)
@@ -17,12 +17,14 @@ XBT_LOG_NEW_SUBCATEGORY(xbt_dict_search,xbt_dict,"Dictionaries internals: search
 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_remove,xbt_dict,"Dictionaries internals: elements removal");
 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse,xbt_dict,"Dictionaries internals: post-removal cleanup");
 
+xbt_mallocator_t dict_elm_mallocator = NULL;
+
 xbt_dictelm_t xbt_dictelm_new(const char *key,
                              int key_len,
                              void *content,
                              void_f_pvoid_t free_f,
                              xbt_dictelm_t next) {
-  xbt_dictelm_t element = xbt_new(s_xbt_dictelm_t, 1);
+  xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
   
   element->key = xbt_new(char, key_len + 1);
   strncpy(element->key, key, key_len);
@@ -44,6 +46,18 @@ void xbt_dictelm_free(xbt_dictelm_t element) {
       element->free_f(element->content);
     }
    
-    xbt_free(element);
+    xbt_mallocator_release(dict_elm_mallocator, element);
   }
 }
+
+void* dict_elm_mallocator_new_f(void) {
+  return xbt_new(s_xbt_dictelm_t, 1);
+}
+
+void dict_elm_mallocator_free_f(void* elem) {
+  xbt_free(elem);
+}
+
+void dict_elm_mallocator_reset_f(void* elem) {
+
+}
index 2aae858..17856d2 100644 (file)
@@ -16,6 +16,7 @@
 #include "xbt/ex.h"
 #include "xbt/dynar.h"
 #include "xbt/dict.h"
+#include "xbt/mallocator.h"
 
 typedef struct xbt_dictelm_ *xbt_dictelm_t;
 
@@ -37,6 +38,11 @@ typedef struct xbt_dict_cursor_ s_xbt_dict_cursor_t;
 
 unsigned int xbt_dict_hash(const char *str);
 
+extern xbt_mallocator_t dict_elm_mallocator;
+extern void* dict_elm_mallocator_new_f(void);
+extern void dict_elm_mallocator_free_f(void* elem);
+extern void dict_elm_mallocator_reset_f(void* elem);
+
 /*####[ Function prototypes ]################################################*/
 xbt_dictelm_t xbt_dictelm_new(const char *key,
                              int key_len,
index 927c613..92765f1 100644 (file)
@@ -45,6 +45,7 @@ xbt_exit(){
   if (xbt_initialized == 0) {
     free(xbt_binary_name);
     xbt_fifo_exit();
+    xbt_dict_exit();
   }
   xbt_log_exit();
 }
index 628f1ee..f54aef2 100644 (file)
@@ -14,5 +14,6 @@
 void xbt_log_init(int *argc,char **argv);
 void xbt_log_exit(void);
 void xbt_fifo_exit(void);
+void xbt_dict_exit(void);
 
 #endif /* XBT_MODINTER_H */