Logo AND Algorithmique Numérique Distribuée

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