r14724: Rearrange some source files, install more headers.
[samba.git] / source4 / lib / util / util.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Utility functions for Samba
4    Copyright (C) Andrew Tridgell 1992-1999
5    Copyright (C) John H Terpstra 1996-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    Copyright (C) Paul Ashton 1998 - 1999
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #ifndef _SAMBA_UTIL_H_
25 #define _SAMBA_UTIL_H_
26
27 #include "core.h"
28 #include "charset/charset.h"
29
30 /**
31  * @file
32  * @brief Helpful macros
33  */
34
35 struct substitute_context;
36 struct smbsrv_tcon;
37
38 extern const char *logfile;
39 extern const char *panic_action;
40
41 #include "util/xfile.h"
42 #include "util/debug.h"
43 #include "util/mutex.h"
44 #include "util/byteorder.h"
45 #include "lib/util/util_proto.h"
46
47 /** 
48  * zero a structure 
49  */
50 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
51
52 /** 
53  * zero a structure given a pointer to the structure 
54  */
55 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
56
57 /** 
58  * zero a structure given a pointer to the structure - no zero check 
59  */
60 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
61
62 /** 
63  * pointer difference macro 
64  */
65 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
66
67 /**
68  * work out how many elements there are in a static array 
69  */
70 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
71
72 /**
73  * assert macros 
74  */
75 #define SMB_ASSERT(b) do { if (!(b)) { \
76         DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \
77         smb_panic("assert failed"); abort(); }} while (0)
78
79 /**
80  * determine the lowest of two values
81  */
82 #ifndef MIN
83 #define MIN(a,b) ((a)<(b)?(a):(b))
84 #endif
85
86 /**
87  * determine the highest of two values
88  */
89 #ifndef MAX
90 #define MAX(a,b) ((a)>(b)?(a):(b))
91 #endif
92
93 /**
94  * determine absolute value
95  */
96 #ifndef ABS
97 #define ABS(a) ((a)>0?(a):(-(a)))
98 #endif
99
100 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
101 /**
102  * Free memory if the pointer and zero the pointer.
103  *
104  * @note You are explicitly allowed to pass NULL pointers -- they will
105  * always be ignored.
106  **/
107 #define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
108 #endif
109
110 /** 
111  * Type-safe version of malloc. Allocated one copy of the 
112  * specified data type.
113  */
114 #define malloc_p(type) (type *)malloc(sizeof(type))
115
116 /**
117  * Allocate an array of elements of one data type. Does type-checking.
118  */
119 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
120
121 /** 
122  * Resize an array of elements of one data type. Does type-checking.
123  */
124 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
125
126 #if defined(VALGRIND)
127 #define strlen(x) valgrind_strlen(x)
128 #endif
129
130 /**
131   this is a warning hack. The idea is to use this everywhere that we
132   get the "discarding const" warning from gcc. That doesn't actually
133   fix the problem of course, but it means that when we do get to
134   cleaning them up we can do it by searching the code for
135   discard_const.
136
137   It also means that other error types aren't as swamped by the noise
138   of hundreds of const warnings, so we are more likely to notice when
139   we get new errors.
140
141   Please only add more uses of this macro when you find it
142   _really_ hard to fix const warnings. Our aim is to eventually use
143   this function in only a very few places.
144
145   Also, please call this via the discard_const_p() macro interface, as that
146   makes the return type safe.
147 */
148 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
149
150 /** Type-safe version of discard_const */
151 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
152
153 #endif /* _SAMBA_UTIL_H_ */