Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 XBT_PUBLIC(void) smpi_register_static(void* arg, void_f_pvoid_t free_fn);
52
53 XBT_PUBLIC(void) smpi_free_static(void);
54
55 #define SMPI_VARINIT_STATIC(name,type)                      \
56 static type *name = NULL;                                   \
57 if(!name) {                                                 \
58    name = (type*)malloc(smpi_global_size() * sizeof(type)); \
59    smpi_register_static(name, xbt_free);                    \
60 }
61
62 #define SMPI_VARINIT_STATIC_AND_SET(name,type,expr) \
63 static type *name = NULL;                           \
64 if(!name) {                                         \
65    size_t size = smpi_global_size();                \
66    size_t i;                                        \
67    type value = expr;                               \
68    name = (type*)malloc(size * sizeof(type));       \
69    for(i = 0; i < size; i++) {                      \
70       name[i] = value;                              \
71    }                                                \
72    smpi_register_static(name, xbt_free);            \
73 }
74
75 #define SMPI_VARGET_STATIC(name) name[smpi_process_index()]
76
77 #endif