From 237d193cac7e6e2ea63dc4c1f854e12cf5b725f4 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 30 Sep 2005 19:49:25 +0000 Subject: [PATCH] fix malloc issues on Linux --- dtool/Config.pp | 6 ++++-- dtool/src/cppparser/cppPreprocessor.cxx | 9 +++++++++ dtool/src/dtoolbase/Sources.pp | 3 ++- dtool/src/dtoolbase/dlmalloc.c | 8 ++++++++ dtool/src/dtoolbase/dtoolbase.cxx | 11 +++++++---- dtool/src/dtoolbase/dtoolbase.h | 2 ++ dtool/src/dtoolbase/ptmalloc2_smp.c | 19 +++++++++++++++++-- 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/dtool/Config.pp b/dtool/Config.pp index 95669a1480..bcd0d1d11f 100644 --- a/dtool/Config.pp +++ b/dtool/Config.pp @@ -333,8 +333,10 @@ // ptmalloc2 anyway). We always define this by default on Windows; on // Linux, we define it by default only when DO_MEMORY_USAGE is enabled // (since in that case, we'll be paying the overhead for the extra -// call anyway). -#defer ALTERNATIVE_MALLOC $[or $[WINDOWS_PLATFORM],$[DO_MEMORY_USAGE]] +// call anyway) or when HAVE_THREADS is not defined (since the +// non-thread-safe dlmalloc is a tiny bit faster than the system +// library). +#defer ALTERNATIVE_MALLOC $[or $[WINDOWS_PLATFORM],$[DO_MEMORY_USAGE],$[not $[HAVE_THREADS]]] // Is NSPR installed, and where? This is the Netscape Portable // Runtime library, downloadable as part of the Mozilla package from diff --git a/dtool/src/cppparser/cppPreprocessor.cxx b/dtool/src/cppparser/cppPreprocessor.cxx index e35013cbb5..33b0130445 100644 --- a/dtool/src/cppparser/cppPreprocessor.cxx +++ b/dtool/src/cppparser/cppPreprocessor.cxx @@ -102,7 +102,16 @@ InputFile() { CPPPreprocessor::InputFile:: ~InputFile() { if (_in != NULL) { + // For some reason--compiler bug in gcc 3.2?--explicitly deleting + // the stream pointer does not call the appropriate global delete + // function; instead apparently calling the system delete + // function. So we call the delete function by hand instead. +#ifndef USE_MEMORY_NOWRAPPERS + _in->~istream(); + (*global_operator_delete)(_in); +#else delete _in; +#endif } } diff --git a/dtool/src/dtoolbase/Sources.pp b/dtool/src/dtoolbase/Sources.pp index 9556234fe3..a4df00ca33 100644 --- a/dtool/src/dtoolbase/Sources.pp +++ b/dtool/src/dtoolbase/Sources.pp @@ -10,7 +10,8 @@ nearly_zero.h \ stl_compares.I stl_compares.h \ pallocator.T pallocator.h \ - pdeque.h plist.h pmap.h pset.h pvector.h + pdeque.h plist.h pmap.h pset.h pvector.h \ + dlmalloc.c ptmalloc2_smp.c #define INSTALL_HEADERS \ cmath.I cmath.h \ diff --git a/dtool/src/dtoolbase/dlmalloc.c b/dtool/src/dtoolbase/dlmalloc.c index e4247ba8a3..ab17d49152 100644 --- a/dtool/src/dtoolbase/dlmalloc.c +++ b/dtool/src/dtoolbase/dlmalloc.c @@ -1,3 +1,9 @@ + +#include "dtoolbase.h" + +#ifdef USE_MEMORY_DLMALLOC +#define USE_DL_PREFIX 1 + /* This is a version (aka dlmalloc) of malloc/free/realloc written by Doug Lea and released to the public domain, as explained at @@ -5059,3 +5065,5 @@ History: structure of old version, but most details differ.) */ + +#endif // USE_MEMORY_DLMALLOC diff --git a/dtool/src/dtoolbase/dtoolbase.cxx b/dtool/src/dtoolbase/dtoolbase.cxx index 1f248935bd..4f31e7cea5 100644 --- a/dtool/src/dtoolbase/dtoolbase.cxx +++ b/dtool/src/dtoolbase/dtoolbase.cxx @@ -36,7 +36,6 @@ #define USE_DL_PREFIX 1 #define NO_MALLINFO 1 #include "dlmalloc.h" -#include "dlmalloc.c" void *default_operator_new(size_t size) { void *ptr = dlmalloc(size); @@ -66,10 +65,14 @@ void (*global_operator_delete)(void *ptr) = &default_operator_delete; // ///////////////////////////////////////////////////////////////////// -#elif defined(USE_MEMORY_PTMALLOC2) +#elif defined(USE_MEMORY_PTMALLOC2) && !defined(linux) +// This doesn't appear to work in Linux; perhaps it is clashing with +// the system library. On Linux, fall through to the next case +// instead. #define USE_DL_PREFIX 1 -#include "ptmalloc2_smp.c" +#define NO_MALLINFO 1 +#include "dlmalloc.h" void *default_operator_new(size_t size) { void *ptr = dlmalloc(size); @@ -96,7 +99,7 @@ void (*global_operator_delete)(void *ptr) = &default_operator_delete; // ///////////////////////////////////////////////////////////////////// -#elif defined(USE_MEMORY_MALLOC) +#elif defined(USE_MEMORY_MALLOC) || defined(USE_MEMORY_PTMALLOC2) void *default_operator_new(size_t size) { void *ptr = malloc(size); diff --git a/dtool/src/dtoolbase/dtoolbase.h b/dtool/src/dtoolbase/dtoolbase.h index 9e6ab95854..6266b60924 100644 --- a/dtool/src/dtoolbase/dtoolbase.h +++ b/dtool/src/dtoolbase/dtoolbase.h @@ -100,6 +100,7 @@ #include #endif +/* #ifdef HAVE_MALLOC_H #include #endif @@ -107,6 +108,7 @@ #ifdef HAVE_SYS_MALLOC_H #include #endif +*/ #ifdef HAVE_ALLOCA_H #include diff --git a/dtool/src/dtoolbase/ptmalloc2_smp.c b/dtool/src/dtoolbase/ptmalloc2_smp.c index a21814b28f..128ceef0e4 100644 --- a/dtool/src/dtoolbase/ptmalloc2_smp.c +++ b/dtool/src/dtoolbase/ptmalloc2_smp.c @@ -1,3 +1,12 @@ +/* drose: Note that this file is released under an unrestricted + license as well as the LGPL, in spite of the comments below. See + http://www.malloc.de . */ + +#include "dtoolbase.h" + +#if defined(USE_MEMORY_PTMALLOC2) && !defined(linux) +#define USE_DL_PREFIX 1 + /* Malloc implementation for multiple threads without lock contention. Copyright (C) 1996,1997,1998,1999,2000,01,02 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -2239,7 +2248,7 @@ struct malloc_chunk { }; -typedef struct malloc_chunk* mchunkptr; +/*typedef struct malloc_chunk* mchunkptr;*/ /* malloc_chunk details: @@ -2859,7 +2868,7 @@ struct malloc_par { char* sbrk_base; }; -typedef struct malloc_state *mstate; +/*typedef struct malloc_state *mstate;*/ /* There are several instances of this struct ("arenas") in this malloc. If you are adapting this malloc in a way that does NOT use @@ -5311,6 +5320,9 @@ mremap_chunk(p, new_size) mchunkptr p; size_t new_size; /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */ new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask; +#ifndef MREMAP_MAYMOVE +#define MREMAP_MAYMOVE 1 /* terrible hack--drose */ +#endif cp = (char *)mremap((char *)p - offset, size + offset, new_size, MREMAP_MAYMOVE); @@ -8207,3 +8219,6 @@ History: structure of old version, but most details differ.) */ + +#endif // USE_MEMORY_PTMALLOC2 +