Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move declarations for xbt_abort and xbt_die to xbt/asserts.h.
[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-2021. 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
16 #include <simgrid/config.h>
17
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdarg.h>             /* va_list */
21
22 SG_BEGIN_DECL
23
24 #ifdef XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL
25 XBT_LOG_NEW_CATEGORY(xbt, "All XBT categories (simgrid toolbox)");
26 XBT_LOG_NEW_SUBCATEGORY(xbt_help, xbt, "Help messages");
27 #else
28 XBT_LOG_EXTERNAL_CATEGORY(xbt);
29 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
30 #endif
31
32 /** @addtogroup XBT_syscall
33  *  @brief Malloc and associated functions, killing the program on error (with @ref XBT_ex)
34  *
35  *  @{
36  */
37
38 /** @brief Like strdup, but xbt_die() on error */
39 static XBT_ALWAYS_INLINE char *xbt_strdup(const char *s) {
40   char *res = NULL;
41   if (s) {
42     res = strdup(s);
43     if (!res)
44       xbt_die("memory allocation error (strdup returned NULL)");
45   }
46   return res;
47 }
48
49 /** @brief Like malloc, but xbt_die() on error
50     @hideinitializer */
51 static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) {
52   void* res = malloc(n);
53   if (!res)
54     xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
55   return res;
56 }
57
58 /** @brief like malloc, but xbt_die() on error and memset data to 0
59     @hideinitializer */
60 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
61   void* res = calloc(n, 1);
62   if (!res)
63     xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
64   return res;
65 }
66
67 /** @brief like realloc, but xbt_die() on error
68     @hideinitializer */
69 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
70   void *res = NULL;
71   if (s) {
72     if (p) {
73       res = realloc(p, s);
74       if (!res)
75         xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
76     } else {
77       res = xbt_malloc(s);
78     }
79   } else {
80     free(p);
81   }
82   return res;
83 }
84
85 /** @brief like free
86     @hideinitializer */
87 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
88
89 #ifdef __cplusplus
90 #define XBT_FREE_NOEXCEPT noexcept(noexcept(::free))
91 #else
92 #define XBT_FREE_NOEXCEPT
93 #endif
94
95 /** @brief like free, but you can be sure that it is a function  */
96 XBT_PUBLIC void xbt_free_f(void* p) XBT_FREE_NOEXCEPT;
97 /** @brief should be given a pointer to pointer, and frees the second one */
98 XBT_PUBLIC void xbt_free_ref(void* d) XBT_FREE_NOEXCEPT;
99
100 SG_END_DECL
101
102 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
103 /** @brief like calloc, but xbt_die() on error
104     @hideinitializer */
105 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
106
107 /** @} */
108
109 #endif                          /* XBT_SYSDEP_H */