~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to lib/util/memory.h

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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_MEMORY_H_
 
22
#define _SAMBA_MEMORY_H_
 
23
 
 
24
#ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
 
25
/**
 
26
 * Free memory if the pointer and zero the pointer.
 
27
 *
 
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(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
 
32
#endif
 
33
 
 
34
/** 
 
35
 * Type-safe version of malloc. Allocated one copy of the 
 
36
 * specified data type.
 
37
 */
 
38
#define malloc_p(type) (type *)malloc(sizeof(type))
 
39
 
 
40
/**
 
41
 * Allocate an array of elements of one data type. Does type-checking.
 
42
 */
 
43
#define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count, false)
 
44
 
 
45
/** 
 
46
 * Resize an array of elements of one data type. Does type-checking.
 
47
 */
 
48
#define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count, false)
 
49
 
 
50
/** 
 
51
 * zero a structure 
 
52
 */
 
53
#ifndef ZERO_STRUCT
 
54
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
 
55
#endif
 
56
 
 
57
/** 
 
58
 * zero a structure given a pointer to the structure 
 
59
 */
 
60
#ifndef ZERO_STRUCTP
 
61
#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
 
62
#endif
 
63
 
 
64
/** 
 
65
 * zero a structure given a pointer to the structure - no zero check 
 
66
 */
 
67
#ifndef ZERO_STRUCTPN
 
68
#define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
 
69
#endif
 
70
 
 
71
/* zero an array - note that sizeof(array) must work - ie. it must not be a
 
72
   pointer */
 
73
#ifndef ZERO_ARRAY
 
74
#define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
 
75
#endif
 
76
 
 
77
/**
 
78
 * work out how many elements there are in a static array 
 
79
 */
 
80
#ifndef ARRAY_SIZE
 
81
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 
82
#endif
 
83
 
 
84
/** 
 
85
 * pointer difference macro 
 
86
 */
 
87
#ifndef PTR_DIFF
 
88
#define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
 
89
#endif
 
90
 
 
91
/**
 
92
  this is a warning hack. The idea is to use this everywhere that we
 
93
  get the "discarding const" warning from gcc. That doesn't actually
 
94
  fix the problem of course, but it means that when we do get to
 
95
  cleaning them up we can do it by searching the code for
 
96
  discard_const.
 
97
 
 
98
  It also means that other error types aren't as swamped by the noise
 
99
  of hundreds of const warnings, so we are more likely to notice when
 
100
  we get new errors.
 
101
 
 
102
  Please only add more uses of this macro when you find it
 
103
  _really_ hard to fix const warnings. Our aim is to eventually use
 
104
  this function in only a very few places.
 
105
 
 
106
  Also, please call this via the discard_const_p() macro interface, as that
 
107
  makes the return type safe.
 
108
*/
 
109
#ifndef discard_const
 
110
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
 
111
#endif
 
112
 
 
113
/** Type-safe version of discard_const */
 
114
#ifndef discard_const_p
 
115
#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
 
116
#endif
 
117
 
 
118
#endif /* _SAMBA_MEMORY_H_ */