Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't rely on the non-standard on_exit() function.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 12 Jun 2012 17:11:18 +0000 (19:11 +0200)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 12 Jun 2012 17:11:18 +0000 (19:11 +0200)
include/smpi/smpi_cocci.h
src/smpi/smpi_c99.c
src/smpi/smpi_global.c

index 2aff10b..ace208f 100644 (file)
@@ -47,16 +47,11 @@ void __attribute__((weak,destructor)) __postfini_##name(void) { \
  *
  * This function is rather internal, mainly used for the
  * privatization of global variables through coccinelle.
- *
- * Since its implementation relies on the on_exit() function that
- * is not implemented on Mac, this function is a no-op on that
- * architecture. But the only issue raised is that the memory is
- * not raised right before the process terminaison. This is only
- * important if you want to run valgrind on the code, or
- * equivalent.
  */
 XBT_PUBLIC(void) smpi_register_static(void* arg);
 
+XBT_PUBLIC(void) smpi_free_static(void);
+
 #define SMPI_VARINIT_STATIC(name,type)                      \
 static type *name = NULL;                                   \
 if(!name) {                                                 \
index df84a2e..32e0265 100644 (file)
@@ -4,18 +4,23 @@
 /* This program is free software; you can redistribute it and/or modify it
   * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include <stdlib.h>
+#include <xbt/dynar.h>
 #include "private.h"
 
-static void smpi_free_static(int status, void* arg) {
-   free(arg);
+static xbt_dynar_t registered_static_stack = NULL;
+
+void smpi_register_static(void* arg)
+{
+  if (!registered_static_stack)
+    registered_static_stack = xbt_dynar_new(sizeof(void*), NULL);
+  xbt_dynar_push_as(registered_static_stack, void*, arg);
 }
 
-void smpi_register_static(void* arg) {
-#ifndef __APPLE__
-  // on_exit is not implemented on Apple.
-  // That's fine, the memory won't be released on UNIX process terminaison.
-  // This means that valgrind will report it as leaked (but who cares?)
-   on_exit(&smpi_free_static, arg);
-#endif
+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);
+  }
+  xbt_dynar_free(&registered_static_stack);
 }
index 2b8c68d..66a76cd 100644 (file)
@@ -228,6 +228,8 @@ void smpi_global_destroy(void)
   }
   xbt_free(process_data);
   process_data = NULL;
+
+  smpi_free_static();
 }
 
 /* Fortran specific stuff */