1#ifndef OPENXLSX_XLSTRINGARENA_HPP
2#define OPENXLSX_XLSTRINGARENA_HPP
26 explicit XLStringArena(
size_t blockSize = 4 * 1024 * 1024) : m_blockSize(blockSize), m_currentOffset(blockSize) {}
43 static constexpr char emptyStr[] =
"";
47 const size_t needed = str.size() + 1;
49 if (m_currentOffset + needed > blockCapacity()) {
51 const size_t allocSize = std::max(m_blockSize, needed);
52 if (!m_freeBlocks.empty() && m_freeBlocks.back().capacity >= allocSize) {
53 m_activeBlocks.push_back(std::move(m_freeBlocks.back()));
54 m_freeBlocks.pop_back();
57 m_activeBlocks.push_back({std::make_unique<char[]>(allocSize), allocSize});
62 char* dest = m_activeBlocks.back().data.get() + m_currentOffset;
63 std::copy(str.begin(), str.end(), dest);
64 dest[str.size()] =
'\0';
66 m_currentOffset += needed;
67 return {dest, str.size()};
78 for (
auto& blk : m_activeBlocks) m_freeBlocks.push_back(std::move(blk));
79 m_activeBlocks.clear();
80 m_currentOffset = m_blockSize;
88 m_activeBlocks.clear();
90 m_currentOffset = m_blockSize;
99 for (
const auto& blk : m_activeBlocks) total += blk.capacity;
106 [[nodiscard]]
size_t used() const noexcept {
return m_currentOffset; }
111 std::unique_ptr<char[]> data;
115 [[nodiscard]]
size_t blockCapacity() const noexcept {
return m_activeBlocks.empty() ? 0 : m_activeBlocks.back().capacity; }
118 size_t m_currentOffset;
119 std::vector<Block> m_activeBlocks;
120 std::vector<Block> m_freeBlocks;
String memory pool (Arena Allocator) with block recycling.
Definition XLStringArena.hpp:23
std::string_view store(std::string_view str)
Store a string in the arena and return a non-owning view.
Definition XLStringArena.hpp:40
XLStringArena(size_t blockSize=4 *1024 *1024)
Definition XLStringArena.hpp:26
void reset() noexcept
Release all memory (active + free blocks). Use at shutdown.
Definition XLStringArena.hpp:86
XLStringArena & operator=(const XLStringArena &)=delete
size_t capacity() const noexcept
Total bytes allocated (active blocks only).
Definition XLStringArena.hpp:96
XLStringArena(const XLStringArena &)=delete
size_t used() const noexcept
Bytes used in the current active block.
Definition XLStringArena.hpp:106
void clear() noexcept
Recycle all blocks for reuse without freeing heap memory.
Definition XLStringArena.hpp:76
XLStringArena(XLStringArena &&) noexcept=default
Definition IZipArchive.hpp:18
Definition XLCellIterator.hpp:121