2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1999
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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_MEMORY_H_
22 #define _SAMBA_MEMORY_H_
24 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
26 * Free memory if the pointer and zero the pointer.
28 * @note You are explicitly allowed to pass NULL pointers -- they will
31 #define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
35 * Type-safe version of malloc. Allocated one copy of the
36 * specified data type.
38 #define malloc_p(type) (type *)malloc(sizeof(type))
41 * Allocate an array of elements of one data type. Does type-checking.
43 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count, false)
46 * Resize an array of elements of one data type. Does type-checking.
48 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count, false)
54 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
58 * zero a structure given a pointer to the structure
61 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
65 * zero a structure given a pointer to the structure - no zero check
68 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
71 /* zero an array - note that sizeof(array) must work - ie. it must not be a
74 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
78 * work out how many elements there are in a static array
81 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
85 * pointer difference macro
88 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
93 #endif /* _SAMBA_MEMORY_H_ */