Logo AND Algorithmique Numérique Distribuée

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