From 6e5dd2cc63635a775669d3948a2ac76d1bbc65fe Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 21 Dec 2011 01:25:38 +0000 Subject: [PATCH] fix memory crashes on win32/Eigen build --- dtool/Config.FreeBSD.pp | 4 --- dtool/Config.Linux.pp | 4 --- dtool/Config.OSX.pp | 4 --- dtool/Config.Win32.pp | 3 -- dtool/Config.Win64.pp | 3 -- dtool/Config.pp | 7 ++++ dtool/LocalSetup.pp | 40 +++-------------------- dtool/pptempl/compilerSettings.pp | 5 ++- dtool/src/dtoolbase/dtoolbase.h | 12 +++++++ dtool/src/dtoolbase/memoryHook.h | 23 ++++++++----- dtool/src/dtoolbase/ptmalloc2_smp_src.cxx | 8 ++--- makepanda/makepanda.py | 4 --- 12 files changed, 42 insertions(+), 75 deletions(-) diff --git a/dtool/Config.FreeBSD.pp b/dtool/Config.FreeBSD.pp index e324a760c1..9db29333c8 100644 --- a/dtool/Config.FreeBSD.pp +++ b/dtool/Config.FreeBSD.pp @@ -289,10 +289,6 @@ // We need 64-bit file i/o #define __USE_LARGEFILE64 1 -// Modern versions of gcc do support the latest STL allocator -// definitions. -#define USE_STL_ALLOCATOR 1 - // The dynamic library file extension (usually .so .dll or .dylib): #define DYNAMIC_LIB_EXT .so #define STATIC_LIB_EXT .a diff --git a/dtool/Config.Linux.pp b/dtool/Config.Linux.pp index e571ce15d3..35a8b99c79 100644 --- a/dtool/Config.Linux.pp +++ b/dtool/Config.Linux.pp @@ -332,10 +332,6 @@ // We need 64-bit file i/o #define __USE_LARGEFILE64 1 -// Modern versions of gcc do support the latest STL allocator -// definitions. -#define USE_STL_ALLOCATOR 1 - // The dynamic library file extension (usually .so .dll or .dylib): #define DYNAMIC_LIB_EXT .so #define STATIC_LIB_EXT .a diff --git a/dtool/Config.OSX.pp b/dtool/Config.OSX.pp index dd935c8d03..66a886950c 100644 --- a/dtool/Config.OSX.pp +++ b/dtool/Config.OSX.pp @@ -277,10 +277,6 @@ // Do we have RTTI (and )? #define HAVE_RTTI 1 -// Modern versions of gcc do support the latest STL allocator -// definitions. -#define USE_STL_ALLOCATOR 1 - // Do we have ? #define PHAVE_STDINT_H 1 diff --git a/dtool/Config.Win32.pp b/dtool/Config.Win32.pp index f6344844bc..a1394360c4 100644 --- a/dtool/Config.Win32.pp +++ b/dtool/Config.Win32.pp @@ -160,9 +160,6 @@ // Do we have ? #define PHAVE_STDINT_H -// MSVC7 does support the latest STL allocator definitions. -#define USE_STL_ALLOCATOR 1 - // can Intel C++ build this directory successfully (if not, change CC to msvc) #define NOT_INTEL_BUILDABLE false diff --git a/dtool/Config.Win64.pp b/dtool/Config.Win64.pp index 6ebfcf0145..72e43f74ef 100755 --- a/dtool/Config.Win64.pp +++ b/dtool/Config.Win64.pp @@ -160,9 +160,6 @@ // Do we have ? #define PHAVE_STDINT_H -// MSVC7 does support the latest STL allocator definitions. -#define USE_STL_ALLOCATOR 1 - // can Intel C++ build this directory successfully (if not, change CC to msvc) #define NOT_INTEL_BUILDABLE false diff --git a/dtool/Config.pp b/dtool/Config.pp index 73e2e8f4c3..c6cddc7930 100644 --- a/dtool/Config.pp +++ b/dtool/Config.pp @@ -381,6 +381,13 @@ // on DirectX rendering. #defer SUPPORT_IMMEDIATE_MODE $[<= $[OPTIMIZE], 3] +// These are two optional alternative memory-allocation schemes +// available within Panda. You can experiment with either of them to +// see if they give better performance than the system malloc(), but +// at the time of this writing, it doesn't appear that they do. +#define USE_MEMORY_DLMALLOC +#define USE_MEMORY_PTMALLOC2 + // Panda contains some experimental code to compile for IPhone. This // requires the Apple IPhone SDK, which is currently only available // for OS X platforms. Set this to either "iPhoneSimulator" or diff --git a/dtool/LocalSetup.pp b/dtool/LocalSetup.pp index c5dae86c98..b000456fc1 100644 --- a/dtool/LocalSetup.pp +++ b/dtool/LocalSetup.pp @@ -501,6 +501,10 @@ $[cdefine SIMULATE_NETWORK_DELAY] /* Define if we want to allow immediate mode OpenGL rendering. */ $[cdefine SUPPORT_IMMEDIATE_MODE] +/* Define for either of the alternative malloc schemes. */ +$[cdefine USE_MEMORY_DLMALLOC] +$[cdefine USE_MEMORY_PTMALLOC2] + /* Define if we want to compile in support for pipelining. */ $[cdefine DO_PIPELINING] @@ -695,38 +699,6 @@ $[cdefine USE_TAU] /* Define if needed to have 64-bit file i/o */ $[cdefine __USE_LARGEFILE64] -/* Which memory allocation scheme should we use? */ -#define USE_MEMORY_DLMALLOC -#define USE_MEMORY_PTMALLOC2 -#define USE_MEMORY_MALLOC -#define USE_MEMORY_NOWRAPPERS -#if $[ALTERNATIVE_MALLOC] - #if $[and $[WIN32_PLATFORM], $[HAVE_THREADS], $[not $[SIMPLE_THREADS]]] - // A fast thread-safe alternative implementation, but which only - // seems to be a good choice on Windows. (It crashes on Linux and - // isn't thread-safe on OSX). - #set USE_MEMORY_PTMALLOC2 1 - #else - // A faster, but non-thread-safe, alternative implementation. - // When threading support is compiled in, we use a global mutex to - // protect it. - #set USE_MEMORY_DLMALLOC 1 - #endif -#else - #if $[DO_MEMORY_USAGE] - // Redefine new and delete to malloc(), and also provide hooks for - // the benefit of the MemoryUsage class. - #set USE_MEMORY_MALLOC 1 - #else - // Don't redefine new and delete at all. - #set USE_MEMORY_NOWRAPPERS 1 - #endif -#endif -$[cdefine USE_MEMORY_DLMALLOC] -$[cdefine USE_MEMORY_PTMALLOC2] -$[cdefine USE_MEMORY_MALLOC] -$[cdefine USE_MEMORY_NOWRAPPERS] - // To activate the DELETED_CHAIN macros. $[cdefine USE_DELETED_CHAIN] @@ -740,10 +712,6 @@ $[cdefine WANT_NATIVE_NET] # pragma warning( disable : 4996 4275 4267 4099 4049 4013 4005 ) #endif - -/* Can we define a modern-style STL allocator? */ -$[cdefine USE_STL_ALLOCATOR] - /* Static linkage instead of the normal dynamic linkage? */ $[cdefine LINK_ALL_STATIC] diff --git a/dtool/pptempl/compilerSettings.pp b/dtool/pptempl/compilerSettings.pp index b7d53feb15..a27e735ae8 100644 --- a/dtool/pptempl/compilerSettings.pp +++ b/dtool/pptempl/compilerSettings.pp @@ -179,7 +179,7 @@ #define LDFLAGS_OPT3 /NODEFAULTLIB:MSVCRTD.LIB /OPT:REF #define LDFLAGS_OPT4 /NODEFAULTLIB:MSVCRTD.LIB /OPT:REF $[LDFLAGS_OPT4] - #define COMMONFLAGS /DHAVE_DINKUM /Zc:forScope + #define COMMONFLAGS /DHAVE_DINKUM /Zc:forScope /bigobj // use "unsafe" QIfist flt->int rounding only if FAST_FLT_TO_INT is defined #define REGULAR_OPTFLAGS /O2 /Ob2 $[if $[ne $[FAST_FLT_TO_INT],], /QIfist,] @@ -260,8 +260,7 @@ #define LDFLAGS_OPT3 /NODEFAULTLIB:MSVCRTD.LIB /OPT:REF #define LDFLAGS_OPT4 /NODEFAULTLIB:MSVCRTD.LIB /OPT:REF $[LDFLAGS_OPT4] - // use /bigobj in 64-bit environment - #define COMMONFLAGS /DHAVE_DINKUM /Zc:forScope $[if $[eq $[USE_COMPILER],MSVC9x64], /bigobj,] + #define COMMONFLAGS /DHAVE_DINKUM /Zc:forScope /bigobj // use "unsafe" QIfist flt->int rounding only if FAST_FLT_TO_INT is defined #define REGULAR_OPTFLAGS /O2 /Ob2 $[if $[ne $[FAST_FLT_TO_INT],], /QIfist,] diff --git a/dtool/src/dtoolbase/dtoolbase.h b/dtool/src/dtoolbase/dtoolbase.h index 3d4ef43094..f17612dedb 100644 --- a/dtool/src/dtoolbase/dtoolbase.h +++ b/dtool/src/dtoolbase/dtoolbase.h @@ -336,6 +336,18 @@ #define ALIGN_16BYTE #endif +/* Determine our memory-allocation requirements. */ +#if defined(USE_MEMORY_PTMALLOC2) || defined(USE_MEMORY_DLMALLOC) || defined(DO_MEMORY_USAGE) +/* If we are using our own alternative malloc, or if we're building + with DO_MEMORY_USAGE in effect, it follows we need to use our + custom STL allocator. */ +#define USE_STL_ALLOCATOR 1 +#else +/* Otherwise, if we have no custom memory management needs at all, we + might as well turn it all off and go straight to the OS-level + calls. */ +#define USE_MEMORY_NOWRAPPERS 1 +#endif /* We define the macros BEGIN_PUBLISH and END_PUBLISH to bracket diff --git a/dtool/src/dtoolbase/memoryHook.h b/dtool/src/dtoolbase/memoryHook.h index a97d3a7b02..2ee9439031 100644 --- a/dtool/src/dtoolbase/memoryHook.h +++ b/dtool/src/dtoolbase/memoryHook.h @@ -23,24 +23,31 @@ class DeletedBufferChain; + // Do we need to implement memory-alignment enforcement within the // MemoryHook class, or will the underlying malloc implementation // provide it automatically? -#if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2) -// Both of these specialized malloc implementations perform the -// required alignment. +#if !defined(LINMATH_ALIGN) +// We don't actually require any special memory-alignment beyond what +// the underlying implementation is likely to provide anyway. #undef MEMORY_HOOK_DO_ALIGN +#elif defined(USE_MEMORY_DLMALLOC) +// This specialized malloc implementation can perform the required +// alignment. +#undef MEMORY_HOOK_DO_ALIGN + +#elif defined(USE_MEMORY_PTMALLOC2) +// But not this one. For some reason it crashes when we try to build +// it with alignment 16. So if we're using ptmalloc2, we need to +// enforce alignment externally. +#define MEMORY_HOOK_DO_ALIGN 1 + #elif defined(IS_OSX) || defined(_WIN64) // The OS-provided malloc implementation will do the required // alignment. #undef MEMORY_HOOK_DO_ALIGN -#elif !defined(LINMATH_ALIGN) -// We don't actually require any special memory-alignment beyond what -// the underlying implementation is likely to provide anyway. -#undef MEMORY_HOOK_DO_ALIGN - #else // Whoops, we need memory alignment, and we have to provide it ourselves. #define MEMORY_HOOK_DO_ALIGN 1 diff --git a/dtool/src/dtoolbase/ptmalloc2_smp_src.cxx b/dtool/src/dtoolbase/ptmalloc2_smp_src.cxx index 744d8a75c5..1de71ccb98 100644 --- a/dtool/src/dtoolbase/ptmalloc2_smp_src.cxx +++ b/dtool/src/dtoolbase/ptmalloc2_smp_src.cxx @@ -749,12 +749,8 @@ extern "C" { are optimized for the case of 8-byte alignment. */ -#ifdef LINMATH_ALIGN -// drose: We require 16-byte alignment of certain structures, to -// support SSE2. We don't strictly have to align *everything*, but -// it's just easier to do so. -#define MALLOC_ALIGNMENT ((size_t)16U) -#endif +/* drose: it turns out that setting MALLOC_ALIGNMENT to 16 doesn't + work; something crashes internally. Too bad. */ #ifndef MALLOC_ALIGNMENT #define MALLOC_ALIGNMENT (2 * SIZE_SZ) diff --git a/makepanda/makepanda.py b/makepanda/makepanda.py index df2959774c..0128d38b6b 100755 --- a/makepanda/makepanda.py +++ b/makepanda/makepanda.py @@ -1526,10 +1526,6 @@ DTOOL_CONFIG=[ ("IS_OSX", 'UNDEF', 'UNDEF'), ("IS_FREEBSD", 'UNDEF', 'UNDEF'), ("GLOBAL_OPERATOR_NEW_EXCEPTIONS", 'UNDEF', '1'), - ("USE_STL_ALLOCATOR", '1', '1'), - ("USE_MEMORY_DLMALLOC", 'UNDEF', 'UNDEF'), - ("USE_MEMORY_PTMALLOC2", '1', 'UNDEF'), - ("USE_MEMORY_MALLOC", 'UNDEF', '1'), ("HAVE_EIGEN", 'UNDEF', 'UNDEF'), ("LINMATH_ALIGN", '1', '1'), ("HAVE_ZLIB", 'UNDEF', 'UNDEF'),