55#include < scratchcpp/string_functions.h>
66#include < memory>
77#include < unordered_set>
8- #include < unordered_map>
98#include < map>
109#include < cassert>
1110#include < algorithm>
1211
1312namespace libscratchcpp
1413{
1514
16- class Thread ;
17-
1815static std::unordered_set<std::unique_ptr<StringPtr>> strings;
1916static std::multimap<size_t , StringPtr *> freeStrings; // sorted by allocated space
2017
21- static std::unordered_map<Thread *, std::unordered_set<StringPtr *>> threadStrings;
22- static Thread *currentThread = nullptr ;
23-
2418extern " C"
2519{
2620 /* !
2721 * Use this instead of dynamically allocating StringPtr.
28- * \param[in] internal Whether the string should be deallocated when the current thread is removed (it's only used within the thread).
2922 * \note The returned string is uninitialized. Use e. g. string_assign_cstring() to initialize it.
3023 */
31- StringPtr *string_pool_new (bool internal )
24+ StringPtr *string_pool_new ()
3225 {
3326 if (freeStrings.empty ()) {
3427 auto str = std::make_unique<StringPtr>();
3528 StringPtr *ptr = str.get ();
3629 assert (strings.find (str) == strings.cend ());
3730 strings.insert (std::move (str));
3831
39- if (internal && currentThread)
40- threadStrings[currentThread].insert (ptr);
41-
4232 return ptr;
4333 }
4434
@@ -47,51 +37,16 @@ extern "C"
4737 StringPtr *ptr = last->second ;
4838 freeStrings.erase (last);
4939
50- if (internal && currentThread)
51- threadStrings[currentThread].insert (ptr);
52-
5340 return ptr;
5441 }
5542
5643 /* ! Invalidates the given StringPtr so that it can be used for new strings later. */
5744 void string_pool_free (StringPtr *str)
5845 {
59- if (currentThread)
60- threadStrings[currentThread].erase (str);
61-
6246 assert (std::find_if (freeStrings.begin (), freeStrings.end (), [str](const std::pair<size_t , StringPtr *> &p) { return p.second == str; }) == freeStrings.end ());
6347 assert (std::find_if (strings.begin (), strings.end (), [str](const std::unique_ptr<StringPtr> &p) { return p.get () == str; }) != strings.end ());
6448 freeStrings.insert (std::pair<size_t , StringPtr *>(str->allocatedSize , str));
6549 }
6650}
6751
68- /* string_pool_p.h */
69-
70- void string_pool_add_thread (Thread *thread)
71- {
72- // Start capturing allocated strings in the thread
73- assert (threadStrings.find (thread) == threadStrings.cend ());
74- threadStrings[thread] = {};
75- }
76-
77- void string_pool_remove_thread (Thread *thread)
78- {
79- // Free all strings in the thread (garbage collection)
80- assert (threadStrings.find (thread) != threadStrings.cend ());
81- auto &strings = threadStrings[thread];
82-
83- for (StringPtr *str : strings)
84- freeStrings.insert (std::pair<size_t , StringPtr *>(str->allocatedSize , str));
85-
86- threadStrings.erase (thread);
87-
88- if (currentThread == thread)
89- currentThread = nullptr ;
90- }
91-
92- void string_pool_set_thread (Thread *thread)
93- {
94- currentThread = thread;
95- }
96-
9752} // namespace libscratchcpp
0 commit comments