Browse Source

add dallocator

David Rose 24 years ago
parent
commit
c538051c05

+ 2 - 0
dtool/src/dtoolbase/Sources.pp

@@ -5,12 +5,14 @@
   #define TARGET dtoolbase
   #define TARGET dtoolbase
   
   
   #define SOURCES \
   #define SOURCES \
+    dallocator.T dallocator.h \
     dtoolbase.cxx dtoolbase.h dtoolbase_cc.h dtoolsymbols.h \
     dtoolbase.cxx dtoolbase.h dtoolbase_cc.h dtoolsymbols.h \
     fakestringstream.h \
     fakestringstream.h \
     pallocator.T pallocator.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
 
 
   #define INSTALL_HEADERS \
   #define INSTALL_HEADERS \
+    dallocator.T dallocator.h \
     dtoolbase.h dtoolbase_cc.h dtoolsymbols.h fakestringstream.h \
     dtoolbase.h dtoolbase_cc.h dtoolsymbols.h fakestringstream.h \
     pallocator.T pallocator.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

+ 52 - 0
dtool/src/dtoolbase/dallocator.T

@@ -0,0 +1,52 @@
+// Filename: dallocator.T
+// Created by:  drose (05Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifdef GCC_STYLE_ALLOCATOR
+
+#ifndef NDEBUG
+template<class Type>
+INLINE void *dallocator<Type>::
+allocate(size_t n) {
+  return default_operator_new(n);
+}
+
+template<class Type>
+INLINE void dallocator<Type>::
+deallocate(void *p, size_t) {
+  default_operator_delete(p);
+}
+#endif  // NDEBUG
+
+#else  // GCC_STYLE_ALLOCATOR
+
+#ifndef NDEBUG
+template<class Type>
+INLINE dallocator<Type>::pointer dallocator<Type>::
+allocate(dallocator<Type>::size_type n, allocator<void>::const_pointer) {
+  return (dallocator<Type>::pointer)default_operator_new(n * sizeof(Type));
+}
+
+template<class Type>
+INLINE void dallocator<Type>::
+//deallocate(dallocator<Type>::pointer p, allocator<Type>::size_type) {
+deallocate(void *p, allocator<Type>::size_type) {
+  default_operator_delete(p);
+}
+#endif  // NDEBUG
+
+#endif  // GCC_STYLE_ALLOCATOR

+ 68 - 0
dtool/src/dtoolbase/dallocator.h

@@ -0,0 +1,68 @@
+// Filename: dallocator.h
+// Created by:  drose (05Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef DALLOCATOR_H
+#define DALLOCATOR_H
+
+#include "dtoolbase.h"
+
+#include <memory>
+
+////////////////////////////////////////////////////////////////////
+//       Class : dallocator
+// Description : This is similar to pallocator, but always uses the
+//               default new and delete handlers defined in
+//               dtoolbase_cc.h; it never calls the hooks assigned by
+//               redefining global_operator_new, etc.
+//
+//               This is needed in those rare cases when we need to
+//               allocate memory for STL without going through the
+//               callback hooks, for instance to implement STL tables
+//               within the MemoryUsage class itself.
+////////////////////////////////////////////////////////////////////
+
+#ifdef GCC_STYLE_ALLOCATOR
+// Early versions of gcc used its own kind of allocator, somewhat
+// different from the STL standard.
+
+template<class Type>
+class dallocator : public alloc {
+public:
+#ifndef NDEBUG
+  static void *allocate(size_t n);
+  static void deallocate(void *p, size_t n);
+#endif  // NDEBUG
+};
+
+#else  // GCC_STYLE_ALLOCATOR
+
+template<class Type>
+class dallocator : public allocator<Type> {
+public:
+#ifndef NDEBUG
+  INLINE pointer allocate(size_type n, allocator<void>::const_pointer hint = 0);
+  //  INLINE void deallocate(pointer p, size_type n);
+  INLINE void deallocate(void *p, size_type n);
+#endif  // NDEBUG
+};
+#endif  // GCC_STYLE_ALLOCATOR
+
+#include "dallocator.T"
+
+#endif
+

+ 1 - 1
dtool/src/dtoolbase/pallocator.T

@@ -45,7 +45,7 @@ template<class Type>
 INLINE void pallocator<Type>::
 INLINE void pallocator<Type>::
 //deallocate(pallocator<Type>::pointer p, allocator<Type>::size_type) {
 //deallocate(pallocator<Type>::pointer p, allocator<Type>::size_type) {
 deallocate(void *p, allocator<Type>::size_type) {
 deallocate(void *p, allocator<Type>::size_type) {
-  (*global_operator_delete)((void *)p);
+  (*global_operator_delete)(p);
 }
 }
 #endif  // NDEBUG
 #endif  // NDEBUG
 
 

+ 0 - 1
dtool/src/dtoolbase/pallocator.h

@@ -22,7 +22,6 @@
 #include "dtoolbase.h"
 #include "dtoolbase.h"
 
 
 #include <memory>
 #include <memory>
-#include <vector>
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : pallocator
 //       Class : pallocator

+ 4 - 2
panda/src/express/memoryUsage.h

@@ -26,6 +26,7 @@
 #include "memoryUsagePointerCounts.h"
 #include "memoryUsagePointerCounts.h"
 
 
 #include "pmap.h"
 #include "pmap.h"
+#include "dallocator.h"
 
 
 class ReferenceCount;
 class ReferenceCount;
 class MemoryUsagePointers;
 class MemoryUsagePointers;
@@ -125,7 +126,7 @@ private:
   static MemoryUsage *_global_ptr;
   static MemoryUsage *_global_ptr;
 
 
   // Cannot use a pmap, since that would be recursive!
   // Cannot use a pmap, since that would be recursive!
-  typedef map<void *, MemoryInfo> Table;
+  typedef map<void *, MemoryInfo, less<void *>, dallocator<MemoryInfo> > Table;
   Table _table;
   Table _table;
   int _freeze_index;
   int _freeze_index;
   int _count;
   int _count;
@@ -140,7 +141,8 @@ private:
 
 
   private:
   private:
     // Cannot use a pmap, since that would be recursive!
     // Cannot use a pmap, since that would be recursive!
-    typedef map<TypeHandle, MemoryUsagePointerCounts> Counts;
+    typedef map<TypeHandle, MemoryUsagePointerCounts, 
+      less<TypeHandle>, dallocator<MemoryUsagePointerCounts> > Counts;
     Counts _counts;
     Counts _counts;
   };
   };
   TypeHistogram _trend_types;
   TypeHistogram _trend_types;