Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
that's perfectly fine to not free that memory on process terminaison on Apple
[simgrid.git] / include / smpi / smpi_cocci.h
1 /* Copyright (c) 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SMPI_COCCI_H
8 #define SMPI_COCCI_H
9
10 #include <xbt/misc.h>
11
12 /* Macros used by coccinelle-generated code */
13
14 #define SMPI_VARINIT_GLOBAL(name,type)                          \
15 type *name = NULL;                                              \
16 void __attribute__((weak,constructor)) __preinit_##name(void) { \
17    if(!name)                                                    \
18       name = (type*)malloc(smpi_global_size() * sizeof(type));  \
19 }                                                               \
20 void __attribute__((weak,destructor)) __postfini_##name(void) { \
21    free(name);                                                  \
22    name = NULL;                                                 \
23 }
24
25 #define SMPI_VARINIT_GLOBAL_AND_SET(name,type,expr)             \
26 type *name = NULL;                                              \
27 void __attribute__((weak,constructor)) __preinit_##name(void) { \
28    size_t size = smpi_global_size();                            \
29    size_t i;                                                    \
30    type value = expr;                                           \
31    if(!name) {                                                  \
32       name = (type*)malloc(size * sizeof(type));                \
33       for(i = 0; i < size; i++) {                               \
34          name[i] = value;                                       \
35       }                                                         \
36    }                                                            \
37 }                                                               \
38 void __attribute__((weak,destructor)) __postfini_##name(void) { \
39    free(name);                                                  \
40    name = NULL;                                                 \
41 }
42
43 #define SMPI_VARGET_GLOBAL(name) name[smpi_process_index()]
44
45 /* The following handle local static variables */
46 /** @brief Make sure that the passed pointer is freed on process exit.
47  *
48  * This function is rather internal, mainly used for the
49  * privatization of global variables through coccinelle.
50  *
51  * Since its implementation relies on the on_exit() function that
52  * is not implemented on Mac, this function is a no-op on that
53  * architecture. But the only issue raised is that the memory is
54  * not raised right before the process terminaison. This is only
55  * important if you want to run valgrind on the code, or
56  * equivalent.
57  */
58 XBT_PUBLIC(void) smpi_register_static(void* arg);
59
60 #define SMPI_VARINIT_STATIC(name,type)                      \
61 static type *name = NULL;                                   \
62 if(!name) {                                                 \
63    name = (type*)malloc(smpi_global_size() * sizeof(type)); \
64    smpi_register_static(name);                              \
65 }
66
67 #define SMPI_VARINIT_STATIC_AND_SET(name,type,expr) \
68 static type *name = NULL;                           \
69 if(!name) {                                         \
70    size_t size = smpi_global_size();                \
71    size_t i;                                        \
72    type value = expr;                               \
73    name = (type*)malloc(size * sizeof(type));       \
74    for(i = 0; i < size; i++) {                      \
75       name[i] = value;                              \
76    }                                                \
77    smpi_register_static(name);                      \
78 }
79
80 #define SMPI_VARGET_STATIC(name) name[smpi_process_index()]
81
82 #endif