RavEngine
Loading...
Searching...
No Matches
CmFlushPool.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 CM_FLUSH_POOL_H
30#define CM_FLUSH_POOL_H
31
32#include "foundation/Px.h"
33#include "foundation/PxUserAllocated.h"
34#include "foundation/PxBitUtils.h"
35#include "foundation/PxMutex.h"
36#include "foundation/PxArray.h"
37
38/*
39Pool used to allocate variable sized tasks. It's intended to be cleared after a short period (time step).
40*/
41
42namespace physx
43{
44namespace Cm
45{
46 static const PxU32 sSpareChunkCount = 2;
47
49 {
50 PX_NOCOPY(FlushPool)
51 public:
52 FlushPool(PxU32 chunkSize) : mChunks("FlushPoolChunk"), mChunkIndex(0), mOffset(0), mChunkSize(chunkSize)
53 {
54 mChunks.pushBack(static_cast<PxU8*>(PX_ALLOC(mChunkSize, "PxU8")));
55 }
56
58 {
59 for (PxU32 i = 0; i < mChunks.size(); ++i)
60 PX_FREE(mChunks[i]);
61 }
62
63 // alignment must be a power of two
64 void* allocate(PxU32 size, PxU32 alignment=16)
65 {
66 PxMutex::ScopedLock lock(mMutex);
67 return allocateNotThreadSafe(size, alignment);
68 }
69
70 // alignment must be a power of two
71 void* allocateNotThreadSafe(PxU32 size, PxU32 alignment=16)
72 {
73 PX_ASSERT(PxIsPowerOfTwo(alignment));
74 PX_ASSERT(size <= mChunkSize && !mChunks.empty());
75
76 // padding for alignment
77 size_t unalignedStart = size_t(mChunks[mChunkIndex]+mOffset);
78 PxU32 pad = PxU32(((unalignedStart+alignment-1)&~(size_t(alignment)-1)) - unalignedStart);
79
80 if (mOffset + size + pad > mChunkSize)
81 {
82 mChunkIndex++;
83 mOffset = 0;
84 if (mChunkIndex >= mChunks.size())
85 mChunks.pushBack(static_cast<PxU8*>(PX_ALLOC(mChunkSize, "PxU8")));
86
87 // update padding to ensure new alloc is aligned
88 unalignedStart = size_t(mChunks[mChunkIndex]);
89 pad = PxU32(((unalignedStart+alignment-1)&~(size_t(alignment)-1)) - unalignedStart);
90 }
91
92 void* ptr = mChunks[mChunkIndex] + mOffset + pad;
93 PX_ASSERT((size_t(ptr)&(size_t(alignment)-1)) == 0);
94 mOffset += size + pad;
95 return ptr;
96 }
97
98 void clear(PxU32 spareChunkCount = sSpareChunkCount)
99 {
100 PxMutex::ScopedLock lock(mMutex);
101
102 clearNotThreadSafe(spareChunkCount);
103 }
104
105 void clearNotThreadSafe(PxU32 spareChunkCount = sSpareChunkCount)
106 {
107 PX_UNUSED(spareChunkCount);
108
109 //release memory not used previously
110 PxU32 targetSize = mChunkIndex+sSpareChunkCount;
111 while (mChunks.size() > targetSize)
112 {
113 PxU8* ptr = mChunks.popBack();
114 PX_FREE(ptr);
115 }
116
117 mChunkIndex = 0;
118 mOffset = 0;
119 }
120
121 void resetNotThreadSafe()
122 {
123 PxU8* firstChunk = mChunks[0];
124
125 for (PxU32 i = 1; i < mChunks.size(); ++i)
126 PX_FREE(mChunks[i]);
127
128 mChunks.clear();
129 mChunks.pushBack(firstChunk);
130 mChunkIndex = 0;
131 mOffset = 0;
132 }
133
134 void lock()
135 {
136 mMutex.lock();
137 }
138
139 void unlock()
140 {
141 mMutex.unlock();
142 }
143
144 private:
145 PxMutex mMutex;
146 PxArray<PxU8*> mChunks;
147 PxU32 mChunkIndex;
148 PxU32 mOffset;
149 PxU32 mChunkSize;
150 };
151
152
153} // namespace Cm
154
155}
156
157#endif
Definition CmFlushPool.h:49
Definition PxArray.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_INLINE void clear()
Definition PxArray.h:250
PX_FORCE_INLINE bool empty() const
Definition PxArray.h:261
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE T popBack()
Definition PxArray.h:311
Definition PxMutex.h:93
Definition PxMutex.h:89
PX_FORCE_INLINE void unlock() const
Definition PxMutex.h:149
PX_FORCE_INLINE void lock() const
Definition PxMutex.h:130
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39