Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Extend smpi_register_static to accept any free function.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Thu, 21 Feb 2013 13:41:07 +0000 (14:41 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Thu, 21 Feb 2013 13:41:07 +0000 (14:41 +0100)
include/smpi/smpi_cocci.h
src/smpi/smpi_c99.c

index ace208f..f082355 100644 (file)
@@ -48,7 +48,7 @@ void __attribute__((weak,destructor)) __postfini_##name(void) { \
  * This function is rather internal, mainly used for the
  * privatization of global variables through coccinelle.
  */
-XBT_PUBLIC(void) smpi_register_static(void* arg);
+XBT_PUBLIC(void) smpi_register_static(void* arg, void_f_pvoid_t free_fn);
 
 XBT_PUBLIC(void) smpi_free_static(void);
 
@@ -56,7 +56,7 @@ XBT_PUBLIC(void) smpi_free_static(void);
 static type *name = NULL;                                   \
 if(!name) {                                                 \
    name = (type*)malloc(smpi_global_size() * sizeof(type)); \
-   smpi_register_static(name);                              \
+   smpi_register_static(name, xbt_free);                    \
 }
 
 #define SMPI_VARINIT_STATIC_AND_SET(name,type,expr) \
@@ -69,7 +69,7 @@ if(!name) {                                         \
    for(i = 0; i < size; i++) {                      \
       name[i] = value;                              \
    }                                                \
-   smpi_register_static(name);                      \
+   smpi_register_static(name, xbt_free);            \
 }
 
 #define SMPI_VARGET_STATIC(name) name[smpi_process_index()]
index 32e0265..88f8467 100644 (file)
@@ -7,20 +7,27 @@
 #include <xbt/dynar.h>
 #include "private.h"
 
+typedef struct s_smpi_static {
+  void *ptr;
+  void_f_pvoid_t free_fn;
+} s_smpi_static_t;
+
 static xbt_dynar_t registered_static_stack = NULL;
 
-void smpi_register_static(void* arg)
+void smpi_register_static(void* arg, void_f_pvoid_t free_fn)
 {
+  s_smpi_static_t elm = { arg, free_fn };
   if (!registered_static_stack)
-    registered_static_stack = xbt_dynar_new(sizeof(void*), NULL);
-  xbt_dynar_push_as(registered_static_stack, void*, arg);
+    registered_static_stack = xbt_dynar_new(sizeof(s_smpi_static_t), NULL);
+  xbt_dynar_push_as(registered_static_stack, s_smpi_static_t, elm);
 }
 
 void smpi_free_static(void)
 {
   while (!xbt_dynar_is_empty(registered_static_stack)) {
-    void *p = xbt_dynar_pop_as(registered_static_stack, void*);
-    free(p);
+    s_smpi_static_t elm =
+      xbt_dynar_pop_as(registered_static_stack, s_smpi_static_t);
+    elm.free_fn(elm.ptr);
   }
   xbt_dynar_free(&registered_static_stack);
 }