Add format utility
This commit is contained in:
@@ -434,6 +434,40 @@ public:
|
||||
return current_block_ ? current_block_->size - current_block_->offset : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get all available space in the current block and claim it
|
||||
* immediately.
|
||||
*
|
||||
* This method returns a pointer to all remaining space in the current block
|
||||
* and immediately marks it as used in the arena. The caller should use
|
||||
* realloc() to shrink the allocation to the actual amount needed.
|
||||
*
|
||||
* If no block exists or current block is full, creates a new block.
|
||||
*
|
||||
* @return Pointer to allocated space and the number of bytes allocated
|
||||
* @note The caller must call realloc() to return unused space
|
||||
* @note This is designed for speculative operations like printf formatting
|
||||
* @note Postcondition: always returns at least 1 byte
|
||||
*/
|
||||
struct AllocatedSpace {
|
||||
char *ptr;
|
||||
size_t allocated_bytes;
|
||||
};
|
||||
|
||||
AllocatedSpace allocate_remaining_space() {
|
||||
if (!current_block_ || available_in_current_block() == 0) {
|
||||
add_block(initial_block_size_);
|
||||
}
|
||||
|
||||
char *allocated_ptr = current_block_->data() + current_block_->offset;
|
||||
size_t available = available_in_current_block();
|
||||
|
||||
// Claim all remaining space
|
||||
current_block_->offset = current_block_->size;
|
||||
|
||||
return {allocated_ptr, available};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the total number of blocks in the allocator.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user