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