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-2023. 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>
16 #include <simgrid/config.h>
20 #include <stdarg.h> /* va_list */
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");
28 XBT_LOG_EXTERNAL_CATEGORY(xbt);
29 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
32 /** @addtogroup XBT_syscall
33 * @brief Malloc and associated functions, killing the program on error (with @ref XBT_ex)
38 /** @brief Like strdup, but xbt_die() on error */
39 static XBT_ALWAYS_INLINE char *xbt_strdup(const char *s) {
43 xbt_assert(res, "memory allocation error (strdup returned NULL)");
48 /** @brief Like malloc, but xbt_die() on error
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);
56 /** @brief like malloc, but xbt_die() on error and memset data to 0
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);
64 /** @brief like realloc, but xbt_die() on error
66 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
71 xbt_assert(res, "memory (re)allocation of %lu bytes failed", (unsigned long)s);
81 /** @brief like free */
82 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
85 #define XBT_FREE_NOEXCEPT noexcept(noexcept(::free))
87 #define XBT_FREE_NOEXCEPT
90 /** @brief like free, but you can be sure that it is a function */
91 XBT_PUBLIC void xbt_free_f(void* p) XBT_FREE_NOEXCEPT;
92 /** @brief should be given a pointer to pointer, and frees the second one */
93 XBT_PUBLIC void xbt_free_ref(void* d) XBT_FREE_NOEXCEPT;
97 #define xbt_new(type, count) ((type*)xbt_malloc (sizeof (type) * (count)))
98 /** @brief like calloc, but xbt_die() on error
100 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
104 #endif /* XBT_SYSDEP_H */