RavEngine
Loading...
Searching...
No Matches
ExtSharedQueueEntryPool.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 EXT_SHARED_QUEUE_ENTRY_POOL_H
30#define EXT_SHARED_QUEUE_ENTRY_POOL_H
31
32#include "foundation/PxAllocator.h"
33#include "foundation/PxArray.h"
34#include "foundation/PxSList.h"
35
36namespace physx
37{
38namespace Ext
39{
41 {
42 public:
43 SharedQueueEntry(void* objectRef) : mObjectRef(objectRef), mPooledEntry(false) {}
44 SharedQueueEntry() : mObjectRef(NULL), mPooledEntry(true) {}
45
46 public:
47 void* mObjectRef;
48 bool mPooledEntry; // True if the entry was preallocated in a pool
49 };
50
51#if PX_VC
52#pragma warning(push)
53#pragma warning(disable:4324) // Padding was added at the end of a structure because of a __declspec(align) value.
54#endif // Because of the SList member I assume*/
55
56 template<class Alloc = typename PxAllocatorTraits<SharedQueueEntry>::Type >
57 class SharedQueueEntryPool : private Alloc
58 {
59 public:
60 SharedQueueEntryPool(PxU32 poolSize, const Alloc& alloc = Alloc("SharedQueueEntryPool"));
62
63 SharedQueueEntry* getEntry(void* objectRef);
64 void putEntry(SharedQueueEntry& entry);
65
66 private:
67 SharedQueueEntry* mTaskEntryPool;
68 PxSList mTaskEntryPtrPool;
69 };
70
71#if PX_VC
72#pragma warning(pop)
73#endif
74
75template <class Alloc>
76SharedQueueEntryPool<Alloc>::SharedQueueEntryPool(PxU32 poolSize, const Alloc& alloc)
77 : Alloc(alloc)
78{
79 PxAlignedAllocator<PX_SLIST_ALIGNMENT, Alloc> alignedAlloc("SharedQueueEntryPool");
80
81 mTaskEntryPool = poolSize ? reinterpret_cast<SharedQueueEntry*>(alignedAlloc.allocate(sizeof(SharedQueueEntry) * poolSize, __FILE__, __LINE__)) : NULL;
82
83 if (mTaskEntryPool)
84 {
85 for(PxU32 i=0; i < poolSize; i++)
86 {
87 PX_ASSERT((size_t(&mTaskEntryPool[i]) & (PX_SLIST_ALIGNMENT-1)) == 0); // The SList entry must be aligned according to PX_SLIST_ALIGNMENT
88
89 PX_PLACEMENT_NEW(&mTaskEntryPool[i], SharedQueueEntry)();
90 PX_ASSERT(mTaskEntryPool[i].mPooledEntry == true);
91 mTaskEntryPtrPool.push(mTaskEntryPool[i]);
92 }
93 }
94}
95
96
97template <class Alloc>
98SharedQueueEntryPool<Alloc>::~SharedQueueEntryPool()
99{
100 if (mTaskEntryPool)
101 {
102 PxAlignedAllocator<PX_SLIST_ALIGNMENT, Alloc> alignedAlloc("SharedQueueEntryPool");
103 alignedAlloc.deallocate(mTaskEntryPool);
104 }
105}
106
107
108template <class Alloc>
109SharedQueueEntry* SharedQueueEntryPool<Alloc>::getEntry(void* objectRef)
110{
111 SharedQueueEntry* e = static_cast<SharedQueueEntry*>(mTaskEntryPtrPool.pop());
112 if (e)
113 {
114 PX_ASSERT(e->mPooledEntry == true);
115 e->mObjectRef = objectRef;
116 return e;
117 }
118 else
119 {
120 PxAlignedAllocator<PX_SLIST_ALIGNMENT, Alloc> alignedAlloc;
121 e = reinterpret_cast<SharedQueueEntry*>(alignedAlloc.allocate(sizeof(SharedQueueEntry), __FILE__, __LINE__));
122 if (e)
123 {
124 PX_PLACEMENT_NEW(e, SharedQueueEntry)(objectRef);
125 PX_ASSERT(e->mPooledEntry == false);
126 }
127
128 return e;
129 }
130}
131
132
133template <class Alloc>
134void SharedQueueEntryPool<Alloc>::putEntry(SharedQueueEntry& entry)
135{
136 if (entry.mPooledEntry)
137 {
138 entry.mObjectRef = NULL;
139 mTaskEntryPtrPool.push(entry);
140 }
141 else
142 {
143 PxAlignedAllocator<PX_SLIST_ALIGNMENT, Alloc> alignedAlloc;
144 alignedAlloc.deallocate(&entry);
145 }
146}
147
148} // namespace Ext
149} // namespace physx
150
151#endif
Definition ExtSharedQueueEntryPool.h:58
Definition ExtSharedQueueEntryPool.h:41
Definition PxAlignedMalloc.h:54
Definition PxSList.h:53
Definition PxSList.h:90
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39