X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/215ed67257173f5e26c9489ec96c4ced08d1614a..8d62bb541920e2e4e0f7d1fa26a7f6eec79370c2:/include/xbt/sysdep.h diff --git a/include/xbt/sysdep.h b/include/xbt/sysdep.h index 7d41431806..160fb7e396 100644 --- a/include/xbt/sysdep.h +++ b/include/xbt/sysdep.h @@ -1,108 +1,104 @@ -/* $Id$ */ /* xbt/sysdep.h -- all system dependency */ /* no system header should be loaded out of this file so that we have only */ /* one file to check when porting to another OS */ -/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ +/* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#ifndef _XBT_SYSDEP_H -#define _XBT_SYSDEP_H +#ifndef XBT_SYSDEP_H +#define XBT_SYSDEP_H + +#include +#include + +#include #include -#include -#include -#include /* va_list */ - -#include "xbt/misc.h" -#include "xbt/asserts.h" - -SG_BEGIN_DECL() +#include +#include /* va_list */ + +SG_BEGIN_DECL + +#ifdef XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL +XBT_LOG_NEW_CATEGORY(xbt, "All XBT categories (SimGrid toolbox)"); +XBT_LOG_NEW_SUBCATEGORY(xbt_help, xbt, "Help messages"); +#else +XBT_LOG_EXTERNAL_CATEGORY(xbt); +XBT_LOG_EXTERNAL_CATEGORY(xbt_help); +#endif /** @addtogroup XBT_syscall - * @brief Malloc and associated functions, killing the program on error (with \ref XBT_ex) + * @brief Malloc and associated functions, killing the program on error (with @ref XBT_ex) * - *
Top [\ref index]::[\ref XBT_API] - *
Prev - *
Next [\ref XBT_ex]
* @{ */ -#if defined(__GNUC__) || defined(DOXYGEN) /** @brief Like strdup, but xbt_die() on error */ -static inline char *xbt_strdup(const char *s) { +static XBT_ALWAYS_INLINE char *xbt_strdup(const char *s) { char *res = NULL; if (s) { - res=strdup(s); - if (!res) - xbt_die("memory allocation error"); - } + res = strdup(s); + xbt_assert(res, "memory allocation error (strdup returned NULL)"); + } return res; } -/** @brief Like malloc, but xbt_die() on error + +/** @brief Like malloc, but xbt_die() on error @hideinitializer */ -static inline void *xbt_malloc(int n){ - void *res=malloc(n); - if (!res) - xbt_die("Memory allocation failed"); +static XBT_ALWAYS_INLINE void *xbt_malloc(size_t n) { + void* res = malloc(n); + xbt_assert(res, "Memory allocation of %lu bytes failed", (unsigned long)n); return res; } /** @brief like malloc, but xbt_die() on error and memset data to 0 @hideinitializer */ -static inline void *xbt_malloc0(int n) { - void *res=calloc(n,1); - if (!res) - xbt_die("Memory callocation failed"); +static XBT_ALWAYS_INLINE void *xbt_malloc0(size_t n) { + void* res = calloc(n, 1); + xbt_assert(res, "Memory callocation of %lu bytes failed", (unsigned long)n); return res; } - -/** @brief like realloc, but xbt_die() on error + +/** @brief like realloc, but xbt_die() on error @hideinitializer */ -static inline void *xbt_realloc(void*p,int s){ - void *res=res; +static XBT_ALWAYS_INLINE void *xbt_realloc(void *p, size_t s) { + void *res = NULL; if (s) { if (p) { - res=realloc(p,s); - if (!res) - xbt_die("memory allocation error"); + res = realloc(p, s); + xbt_assert(res, "memory (re)allocation of %lu bytes failed", (unsigned long)s); } else { - res=xbt_malloc(s); + res = xbt_malloc(s); } } else { - if (p) { - free(p); - } + free(p); } return res; } -#else /* non __GNUC__ */ -# define xbt_strdup(s) strdup(s) -# define xbt_malloc(n) malloc(n) -# define xbt_malloc0(n) calloc(n,1) -# define xbt_realloc(p,s) realloc(p,s) -#endif /* __GNUC__ ? */ - -/** @brief like free - @hideinitializer */ -#define xbt_free free /*nothing specific to do here. A poor valgrind replacement?*/ -/*#define xbt_free_fct free * replacement with the guareenty of being a function FIXME:KILLME*/ - -/** @brief like calloc, but xbt_die() on error and don't memset to 0 - @hideinitializer */ + +/** @brief like free */ +#define xbt_free(p) free(p) /*nothing specific to do here. A poor valgrind replacement? */ + +#ifdef __cplusplus +#define XBT_FREE_NOEXCEPT noexcept(noexcept(::free)) +#else +#define XBT_FREE_NOEXCEPT +#endif + +/** @brief like free, but you can be sure that it is a function */ +XBT_PUBLIC void xbt_free_f(void* p) XBT_FREE_NOEXCEPT; +/** @brief should be given a pointer to pointer, and frees the second one */ +XBT_PUBLIC void xbt_free_ref(void* d) XBT_FREE_NOEXCEPT; + +SG_END_DECL + #define xbt_new(type, count) ((type*)xbt_malloc (sizeof (type) * (count))) /** @brief like calloc, but xbt_die() on error @hideinitializer */ #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count))) -/** @} */ - -/* FIXME: better place? */ -int vasprintf (char **ptr, const char *fmt, va_list ap); -char *bprintf(const char*fmt, ...) _XBT_GNUC_PRINTF(1,2); - -SG_END_DECL() +/** @} */ -#endif /* _XBT_SYSDEP_H */ +#endif /* XBT_SYSDEP_H */