Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ArenaAllocator Class Reference
Collaboration diagram for duckdb::ArenaAllocator:

Public Member Functions

DUCKDB_API ArenaAllocator (Allocator &allocator, idx_t initial_capacity=ARENA_ALLOCATOR_INITIAL_CAPACITY)
data_ptr_t Allocate (idx_t len)
DUCKDB_API data_ptr_t Reallocate (data_ptr_t pointer, idx_t old_size, idx_t size)
DUCKDB_API data_ptr_t AllocateAligned (idx_t size)
DUCKDB_API data_ptr_t ReallocateAligned (data_ptr_t pointer, idx_t old_size, idx_t size)
DUCKDB_API void AlignNext ()
 Increment the internal cursor (if required) so the next allocation is guaranteed to be aligned to 8 bytes.
DUCKDB_API void ShrinkHead (idx_t shrink_size) const
DUCKDB_API void Reset ()
 Resets the current head and destroys all previous arena chunks.
DUCKDB_API void Destroy ()
DUCKDB_API void Move (ArenaAllocator &allocator)
DUCKDB_API ArenaChunk * GetHead ()
DUCKDB_API ArenaChunk * GetTail ()
DUCKDB_API bool IsEmpty () const
DUCKDB_API idx_t SizeInBytes () const
 Get the total used size (not cached).
DUCKDB_API idx_t AllocationSize () const
 Get the currently allocated size in bytes (cached, read from "allocated_size").
Allocator & GetAllocator ()
 Returns an "Allocator" wrapper for this arena allocator.
template<class T, class... ARGS>
T * Make (ARGS &&... args)
template<class T, class... ARGS>
arena_ptr< T > MakePtr (ARGS &&... args)
template<class T, class... ARGS>
unsafe_arena_ptr< T > MakeUnsafePtr (ARGS &&... args)
String MakeString (const char *data, const size_t len)
String MakeString (const std::string &data)

Static Public Attributes

static constexpr const idx_t ARENA_ALLOCATOR_INITIAL_CAPACITY = 2048
static constexpr const idx_t ARENA_ALLOCATOR_MAX_CAPACITY = 1ULL << 24ULL

Private Member Functions

void AllocateNewBlock (idx_t min_size)

Private Attributes

Allocator & allocator
 Internal allocator that is used by the arena allocator.
idx_t initial_capacity
unsafe_unique_ptr< ArenaChunk > head
ArenaChunk * tail
Allocator arena_allocator
 An allocator wrapper using this arena allocator.
idx_t allocated_size = 0
 The total allocated size.

Constructor & Destructor Documentation

◆ ArenaAllocator()

duckdb::ArenaAllocator::ArenaAllocator ( Allocator & allocator,
idx_t initial_capacity = ARENA_ALLOCATOR_INITIAL_CAPACITY )
explicit
386095 : allocator(allocator), initial_capacity(initial_capacity),
386096 arena_allocator(ArenaAllocatorAllocate, ArenaAllocatorFree, ArenaAllocateReallocate,
386097 make_uniq<ArenaAllocatorData>(*this)) {
386098 head = nullptr;
386099 tail = nullptr;
386100}
Allocator arena_allocator
An allocator wrapper using this arena allocator.
Definition duckdb.hpp:9597
Allocator & allocator
Internal allocator that is used by the arena allocator.
Definition duckdb.hpp:9592

◆ ~ArenaAllocator()

duckdb::ArenaAllocator::~ArenaAllocator ( )
386102 {
386103}

Member Function Documentation

◆ Allocate()

data_ptr_t duckdb::ArenaAllocator::Allocate ( idx_t len)
inline
9508 {
9509 D_ASSERT(!head || head->current_position <= head->maximum_size);
9510 if (!head || head->current_position + len > head->maximum_size) {
9511 AllocateNewBlock(len);
9512 }
9513 D_ASSERT(head->current_position + len <= head->maximum_size);
9514 auto result = head->data.get() + head->current_position;
9515 head->current_position += len;
9516 return result;
9517 }

◆ Reallocate()

