s3-lib Move source3-specific malloc replacements into a seperate file
authorAndrew Bartlett <abartlet@samba.org>
Tue, 31 May 2011 00:42:30 +0000 (10:42 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 31 May 2011 00:57:19 +0000 (02:57 +0200)
This will make it easier to create a dep tree for otherwise simple
libraries.

Andrew Bartlett

source3/Makefile.in
source3/lib/util.c
source3/lib/util_malloc.c [new file with mode: 0644]
source3/wscript_build

index 7fbefda25e59457ae0eaa4d0743b3efb776b8e0f..c768a402d4837b96e8b6d5e3603575131798cde9 100644 (file)
@@ -462,7 +462,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
          ../lib/util/base64.o lib/util_sid.o \
          ../lib/util/charset/util_unistr.o \
          ../lib/util/charset/util_unistr_w.o ../lib/util/charset/codepoints.o ../lib/util/charset/util_str.o lib/util_file.o \
-         lib/util.o lib/namearray.o lib/util_cmdline.o lib/util_names.o \
+         lib/util.o lib/util_malloc.o lib/namearray.o lib/util_cmdline.o lib/util_names.o \
          lib/util_sock.o lib/sock_exec.o lib/util_sec.o \
          lib/substitute.o lib/substitute_generic.o ../lib/util/substitute.o lib/dbwrap_util.o \
          lib/ms_fnmatch.o ../lib/util/ms_fnmatch.o lib/errmap_unix.o \
index f86f51790fcd30a1a2776322c5ec3313a37e635a..173e9067cc24da3ea2f3d2d6e79666e8b662503b 100644 (file)
@@ -430,149 +430,6 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
        return status;
 }
 
-#if defined(PARANOID_MALLOC_CHECKER)
-
-/****************************************************************************
- Internal malloc wrapper. Externally visible.
-****************************************************************************/
-
-void *malloc_(size_t size)
-{
-       if (size == 0) {
-               return NULL;
-       }
-#undef malloc
-       return malloc(size);
-#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
-}
-
-/****************************************************************************
- Internal calloc wrapper. Not externally visible.
-****************************************************************************/
-
-static void *calloc_(size_t count, size_t size)
-{
-       if (size == 0 || count == 0) {
-               return NULL;
-       }
-#undef calloc
-       return calloc(count, size);
-#define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
-}
-
-/****************************************************************************
- Internal realloc wrapper. Not externally visible.
-****************************************************************************/
-
-static void *realloc_(void *ptr, size_t size)
-{
-#undef realloc
-       return realloc(ptr, size);
-#define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
-}
-
-#endif /* PARANOID_MALLOC_CHECKER */
-
-/****************************************************************************
- Type-safe memalign
-****************************************************************************/
-
-void *memalign_array(size_t el_size, size_t align, unsigned int count)
-{
-       if (count >= MAX_ALLOC_SIZE/el_size) {
-               return NULL;
-       }
-
-       return sys_memalign(align, el_size*count);
-}
-
-/****************************************************************************
- Type-safe calloc.
-****************************************************************************/
-
-void *calloc_array(size_t size, size_t nmemb)
-{
-       if (nmemb >= MAX_ALLOC_SIZE/size) {
-               return NULL;
-       }
-       if (size == 0 || nmemb == 0) {
-               return NULL;
-       }
-#if defined(PARANOID_MALLOC_CHECKER)
-       return calloc_(nmemb, size);
-#else
-       return calloc(nmemb, size);
-#endif
-}
-
-/****************************************************************************
- Expand a pointer to be a particular size.
- Note that this version of Realloc has an extra parameter that decides
- whether to free the passed in storage on allocation failure or if the
- new size is zero.
-
- This is designed for use in the typical idiom of :
-
- p = SMB_REALLOC(p, size)
- if (!p) {
-    return error;
- }
-
- and not to have to keep track of the old 'p' contents to free later, nor
- to worry if the size parameter was zero. In the case where NULL is returned
- we guarentee that p has been freed.
-
- If free later semantics are desired, then pass 'free_old_on_error' as False which
- guarentees that the old contents are not freed on error, even if size == 0. To use
- this idiom use :
-
- tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
- if (!tmp) {
-    SAFE_FREE(p);
-    return error;
- } else {
-    p = tmp;
- }
-
- Changes were instigated by Coverity error checking. JRA.
-****************************************************************************/
-
-void *Realloc(void *p, size_t size, bool free_old_on_error)
-{
-       void *ret=NULL;
-
-       if (size == 0) {
-               if (free_old_on_error) {
-                       SAFE_FREE(p);
-               }
-               DEBUG(2,("Realloc asked for 0 bytes\n"));
-               return NULL;
-       }
-
-#if defined(PARANOID_MALLOC_CHECKER)
-       if (!p) {
-               ret = (void *)malloc_(size);
-       } else {
-               ret = (void *)realloc_(p,size);
-       }
-#else
-       if (!p) {
-               ret = (void *)malloc(size);
-       } else {
-               ret = (void *)realloc(p,size);
-       }
-#endif
-
-       if (!ret) {
-               if (free_old_on_error && p) {
-                       SAFE_FREE(p);
-               }
-               DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
-       }
-
-       return(ret);
-}
-
 /****************************************************************************
  (Hopefully) efficient array append.
 ****************************************************************************/
