Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Typos : a lot of mecanism -> mechanism and functional -> functionnal
[simgrid.git] / include / xbt / sysdep.h
1 /* $Id$ */
2
3 /*  xbt/sysdep.h -- all system dependency                                   */
4 /*  no system header should be loaded out of this file so that we have only */
5 /*  one file to check when porting to another OS                            */
6
7 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #ifndef _XBT_SYSDEP_H
13 #define _XBT_SYSDEP_H
14
15 #include <string.h>
16 #include <stdlib.h> 
17    
18 #include "xbt/misc.h"
19 BEGIN_DECL()
20
21 /** @addtogroup XBT_syscall
22  *  @{
23  */
24
25 /** @brief like strdup, but xbt_die() on error 
26     @hideinitializer */
27 #define xbt_strdup(s)  ((s)?(strdup(s)?:(xbt_die("memory allocation error"),NULL))\
28                             :(NULL))
29 /** @brief like malloc, but xbt_die() on error 
30     @hideinitializer */
31 #define xbt_malloc(n)   (malloc(n) ?: (xbt_die("memory allocation error"),NULL))
32
33 /** @brief like malloc, but xbt_die() on error and memset data to 0
34     @hideinitializer */
35 #define xbt_malloc0(n)  (calloc( (n),1 ) ?: (xbt_die("memory allocation error"),NULL))
36   
37 /** @brief like realloc, but xbt_die() on error 
38     @hideinitializer */
39 #define xbt_realloc(p,s) (s? (p? (realloc(p,s)?:(xbt_die("memory allocation error"),NULL)) \
40                                 : xbt_malloc(s)) \
41                             : (p? (free(p),NULL) \
42                                 : NULL))
43 /** @brief like free
44     @hideinitializer */
45 #define xbt_free free /*nothing specific to do here. A poor valgrind replacement?*/
46 /*#define xbt_free_fct free * replacement with the guareenty of being a function  FIXME:KILLME*/
47    
48 /** @brief like calloc, but xbt_die() on error and don't memset to 0
49     @hideinitializer */
50 #define xbt_new(type, count)  ((type*)xbt_malloc (sizeof (type) * (count)))
51 /** @brief like calloc, but xbt_die() on error
52     @hideinitializer */
53 #define xbt_new0(type, count) ((type*)xbt_malloc0 (sizeof (type) * (count)))
54
55 /** @} */
56   
57 /* Attributes are only in recent versions of GCC */
58 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
59 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )    \
60            __attribute__((__format__ (__printf__, format_idx, arg_idx)))
61 # define _XBT_GNUC_SCANF( format_idx, arg_idx )     \
62                __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
63 # define _XBT_GNUC_FORMAT( arg_idx )                \
64                    __attribute__((__format_arg__ (arg_idx)))
65 # define _XBT_GNUC_NORETURN __attribute__((__noreturn__))
66
67 #else   /* !__GNUC__ */
68 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )
69 # define _XBT_GNUC_SCANF( format_idx, arg_idx )
70 # define _XBT_GNUC_FORMAT( arg_idx )
71 # define _XBT_GNUC_NORETURN
72
73 #endif  /* !__GNUC__ */
74
75 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
76
77 #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
78
79 # define _XBT_GNUC_FUNCTION __FUNCTION__
80 # define _XBT_INLINE inline
81 #else
82 # define _XBT_GNUC_FUNCTION "function"
83 # define _XBT_INLINE 
84 #endif
85
86 END_DECL()
87    
88 #include "xbt/error.h" /* needed for xbt_die */
89
90 #endif /* _XBT_SYSDEP_H */