data_ptr_t duckdb::ArenaAllocator::Reallocate ( data_ptr_t pointer,
idx_t old_size,
idx_t size )
386138 {
386139 D_ASSERT(head);
386140 if (old_size == size) {
386141 // nothing to do
386142 return pointer;
386143 }
386144
386145 const auto head_ptr = head->data.get() + head->current_position - old_size;
386146 int64_t current_position = NumericCast<int64_t>(head->current_position);
386147 int64_t diff = NumericCast<int64_t>(size) - NumericCast<int64_t>(old_size);
386148 if (pointer == head_ptr &&
386149 (size < old_size || current_position + diff <= NumericCast<int64_t>(head->maximum_size))) {
386150 // passed pointer is the head pointer, and the diff fits on the current chunk
386151 head->current_position = NumericCast<idx_t>(current_position + diff);
386152 return pointer;
386153 } else {
386154 // allocate new memory
386155 auto result = Allocate(size);
386156 memcpy(result, pointer, old_size);
386157 return result;
386158 }
386159}
::int64_t int64_t

◆ AllocateAligned()

data_ptr_t duckdb::ArenaAllocator::AllocateAligned ( idx_t size)
386168 {
386169 AlignNext();
386170 return Allocate(AlignValue<idx_t>(size));
386171}
DUCKDB_API void AlignNext()
Increment the internal cursor (if required) so the next allocation is guaranteed to be aligned to 8 b...
Definition duckdb.cpp:386161
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126

◆ ReallocateAligned()

data_ptr_t duckdb::ArenaAllocator::ReallocateAligned ( data_ptr_t pointer,
idx_t old_size,
idx_t size )
386173 {
386174 AlignNext();
386175 return Reallocate(pointer, old_size, AlignValue<idx_t>(size));
386176}

◆ AlignNext()

void duckdb::ArenaAllocator::AlignNext ( )

Increment the internal cursor (if required) so the next allocation is guaranteed to be aligned to 8 bytes.

386161 {
386162 if (head && !ValueIsAligned<idx_t>(head->current_position)) {
386163 // move the current position forward so that the next allocation is aligned
386164 head->current_position = AlignValue<idx_t>(head->current_position);
386165 }
386166}
Here is the call graph for this function:

◆ ShrinkHead()

DUCKDB_API void duckdb::ArenaAllocator::ShrinkHead ( idx_t shrink_size) const
inline

This shrinks the LAST allocation that was made using the allocator Note that we can ONLY safely call this method if Allocate has been called previously with a size >= shrink_size

9528 {
9529 D_ASSERT(head && head->current_position >= shrink_size);
9530 head->current_position -= shrink_size;
9531 }

◆ Reset()

void duckdb::ArenaAllocator::Reset ( )

Resets the current head and destroys all previous arena chunks.

386178 {
386179 if (head) {
386180 // destroy all chunks except the current one
386181 if (head->next) {
386182 auto current_next = std::move(head->next);
386183 while (current_next) {
386184 current_next = std::move(current_next->next);
386185 }
386186 }
386187 tail = head.get();
386188
386189 // reset the head
386190 head->current_position = 0;
386191 head->prev = nullptr;
386192 }
386193 allocated_size = 0;
386194}
idx_t allocated_size
The total allocated size.
Definition duckdb.hpp:9599

◆ Destroy()

void duckdb::ArenaAllocator::Destroy ( )
386196 {
386197 head = nullptr;
386198 tail = nullptr;
386199 allocated_size = 0;
386200}

◆ Move()

void duckdb::ArenaAllocator::Move ( ArenaAllocator & allocator)
386202 {
386203 D_ASSERT(!other.head);
386204 other.tail = tail;
386205 other.head = std::move(head);
386206 other.initial_capacity = initial_capacity;
386207 other.allocated_size = allocated_size;
386208 Destroy();
386209}

◆ GetHead()

ArenaChunk * duckdb::ArenaAllocator::GetHead ( )
386211 {
386212 return head.get();
386213}

◆ GetTail()

ArenaChunk * duckdb::ArenaAllocator::GetTail ( )
386215 {
386216 return tail;
386217}

◆ IsEmpty()

bool duckdb::ArenaAllocator::IsEmpty ( ) const
386219 {
386220 return head == nullptr;
386221}

◆ SizeInBytes()

idx_t duckdb::ArenaAllocator::SizeInBytes ( ) const