diff --git a/source3/lib/util_malloc.c b/source3/lib/util_malloc.c
new file mode 100644 (file)
index 0000000..c052adc
--- /dev/null
@@ -0,0 +1,171 @@
+/* 
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+   Copyright (C) Andrew Tridgell 1992-1998
+   Copyright (C) Jeremy Allison 2001-2007
+   Copyright (C) Simo Sorce 2001
+   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
+   Copyright (C) James Peach 2006
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+
+/* Max allowable allococation - 256mb - 0x10000000 */
+#define MAX_ALLOC_SIZE (1024*1024*256)
+
+#if defined(PARANOID_MALLOC_CHECKER)
+
+/****************************************************************************
+ Internal malloc wrapper. Externally visible.
+****************************************************************************/
+
+void *malloc_(size_t size)
+{
+       if (size == 0) {
+               return NULL;
+       }
+#undef malloc
+       return malloc(size);
+#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal calloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *calloc_(size_t count, size_t size)
+{
+       if (size == 0 || count == 0) {
+               return NULL;
+       }
+#undef calloc
+       return calloc(count, size);
+#define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal realloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *realloc_(void *ptr, size_t size)
+{
+#undef realloc
+       return realloc(ptr, size);
+#define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
+}
+
+#endif /* PARANOID_MALLOC_CHECKER */
+
+/****************************************************************************
+ Type-safe memalign
+****************************************************************************/
+
+void *memalign_array(size_t el_size, size_t align, unsigned int count)
+{
+       if (count >= MAX_ALLOC_SIZE/el_size) {
+               return NULL;
+       }
+
+       return sys_memalign(align, el_size*count);
+}
+
+/****************************************************************************
+ Type-safe calloc.
+****************************************************************************/
+
+void *calloc_array(size_t size, size_t nmemb)
+{
+       if (nmemb >= MAX_ALLOC_SIZE/size) {
+               return NULL;
+       }
+       if (size == 0 || nmemb == 0) {
+               return NULL;
+       }
+#if defined(PARANOID_MALLOC_CHECKER)
+       return calloc_(nmemb, size);
+#else
+       return calloc(nmemb, size);
+#endif
+}
+
+/****************************************************************************
+ Expand a pointer to be a particular size.
+ Note that this version of Realloc has an extra parameter that decides
+ whether to free the passed in storage on allocation failure or if the
+ new size is zero.
+
+ This is designed for use in the typical idiom of :
+
+ p = SMB_REALLOC(p, size)
+ if (!p) {
+    return error;
+ }
+
+ and not to have to keep track of the old 'p' contents to free later, nor
+ to worry if the size parameter was zero. In the case where NULL is returned
+ we guarentee that p has been freed.
+
+ If free later semantics are desired, then pass 'free_old_on_error' as False which
+ guarentees that the old contents are not freed on error, even if size == 0. To use
+ this idiom use :
+
+ tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
+ if (!tmp) {
+    SAFE_FREE(p);
+    return error;
+ } else {
+    p = tmp;
+ }
+
+ Changes were instigated by Coverity error checking. JRA.
+****************************************************************************/
+
+void *Realloc(void *p, size_t size, bool free_old_on_error)
+{
+       void *ret=NULL;
+
+       if (size == 0) {
+               if (free_old_on_error) {
+                       SAFE_FREE(p);
+               }
+               DEBUG(2,("Realloc asked for 0 bytes\n"));
+               return NULL;
+       }
+
+#if defined(PARANOID_MALLOC_CHECKER)
+       if (!p) {
+               ret = (void *)malloc_(size);
+       } else {
+               ret = (void *)realloc_(p,size);
+       }
+#else
+       if (!p) {
+               ret = (void *)malloc(size);
+       } else {
+               ret = (void *)realloc(p,size);
+       }
+#endif
+
+       if (!ret) {
+               if (free_old_on_error && p) {
+                       SAFE_FREE(p);
+               }
+               DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
+       }
+
+       return(ret);
+}
+
index 842d95e306b7406f9b7958c773f78d9561643436..21ada043304187f25aac824962de0d4e30fc6f8a 100755 (executable)
@@ -792,7 +792,7 @@ bld.SAMBA3_SUBSYSTEM('KRBCLIENT',
 
 bld.SAMBA3_SUBSYSTEM('samba3core',
                    source=LIB_SRC,
-                   deps='LIBCRYPTO ndr security NDR_SECURITY samba-util NDR_MESSAGING LIBASYNC_REQ tdb-wrap3 UTIL_TDB UTIL_PW SAMBA_VERSION KRB5_WRAP flag_mapping util_reg PTHREADPOOL interfaces cap string_init param util_str CHARSET3 namearray SECRETS3 dbwrap_util util_sec',
+                   deps='LIBCRYPTO ndr security NDR_SECURITY samba-util NDR_MESSAGING LIBASYNC_REQ tdb-wrap3 UTIL_TDB UTIL_PW SAMBA_VERSION KRB5_WRAP flag_mapping util_reg PTHREADPOOL interfaces cap string_init param util_str CHARSET3 namearray SECRETS3 dbwrap_util util_sec util_malloc',
                    vars=locals())
 
 bld.SAMBA3_SUBSYSTEM('LIB_NONSMBD',
@@ -998,6 +998,12 @@ bld.SAMBA3_LIBRARY('dbwrap_util',
                    private_library=True,
                    allow_undefined_symbols=False)
 
+bld.SAMBA3_LIBRARY('util_malloc',
+                   source='''lib/util_malloc.c''',
+                   deps='samba-util-common',
+                   private_library=True,
+                   allow_undefined_symbols=False)
+
 bld.SAMBA3_LIBRARY('string_init',
                    source='''lib/string_init.c''',
                    deps='samba-util-common',