Panda3D
|
00001 // Filename: pallocator.h 00002 // Created by: drose (05Jun01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef PALLOCATOR_H 00016 #define PALLOCATOR_H 00017 00018 #include <memory> 00019 #include "dtoolbase.h" 00020 #include "memoryHook.h" 00021 #include "deletedChain.h" 00022 #include "typeHandle.h" 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Class : pallocator 00026 // Description : This is our own Panda specialization on the default 00027 // STL allocator. Its main purpose is to call the hooks 00028 // for MemoryUsage to properly track STL-allocated 00029 // memory. 00030 // 00031 // pvector, pmap, etc. are all defined in this directory 00032 // to use a pallocator. 00033 // 00034 // pallocator actually comes it two flavors now: 00035 // pallocator_single, which can only allocate single 00036 // instances of an object, and pallocator_array, which 00037 // can allocate arrays of objects. 00038 //////////////////////////////////////////////////////////////////// 00039 00040 #ifndef USE_STL_ALLOCATOR 00041 // If we're not trying to make custom allocators (either we don't know 00042 // what kind of syntax this STL library wants, or we're compiling with 00043 // OPTIMIZE 4), then simply use the standard allocator. 00044 #define pallocator_single allocator 00045 #define pallocator_array allocator 00046 00047 #else 00048 00049 template<class Type> 00050 class pallocator_single : public allocator<Type> { 00051 public: 00052 // Nowadays we cannot implicitly inherit typedefs from base classes 00053 // in a template class; we must explicitly copy them here. 00054 typedef TYPENAME allocator<Type>::pointer pointer; 00055 typedef TYPENAME allocator<Type>::reference reference; 00056 typedef TYPENAME allocator<Type>::const_pointer const_pointer; 00057 typedef TYPENAME allocator<Type>::const_reference const_reference; 00058 typedef TYPENAME allocator<Type>::size_type size_type; 00059 00060 INLINE pallocator_single(TypeHandle type_handle) throw(); 00061 00062 // template member functions in VC++ can only be defined in-class. 00063 template<class U> 00064 INLINE pallocator_single(const pallocator_single<U> ©) throw() : 00065 _type_handle(copy._type_handle) { } 00066 00067 INLINE pointer allocate(size_type n, allocator<void>::const_pointer hint = 0); 00068 INLINE void deallocate(pointer p, size_type n); 00069 00070 template<class U> struct rebind { 00071 typedef pallocator_single<U> other; 00072 }; 00073 00074 TypeHandle _type_handle; 00075 }; 00076 00077 template<class Type> 00078 class pallocator_array : public allocator<Type> { 00079 public: 00080 // Nowadays we cannot implicitly inherit typedefs from base classes 00081 // in a template class; we must explicitly copy them here. 00082 typedef TYPENAME allocator<Type>::pointer pointer; 00083 typedef TYPENAME allocator<Type>::reference reference; 00084 typedef TYPENAME allocator<Type>::const_pointer const_pointer; 00085 typedef TYPENAME allocator<Type>::const_reference const_reference; 00086 typedef TYPENAME allocator<Type>::size_type size_type; 00087 00088 INLINE pallocator_array(TypeHandle type_handle = TypeHandle::none()) throw(); 00089 00090 // template member functions in VC++ can only be defined in-class. 00091 template<class U> 00092 INLINE pallocator_array(const pallocator_array<U> ©) throw() : 00093 _type_handle(copy._type_handle) { } 00094 00095 INLINE pointer allocate(size_type n, allocator<void>::const_pointer hint = 0); 00096 INLINE void deallocate(pointer p, size_type n); 00097 00098 template<class U> struct rebind { 00099 typedef pallocator_array<U> other; 00100 }; 00101 00102 TypeHandle _type_handle; 00103 }; 00104 00105 #include "pallocator.T" 00106 00107 #endif // USE_STL_ALLOCATOR 00108 00109 #endif 00110