5 #include "small_vector.h"
7 // XXX: str arena hardcoded now to handle at most 1024 strings
11 static const size_t PreAllocBufSize = 256;
12 static const size_t NStrs = 1024;
14 static const size_t MinStrReserveLength = 2 * CACHELINE_SIZE;
15 static_assert(PreAllocBufSize >= MinStrReserveLength, "xx");
20 for (size_t i = 0; i < NStrs; i++)
21 strs[i].reserve(PreAllocBufSize);
24 // non-copyable/non-movable for the time being
25 str_arena(str_arena &&) = delete;
26 str_arena(const str_arena &) = delete;
27 str_arena &operator=(const str_arena &) = delete;
36 // next() is guaranteed to return an empty string
40 if (likely(n < NStrs)) {
41 std::string * const px = &strs[n++];
43 INVARIANT(manages(px));
46 // only loaders need this- and this allows us to use a unified
47 // str_arena for loaders/workers
48 overflow.emplace_back(new std::string);
50 return overflow.back().get();
60 return_last(std::string *px)
67 manages(const std::string *px) const
69 return manages_local(px) || manages_overflow(px);
75 manages_local(const std::string *px) const
79 if (px >= &strs[NStrs])
81 return 0 == ((reinterpret_cast<const char *>(px) -
82 reinterpret_cast<const char *>(&strs[0])) % sizeof(std::string));
86 manages_overflow(const std::string *px) const
88 for (auto &p : overflow)
95 std::string strs[NStrs];
96 std::vector<std::unique_ptr<std::string>> overflow;
100 class scoped_str_arena {
102 scoped_str_arena(str_arena *arena)
107 scoped_str_arena(str_arena &arena)
112 scoped_str_arena(scoped_str_arena &&) = default;
115 scoped_str_arena(const scoped_str_arena &) = delete;
116 scoped_str_arena &operator=(const scoped_str_arena &) = delete;
125 inline ALWAYS_INLINE str_arena *