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