Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not allow to xbt_new things with (non-trivial) constructors
[simgrid.git] / include / xbt / sysdep.h
1 /*  xbt/sysdep.h -- all system dependency                                   */
2 /*  no system header should be loaded out of this file so that we have only */
3 /*  one file to check when porting to another OS                            */
4
5 /* Copyright (c) 2004-2014. The SimGrid Team.
6  * All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #ifndef _XBT_SYSDEP_H
12 #define _XBT_SYSDEP_H
13
14 #ifdef __cplusplus
15 #include <type_traits>
16 #endif
17
18 #include "xbt/log.h"
19 #include "xbt/misc.h"
20 #include "xbt/asserts.h"
21
22 #include "simgrid_config.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdarg.h>             /* va_list */
27
28 SG_BEGIN_DECL()
29
30 /* They live in asserts.h, but need to be declared before this module.
31    double declaration to cut dependency cycle */
32 /**
33  * @addtogroup XBT_error
34  *
35  * @{
36  */
37 /** @brief Kill the program in silence */
38 XBT_PUBLIC(void) xbt_abort(void) _XBT_GNUC_NORETURN;
39
40 /**
41  * @brief Kill the program with an error message
42  * \param ... a format string and its arguments
43  *
44  * Things are so messed up that the only thing to do now, is to stop the
45  * program.
46  *
47  * The message is handled by a CRITICAL logging request, and may consist of a
48  * format string with arguments.
49  */
50 #define xbt_die(...)                            \
51   do {                                          \
52     XBT_CCRITICAL(xbt, __VA_ARGS__);            \
53     xbt_abort();                                \
54   } while (0)
55 /** @} */
56
57 XBT_LOG_EXTERNAL_CATEGORY(xbt);
58
59 /* these ones live in str.h, but redeclare them here so that we do
60    not need to load the whole str.h and its heavy dependencies */
61 #ifndef __USE_GNU               /* do not redeclare existing headers */
62 XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt,   /*args */
63                          ...) _XBT_GNUC_PRINTF(2, 3);
64 XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap);
65 #endif
66 XBT_PUBLIC(char *) bprintf(const char *fmt, ...) _XBT_GNUC_PRINTF(1, 2);
67
68 /** @addtogroup XBT_syscall
69  *  @brief Malloc and associated functions, killing the program on error (with \ref XBT_ex)
70  *
71  *  @{
72  */
73
74 #if defined(__GNUC__) || defined(DOXYGEN)
75 /** @brief Like strdup, but xbt_die() on error */
76 static inline __attribute__ ((always_inline))
77 char *xbt_strdup(const char *s)
78 {
79   char *res = NULL;
80   if (s) {
81     res = strdup(s);
82     if (!res)
83       xbt_die("memory allocation error (strdup returned NULL)");
84   }
85   return res;
86 }
87
88 XBT_PUBLIC(void) xbt_backtrace_display_current(void);
89
90 /** @brief Like malloc, but xbt_die() on error
91     @hideinitializer */
92 static inline __attribute__ ((always_inline))
93 void *xbt_malloc(size_t n)
94 {
95   void *res;
96 /*  if (n==0) {
97      xbt_backtrace_display_current();
98      xbt_die("malloc(0) is not portable");
99   }*/
100
101   res = malloc(n);
102   if (!res)
103     xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
104   return res;
105 }
106
107 /** @brief like malloc, but xbt_die() on error and memset data to 0
108     @hideinitializer */
109 static inline __attribute__ ((always_inline))
110 void *xbt_malloc0(size_t n)
111 {
112   void *res;
113   //if (n==0) xbt_die("calloc(0) is not portable");
114   res = calloc(n, 1);
115   if (!res)
116     xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
117   return res;
118 }
119
120 /** @brief like realloc, but xbt_die() on error
121     @hideinitializer */
122 static inline __attribute__ ((always_inline))
123 void *xbt_realloc(void *p, size_t s)
124 {
125   void *res = NULL;
126   //if (s==0) xbt_die("realloc(0) is not portable");
127   if (s) {
128     if (p) {
129       res = realloc(p, s);
130       if (!res)
131         xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
132     } else {
133       res = xbt_malloc(s);
134     }
135   } else {
136     free(p);
137   }
138   return res;
139 }
140 #else                           /* non __GNUC__  */
141 #  define xbt_strdup(s)    strdup(s)
142 #  define xbt_malloc(n)    malloc(n)
143 #  define xbt_malloc0(n)   calloc(n,1)
144 #  define xbt_realloc(p,s) realloc(p,s)
145 #endif                          /* __GNUC__ ? */
146
147 /** @brief like free
148     @hideinitializer */
149 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
150
151 /** @brief like free, but you can be sure that it is a function  */
152 XBT_PUBLIC(void) xbt_free_f(void *p);
153 /** @brief should be given a pointer to pointer, and frees the second one */
154 XBT_PUBLIC(void) xbt_free_ref(void *d);
155
156 SG_END_DECL()
157
158 /** @brief like calloc, but xbt_die() on error and don't memset to 0
159     @hideinitializer */
160 #ifndef __cplusplus
161
162 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
163 /** @brief like calloc, but xbt_die() on error
164     @hideinitializer */
165 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
166
167 #else
168
169 /** C++ wrapper for xtb_new
170  *
171  *  This ensures that we do not xbt_new things that need a constructor.
172  */
173 template<typename T> inline
174 T* xbt_new_(size_t count)
175 {
176   static_assert(std::is_trivial<T>(),
177     "Cannot xbt_new this type");
178   return (T*) xbt_malloc(sizeof(T) * count);
179 }
180
181 template<typename T> inline
182 T* xbt_new0_(size_t count)
183 {
184   static_assert(std::is_trivial<T>(),
185     "Cannot xbt_new0 this type");
186   return (T*) xbt_malloc0(sizeof(T) * count);
187 }
188
189 #define xbt_new(type, count)  ::xbt_new_<type>(count)
190 #define xbt_new0(type, count) ::xbt_new0_<type>(count)
191
192 #endif
193
194 /** @} */
195
196 #endif                          /* _XBT_SYSDEP_H */