Get the total used size (not cached).

386223 {
386224 idx_t total_size = 0;
386225 if (!IsEmpty()) {
386226 auto current = head.get();
386227 while (current != nullptr) {
386228 total_size += current->current_position;
386229 current = current->next.get();
386230 }
386231 }
386232 return total_size;
386233}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ AllocationSize()

idx_t duckdb::ArenaAllocator::AllocationSize ( ) const

Get the currently allocated size in bytes (cached, read from "allocated_size").

386235 {
386236 D_ASSERT(head || allocated_size == 0);
386237 return allocated_size;
386238}

◆ GetAllocator()

Allocator & duckdb::ArenaAllocator::GetAllocator ( )
inline

Returns an "Allocator" wrapper for this arena allocator.

9548 {
9549 return arena_allocator;
9550 }

◆ Make()

template<class T, class... ARGS>
T * duckdb::ArenaAllocator::Make ( ARGS &&... args)
inline
9553 {
9554 auto mem = AllocateAligned(sizeof(T));
9555 return new (mem) T(std::forward<ARGS>(args)...);
9556 }

◆ MakePtr()

template<class T, class... ARGS>
arena_ptr< T > duckdb::ArenaAllocator::MakePtr ( ARGS &&... args)
inline
9559 {
9560 return arena_ptr<T>(Make<T>(std::forward<ARGS>(args)...));
9561 }

◆ MakeUnsafePtr()

template<class T, class... ARGS>
unsafe_arena_ptr< T > duckdb::ArenaAllocator::MakeUnsafePtr ( ARGS &&... args)
inline
9564 {
9565 return unsafe_arena_ptr<T>(Make<T>(std::forward<ARGS>(args)...));
9566 }

◆ MakeString() [1/2]

String duckdb::ArenaAllocator::MakeString ( const char * data,
const size_t len )
inline
9568 {
9569 data_ptr_t mem = nullptr;
9570
9571 D_ASSERT(len < NumericLimits<uint32_t>::Maximum());
9572 const auto size = static_cast<uint32_t>(len);
9573 if (!String::CanBeInlined(size)) {
9574 // If the string can't be inlined, we allocate it on the arena allocator
9575 mem = AllocateAligned(sizeof(char) * size + 1); // +1 for null terminator
9576 memcpy(mem, data, size);
9577 mem[size] = '\0';
9578 }
9579
9580 return String::Reference(mem ? reinterpret_cast<char *>(mem) : data, size);
9581 }
GOpaque< Size > size(const GMat &src)
::uint32_t uint32_t

◆ MakeString() [2/2]

String duckdb::ArenaAllocator::MakeString ( const std::string & data)
inline
9583 {
9584 return MakeString(data.c_str(), data.size());
9585 }

◆ AllocateNewBlock()

void duckdb::ArenaAllocator::AllocateNewBlock ( idx_t min_size)
private
386105 {
386106 idx_t capacity;
386107 // start off with either (1) initial capacity (if we have no block) or (2) capacity of the previous block
386108 if (!head) {
386109 capacity = initial_capacity;
386110 } else {
386111 capacity = head->maximum_size;
386112 }
386113 // capacity of the previous block can be bigger than the max capacity if we allocate len > max capacity
386114 // for new blocks - try to set it back to the max capacity
386115 if (capacity > ARENA_ALLOCATOR_MAX_CAPACITY) {
386116 capacity = ARENA_ALLOCATOR_MAX_CAPACITY;
386117 }
386118 // if we are below the max capacity - double the size of the block
386119 if (capacity < ARENA_ALLOCATOR_MAX_CAPACITY) {
386120 capacity *= 2;
386121 }
386122 // we double the size until we can fit `len`
386123 // this is generally only relevant if len is very large
386124 while (capacity < min_size) {
386125 capacity *= 2;
386126 }
386127 auto new_chunk = make_unsafe_uniq<ArenaChunk>(allocator, capacity);
386128 if (head) {
386129 head->prev = new_chunk.get();
386130 new_chunk->next = std::move(head);
386131 } else {
386132 tail = new_chunk.get();
386133 }
386134 head = std::move(new_chunk);
386135 allocated_size += capacity;
386136}

The documentation for this class was generated from the following files: