Panda3D
pallocator.h
1 // Filename: pallocator.h
2 // Created by: drose (05Jun01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PALLOCATOR_H
16 #define PALLOCATOR_H
17 
18 #include <memory>
19 #include "dtoolbase.h"
20 #include "memoryHook.h"
21 #include "deletedChain.h"
22 #include "typeHandle.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : pallocator
26 // Description : This is our own Panda specialization on the default
27 // STL allocator. Its main purpose is to call the hooks
28 // for MemoryUsage to properly track STL-allocated
29 // memory.
30 //
31 // pvector, pmap, etc. are all defined in this directory
32 // to use a pallocator.
33 //
34 // pallocator actually comes it two flavors now:
35 // pallocator_single, which can only allocate single
36 // instances of an object, and pallocator_array, which
37 // can allocate arrays of objects.
38 ////////////////////////////////////////////////////////////////////
39 
40 #ifndef USE_STL_ALLOCATOR
41 // If we're not trying to make custom allocators (either we don't know
42 // what kind of syntax this STL library wants, or we're compiling with
43 // OPTIMIZE 4), then simply use the standard allocator.
44 #define pallocator_single allocator
45 #define pallocator_array allocator
46 
47 #else
48 
49 template<class Type>
50 class pallocator_single : public allocator<Type> {
51 public:
52  // Nowadays we cannot implicitly inherit typedefs from base classes
53  // in a template class; we must explicitly copy them here.
54  typedef TYPENAME allocator<Type>::pointer pointer;
55  typedef TYPENAME allocator<Type>::reference reference;
56  typedef TYPENAME allocator<Type>::const_pointer const_pointer;
57  typedef TYPENAME allocator<Type>::const_reference const_reference;
58  typedef TYPENAME allocator<Type>::size_type size_type;
59 
60  INLINE pallocator_single(TypeHandle type_handle) throw();
61 
62  // template member functions in VC++ can only be defined in-class.
63  template<class U>
64  INLINE pallocator_single(const pallocator_single<U> &copy) throw() :
65  _type_handle(copy._type_handle) { }
66 
67  INLINE pointer allocate(size_type n, allocator<void>::const_pointer hint = 0);
68  INLINE void deallocate(pointer p, size_type n);
69 
70  template<class U> struct rebind {
72  };
73 
74  TypeHandle _type_handle;
75 };
76 
77 template<class Type>
78 class pallocator_array : public allocator<Type> {
79 public:
80  // Nowadays we cannot implicitly inherit typedefs from base classes
81  // in a template class; we must explicitly copy them here.
82  typedef TYPENAME allocator<Type>::pointer pointer;
83  typedef TYPENAME allocator<Type>::reference reference;
84  typedef TYPENAME allocator<Type>::const_pointer const_pointer;
85  typedef TYPENAME allocator<Type>::const_reference const_reference;
86  typedef TYPENAME allocator<Type>::size_type size_type;
87 
88  INLINE pallocator_array(TypeHandle type_handle = TypeHandle::none()) throw();
89 
90  // template member functions in VC++ can only be defined in-class.
91  template<class U>
92  INLINE pallocator_array(const pallocator_array<U> &copy) throw() :
93  _type_handle(copy._type_handle) { }
94 
95  INLINE pointer allocate(size_type n, allocator<void>::const_pointer hint = 0);
96  INLINE void deallocate(pointer p, size_type n);
97 
98  template<class U> struct rebind {
99  typedef pallocator_array<U> other;
100  };
101 
102  TypeHandle _type_handle;
103 };
104 
105 #include "pallocator.T"
106 
107 #endif // USE_STL_ALLOCATOR
108 
109 #endif
110 
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
This is our own Panda specialization on the default STL allocator.
Definition: pallocator.h:50
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85