Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added macros definitions and runtime support for cocci-processed local static variables.
[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
47 XBT_PUBLIC(void) smpi_register_static(void* arg);
48
49 #define SMPI_VARINIT_STATIC(name,type)                      \
50 static type *name = NULL;                                   \
51 if(!name) {                                                 \
52    name = (type*)malloc(smpi_global_size() * sizeof(type)); \
53    smpi_register_static(name);                              \
54 }
55
56 #define SMPI_VARINIT_STATIC_AND_SET(name,type,expr) \
57 static type *name = NULL;                           \
58 if(!name) {                                         \
59    size_t size = smpi_global_size();                \
60    size_t i;                                        \
61    type value = expr;                               \
62    name = (type*)malloc(size * sizeof(type));       \
63    for(i = 0; i < size; i++) {                      \
64       name[i] = value;                              \
65    }                                                \
66    smpi_register_static(name);                      \
67 }
68
69 #define SMPI_VARGET_STATIC(name) name[smpi_process_index()]
70
71 #endif