// This code contains NVIDIA Confidential Information and is disclosed to you // under a form of NVIDIA software license agreement provided separately to you. // // Notice // NVIDIA Corporation and its licensors retain all intellectual property and // proprietary rights in and to this software and related documentation and // any modifications thereto. Any use, reproduction, disclosure, or // distribution of this software and related documentation without an express // license agreement from NVIDIA Corporation is strictly prohibited. // // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT, // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. // // Information and code furnished is believed to be accurate and reliable. // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such // information or for any infringement of patents or other rights of third parties that may // result from its use. No license is granted by implication or otherwise under any patent // or patent rights of NVIDIA Corporation. Details are subject to change without notice. // This code supersedes and replaces all information previously supplied. // NVIDIA Corporation products are not authorized for use as critical // components in life support devices or systems without express written approval of // NVIDIA Corporation. // // Copyright (c) 2008-2016 NVIDIA Corporation. All rights reserved. // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. #ifndef PX_PHYSICS_COMMON_PREALLOCATINGPOOL #define PX_PHYSICS_COMMON_PREALLOCATINGPOOL #include "foundation/Px.h" #include "PsUserAllocated.h" #include "CmPhysXCommon.h" #include "PsSort.h" #include "PsArray.h" /* Pool used to allocate variable sized tasks. It's intended to be cleared after a short period (time step). */ namespace physx { namespace Cm { class PreallocatingRegion { public: PX_FORCE_INLINE PreallocatingRegion() : mMemory(NULL), mFirstFree(NULL), mNbElements(0) {} void init(PxU32 maxElements, PxU32 elementSize, const char* typeName) { mFirstFree = NULL; mNbElements = 0; PX_ASSERT(typeName); PX_UNUSED(typeName); mMemory = reinterpret_cast(PX_ALLOC(sizeof(PxU8)*elementSize*maxElements, typeName?typeName:"SceneSim Pool")); // ### addActor alloc PX_ASSERT(elementSize*maxElements>=sizeof(void*)); } void reset() { PX_FREE_AND_RESET(mMemory); } PX_FORCE_INLINE PxU8* allocateMemory(PxU32 maxElements, PxU32 elementSize) { if(mFirstFree) { PxU8* recycled = reinterpret_cast(mFirstFree); void** recycled32 = reinterpret_cast(recycled); mFirstFree = *recycled32; return recycled; } else { if(mNbElements==maxElements) return NULL; // Out of memory const PxU32 freeIndex = mNbElements++; return mMemory + freeIndex * elementSize; } } void deallocateMemory(PxU32 maxElements, PxU32 elementSize, PxU8* element) { PX_ASSERT(element); PX_ASSERT(element>=mMemory && element(element); *recycled32 = mFirstFree; mFirstFree = element; } PX_FORCE_INLINE bool operator < (const PreallocatingRegion& p) const { return mMemory < p.mMemory; } PX_FORCE_INLINE bool operator > (const PreallocatingRegion& p) const { return mMemory > p.mMemory; } PxU8* mMemory; void* mFirstFree; PxU32 mNbElements; }; class PreallocatingRegionManager { public: PreallocatingRegionManager(PxU32 maxElements, PxU32 elementSize, const char* typeName) : mMaxElements (maxElements) , mElementSize (elementSize) , mActivePoolIndex (0) , mPools(PX_DEBUG_EXP("MyPoolManagerPools")) , mNeedsSorting (true) , mTypeName (typeName) { PreallocatingRegion tmp; tmp.init(maxElements, elementSize, mTypeName); mPools.pushBack(tmp); } ~PreallocatingRegionManager() { const PxU32 nbPools = mPools.size(); for(PxU32 i=0;iavailableSpace) { PreallocatingRegion tmp; tmp.init(maxElements, elementSize, mTypeName); mPools.pushBack(tmp); availableSpace += maxElements; } } PX_FORCE_INLINE PxU8* allocateMemory() { PX_ASSERT(mActivePoolIndex>1; PreallocatingRegion& candidate = mPools[PxU32(mid)]; if(contains(candidate.mMemory, slabSize, element)) { candidate.deallocateMemory(maxElements, elementSize, element); // when we sorted earlier we trashed the active index, but at least this region has a free element if(mNeedsSorting) mActivePoolIndex = PxU32(mid); mNeedsSorting = false; return; } if(candidate.mMemory=memory && element mPools; bool mNeedsSorting; const char* mTypeName; }; template class PreallocatingPool : public Ps::UserAllocated { PreallocatingPool& operator=(const PreallocatingPool&); public: PreallocatingPool(PxU32 maxElements, const char* typeName) : mPool(maxElements, sizeof(T), typeName) { } ~PreallocatingPool() { } PX_FORCE_INLINE void preAllocate(PxU32 n) { mPool.preAllocate(n); } PX_INLINE T* allocate() { return reinterpret_cast(mPool.allocateMemory()); } PX_FORCE_INLINE T* allocateAndPrefetch() { T* t = reinterpret_cast(mPool.allocateMemory()); Ps::prefetch(t, sizeof(T)); return t; } PX_INLINE T* construct() { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T() : 0; } template PX_INLINE T* construct(A1& a) { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T(a) : 0; } template PX_INLINE T* construct(A1& a, A2& b) { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T(a,b) : 0; } template PX_INLINE T* construct(A1& a, A2& b, A3& c) { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T(a,b,c) : 0; } template PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d) { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T(a,b,c,d) : 0; } template PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e) { T* t = reinterpret_cast(mPool.allocateMemory()); return t ? new (t) T(a,b,c,d,e) : 0; } //// PX_INLINE T* construct(T* t) { PX_ASSERT(t); return new (t) T(); } template PX_INLINE T* construct(T* t, A1& a) { PX_ASSERT(t); return new (t) T(a); } template PX_INLINE T* construct(T* t, A1& a, A2& b) { PX_ASSERT(t); return new (t) T(a,b); } template PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c) { PX_ASSERT(t); return new (t) T(a,b,c); } template PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c, A4& d) { PX_ASSERT(t); return new (t) T(a,b,c,d); } template PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c, A4& d, A5& e) { PX_ASSERT(t); return new (t) T(a,b,c,d,e); } PX_INLINE void destroy(T* const p) { if(p) { p->~T(); mPool.deallocateMemory(reinterpret_cast(p)); } } PX_INLINE void releasePreallocated(T* const p) { if(p) mPool.deallocateMemory(reinterpret_cast(p)); } protected: PreallocatingRegionManager mPool; }; template class BufferedPreallocatingPool : public PreallocatingPool { Ps::Array mDeletedElems; PX_NOCOPY(BufferedPreallocatingPool) public: BufferedPreallocatingPool(PxU32 maxElements, const char* typeName) : PreallocatingPool(maxElements, typeName) { } PX_INLINE void destroy(T* const p) { if (p) { p->~T(); mDeletedElems.pushBack(p); } } void processPendingDeletedElems() { for (PxU32 i = 0; i < mDeletedElems.size(); ++i) this->mPool.deallocateMemory(reinterpret_cast(mDeletedElems[i])); mDeletedElems.clear(); } }; } // namespace Cm } #endif