Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59df441b654486a3b53fb456146b017335c0d225
[simgrid.git] / include / xbt / sysdep.h
1 /* $Id$ */
2
3 /* gras/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 /* Authors: Martin Quinson                                                  */
8 /* Copyright (C) 2004 the OURAGAN project.                                  */
9
10 /* This program is free software; you can redistribute it and/or modify it
11    under the terms of the license (GNU LGPL) which comes with this package. */
12
13
14 #ifndef _GRAS_SYSDEP_H
15 #define _GRAS_SYSDEP_H
16
17 #include <string.h> /* Included directly for speed */
18
19 #include <time.h> /* FIXME: remove */
20 #include <unistd.h> /* FIXME: remove */
21 #include <stdlib.h> 
22
23 #include "xbt/misc.h"
24 BEGIN_DECL
25 #if 0
26 #define __CALLOC_OP(n, s)  calloc((n), (s))
27   #define __REALLOC_OP(n, s)  realloc((n), (s))
28 #define CALLOC(n, s)  ((__CALLOC_OP  ((n)?:(FAILURE("attempt to alloc 0 bytes"), 0), (s)?:(FAILURE("attempt to alloc 0 bytes"), 0)))?:(FAILURE("memory allocation error"), NULL))
29   /* #define REALLOC(p, s) ((__REALLOC_OP ((p), (s)?:(FAILURE("attempt to alloc 0 bytes"), 0)))?:(FAILURE("memory reallocation error"), NULL)) */
30   #define REALLOC(p, s) (__REALLOC_OP ((p), (s)))
31 #endif
32   
33 #define gras_strdup(s)  ((s)?(strdup(s)?:(gras_die("memory allocation error"),NULL))\
34                             :(NULL))
35 #define gras_malloc(n)   (malloc(n) ?: (gras_die("memory allocation error"),NULL))
36 #define gras_malloc0(n)  (calloc( (n),1 ) ?: (gras_die("memory allocation error"),NULL))
37 #define gras_realloc(p,s) (s? (p? (realloc(p,s)?:gras_die("memory allocation error"),NULL) \
38                                 : gras_malloc(s)) \
39                             : (p? (free(p),NULL) \
40                                 : NULL))
41 #define gras_free free /*nothing specific to do here. A poor valgrind replacement?*/
42 #define gras_free_fct free /* replacement with the guareenty of being a function */
43    
44 #define gras_new(type, count)  ((type*)gras_malloc (sizeof (type) * (count)))
45 #define gras_new0(type, count) ((type*)gras_malloc0 (sizeof (type) * (count)))
46
47 /* Attributes are only in recent versions of GCC */
48
49 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
50 # define _GRAS_GNUC_PRINTF( format_idx, arg_idx )    \
51            __attribute__((__format__ (__printf__, format_idx, arg_idx)))
52 # define _GRAS_GNUC_SCANF( format_idx, arg_idx )     \
53                __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
54 # define _GRAS_GNUC_FORMAT( arg_idx )                \
55                    __attribute__((__format_arg__ (arg_idx)))
56 # define _GRAS_GNUC_NORETURN __attribute__((__noreturn__))
57
58 #else   /* !__GNUC__ */
59 # define _GRAS_GNUC_PRINTF( format_idx, arg_idx )
60 # define _GRAS_GNUC_SCANF( format_idx, arg_idx )
61 # define _GRAS_GNUC_FORMAT( arg_idx )
62 # define _GRAS_GNUC_NORETURN
63
64 #endif  /* !__GNUC__ */
65
66 /* inline and __FUNCTION__ are only in GCC when -ansi is of */
67
68 #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
69
70 # define _GRAS_GNUC_FUNCTION __FUNCTION__
71 # define _GRAS_INLINE inline
72 #else
73 # define _GRAS_GNUC_FUNCTION "function"
74 # define _GRAS_INLINE 
75 #endif
76
77
78 void gras_abort(void) _GRAS_GNUC_NORETURN;
79
80 /* FIXME: This is a very good candidate to rewrite (along with a proper string stuff) 
81    but I'm too lazy right now, so copy the definition */
82 long int strtol(const char *nptr, char **endptr, int base);
83 double strtod(const char *nptr, char **endptr);
84 int atoi(const char *nptr);
85
86 END_DECL   
87
88 #endif /* _GRAS_SYSDEP_H */