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