OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLStringArena.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLSTRINGARENA_HPP
12#define OPENXLSX_XLSTRINGARENA_HPP
13
14#include <algorithm>
15#include <cstdint>
16#include <memory>
17#include <string_view>
18#include <vector>
19
20namespace OpenXLSX
21{
22
33 {
34 public:
35 // Default block size: 4 MB accommodates many short strings without fragmentation.
36 explicit XLStringArena(size_t blockSize = 4 * 1024 * 1024) : m_blockSize(blockSize), m_currentOffset(blockSize) {}
37
38 // Disable copy — string_view pointers would dangle after a copy
39 XLStringArena(const XLStringArena&) = delete;
41
42 XLStringArena(XLStringArena&&) noexcept = default;
43 XLStringArena& operator=(XLStringArena&&) noexcept = default;
44
50 [[nodiscard]] std::string_view store(std::string_view str)
51 {
52 if (str.empty()) {
53 static constexpr char emptyStr[] = "";
54 return {emptyStr, 0};
55 }
56
57 const size_t needed = str.size() + 1; // +1 for null terminator
58
59 if (m_currentOffset + needed > blockCapacity()) {
60 // Try to reuse a free block, otherwise allocate a new one
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();
65 }
66 else {
67 m_activeBlocks.push_back({std::make_unique<char[]>(allocSize), allocSize});
68 }
69 m_currentOffset = 0;
70 }
71
72 char* dest = m_activeBlocks.back().data.get() + m_currentOffset;
73 std::copy(str.begin(), str.end(), dest);
74 dest[str.size()] = '\0';
75
76 m_currentOffset += needed;
77 return {dest, str.size()};
78 }
79
86 void clear() noexcept
87 {
88 for (auto& blk : m_activeBlocks) m_freeBlocks.push_back(std::move(blk));
89 m_activeBlocks.clear();
90 m_currentOffset = m_blockSize; // force new-block allocation on next store
91 }
92
96 void reset() noexcept
97 {
98 m_activeBlocks.clear();
99 m_freeBlocks.clear();
100 m_currentOffset = m_blockSize;
101 }
102
106 [[nodiscard]] size_t capacity() const noexcept
107 {
108 size_t total = 0;
109 for (const auto& blk : m_activeBlocks) total += blk.capacity;
110 return total;
111 }
112
116 [[nodiscard]] size_t used() const noexcept { return m_currentOffset; }
117
118 private:
119 struct Block
120 {
121 std::unique_ptr<char[]> data;
122 size_t capacity{0};
123 };
124
125 [[nodiscard]] size_t blockCapacity() const noexcept { return m_activeBlocks.empty() ? 0 : m_activeBlocks.back().capacity; }
126
127 size_t m_blockSize; // target size for new blocks
128 size_t m_currentOffset; // write position within the current active block
129 std::vector<Block> m_activeBlocks; // currently in-use blocks
130 std::vector<Block> m_freeBlocks; // recycled blocks awaiting reuse
131 };
132
133} // namespace OpenXLSX
134
135#endif // OPENXLSX_XLSTRINGARENA_HPP
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