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 */
5 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
13 #include <xbt/asserts.h>
17 #include <simgrid/config.h>
21 #include <stdarg.h> /* va_list */
25 /* They live in asserts.h, but need to be declared before this module.
26 double declaration to cut dependency cycle */
28 * @addtogroup XBT_error
32 /** @brief Kill the program in silence */
33 XBT_ATTRIB_NORETURN XBT_PUBLIC void xbt_abort(void);
36 * @brief Kill the program with an error message
37 * @param ... a format string and its arguments
39 * Things are so messed up that the only thing to do now, is to stop the program.
41 * The message is handled by a CRITICAL logging request, and may consist of a format string with arguments.
43 #define xbt_die(...) \
45 XBT_CCRITICAL(xbt, __VA_ARGS__); \
50 #ifdef XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL
51 XBT_LOG_NEW_CATEGORY(xbt, "All XBT categories (simgrid toolbox)");
53 XBT_LOG_EXTERNAL_CATEGORY(xbt);
56 /** @addtogroup XBT_syscall
57 * @brief Malloc and associated functions, killing the program on error (with @ref XBT_ex)
62 /** @brief Like strdup, but xbt_die() on error */
63 static XBT_ALWAYS_INLINE char *xbt_strdup(const char *s) {
68 xbt_die("memory allocation error (strdup returned NULL)");
73 /** @brief Like malloc, but xbt_die() on error
75 static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) {
76 void* res = malloc(n);
78 xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
82 /** @brief like malloc, but xbt_die() on error and memset data to 0
84 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
85 void* res = calloc(n, 1);
87 xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
91 /** @brief like realloc, but xbt_die() on error
93 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
99 xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
111 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
114 #define XBT_FREE_NOEXCEPT noexcept(noexcept(::free))
116 #define XBT_FREE_NOEXCEPT
119 /** @brief like free, but you can be sure that it is a function */
120 XBT_PUBLIC void xbt_free_f(void* p) XBT_FREE_NOEXCEPT;
121 /** @brief should be given a pointer to pointer, and frees the second one */
122 XBT_PUBLIC void xbt_free_ref(void* d) XBT_FREE_NOEXCEPT;
126 #define xbt_new(type, count) ((type*)xbt_malloc (sizeof (type) * (count)))
127 /** @brief like calloc, but xbt_die() on error
129 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
133 #endif /* XBT_SYSDEP_H */