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-2021. 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) {
44 xbt_die("memory allocation error (strdup returned NULL)");
49 /** @brief Like malloc, but xbt_die() on error
51 static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) {
52 void* res = malloc(n);
54 xbt_die("Memory allocation of %lu bytes failed", (unsigned long)n);
58 /** @brief like malloc, but xbt_die() on error and memset data to 0
60 static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) {
61 void* res = calloc(n, 1);
63 xbt_die("Memory callocation of %lu bytes failed", (unsigned long)n);
67 /** @brief like realloc, but xbt_die() on error
69 static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) {
75 xbt_die("memory (re)allocation of %lu bytes failed", (unsigned long)s);
87 #define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */
90 #define XBT_FREE_NOEXCEPT noexcept(noexcept(::free))
92 #define XBT_FREE_NOEXCEPT
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;
102 #define xbt_new(type, count) ((type*)xbt_malloc (sizeof (type) * (count)))
103 /** @brief like calloc, but xbt_die() on error
105 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
109 #endif /* XBT_SYSDEP_H */