2#include <OsoMemoryProfiler/OsoMemoryProfiler.h>
3#include "ImmutableString.h"
4#include "Collections/PoolBase.h"
10 constexpr size_t S =
sizeof(size_t);
11 constexpr size_t shortStringSize = 32;
13 constexpr size_t getByteSize(
size_t N)
18 constexpr bool isShortString(
size_t byteSize)
20 return byteSize < shortStringSize;
28 StaticStore& getStore()
30 static StaticStore store;
41 const size_t N = view.
size();
43 assert(ptr ==
nullptr);
45 const size_t byteSize = getByteSize(N);
46 if (isShortString(byteSize)) {
47 StaticStore& store = getStore();
48 std::lock_guard guard(store.lock);
49 ptr =
static_cast<char*
>(store.shortStringPool.allocate());
52 ptr =
new char[byteSize];
53 OsoMP_Allocate(uint8_t(MemBlockType::Strings), ptr, byteSize,
nullptr, 0);
56 std::memcpy(ptr, &N, S);
57 std::memcpy(ptr + S, view.
data(), N);
62void Cogs::ImmutableString::release()
66 std::memcpy(&N, ptr,
sizeof(N));
68 const size_t byteSize = getByteSize(N);
69 if (isShortString(byteSize)) {
70 StaticStore& store = getStore();
71 std::lock_guard guard(store.lock);
72 store.shortStringPool.deallocate(ptr);
75 OsoMP_Free(uint8_t(MemBlockType::Strings), ptr,
nullptr, 0);
constexpr ImmutableString() noexcept=default
Create an empty string.
static Allocator * defaultAllocator()
Gets the system default allocator.
Provides a weakly referenced view over the contents of a string.
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
constexpr size_t size() const noexcept
Get the size of the string.
constexpr bool empty() const noexcept
Check if the string is empty.
Contains collection classes used to manage object storage.
Contains all Cogs related functionality.