|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <scratchcpp/string_pool.h> |
| 4 | +#include <scratchcpp/stringptr.h> |
| 5 | +#include <scratchcpp/string_functions.h> |
| 6 | +#include <memory> |
| 7 | +#include <unordered_set> |
| 8 | +#include <unordered_map> |
| 9 | +#include <map> |
| 10 | +#include <cassert> |
| 11 | + |
| 12 | +namespace libscratchcpp |
| 13 | +{ |
| 14 | + |
| 15 | +class Thread; |
| 16 | + |
| 17 | +static std::unordered_set<std::unique_ptr<StringPtr>> strings; |
| 18 | +static std::multimap<size_t, StringPtr *> freeStrings; // sorted by allocated space |
| 19 | + |
| 20 | +static std::unordered_map<Thread *, std::unordered_set<StringPtr *>> threadStrings; |
| 21 | +static Thread *currentThread = nullptr; |
| 22 | + |
| 23 | +extern "C" |
| 24 | +{ |
| 25 | + /*! |
| 26 | + * Use this instead of dynamically allocating StringPtr. |
| 27 | + * \note The returned string is uninitialized. Use e. g. string_assign_cstring() to initialize it. |
| 28 | + */ |
| 29 | + StringPtr *string_pool_new() |
| 30 | + { |
| 31 | + if (freeStrings.empty()) { |
| 32 | + auto str = std::make_unique<StringPtr>(); |
| 33 | + StringPtr *ptr = str.get(); |
| 34 | + assert(strings.find(str) == strings.cend()); |
| 35 | + strings.insert(std::move(str)); |
| 36 | + |
| 37 | + if (currentThread) |
| 38 | + threadStrings[currentThread].insert(ptr); |
| 39 | + |
| 40 | + return ptr; |
| 41 | + } |
| 42 | + |
| 43 | + // Optimization: pick string with the highest capacity to minimize allocs |
| 44 | + auto last = std::prev(freeStrings.end()); |
| 45 | + StringPtr *ptr = last->second; |
| 46 | + freeStrings.erase(last); |
| 47 | + |
| 48 | + if (currentThread) |
| 49 | + threadStrings[currentThread].insert(ptr); |
| 50 | + |
| 51 | + return ptr; |
| 52 | + } |
| 53 | + |
| 54 | + /*! Invalidates the given StringPtr so that it can be used for new strings later. */ |
| 55 | + void string_pool_free(StringPtr *str) |
| 56 | + { |
| 57 | + if (currentThread) { |
| 58 | + assert(threadStrings[currentThread].find(str) != threadStrings[currentThread].cend()); |
| 59 | + threadStrings[currentThread].erase(str); |
| 60 | + } |
| 61 | + |
| 62 | + freeStrings.insert(std::pair<size_t, StringPtr *>(str->allocatedSize, str)); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/* string_pool_p.h */ |
| 67 | + |
| 68 | +void string_pool_add_thread(Thread *thread) |
| 69 | +{ |
| 70 | + // Start capturing allocated strings in the thread |
| 71 | + assert(threadStrings.find(thread) == threadStrings.cend()); |
| 72 | + threadStrings[thread] = {}; |
| 73 | +} |
| 74 | + |
| 75 | +void string_pool_remove_thread(Thread *thread) |
| 76 | +{ |
| 77 | + // Free all strings in the thread (garbage collection) |
| 78 | + assert(threadStrings.find(thread) != threadStrings.cend()); |
| 79 | + auto &strings = threadStrings[thread]; |
| 80 | + |
| 81 | + for (StringPtr *str : strings) |
| 82 | + freeStrings.insert(std::pair<size_t, StringPtr *>(str->allocatedSize, str)); |
| 83 | + |
| 84 | + strings.clear(); |
| 85 | + |
| 86 | + if (currentThread == thread) |
| 87 | + currentThread = nullptr; |
| 88 | +} |
| 89 | + |
| 90 | +void string_pool_set_thread(Thread *thread) |
| 91 | +{ |
| 92 | + currentThread = thread; |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace libscratchcpp |
0 commit comments