Logo AND Algorithmique Numérique Distribuée

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