realloc_raw
This commit is contained in:
@@ -232,11 +232,44 @@ public:
|
||||
* - If new_size == 0, returns nullptr (no deallocation occurs)
|
||||
* - If ptr is null, behaves like allocate(new_size, alignment)
|
||||
* - If ptr was the last allocation and space exists, extends in place
|
||||
* - Otherwise allocates new space and copies min(old_size, new_size) bytes
|
||||
*
|
||||
* ## Performance:
|
||||
* - In-place extension: O(1) - just updates offset
|
||||
* - Copy required: O(min(old_size, new_size)) for the memcpy
|
||||
* ## Example:
|
||||
* ```cpp
|
||||
* void* ptr = arena.allocate(100);
|
||||
* // ... use ptr ...
|
||||
* ptr = arena.realloc_raw(ptr, 100, 200); // May extend in place or copy
|
||||
* ```
|
||||
*
|
||||
* ## Safety Notes:
|
||||
* - The caller must provide the correct old_size - this is not tracked
|
||||
* - The old pointer becomes invalid if a copy occurs
|
||||
* - Like malloc/realloc, the contents beyond old_size are uninitialized
|
||||
* - When copying to new location, uses the specified alignment
|
||||
*/
|
||||
void *realloc_raw(void *ptr, size_t old_size, size_t new_size,
|
||||
size_t alignment = alignof(std::max_align_t));
|
||||
|
||||
/**
|
||||
* @brief Reallocate memory, extending in place if possible or copying to a
|
||||
* new location.
|
||||
*
|
||||
* This method provides realloc-like functionality for the arena allocator.
|
||||
* If the given pointer was the last allocation and there's sufficient space
|
||||
* in the current block to extend it, the allocation is grown in place.
|
||||
* Otherwise, a new allocation is made and the old data is copied.
|
||||
*
|
||||
* @param ptr Pointer to the existing allocation (must be from this allocator)
|
||||
* @param old_size Size of the existing allocation in T's
|
||||
* @param new_size Desired new size in T's
|
||||
* @return Pointer to the reallocated memory (may be the same as ptr or
|
||||
* different)
|
||||
* @throws std::bad_alloc if memory allocation fails
|
||||
*
|
||||
* ## Behavior:
|
||||
* - If new_size == old_size, returns ptr unchanged
|
||||
* - If new_size == 0, returns nullptr (no deallocation occurs)
|
||||
* - If ptr is null, behaves like allocate(new_size, alignment)
|
||||
* - If ptr was the last allocation and space exists, extends in place
|
||||
*
|
||||
* ## Example:
|
||||
* ```cpp
|
||||
@@ -251,8 +284,10 @@ public:
|
||||
* - Like malloc/realloc, the contents beyond old_size are uninitialized
|
||||
* - When copying to new location, uses the specified alignment
|
||||
*/
|
||||
void *realloc(void *ptr, size_t old_size, size_t new_size,
|
||||
size_t alignment = alignof(std::max_align_t));
|
||||
template <typename T> T *realloc(T *ptr, size_t old_size, size_t new_size) {
|
||||
return static_cast<T *>(realloc_raw(ptr, old_size * sizeof(T),
|
||||
new_size * sizeof(T), alignof(T)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct an object of type T in the arena using placement new.
|
||||
|
||||
Reference in New Issue
Block a user