s3:printing: Allow to run samba-bgqd as a standalone systemd service
[Samba.git] / lib / util / memory.h
blob40c66d824a1cf60e4c65981d4c8ce500c44feb61
1 /*
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 */
25 /**
26 * Free memory if the pointer and zero the pointer.
28 * @note You are explicitly allowed to pass NULL pointers -- they will
29 * always be ignored.
30 **/
31 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
32 #endif
34 /**
35 * Zero string and free memory if the pointer and zero the pointer.
37 * @note You are explicitly allowed to pass NULL pointers -- they will
38 * always be ignored.
39 **/
40 #define BURN_FREE_STR(x) do { \
41 if ((x) != NULL) { \
42 size_t s = strlen(x); \
43 memset_s((x), s, 0, s); \
44 free(x); (x) = NULL; \
45 } \
46 } while(0)
48 /**
49 * Zero and free memory if the pointer and zero the pointer.
51 * @note You are explicitly allowed to pass NULL pointers -- they will
52 * always be ignored.
53 **/
54 #define BURN_FREE(x, s) do { \
55 if ((x) != NULL) { \
56 memset_s((x), (s), 0, (s)); \
57 free(x); (x) = NULL; \
58 } \
59 } while(0)
61 /**
62 * Type-safe version of malloc. Allocated one copy of the
63 * specified data type.
65 #define malloc_p(type) (type *)malloc(sizeof(type))
67 /**
68 * Allocate an array of elements of one data type. Does type-checking.
70 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count, false)
72 /**
73 * Resize an array of elements of one data type. Does type-checking.
75 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count, false)
77 /**
78 * Zero a structure.
80 #ifndef ZERO_STRUCT
81 #define ZERO_STRUCT(x) memset_s((char *)&(x), sizeof(x), 0, sizeof(x))
82 #endif
84 /**
85 * Zero a structure given a pointer to the structure.
87 #ifndef ZERO_STRUCTP
88 #define ZERO_STRUCTP(x) do { \
89 if ((x) != NULL) { \
90 memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x))); \
91 } \
92 } while(0)
93 #endif
95 /**
96 * Zero a structure given a pointer to the structure - no zero check.
98 #ifndef ZERO_STRUCTPN
99 #define ZERO_STRUCTPN(x) memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x)))
100 #endif
103 * Zero an array - note that sizeof(array) must work - ie. it must not be a
104 * pointer.
106 #ifndef ZERO_ARRAY
107 #define ZERO_ARRAY(x) memset_s((char *)(x), sizeof(x), 0, sizeof(x))
108 #endif
111 * Zero a given len of an array
113 #define ZERO_ARRAY_LEN(x, l) memset_s((char *)(x), (l), 0, (l))
116 * Work out how many elements there are in a static array
118 #ifndef ARRAY_SIZE
119 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
120 #endif
123 * Pointer difference macro.
125 #ifndef PTR_DIFF
126 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
127 #endif
129 #endif /* _SAMBA_MEMORY_H_ */