doc
c_macro.h
Go to the documentation of this file.
1 /*
2  * cynapses libc functions
3  *
4  * Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * vim: ts=2 sw=2 et cindent
21  */
22 
23 /**
24  * @file c_macro.h
25  *
26  * @brief cynapses libc macro definitions
27  *
28  * @defgroup cynMacroInternals cynapses libc macro definitions
29  * @ingroup cynLibraryAPI
30  *
31  * @{
32  */
33 #ifndef _C_MACRO_H
34 #define _C_MACRO_H
35 
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #ifdef malloc
40 #undef malloc
41 #endif
42 #define malloc(x) DO_NOT_CALL_MALLOC__USE_C_MALLOC_INSTEAD
43 
44 #ifdef calloc
45 #undef calloc
46 #endif
47 #define calloc(x,y) DO_NOT_CALL_CALLOC__USE_C_CALLOC_INSTEAD
48 
49 #ifdef realloc
50 #undef realloc
51 #endif
52 #define realloc(x,y) DO_NOT_CALL_REALLOC__USE_C_REALLOC_INSTEAD
53 
54 #ifdef dirname
55 #undef dirname
56 #endif
57 #define dirname(x) DO_NOT_CALL_MALLOC__USE_C_DIRNAME_INSTEAD
58 
59 #ifdef basename
60 #undef basename
61 #endif
62 #define basename(x) DO_NOT_CALL_MALLOC__USE_C_BASENAME_INSTEAD
63 
64 #ifdef strdup
65 #undef strdup
66 #endif
67 #define strdup(x) DO_NOT_CALL_STRDUP__USE_C_STRDUP_INSTEAD
68 
69 #define INT_TO_POINTER(i) (void *) i
70 #define POINTER_TO_INT(p) *((int *) (p))
71 
72 /** Zero a structure */
73 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
74 
75 /** Zero a structure given a pointer to the structure */
76 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
77 
78 /** Free memory and zero the pointer */
79 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
80 
81 /** Get the smaller value */
82 #define MIN(a,b) ((a) < (b) ? (a) : (b))
83 
84 /** Get the bigger value */
85 #define MAX(a,b) ((a) < (b) ? (b) : (a))
86 
87 /** Get the size of an array */
88 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
89 
90 /**
91  * }@
92  */
93 #endif /* _C_MACRO_H */
94