Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Typed template for Extendable::get_data.
[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-2022. 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     xbt_assert(res, "memory allocation error (strdup returned NULL)");
44   }
45   return res;
46 }
47
48 /** @brief Like malloc, but xbt_die() on error
49     @hideinitializer */
50 static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) {
51   void* res = malloc(n);
52   xbt_assert(res, "Memory allocation of %lu bytes failed", (unsigned long)n);
53   return res;
54 }
55
56 /** @brief like malloc, but xbt_die() on error and memset data to 0
57     @hideinitializer */
58 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
59   void* res = calloc(n, 1);
60   xbt_assert(res, "Memory callocation of %lu bytes failed", (unsigned long)n);
61   return res;
62 }
63
64 /** @brief like realloc, but xbt_die() on error
65     @hideinitializer */
66 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
67   void *res = NULL;
68   if (s) {
69     if (p) {
70       res = realloc(p, s);
71       xbt_assert(res, "memory (re)allocation of %lu bytes failed", (unsigned long)s);
72     } else {
73       res = xbt_malloc(s);
74     }
75   } else {
76     free(p);
77   }
78   return res;
79 }
80
81 /** @brief like free
82     @hideinitializer */
83 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
84
85 #ifdef __cplusplus
86 #define XBT_FREE_NOEXCEPT noexcept(noexcept(::free))
87 #else
88 #define XBT_FREE_NOEXCEPT
89 #endif
90
91 /** @brief like free, but you can be sure that it is a function  */
92 XBT_PUBLIC void xbt_free_f(void* p) XBT_FREE_NOEXCEPT;
93 /** @brief should be given a pointer to pointer, and frees the second one */
94 XBT_PUBLIC void xbt_free_ref(void* d) XBT_FREE_NOEXCEPT;
95
96 SG_END_DECL
97
98 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
99 /** @brief like calloc, but xbt_die() on error
100     @hideinitializer */
101 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
102
103 /** @} */
104
105 #endif                          /* XBT_SYSDEP_H */