s3-passdb: Keep caches coherent
[idra/samba.git] / source3 / lib / util_malloc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2001-2007
6    Copyright (C) Simo Sorce 2001
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8    Copyright (C) James Peach 2006
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25
26 /* Max allowable allococation - 256mb - 0x10000000 */
27 #define MAX_ALLOC_SIZE (1024*1024*256)
28
29 #if defined(PARANOID_MALLOC_CHECKER)
30
31 /****************************************************************************
32  Internal malloc wrapper. Externally visible.
33 ****************************************************************************/
34
35 void *malloc_(size_t size)
36 {
37         if (size == 0) {
38                 return NULL;
39         }
40 #undef malloc
41         return malloc(size);
42 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
43 }
44
45 /****************************************************************************
46  Internal calloc wrapper. Not externally visible.
47 ****************************************************************************/
48
49 static void *calloc_(size_t count, size_t size)
50 {
51         if (size == 0 || count == 0) {
52                 return NULL;
53         }
54 #undef calloc
55         return calloc(count, size);
56 #define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
57 }
58
59 /****************************************************************************
60  Internal realloc wrapper. Not externally visible.
61 ****************************************************************************/
62
63 static void *realloc_(void *ptr, size_t size)
64 {
65 #undef realloc
66         return realloc(ptr, size);
67 #define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
68 }
69
70 #endif /* PARANOID_MALLOC_CHECKER */
71
72 /****************************************************************************
73  Type-safe memalign
74 ****************************************************************************/
75
76 void *memalign_array(size_t el_size, size_t align, unsigned int count)
77 {
78         if (count >= MAX_ALLOC_SIZE/el_size) {
79                 return NULL;
80         }
81
82         return sys_memalign(align, el_size*count);
83 }
84
85 /****************************************************************************
86  Type-safe calloc.
87 ****************************************************************************/
88
89 void *calloc_array(size_t size, size_t nmemb)
90 {
91         if (nmemb >= MAX_ALLOC_SIZE/size) {
92                 return NULL;
93         }
94         if (size == 0 || nmemb == 0) {
95                 return NULL;
96         }
97 #if defined(PARANOID_MALLOC_CHECKER)
98         return calloc_(nmemb, size);
99 #else
100         return calloc(nmemb, size);
101 #endif
102 }
103
104 /****************************************************************************
105  Expand a pointer to be a particular size.
106  Note that this version of Realloc has an extra parameter that decides
107  whether to free the passed in storage on allocation failure or if the
108  new size is zero.
109
110  This is designed for use in the typical idiom of :
111
112  p = SMB_REALLOC(p, size)
113  if (!p) {
114     return error;
115  }
116
117  and not to have to keep track of the old 'p' contents to free later, nor
118  to worry if the size parameter was zero. In the case where NULL is returned
119  we guarentee that p has been freed.
120
121  If free later semantics are desired, then pass 'free_old_on_error' as False which
122  guarentees that the old contents are not freed on error, even if size == 0. To use
123  this idiom use :
124
125  tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
126  if (!tmp) {
127     SAFE_FREE(p);
128     return error;
129  } else {
130     p = tmp;
131  }
132
133  Changes were instigated by Coverity error checking. JRA.
134 ****************************************************************************/
135
136 void *Realloc(void *p, size_t size, bool free_old_on_error)
137 {
138         void *ret=NULL;
139
140         if (size == 0) {
141                 if (free_old_on_error) {
142                         SAFE_FREE(p);
143                 }
144                 DEBUG(2,("Realloc asked for 0 bytes\n"));
145                 return NULL;
146         }
147
148 #if defined(PARANOID_MALLOC_CHECKER)
149         if (!p) {
150                 ret = (void *)malloc_(size);
151         } else {
152                 ret = (void *)realloc_(p,size);
153         }
154 #else
155         if (!p) {
156                 ret = (void *)malloc(size);
157         } else {
158                 ret = (void *)realloc(p,size);
159         }
160 #endif
161
162         if (!ret) {
163                 if (free_old_on_error && p) {
164                         SAFE_FREE(p);
165                 }
166                 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
167         }
168
169         return(ret);
170 }
171