1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | #ifndef PX_XML_MEMORY_ALLOCATOR_H |
30 | #define PX_XML_MEMORY_ALLOCATOR_H |
31 | |
32 | #include "foundation/PxSimpleTypes.h" |
33 | |
34 | namespace physx { |
35 | |
36 | class XmlMemoryAllocator |
37 | { |
38 | protected: |
39 | virtual ~XmlMemoryAllocator(){} |
40 | public: |
41 | virtual PxU8* allocate(PxU32 inSize) = 0; |
42 | virtual void deallocate( PxU8* inMem ) = 0; |
43 | virtual PxAllocatorCallback& getAllocator() = 0; |
44 | template<typename TObjectType> |
45 | TObjectType* allocate() |
46 | { |
47 | TObjectType* retval = reinterpret_cast< TObjectType* >( allocate( inSize: sizeof( TObjectType ) ) ); |
48 | new (retval) TObjectType(); |
49 | return retval; |
50 | } |
51 | |
52 | template<typename TObjectType, typename TArgType> |
53 | TObjectType* allocate(const TArgType &arg) |
54 | { |
55 | TObjectType* retval = reinterpret_cast< TObjectType* >( allocate( inSize: sizeof( TObjectType ) ) ); |
56 | new (retval) TObjectType(arg); |
57 | return retval; |
58 | } |
59 | |
60 | template<typename TObjectType> |
61 | void deallocate( TObjectType* inObject ) |
62 | { |
63 | deallocate( inMem: reinterpret_cast<PxU8*>( inObject ) ); |
64 | } |
65 | template<typename TObjectType> |
66 | inline TObjectType* batchAllocate(PxU32 inCount ) |
67 | { |
68 | TObjectType* retval = reinterpret_cast<TObjectType*>( allocate( inSize: sizeof(TObjectType) * inCount ) ); |
69 | for ( PxU32 idx = 0; idx < inCount; ++idx ) |
70 | { |
71 | new (retval + idx) TObjectType(); |
72 | } |
73 | return retval; |
74 | } |
75 | |
76 | template<typename TObjectType, typename TArgType> |
77 | inline TObjectType* batchAllocate(PxU32 inCount, const TArgType &arg) |
78 | { |
79 | TObjectType* retval = reinterpret_cast<TObjectType*>( allocate( inSize: sizeof(TObjectType) * inCount ) ); |
80 | for ( PxU32 idx = 0; idx < inCount; ++idx ) |
81 | { |
82 | new (retval + idx) TObjectType(arg); |
83 | } |
84 | return retval; |
85 | } |
86 | |
87 | |
88 | //Duplicate function definition for gcc. |
89 | template<typename TObjectType> |
90 | inline TObjectType* batchAllocate(TObjectType*, PxU32 inCount ) |
91 | { |
92 | TObjectType* retval = reinterpret_cast<TObjectType*>( allocate( inSize: sizeof(TObjectType) * inCount ) ); |
93 | for ( PxU32 idx = 0; idx < inCount; ++idx ) |
94 | { |
95 | new (retval + idx) TObjectType(); |
96 | } |
97 | return retval; |
98 | } |
99 | }; |
100 | |
101 | struct XmlMemoryAllocatorImpl : public XmlMemoryAllocator |
102 | { |
103 | Sn::TMemoryPoolManager mManager; |
104 | |
105 | XmlMemoryAllocatorImpl( PxAllocatorCallback& inAllocator ) |
106 | : mManager( inAllocator ) |
107 | { |
108 | } |
109 | XmlMemoryAllocatorImpl &operator=(const XmlMemoryAllocatorImpl &); |
110 | virtual PxAllocatorCallback& getAllocator() |
111 | { |
112 | return mManager.getWrapper().getAllocator(); |
113 | } |
114 | |
115 | virtual PxU8* allocate(PxU32 inSize ) |
116 | { |
117 | if ( !inSize ) |
118 | return NULL; |
119 | |
120 | return mManager.allocate( inSize ); |
121 | } |
122 | virtual void deallocate( PxU8* inMem ) |
123 | { |
124 | if ( inMem ) |
125 | mManager.deallocate( inMemory: inMem ); |
126 | } |
127 | }; |
128 | } |
129 | #endif |
130 | |