util_malloc: Remove unused constant.
[metze/samba/wip.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 #if defined(PARANOID_MALLOC_CHECKER)
27
28 /****************************************************************************
29  Internal malloc wrapper. Externally visible.
30 ****************************************************************************/
31
32 void *malloc_(size_t size)
33 {
34         if (size == 0) {
35                 return NULL;
36         }
37 #undef malloc
38         return malloc(size);
39 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
40 }
41
42 /****************************************************************************
43  Internal calloc wrapper. Not externally visible.
44 ****************************************************************************/
45
46 static void *calloc_(size_t count, size_t size)
47 {
48         if (size == 0 || count == 0) {
49                 return NULL;
50         }
51 #undef calloc
52         return calloc(count, size);
53 #define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
54 }
55
56 /****************************************************************************
57  Internal realloc wrapper. Not externally visible.
58 ****************************************************************************/
59
60 static void *realloc_(void *ptr, size_t size)
61 {
62 #undef realloc
63         return realloc(ptr, size);
64 #define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
65 }
66
67 #endif /* PARANOID_MALLOC_CHECKER */
68
69 /****************************************************************************
70  Expand a pointer to be a particular size.
71  Note that this version of Realloc has an extra parameter that decides
72  whether to free the passed in storage on allocation failure or if the
73  new size is zero.
74
75  This is designed for use in the typical idiom of :
76
77  p = SMB_REALLOC(p, size)
78  if (!p) {
79     return error;
80  }
81
82  and not to have to keep track of the old 'p' contents to free later, nor
83  to worry if the size parameter was zero. In the case where NULL is returned
84  we guarentee that p has been freed.
85
86  If free later semantics are desired, then pass 'free_old_on_error' as False which
87  guarentees that the old contents are not freed on error, even if size == 0. To use
88  this idiom use :
89
90  tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
91  if (!tmp) {
92     SAFE_FREE(p);
93     return error;
94  } else {
95     p = tmp;
96  }
97
98  Changes were instigated by Coverity error checking. JRA.
99 ****************************************************************************/
100
101 void *Realloc(void *p, size_t size, bool free_old_on_error)
102 {
103         void *ret=NULL;
104
105         if (size == 0) {
106                 if (free_old_on_error) {
107                         SAFE_FREE(p);
108                 }
109                 DEBUG(2,("Realloc asked for 0 bytes\n"));
110                 return NULL;
111         }
112
113 #if defined(PARANOID_MALLOC_CHECKER)
114         if (!p) {
115                 ret = (void *)malloc_(size);
116         } else {
117                 ret = (void *)realloc_(p,size);
118         }
119 #else
120         if (!p) {
121                 ret = (void *)malloc(size);
122         } else {
123                 ret = (void *)realloc(p,size);
124         }
125 #endif
126
127         if (!ret) {
128                 if (free_old_on_error && p) {
129                         SAFE_FREE(p);
130                 }
131                 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
132         }
133
134         return(ret);
135 }
136