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 / 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-2017. 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 = malloc(n);
80   if (!res)
81     xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
82   return res;
83 }
84
85 /** @brief like malloc, but xbt_die() on error and memset data to 0
86     @hideinitializer */
87 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
88   void* res = calloc(n, 1);
89   if (!res)
90     xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
91   return res;
92 }
93
94 /** @brief like realloc, but xbt_die() on error
95     @hideinitializer */
96 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
97   void *res = NULL;
98   if (s) {
99     if (p) {
100       res = realloc(p, s);
101       if (!res)
102         xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
103     } else {
104       res = xbt_malloc(s);
105     }
106   } else {
107     free(p);
108   }
109   return res;
110 }
111
112 /** @brief like free
113     @hideinitializer */
114 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
115
116 /** @brief like free, but you can be sure that it is a function  */
117 XBT_PUBLIC(void) xbt_free_f(void *p);
118 /** @brief should be given a pointer to pointer, and frees the second one */
119 XBT_PUBLIC(void) xbt_free_ref(void *d);
120
121 SG_END_DECL()
122
123 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
124 /** @brief like calloc, but xbt_die() on error
125     @hideinitializer */
126 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
127
128 /** @} */
129
130 #endif                          /* XBT_SYSDEP_H */