Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define a struct s_xbt_het_dictelm for holding the free function of a dict element.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Mon, 28 Nov 2011 12:56:53 +0000 (13:56 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Thu, 1 Dec 2011 10:32:48 +0000 (11:32 +0100)
Create a mallocator for these structs, and use them.

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

index e678a99..2bbf177 100644 (file)
@@ -671,12 +671,17 @@ void xbt_dict_preinit(void)
   if (dict_elm_mallocator != NULL) {
     /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
     xbt_mallocator_free(dict_elm_mallocator);
+    xbt_mallocator_free(dict_het_elm_mallocator);
   }
 
   dict_elm_mallocator = xbt_mallocator_new(256,
                                            dict_elm_mallocator_new_f,
                                            dict_elm_mallocator_free_f,
                                            dict_elm_mallocator_reset_f);
+  dict_het_elm_mallocator = xbt_mallocator_new(256,
+                                               dict_het_elm_mallocator_new_f,
+                                               dict_het_elm_mallocator_free_f,
+                                               dict_het_elm_mallocator_reset_f);
 }
 
 /**
@@ -688,6 +693,8 @@ void xbt_dict_postexit(void)
   if (dict_elm_mallocator != NULL) {
     xbt_mallocator_free(dict_elm_mallocator);
     dict_elm_mallocator = NULL;
+    xbt_mallocator_free(dict_het_elm_mallocator);
+    dict_het_elm_mallocator = NULL;
   }
   if (all_sizes) {
     unsigned int count;
index f737b33..db60d22 100644 (file)
@@ -22,12 +22,14 @@ XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse, xbt_dict,
                         "Dictionaries internals: post-removal cleanup");
 
 xbt_mallocator_t dict_elm_mallocator = NULL;
+xbt_mallocator_t dict_het_elm_mallocator = NULL;
 
 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
                               unsigned int hash_code, void *content,
                               void_f_pvoid_t free_f)
 {
-  xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
+  xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator);
+  xbt_dictelm_t element = &het_element->element;
 
   element->key = xbt_new(char, key_len + 1);
   memcpy((void *) element->key, (void *) key, key_len);
@@ -37,7 +39,7 @@ xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
   element->hash_code = hash_code;
 
   element->content = content;
-  element->free_f = free_f;
+  het_element->free_f = free_f;
   element->next = NULL;
 
   return element;
@@ -46,27 +48,34 @@ xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
 {
   if (element != NULL) {
+    xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
     xbt_free(element->key);
 
-    if (element->free_f != NULL && element->content != NULL) {
-      element->free_f(element->content);
+    if (het_element->free_f != NULL && element->content != NULL) {
+      het_element->free_f(element->content);
     }
 
-    xbt_mallocator_release(dict_elm_mallocator, element);
+    xbt_mallocator_release(dict_het_elm_mallocator, het_element);
   }
 }
 
 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element,
                           void *data, void_f_pvoid_t free_ctn)
 {
-  if (element->free_f && element->content)
-    element->free_f(element->content);
+  xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
+  if (het_element->free_f && element->content)
+    het_element->free_f(element->content);
 
   element->content = data;
-  element->free_f = free_ctn;
+  het_element->free_f = free_ctn;
 }
 
 void *dict_elm_mallocator_new_f(void)
 {
   return xbt_new(s_xbt_dictelm_t, 1);
 }
+
+void *dict_het_elm_mallocator_new_f(void)
+{
+  return xbt_new(s_xbt_het_dictelm_t, 1);
+}
index 4c5dcc7..490fe38 100644 (file)
@@ -27,11 +27,15 @@ typedef struct s_xbt_dictelm {
   unsigned int hash_code;
 
   void *content;
-  void_f_pvoid_t free_f;
 
   xbt_dictelm_t next;
 } s_xbt_dictelm_t;
 
+typedef struct s_xbt_het_dictelm {
+  s_xbt_dictelm_t element;
+  void_f_pvoid_t free_f;
+} s_xbt_het_dictelm_t, *xbt_het_dictelm_t;
+
 typedef struct s_xbt_dict {
   void_f_pvoid_t free_f;
   xbt_dictelm_t *table;
@@ -48,6 +52,11 @@ extern void *dict_elm_mallocator_new_f(void);
 #define dict_elm_mallocator_free_f xbt_free_f
 #define dict_elm_mallocator_reset_f ((void_f_pvoid_t)NULL)
 
+extern xbt_mallocator_t dict_het_elm_mallocator;
+extern void *dict_het_elm_mallocator_new_f(void);
+#define dict_het_elm_mallocator_free_f xbt_free_f
+#define dict_het_elm_mallocator_reset_f ((void_f_pvoid_t)NULL)
+
 /*####[ Function prototypes ]################################################*/
 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
                               unsigned int hash_code, void *content,