| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | #ifndef PSFOUNDATION_PSSLIST_H |
| 31 | #define PSFOUNDATION_PSSLIST_H |
| 32 | |
| 33 | #include "foundation/Px.h" |
| 34 | #include "foundation/PxAssert.h" |
| 35 | #include "PsAlignedMalloc.h" |
| 36 | |
| 37 | #if PX_P64_FAMILY |
| 38 | #define PX_SLIST_ALIGNMENT 16 |
| 39 | #else |
| 40 | #define PX_SLIST_ALIGNMENT 8 |
| 41 | #endif |
| 42 | |
| 43 | namespace physx |
| 44 | { |
| 45 | namespace shdfnd |
| 46 | { |
| 47 | |
| 48 | #if PX_VC |
| 49 | #pragma warning(push) |
| 50 | #pragma warning(disable : 4324) // Padding was added at the end of a structure because of a __declspec(align) value. |
| 51 | #endif |
| 52 | |
| 53 | #if !PX_GCC_FAMILY |
| 54 | __declspec(align(PX_SLIST_ALIGNMENT)) |
| 55 | #endif |
| 56 | class SListEntry |
| 57 | { |
| 58 | friend struct SListImpl; |
| 59 | |
| 60 | public: |
| 61 | SListEntry() : mNext(NULL) |
| 62 | { |
| 63 | PX_ASSERT((size_t(this) & (PX_SLIST_ALIGNMENT - 1)) == 0); |
| 64 | } |
| 65 | |
| 66 | // Only use on elements returned by SList::flush() |
| 67 | // because the operation is not atomic. |
| 68 | SListEntry* next() |
| 69 | { |
| 70 | return mNext; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | SListEntry* mNext; |
| 75 | } |
| 76 | #if PX_GCC_FAMILY |
| 77 | __attribute__((aligned(PX_SLIST_ALIGNMENT))); |
| 78 | #else |
| 79 | ; |
| 80 | #endif |
| 81 | |
| 82 | #if PX_VC |
| 83 | #pragma warning(pop) |
| 84 | #endif |
| 85 | |
| 86 | // template-less implementation |
| 87 | struct PX_FOUNDATION_API SListImpl |
| 88 | { |
| 89 | SListImpl(); |
| 90 | ~SListImpl(); |
| 91 | void push(SListEntry* entry); |
| 92 | SListEntry* pop(); |
| 93 | SListEntry* flush(); |
| 94 | static uint32_t getSize(); |
| 95 | }; |
| 96 | |
| 97 | template <typename Alloc = ReflectionAllocator<SListImpl> > |
| 98 | class SListT : protected Alloc |
| 99 | { |
| 100 | public: |
| 101 | SListT(const Alloc& alloc = Alloc()) : Alloc(alloc) |
| 102 | { |
| 103 | mImpl = reinterpret_cast<SListImpl*>(Alloc::allocate(SListImpl::getSize(), __FILE__, __LINE__)); |
| 104 | PX_ASSERT((size_t(mImpl) & (PX_SLIST_ALIGNMENT - 1)) == 0); |
| 105 | PX_PLACEMENT_NEW(mImpl, SListImpl)(); |
| 106 | } |
| 107 | ~SListT() |
| 108 | { |
| 109 | mImpl->~SListImpl(); |
| 110 | Alloc::deallocate(mImpl); |
| 111 | } |
| 112 | |
| 113 | // pushes a new element to the list |
| 114 | void push(SListEntry& entry) |
| 115 | { |
| 116 | mImpl->push(entry: &entry); |
| 117 | } |
| 118 | |
| 119 | // pops an element from the list |
| 120 | SListEntry* pop() |
| 121 | { |
| 122 | return mImpl->pop(); |
| 123 | } |
| 124 | |
| 125 | // removes all items from list, returns pointer to first element |
| 126 | SListEntry* flush() |
| 127 | { |
| 128 | return mImpl->flush(); |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | SListImpl* mImpl; |
| 133 | }; |
| 134 | |
| 135 | typedef SListT<> SList; |
| 136 | |
| 137 | } // namespace shdfnd |
| 138 | } // namespace physx |
| 139 | |
| 140 | #endif // #ifndef PSFOUNDATION_PSSLIST_H |
| 141 | |