diff options
Diffstat (limited to 'source/blender/blenlib/BLI_linear_allocator.hh')
-rw-r--r-- | source/blender/blenlib/BLI_linear_allocator.hh | 85 |
1 files changed, 44 insertions, 41 deletions
diff --git a/source/blender/blenlib/BLI_linear_allocator.hh b/source/blender/blenlib/BLI_linear_allocator.hh index f968f9f15ce..39a3ed27f42 100644 --- a/source/blender/blenlib/BLI_linear_allocator.hh +++ b/source/blender/blenlib/BLI_linear_allocator.hh @@ -33,30 +33,30 @@ namespace blender { template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopyable, NonMovable { private: - Allocator m_allocator; - Vector<void *> m_owned_buffers; - Vector<Span<char>> m_unused_borrowed_buffers; + Allocator allocator_; + Vector<void *> owned_buffers_; + Vector<Span<char>> unused_borrowed_buffers_; - uintptr_t m_current_begin; - uintptr_t m_current_end; - uint m_next_min_alloc_size; + uintptr_t current_begin_; + uintptr_t current_end_; + int64_t next_min_alloc_size_; #ifdef DEBUG - uint m_debug_allocated_amount = 0; + int64_t debug_allocated_amount_ = 0; #endif public: LinearAllocator() { - m_current_begin = 0; - m_current_end = 0; - m_next_min_alloc_size = 64; + current_begin_ = 0; + current_end_ = 0; + next_min_alloc_size_ = 64; } ~LinearAllocator() { - for (void *ptr : m_owned_buffers) { - m_allocator.deallocate(ptr); + for (void *ptr : owned_buffers_) { + allocator_.deallocate(ptr); } } @@ -66,21 +66,23 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya * * The alignment has to be a power of 2. */ - void *allocate(uint size, uint alignment) + void *allocate(const int64_t size, const int64_t alignment) { + BLI_assert(size >= 0); BLI_assert(alignment >= 1); BLI_assert(is_power_of_2_i(alignment)); #ifdef DEBUG - m_debug_allocated_amount += size; + debug_allocated_amount_ += size; #endif - uintptr_t alignment_mask = alignment - 1; - uintptr_t potential_allocation_begin = (m_current_begin + alignment_mask) & ~alignment_mask; - uintptr_t potential_allocation_end = potential_allocation_begin + size; + const uintptr_t alignment_mask = alignment - 1; + const uintptr_t potential_allocation_begin = (current_begin_ + alignment_mask) & + ~alignment_mask; + const uintptr_t potential_allocation_end = potential_allocation_begin + size; - if (potential_allocation_end <= m_current_end) { - m_current_begin = potential_allocation_end; + if (potential_allocation_end <= current_end_) { + current_begin_ = potential_allocation_end; return (void *)potential_allocation_begin; } else { @@ -104,7 +106,7 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya * * This method only allocates memory and does not construct the instance. */ - template<typename T> MutableSpan<T> allocate_array(uint size) + template<typename T> MutableSpan<T> allocate_array(int64_t size) { return MutableSpan<T>((T *)this->allocate(sizeof(T) * size, alignof(T)), size); } @@ -140,22 +142,22 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya */ StringRefNull copy_string(StringRef str) { - uint alloc_size = str.size() + 1; + const int64_t alloc_size = str.size() + 1; char *buffer = (char *)this->allocate(alloc_size, 1); str.copy(buffer, alloc_size); return StringRefNull((const char *)buffer); } - MutableSpan<void *> allocate_elements_and_pointer_array(uint element_amount, - uint element_size, - uint element_alignment) + MutableSpan<void *> allocate_elements_and_pointer_array(int64_t element_amount, + int64_t element_size, + int64_t element_alignment) { void *pointer_buffer = this->allocate(element_amount * sizeof(void *), alignof(void *)); void *elements_buffer = this->allocate(element_amount * element_size, element_alignment); MutableSpan<void *> pointers((void **)pointer_buffer, element_amount); void *next_element_buffer = elements_buffer; - for (uint i : IndexRange(element_amount)) { + for (int64_t i : IndexRange(element_amount)) { pointers[i] = next_element_buffer; next_element_buffer = POINTER_OFFSET(next_element_buffer, element_size); } @@ -164,14 +166,14 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya } template<typename T, typename... Args> - Span<T *> construct_elements_and_pointer_array(uint n, Args &&... args) + Span<T *> construct_elements_and_pointer_array(int64_t n, Args &&... args) { MutableSpan<void *> void_pointers = this->allocate_elements_and_pointer_array( n, sizeof(T), alignof(T)); MutableSpan<T *> pointers = void_pointers.cast<T *>(); - for (uint i : IndexRange(n)) { - new (pointers[i]) T(std::forward<Args>(args)...); + for (int64_t i : IndexRange(n)) { + new ((void *)pointers[i]) T(std::forward<Args>(args)...); } return pointers; @@ -183,7 +185,7 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya */ void provide_buffer(void *buffer, uint size) { - m_unused_borrowed_buffers.append(Span<char>((char *)buffer, size)); + unused_borrowed_buffers_.append(Span<char>((char *)buffer, size)); } template<size_t Size, size_t Alignment> @@ -193,25 +195,26 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya } private: - void allocate_new_buffer(uint min_allocation_size) + void allocate_new_buffer(int64_t min_allocation_size) { - for (uint i : m_unused_borrowed_buffers.index_range()) { - Span<char> buffer = m_unused_borrowed_buffers[i]; + for (int64_t i : unused_borrowed_buffers_.index_range()) { + Span<char> buffer = unused_borrowed_buffers_[i]; if (buffer.size() >= min_allocation_size) { - m_unused_borrowed_buffers.remove_and_reorder(i); - m_current_begin = (uintptr_t)buffer.begin(); - m_current_end = (uintptr_t)buffer.end(); + unused_borrowed_buffers_.remove_and_reorder(i); + current_begin_ = (uintptr_t)buffer.begin(); + current_end_ = (uintptr_t)buffer.end(); return; } } - uint size_in_bytes = power_of_2_min_u(std::max(min_allocation_size, m_next_min_alloc_size)); - m_next_min_alloc_size = size_in_bytes * 2; + const int64_t size_in_bytes = power_of_2_min_u( + std::max(min_allocation_size, next_min_alloc_size_)); + next_min_alloc_size_ = size_in_bytes * 2; - void *buffer = m_allocator.allocate(size_in_bytes, 8, AT); - m_owned_buffers.append(buffer); - m_current_begin = (uintptr_t)buffer; - m_current_end = m_current_begin + size_in_bytes; + void *buffer = allocator_.allocate(size_in_bytes, 8, AT); + owned_buffers_.append(buffer); + current_begin_ = (uintptr_t)buffer; + current_end_ = current_begin_ + size_in_bytes; } }; |