Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
netcards were renamed to netpoints recently
[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-2015. 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 #include "xbt/log.h"
15 #include "xbt/misc.h"
16 #include "xbt/asserts.h"
17
18 #include "simgrid_config.h"
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdarg.h>             /* va_list */
23
24 SG_BEGIN_DECL()
25
26 /* They live in asserts.h, but need to be declared before this module.
27    double declaration to cut dependency cycle */
28 /**
29  * @addtogroup XBT_error
30  *
31  * @{
32  */
33 /** @brief Kill the program in silence */
34 XBT_PUBLIC(void) XBT_ATTRIB_NORETURN xbt_abort(void);
35
36 /**
37  * @brief Kill the program with an error message
38  * \param ... a format string and its arguments
39  *
40  * Things are so messed up that the only thing to do now, is to stop the program.
41  *
42  * The message is handled by a CRITICAL logging request, and may consist of a format string with arguments.
43  */
44 #define xbt_die(...)                            \
45   do {                                          \
46     XBT_CCRITICAL(xbt, __VA_ARGS__);            \
47     xbt_abort();                                \
48   } while (0)
49 /** @} */
50
51 #ifdef XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL
52 XBT_LOG_NEW_CATEGORY(xbt, "All XBT categories (simgrid toolbox)");
53 #else
54 XBT_LOG_EXTERNAL_CATEGORY(xbt);
55 #endif
56
57 /** @addtogroup XBT_syscall
58  *  @brief Malloc and associated functions, killing the program on error (with \ref XBT_ex)
59  *
60  *  @{
61  */
62
63 /** @brief Like strdup, but xbt_die() on error */
64 static XBT_ALWAYS_INLINE char *xbt_strdup(const char *s) {
65   char *res = NULL;
66   if (s) {
67     res = strdup(s);
68     if (!res)
69       xbt_die("memory allocation error (strdup returned NULL)");
70   }
71   return res;
72 }
73
74 XBT_PUBLIC(void) xbt_backtrace_display_current();
75
76 /** @brief Like malloc, but xbt_die() on error
77     @hideinitializer */
78 static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) {
79   void *res;
80 /*  if (n==0) {
81      xbt_backtrace_display_current();
82      xbt_die("malloc(0) is not portable");
83   }*/
84
85   res = malloc(n);
86   if (!res)
87     xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
88   return res;
89 }
90
91 /** @brief like malloc, but xbt_die() on error and memset data to 0
92     @hideinitializer */
93 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
94   void *res;
95   //if (n==0) xbt_die("calloc(0) is not portable");
96   res = calloc(n, 1);
97   if (!res)
98     xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
99   return res;
100 }
101
102 /** @brief like realloc, but xbt_die() on error
103     @hideinitializer */
104 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
105   void *res = NULL;
106   //if (s==0) xbt_die("realloc(0) is not portable");
107   if (s) {
108     if (p) {
109       res = realloc(p, s);
110       if (!res)
111         xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
112     } else {
113       res = xbt_malloc(s);
114     }
115   } else {
116     free(p);
117   }
118   return res;
119 }
120
121 /** @brief like free
122     @hideinitializer */
123 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
124
125 /** @brief like free, but you can be sure that it is a function  */
126 XBT_PUBLIC(void) xbt_free_f(void *p);
127 /** @brief should be given a pointer to pointer, and frees the second one */
128 XBT_PUBLIC(void) xbt_free_ref(void *d);
129
130 SG_END_DECL()
131
132 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
133 /** @brief like calloc, but xbt_die() on error
134     @hideinitializer */
135 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
136
137 /** @} */
138
139 #endif                          /* _XBT_SYSDEP_H */