11#ifndef OPENXLSX_XLSTRINGARENA_HPP
12#define OPENXLSX_XLSTRINGARENA_HPP
36 explicit XLStringArena(
size_t blockSize = 4 * 1024 * 1024) : m_blockSize(blockSize), m_currentOffset(blockSize) {}
53 static constexpr char emptyStr[] =
"";
57 const size_t needed = str.size() + 1;
59 if (m_currentOffset + needed > blockCapacity()) {
61 const size_t allocSize = std::max(m_blockSize, needed);
62 if (!m_freeBlocks.empty() && m_freeBlocks.back().capacity >= allocSize) {
63 m_activeBlocks.push_back(std::move(m_freeBlocks.back()));
64 m_freeBlocks.pop_back();
67 m_activeBlocks.push_back({std::make_unique<char[]>(allocSize), allocSize});
72 char* dest = m_activeBlocks.back().data.get() + m_currentOffset;
73 std::copy(str.begin(), str.end(), dest);
74 dest[str.size()] =
'\0';
76 m_currentOffset += needed;
77 return {dest, str.size()};
88 for (
auto& blk : m_activeBlocks) m_freeBlocks.push_back(std::move(blk));
89 m_activeBlocks.clear();
90 m_currentOffset = m_blockSize;
98 m_activeBlocks.clear();
100 m_currentOffset = m_blockSize;
109 for (
const auto& blk : m_activeBlocks) total += blk.capacity;
116 [[nodiscard]]
size_t used() const noexcept {
return m_currentOffset; }
121 std::unique_ptr<char[]> data;
125 [[nodiscard]]
size_t blockCapacity() const noexcept {
return m_activeBlocks.empty() ? 0 : m_activeBlocks.back().capacity; }
128 size_t m_currentOffset;
129 std::vector<Block> m_activeBlocks;
130 std::vector<Block> m_freeBlocks;
std::string_view store(std::string_view str)
Store a string in the arena and return a non-owning view.
Definition XLStringArena.hpp:50
XLStringArena(size_t blockSize=4 *1024 *1024)
Definition XLStringArena.hpp:36
void reset() noexcept
Release all memory (active + free blocks). Use at shutdown.
Definition XLStringArena.hpp:96
XLStringArena & operator=(const XLStringArena &)=delete
size_t capacity() const noexcept
Total bytes allocated (active blocks only).
Definition XLStringArena.hpp:106
XLStringArena(const XLStringArena &)=delete
size_t used() const noexcept
Bytes used in the current active block.
Definition XLStringArena.hpp:116
void clear() noexcept
Recycle all blocks for reuse without freeing heap memory.
Definition XLStringArena.hpp:86
XLStringArena(XLStringArena &&) noexcept=default
Definition IXLCellProvider.hpp:24
Definition XLCellIterator.hpp:131