RavEngine
Loading...
Searching...
No Matches
SnXmlMemoryAllocator.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SN_XML_MEMORY_ALLOCATOR_H
30#define SN_XML_MEMORY_ALLOCATOR_H
31
32#include "foundation/PxSimpleTypes.h"
33
34namespace physx {
35
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( 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( sizeof( TObjectType ) ) );
56 new (retval) TObjectType(arg);
57 return retval;
58 }
59
60 template<typename TObjectType>
61 void deallocate( TObjectType* inObject )
62 {
63 deallocate( reinterpret_cast<PxU8*>( inObject ) );
64 }
65 template<typename TObjectType>
66 inline TObjectType* batchAllocate(PxU32 inCount )
67 {
68 TObjectType* retval = reinterpret_cast<TObjectType*>( allocate( 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( 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( sizeof(TObjectType) * inCount ) );
93 for ( PxU32 idx = 0; idx < inCount; ++idx )
94 {
95 new (retval + idx) TObjectType();
96 }
97 return retval;
98 }
99 };
100
102 {
103 Sn::TMemoryPoolManager mManager;
104
106 : mManager( inAllocator )
107 {
108 }
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( inMem );
126 }
127 };
128}
129#endif
Definition SnXmlMemoryPool.h:246
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
Definition SnXmlMemoryAllocator.h:37
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition SnXmlMemoryAllocator.h:102