|
| 1 | +// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O. |
| 2 | +// This file is part of the "Nabla Engine". |
| 3 | +// For conditions of distribution and use, see copyright notice in nabla.h |
| 4 | + |
| 5 | +#ifndef __NBL_CORE_ITERATABLE_POOL_ADDRESS_ALLOCATOR_H_INCLUDED__ |
| 6 | +#define __NBL_CORE_ITERATABLE_POOL_ADDRESS_ALLOCATOR_H_INCLUDED__ |
| 7 | + |
| 8 | + |
| 9 | +#include<algorithm> |
| 10 | + |
| 11 | +#include "nbl/core/alloc/PoolAddressAllocator.h" |
| 12 | + |
| 13 | + |
| 14 | +namespace nbl |
| 15 | +{ |
| 16 | +namespace core |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | +//! Can only allocate up to a size of a single block, no support for allocations larger than blocksize |
| 21 | +template<typename _size_type> |
| 22 | +class IteratablePoolAddressAllocator : protected PoolAddressAllocator<_size_type> |
| 23 | +{ |
| 24 | + protected: |
| 25 | + inline size_type* begin() { return &getFreeStack(Base::freeStackCtr); } |
| 26 | + inline size_type& getIteratorOffset(size_type i) {return reinterpret_cast<size_type*>(Base::reservedSpace)[Base::blockCount+i];} |
| 27 | + inline const size_type& getIteratorOffset(size_type i) const {return reinterpret_cast<const size_type*>(Base::reservedSpace)[Base::blockCount+i];} |
| 28 | + |
| 29 | + private: |
| 30 | + using Base = PoolAddressAllocator<_size_type>; |
| 31 | + |
| 32 | + void copySupplementaryState(const IteratablePoolAddressAllocator& other, _size_type newBuffSz) |
| 33 | + { |
| 34 | + std::copy(other.begin(),other.end(),begin()); |
| 35 | + for (auto i=0u; i<std::min(blockCount,other.blockCount); i++) |
| 36 | + getIteratorOffset(i) = other.getIteratorOffset(i); |
| 37 | + } |
| 38 | + // use [freeStackCtr,blockCount) as the iteratable range |
| 39 | + // use [blockCount,blockCount*2u) to store backreferences to iterators |
| 40 | + public: |
| 41 | + _NBL_DECLARE_ADDRESS_ALLOCATOR_TYPEDEFS(_size_type); |
| 42 | + |
| 43 | + IteratablePoolAddressAllocator() : Base() {} |
| 44 | + virtual ~IteratablePoolAddressAllocator() {} |
| 45 | + |
| 46 | + IteratablePoolAddressAllocator(void* reservedSpc, _size_type addressOffsetToApply, _size_type alignOffsetNeeded, _size_type maxAllocatableAlignment, size_type bufSz, size_type blockSz) noexcept : |
| 47 | + Base(reservedSpc,addressOffsetToApply,alignOffsetNeeded,maxAllocatableAlignment,bufSz,blockSz) {} |
| 48 | + |
| 49 | + //! When resizing we require that the copying of data buffer has already been handled by the user of the address allocator |
| 50 | + template<typename... Args> |
| 51 | + IteratablePoolAddressAllocator(_size_type newBuffSz, IteratablePoolAddressAllocator&& other, Args&&... args) noexcept : |
| 52 | + Base(newBuffSz,std::move(other),std::forward<Args>(args)...) |
| 53 | + { |
| 54 | + copyState |
| 55 | + } |
| 56 | + |
| 57 | + template<typename... Args> |
| 58 | + IteratablePoolAddressAllocator(_size_type newBuffSz, const IteratablePoolAddressAllocator& other, Args&&... args) noexcept : |
| 59 | + Base(newBuffSz,other,std::forward<Args>(args)...) |
| 60 | + { |
| 61 | + copyState |
| 62 | + } |
| 63 | + |
| 64 | + IteratablePoolAddressAllocator& operator=(IteratablePoolAddressAllocator&& other) |
| 65 | + { |
| 66 | + Base::operator=(std::move(other)); |
| 67 | + return *this; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + //! Functions that actually differ |
| 72 | + inline size_type alloc_addr(size_type bytes, size_type alignment, size_type hint=0ull) noexcept |
| 73 | + { |
| 74 | + const size_type allocatedAddress = Base::alloc_addr(bytes,alignment,hint); |
| 75 | + if (allocatedAddress!=invalid_address) |
| 76 | + { |
| 77 | + *begin() = allocatedAddress; |
| 78 | + getIteratorOffset(addressToBlockID(allocatedAddress)) = freeStackCtr; |
| 79 | + } |
| 80 | + return allocatedAddress; |
| 81 | + } |
| 82 | + |
| 83 | + inline void free_addr(size_type addr, size_type bytes) noexcept |
| 84 | + { |
| 85 | + const size_type iteratorOffset = getIteratorOffset(addressToBlockID(addr)); |
| 86 | + #ifdef _NBL_DEBUG |
| 87 | + assert(iteratorOffset>=freeStackCtr); |
| 88 | + #endif |
| 89 | + // swap the erased element with either end of the array in the contiguous array |
| 90 | + // not using a swap cause it doesn't matter where the erased element points |
| 91 | + const size_type otherNodeOffset = *begin(); |
| 92 | + reinterpret_cast<size_type*>(Base::reservedSpace)[iteratorOffset] = otherNodeOffset; |
| 93 | + // but I need to patch up the back-link of the moved element |
| 94 | + getIteratorOffset(addressToBlockID(otherNodeOffset)) = iteratorOffset; |
| 95 | + |
| 96 | + Base::free_addr(addr,bytes); |
| 97 | + } |
| 98 | + |
| 99 | + // gets a range of all the allocated addresses |
| 100 | + inline const size_type* begin() const {return &getFreeStack(Base::freeStackCtr);} |
| 101 | + inline const size_type* end() const {return &getFreeStack(Base::blockCount);} |
| 102 | + |
| 103 | + |
| 104 | + inline size_type safe_shrink_size(size_type sizeBound, size_type newBuffAlignmentWeCanGuarantee=1u) noexcept |
| 105 | + { |
| 106 | + if (safe_shrink_size_common(sizeBound,newBuffAlignmentWeCanGuarantee)) |
| 107 | + { |
| 108 | + assert(begin()!=end()); // we already checked that freeStackCtr>0 |
| 109 | + sizeBound = *std::max_element(begin(),end()); |
| 110 | + } |
| 111 | + return AddressAllocatorBase<PoolAddressAllocator<_size_type>,_size_type>::safe_shrink_size(sizeBound,newBuffAlignmentWeCanGuarantee); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + static inline size_type reserved_size(size_type maxAlignment, size_type bufSz, size_type blockSz) noexcept |
| 116 | + { |
| 117 | + size_type maxBlockCount = bufSz/blockSz; |
| 118 | + return maxBlockCount*sizeof(size_type)*size_type(2u); |
| 119 | + } |
| 120 | + static inline size_type reserved_size(const IteratablePoolAddressAllocator<_size_type>& other, size_type bufSz) noexcept |
| 121 | + { |
| 122 | + return reserved_size(other.maxRequestableAlignment,bufSz,other.blockSize); |
| 123 | + } |
| 124 | + |
| 125 | + inline void reset() |
| 126 | + { |
| 127 | + Base::reset(); |
| 128 | + } |
| 129 | + inline size_type max_size() const noexcept |
| 130 | + { |
| 131 | + return Base::max_size(); |
| 132 | + } |
| 133 | + inline size_type min_size() const noexcept |
| 134 | + { |
| 135 | + return Base::min_size(); |
| 136 | + } |
| 137 | + inline size_type get_free_size() const noexcept |
| 138 | + { |
| 139 | + return Base::get_free_size(); |
| 140 | + } |
| 141 | + inline size_type get_allocated_size() const noexcept |
| 142 | + { |
| 143 | + return Base::get_allocated_size(); |
| 144 | + } |
| 145 | + inline size_type get_total_size() const noexcept |
| 146 | + { |
| 147 | + return Base::get_total_size(); |
| 148 | + } |
| 149 | + inline size_type addressToBlockID(size_type addr) const noexcept |
| 150 | + { |
| 151 | + return Base::addressToBlockID(addr); |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | + |
| 156 | +} |
| 157 | +} |
| 158 | + |
| 159 | +#include "nbl/core/alloc/AddressAllocatorConcurrencyAdaptors.h" |
| 160 | + |
| 161 | +namespace nbl |
| 162 | +{ |
| 163 | +namespace core |
| 164 | +{ |
| 165 | + |
| 166 | +// aliases |
| 167 | +template<typename size_type> |
| 168 | +using IteratablePoolAddressAllocatorST = IteratablePoolAddressAllocator<size_type>; |
| 169 | + |
| 170 | +template<typename size_type, class RecursiveLockable> |
| 171 | +using IteratablePoolAddressAllocatorMT = AddressAllocatorBasicConcurrencyAdaptor<IteratablePoolAddressAllocator<size_type>,RecursiveLockable>; |
| 172 | + |
| 173 | +} |
| 174 | +} |
| 175 | + |
| 176 | +#endif |
| 177 | + |
0 commit comments