// Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of NVIDIA CORPORATION nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // Copyright (c) 2008-2025 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_PINNED_ARRAY_H #define PX_PINNED_ARRAY_H #include "foundation/PxArray.h" #include "foundation/PxAllocator.h" #include "foundation/PxBounds3.h" #if !PX_DOXYGEN namespace physx { #endif // PT: the default pinned-memory arrays are defined as PxPinnedArray = PxArray. // The PxVirtualAllocator ultimately uses cuMemHostAlloc via PxgCudaHostMemoryAllocatorCallback / PxgPinnedMemoryAllocate. // We use the CU_MEMHOSTALLOC_DEVICEMAP flag there so cuMemHostGetDevicePointer() can later be used on returned ptr. // // The new pinned-memory arrays are defined as PxPinnedArraySafe = PxArray >. This uses a new // allocator that allocates either from cuMemHostAlloc, *or* fallbacks to regular allocs when we run out of pinned memory. // The issue is that in the second case cuMemHostGetDevicePointer() will fail, so we cannot use this everywhere. // // I think this exposes issues in PxArray itself, for example in the swap function (we don't swap the allocator data there, // so when using a PxVirtualAllocator with PxArray the PxVirtualAllocator members are not swapped). // PT: this class uses the fact that PxArray inherits from the allocator to add new members to the array class. In particular // PxPinnedAllocator::mPinned describes where PxArray::mData has been allocated. The class is mostly designed to be used in // conjunction with PxArray, not as a standalone allocator. template class PxPinnedAllocator { public: PxPinnedAllocator(PxVirtualAllocatorCallback* callback = NULL, int group = 0) : mCallback(callback), mGroup(group), mPinned(0) {} PX_INLINE void* allocate(size_t size, const char* file, int line, uint32_t* cookie=NULL) { PX_ASSERT(mCallback); // PT: returns *previous* pinned value. It will be passed back to the deallocate function. if(cookie) *cookie = mPinned; if(!size) { mPinned = 0xffffffff; return NULL; } // PT: first, try with the pinned-memory allocator void* ptr = mCallback->allocate(size, mGroup, file, line); if(ptr) { mPinned = 1; return ptr; } // PT: if it fails, fallback to regular allocator mPinned = 0; return PxReflectionAllocator::allocate(size, file, line); } PX_INLINE void deallocate(void* ptr, uint32_t* cookie=NULL) { PX_ASSERT(mCallback); if(ptr) { // PT: by default use the internal value, except if we're given an explicit cookie const uint32_t pinned = cookie ? *cookie : mPinned; if(pinned==1) mCallback->deallocate(ptr); else PxReflectionAllocator::deallocate(ptr); } } PX_FORCE_INLINE void setCallback(PxVirtualAllocatorCallback* callback) { mCallback = callback; } PX_FORCE_INLINE PxVirtualAllocatorCallback* getCallback() { return mCallback; } private: PxVirtualAllocatorCallback* mCallback; const int mGroup; uint32_t mPinned; PxPinnedAllocator& operator=(const PxPinnedAllocator&); }; struct PxsCachedTransform; // PT: default versions: template using PxPinnedArray = PxArray; typedef PxArray PxCachedTransformArrayPinned; typedef PxArray PxBoundsArrayPinned; typedef PxArray PxFloatArrayPinned; typedef PxArray PxInt32ArrayPinned; typedef PxArray PxInt16ArrayPinned; typedef PxArray PxInt8ArrayPinned; // PT: new versions template using PxPinnedArraySafe = PxArray >; typedef PxArray > PxCachedTransformArrayPinnedSafe; typedef PxArray > PxBoundsArrayPinnedSafe; typedef PxArray > PxFloatArrayPinnedSafe; typedef PxArray > PxInt32ArrayPinnedSafe; typedef PxArray > PxInt16ArrayPinnedSafe; typedef PxArray > PxInt8ArrayPinnedSafe; #if !PX_DOXYGEN } // namespace physx #endif #endif