37 constexpr static inline int64_t large_buffer_threshold = 4096;
40#ifdef BLI_DEBUG_LINEAR_ALLOCATOR_SIZE
41 int64_t user_requested_size_ = 0;
42 int64_t owned_allocation_size_ = 0;
53 for (
void *
ptr : owned_buffers_) {
54 allocator_.deallocate(
ptr);
70 const uintptr_t alignment_mask = alignment - 1;
71 const uintptr_t potential_allocation_begin = (current_begin_ + alignment_mask) &
73 const uintptr_t potential_allocation_end = potential_allocation_begin +
size;
75 if (potential_allocation_end <= current_end_) {
76#ifdef BLI_DEBUG_LINEAR_ALLOCATOR_SIZE
77 user_requested_size_ +=
size;
79 current_begin_ = potential_allocation_end;
80 return reinterpret_cast<void *
>(potential_allocation_begin);
82 if (size <= large_buffer_threshold) {
83 this->allocate_new_buffer(size + alignment, alignment);
84 return this->
allocate(size, alignment);
86#ifdef BLI_DEBUG_LINEAR_ALLOCATOR_SIZE
87 user_requested_size_ +=
size;
89 return this->allocator_large_buffer(size, alignment);
99 return static_cast<T *
>(this->
allocate(
sizeof(T),
alignof(T)));
109 T *
array =
static_cast<T *
>(this->
allocate(
sizeof(T) * size,
alignof(T)));
123 void *buffer = this->
allocate(
sizeof(T),
alignof(T));
124 T *value =
new (buffer)
T(std::forward<Args>(args)...);
133 template<
typename T,
typename... Args>
138 new (&
array[i])
T(std::forward<Args>(args)...);
163 char *buffer =
static_cast<char *
>(this->
allocate(alloc_size, 1));
164 str.copy(buffer, alloc_size);
172 void *pointer_buffer = this->
allocate(element_amount *
sizeof(
void *),
alignof(
void *));
173 void *elements_buffer = this->
allocate(element_amount * element_size, element_alignment);
176 void *next_element_buffer = elements_buffer;
178 pointers[i] = next_element_buffer;
179 next_element_buffer =
POINTER_OFFSET(next_element_buffer, element_size);
185 template<
typename T,
typename... Args>
189 n,
sizeof(T),
alignof(T));
193 new (
static_cast<void *
>(pointers[i]))
T(std::forward<Args>(args)...);
207 current_end_ = current_begin_ +
size;
210 template<
size_t Size,
size_t Alignment>
228 const void *free_after)
232 if (original_allocation_size <= large_buffer_threshold) {
238 const int64_t freed_bytes_num = current_begin_ - new_begin;
239 if (freed_bytes_num > 0) {
240 current_begin_ = new_begin;
243 current_begin_ = new_begin;
257 owned_buffers_.
extend(other.owned_buffers_);
258#ifdef BLI_DEBUG_LINEAR_ALLOCATOR_SIZE
259 user_requested_size_ += other.user_requested_size_;
260 owned_allocation_size_ += other.owned_allocation_size_;
262 other.owned_buffers_.clear();
263 std::destroy_at(&other);
268 void allocate_new_buffer(
int64_t min_allocation_size,
int64_t min_alignment)
273 int64_t size_in_bytes = min_allocation_size;
274 if (size_in_bytes <= large_buffer_threshold) {
276 const int grow_size = 1 << std::min<int>(owned_buffers_.
size() + 6, 20);
277 size_in_bytes = std::min(large_buffer_threshold,
278 std::max<int64_t>(size_in_bytes, grow_size));
281 void *buffer = this->allocated_owned(size_in_bytes, min_alignment);
283 current_end_ = current_begin_ + size_in_bytes;
286 void *allocator_large_buffer(
const int64_t size,
const int64_t alignment)
288 return this->allocated_owned(size, alignment);
291 void *allocated_owned(
const int64_t size,
const int64_t alignment)
293 void *buffer = allocator_.allocate(size, alignment, __func__);
294 owned_buffers_.
append(buffer);
295#ifdef BLI_DEBUG_LINEAR_ALLOCATOR_SIZE
296 owned_allocation_size_ +=
size;
#define POINTER_OFFSET(v, ofs)
#define BLI_NO_UNIQUE_ADDRESS
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
destruct_ptr< T > construct(Args &&...args)
void provide_buffer(void *buffer, const int64_t size)
MutableSpan< T > allocate_array(int64_t size)
StringRefNull copy_string(StringRef str)
void * allocate(const int64_t size, const int64_t alignment)
void provide_buffer(AlignedBuffer< Size, Alignment > &aligned_buffer)
MutableSpan< T > construct_array(int64_t size, Args &&...args)
void free_end_of_previous_allocation(const int64_t original_allocation_size, const void *free_after)
Span< T * > construct_elements_and_pointer_array(int64_t n, Args &&...args)
MutableSpan< T > construct_array_copy(Span< T > src)
void transfer_ownership_from(LinearAllocator<> &other)
MutableSpan< void * > allocate_elements_and_pointer_array(int64_t element_amount, int64_t element_size, int64_t element_alignment)
constexpr MutableSpan< NewT > cast() const
constexpr T * data() const
constexpr const T * data() const
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr int64_t size() const
void append(const T &value)
void extend(Span< T > array)
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
_W64 unsigned int uintptr_t