2 Unix SMB/CIFS implementation.
3 Utility functions for Samba
4 Copyright (C) Andrew Tridgell 1992-1999
5 Copyright (C) Jelmer Vernooij 2005
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.
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.
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/>.
21 #ifndef _SAMBA_UTIL_H_
22 #define _SAMBA_UTIL_H_
24 #include "charset/charset.h"
28 * @brief Helpful macros
31 struct substitute_context;
34 extern const char *logfile;
35 extern const char *panic_action;
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"
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
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
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.
58 Also, please call this via the discard_const_p() macro interface, as that
59 makes the return type safe.
62 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
65 /** Type-safe version of discard_const */
66 #ifndef discard_const_p
67 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
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)
77 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
79 * Free memory if the pointer and zero the pointer.
81 * @note You are explicitly allowed to pass NULL pointers -- they will
84 #define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
88 * Type-safe version of malloc. Allocated one copy of the
89 * specified data type.
91 #define malloc_p(type) (type *)malloc(sizeof(type))
94 * Allocate an array of elements of one data type. Does type-checking.
96 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
99 * Resize an array of elements of one data type. Does type-checking.
101 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
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__)
107 #if defined(VALGRIND)
108 #define strlen(x) valgrind_strlen(x)
115 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
119 * zero a structure given a pointer to the structure
122 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
126 * zero a structure given a pointer to the structure - no zero check
128 #ifndef ZERO_STRUCTPN
129 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
132 /* zero an array - note that sizeof(array) must work - ie. it must not be a
135 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
139 * work out how many elements there are in a static array
142 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
146 * pointer difference macro
149 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
152 #endif /* _SAMBA_UTIL_H_ */