Bläddra i källkod

integrate dlmalloc etc. with ppremake

David Rose 20 år sedan
förälder
incheckning
5f3b0fc55b
4 ändrade filer med 59 tillägg och 3 borttagningar
  1. 10 0
      dtool/Config.pp
  2. 28 0
      dtool/LocalSetup.pp
  3. 19 2
      dtool/src/dtoolbase/dtoolbase.cxx
  4. 2 1
      dtool/src/dtoolbase/dtoolbase_cc.h

+ 10 - 0
dtool/Config.pp

@@ -326,6 +326,16 @@
 // increase run-time overhead.
 #defer DO_PIPELINING $[<= $[OPTIMIZE], 1]
 
+// Do you want to use one of the alternative malloc implementations?
+// This is almost always a good idea on Windows, where the standard
+// malloc implementation appears to be pretty poor, but probably
+// doesn't matter much on Linux (which is likely to implement
+// 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]]
+
 // Is NSPR installed, and where?  This is the Netscape Portable
 // Runtime library, downloadable as part of the Mozilla package from
 // mozilla.org.  It provides portable threading and networking

+ 28 - 0
dtool/LocalSetup.pp

@@ -431,6 +431,34 @@ $[cdefine HAVE_RTTI]
 /* Must global operator new and delete functions throw exceptions? */
 $[cdefine GLOBAL_OPERATOR_NEW_EXCEPTIONS]
 
+/* 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 $[HAVE_THREADS]
+    // A fast thread-safe alternative implementation.
+    #set USE_MEMORY_PTMALLOC2 1
+  #else
+    // A faster, but non-thread-safe, alternative implementation.
+    #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]
+
 /* What style STL allocator should we declare? */
 #define OLD_STYLE_ALLOCATOR
 #define GNU_STYLE_ALLOCATOR

+ 19 - 2
dtool/src/dtoolbase/dtoolbase.cxx

@@ -29,6 +29,10 @@
 
 #if defined(USE_MEMORY_DLMALLOC)
 
+#ifdef HAVE_THREADS
+#error Cannot use dlmalloc library with threading enabled!
+#endif
+
 #define USE_DL_PREFIX 1
 #define NO_MALLINFO 1
 #include "dlmalloc.h"
@@ -85,14 +89,14 @@ void (*global_operator_delete)(void *ptr) = &default_operator_delete;
 
 /////////////////////////////////////////////////////////////////////
 //
-// Memory manager: NONE
+// Memory manager: MALLOC
 //
 // This option uses the built-in system allocator.  This is a good
 // choice on linux, but it's a terrible choice on windows.
 //
 /////////////////////////////////////////////////////////////////////
 
-#else
+#elif defined(USE_MEMORY_MALLOC)
 
 void *default_operator_new(size_t size) {
   void *ptr = malloc(size);
@@ -110,4 +114,17 @@ void default_operator_delete(void *ptr) {
 void *(*global_operator_new)(size_t size) = &default_operator_new;
 void (*global_operator_delete)(void *ptr) = &default_operator_delete;
 
+/////////////////////////////////////////////////////////////////////
+//
+// Memory manager: NOWRAPPERS
+//
+// Not only do we use the built-in system definitions for new and
+// delete, but we don't even wrap them.  This removes a tiny bit of
+// extra overhead from the above option, but prevents Panda's
+// MemoryUsage class from being able to track memory allocations.
+//
+/////////////////////////////////////////////////////////////////////
+
+#else
+
 #endif

+ 2 - 1
dtool/src/dtoolbase/dtoolbase_cc.h

@@ -139,7 +139,7 @@ typedef ios::seekdir ios_seekdir;
 // Now redefine global operators new and delete so we can optionally
 // provide custom handlers for them.  The MemoryUsage class in Panda
 // takes advantage of this to track the size of allocated pointers.
-
+#ifndef USE_MEMORY_NOWRAPPERS
 EXPCL_DTOOL void *default_operator_new(size_t size);
 EXPCL_DTOOL void default_operator_delete(void *ptr);
 
@@ -183,5 +183,6 @@ INLINE void operator delete[](void *ptr) {
 }
 
 #endif  // GLOBAL_OPERATOR_NEW_EXCEPTIONS
+#endif  // USE_MEMORY_NOWRAPPERS
 
 #endif