├── StringPool ├── StringPoolPerf.xlsx ├── StringPool │ ├── StringPool.vcxproj.filters │ ├── StringPool.vcxproj │ ├── TestStringPool.cpp │ └── StringPool.h └── StringPool.sln ├── README.md └── LICENSE /StringPool/StringPoolPerf.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GiovanniDicanio/StringPool/HEAD/StringPool/StringPoolPerf.xlsx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StringPool 2 | Custom C++ String Pool Allocator, including benchmark code to compare vs. STL strings. 3 | 4 | by Giovanni Dicanio 5 | 6 | Reusable code can be found in the [`StringPool.h` header](https://github.com/GiovanniDicanio/StringPool/blob/master/StringPool/StringPool/StringPool.h). 7 | 8 | This repo constains a VS2015 solution; anyway, I wrote the C++ code to be _cross-platform_ (e.g. there are no dependencies from `` or other Windows-specific headers). 9 | 10 | The basic idea of this custom string pool allocator is to allocate big chunks of memory, and then serve single string allocations carving memory from inside those blocks, with a simple _fast_ pointer increase. 11 | 12 | Moreover, the custom string class implemented to work with this allocator is very fast to sort as well, as it just contains a string pointer and a length data members, and its copy semantics is simple member-wise copy. 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 by Giovanni Dicanio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /StringPool/StringPool/StringPool.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /StringPool/StringPool.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StringPool", "StringPool\StringPool.vcxproj", "{D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Debug|x64.ActiveCfg = Debug|x64 17 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Debug|x64.Build.0 = Debug|x64 18 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Debug|x86.ActiveCfg = Debug|Win32 19 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Debug|x86.Build.0 = Debug|Win32 20 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Release|x64.ActiveCfg = Release|x64 21 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Release|x64.Build.0 = Release|x64 22 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Release|x86.ActiveCfg = Release|Win32 23 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /StringPool/StringPool/StringPool.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {D68B5D81-F1B6-4D35-9C0B-F58B04DFCEDC} 23 | Win32Proj 24 | StringPool 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | 87 | 88 | Level4 89 | Disabled 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | true 92 | 93 | 94 | Console 95 | true 96 | 97 | 98 | 99 | 100 | 101 | 102 | Level4 103 | Disabled 104 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 105 | true 106 | 107 | 108 | Console 109 | true 110 | 111 | 112 | 113 | 114 | Level4 115 | 116 | 117 | MaxSpeed 118 | true 119 | true 120 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 121 | true 122 | 123 | 124 | Console 125 | true 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | Level4 133 | 134 | 135 | MaxSpeed 136 | true 137 | true 138 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 139 | true 140 | 141 | 142 | Console 143 | true 144 | true 145 | true 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------- /StringPool/StringPool/TestStringPool.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Benchmarking the String Pool Allocator. 4 | // 5 | // Compares allocating string vectors and sorting them with 6 | // std::wstring vs. pool-allocated strings (StringPool). 7 | // 8 | // by Giovanni Dicanio 9 | // 10 | ////////////////////////////////////////////////////////////////////////////////////////// 11 | 12 | #include "StringPool.h" 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | 25 | //======================================================================================== 26 | // Benchmark Infrastructure Code 27 | //======================================================================================== 28 | 29 | class Stopwatch 30 | { 31 | public: 32 | Stopwatch() = default; 33 | 34 | void Start() 35 | { 36 | m_start = std::chrono::high_resolution_clock::now(); 37 | } 38 | 39 | void Stop() 40 | { 41 | TimePoint finish = std::chrono::high_resolution_clock::now(); 42 | m_elapsed = finish - m_start; 43 | } 44 | 45 | void PrintTime(const char* s) 46 | { 47 | std::cout << s << ": " << (m_elapsed.count() * 1000.0) << " ms\n"; 48 | } 49 | 50 | private: 51 | typedef std::chrono::time_point TimePoint; 52 | 53 | TimePoint m_start{}; 54 | std::chrono::duration m_elapsed{}; 55 | }; 56 | 57 | 58 | inline void PrintTestConditions() 59 | { 60 | cout << "("; 61 | #if defined(_M_X64) 62 | cout << "64-bit"; 63 | #elif defined(_M_IX86) 64 | cout << "32-bit"; 65 | #endif 66 | 67 | #ifdef TEST_SSO 68 | cout << "; testing with small strings)"; 69 | #else 70 | cout << ")"; 71 | #endif 72 | 73 | cout << "\n\n"; 74 | } 75 | 76 | 77 | //======================================================================================== 78 | // Main Benchmark Code 79 | //======================================================================================== 80 | 81 | void Benchmark() 82 | { 83 | //------------------------------------------------------------------------------------ 84 | // Build the test strings 85 | //------------------------------------------------------------------------------------ 86 | 87 | cout << "Building the string vectors for testing...\n\n"; 88 | const auto shuffled = []() -> vector 89 | { 90 | const wstring lorem[] = { 91 | L"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.", 92 | L"Maecenas porttitor congue massa. Fusce posuere, magna sed", 93 | L"pulvinar ultricies, purus lectus malesuada libero,", 94 | L"sit amet commodo magna eros quis urna.", 95 | L"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.", 96 | L"Pellentesque habitant morbi tristique senectus et netus et", 97 | L"malesuada fames ac turpis egestas. Proin pharetra nonummy pede.", 98 | L"Mauris et orci. [*** add more chars to prevent SSO ***]" 99 | }; 100 | 101 | vector v; 102 | 103 | #ifdef _DEBUG 104 | const int kCount = 2; 105 | #else 106 | const int kCount = 200 * 1000; 107 | #endif 108 | for (int i = 0; i < kCount; ++i) 109 | { 110 | for (auto& s : lorem) 111 | { 112 | 113 | // 114 | // Define TEST_SSO to test with the Small String Optimization 115 | // 116 | 117 | //#define TEST_SSO 118 | #ifdef TEST_SSO 119 | UNREFERENCED_PARAMETER(s); 120 | v.push_back(L"#" + to_wstring(i)); 121 | #else 122 | v.push_back(s + L" (#" + to_wstring(i) + L")"); 123 | #endif 124 | } 125 | } 126 | 127 | mt19937 prng(1729); 128 | 129 | shuffle(v.begin(), v.end(), prng); 130 | 131 | return v; 132 | }(); 133 | 134 | const auto shuffled_ptrs = [&]() -> vector { 135 | vector v; 136 | 137 | for (auto& s : shuffled) { 138 | v.push_back(s.c_str()); 139 | } 140 | 141 | return v; 142 | }(); 143 | 144 | 145 | //------------------------------------------------------------------------------------ 146 | // Benchmark Building the String Vectors 147 | //------------------------------------------------------------------------------------ 148 | 149 | Stopwatch sw; 150 | 151 | sw.Start(); 152 | vector stl1 = shuffled; 153 | sw.Stop(); 154 | sw.PrintTime("Alloc STL1 "); 155 | 156 | sw.Start(); 157 | StringPool::Allocator poolAlloc1; 158 | vector pool1; 159 | pool1.reserve(shuffled_ptrs.size()); 160 | for (const auto& s : shuffled_ptrs) 161 | { 162 | pool1.push_back(poolAlloc1.AllocString(s)); 163 | } 164 | sw.Stop(); 165 | sw.PrintTime("Alloc Pool1"); 166 | 167 | 168 | #define SANITY_CHECK_ON_STRING_VECTOR_CONTENT 169 | #ifdef SANITY_CHECK_ON_STRING_VECTOR_CONTENT 170 | if (pool1.size() != stl1.size()) 171 | { 172 | throw runtime_error("String vectors have different sizes."); 173 | } 174 | 175 | const size_t stringCount = stl1.size(); 176 | for (size_t i = 0; i < stringCount; i++) 177 | { 178 | if (wstring(pool1[i].Str(), pool1[i].Length()) != stl1[i]) 179 | { 180 | throw runtime_error("Mismatch between STL string and pool-allocated string."); 181 | } 182 | } 183 | #endif 184 | 185 | sw.Start(); 186 | vector stl2 = shuffled; 187 | sw.Stop(); 188 | sw.PrintTime("Alloc STL2 "); 189 | 190 | sw.Start(); 191 | StringPool::Allocator poolAlloc2; 192 | vector pool2; 193 | pool2.reserve(shuffled_ptrs.size()); 194 | for (const auto& s : shuffled_ptrs) 195 | { 196 | pool2.push_back(poolAlloc2.AllocString(s)); 197 | } 198 | sw.Stop(); 199 | sw.PrintTime("Alloc Pool2"); 200 | 201 | 202 | sw.Start(); 203 | vector stl3 = shuffled; 204 | sw.Stop(); 205 | sw.PrintTime("Alloc STL3 "); 206 | 207 | sw.Start(); 208 | StringPool::Allocator poolAlloc3; 209 | vector pool3; 210 | pool3.reserve(shuffled_ptrs.size()); 211 | for (const auto& s : shuffled_ptrs) 212 | { 213 | pool3.push_back(poolAlloc3.AllocString(s)); 214 | } 215 | sw.Stop(); 216 | sw.PrintTime("Alloc Pool3"); 217 | 218 | 219 | //------------------------------------------------------------------------------------ 220 | // Benchmark String Vector Sorting 221 | //------------------------------------------------------------------------------------ 222 | 223 | cout << "\nSorting...\n\n"; 224 | 225 | sw.Start(); 226 | sort(stl1.begin(), stl1.end()); 227 | sw.Stop(); 228 | sw.PrintTime("STL1 "); 229 | 230 | sw.Start(); 231 | sort(pool1.begin(), pool1.end()); 232 | sw.Stop(); 233 | sw.PrintTime("Pool1"); 234 | 235 | cout << "\n"; 236 | 237 | sw.Start(); 238 | sort(stl2.begin(), stl2.end()); 239 | sw.Stop(); 240 | sw.PrintTime("STL2 "); 241 | 242 | sw.Start(); 243 | sort(pool2.begin(), pool2.end()); 244 | sw.Stop(); 245 | sw.PrintTime("Pool2"); 246 | 247 | cout << "\n"; 248 | 249 | sw.Start(); 250 | sort(stl3.begin(), stl3.end()); 251 | sw.Stop(); 252 | sw.PrintTime("STL3 "); 253 | 254 | sw.Start(); 255 | sort(pool3.begin(), pool3.end()); 256 | sw.Stop(); 257 | sw.PrintTime("Pool3"); 258 | } 259 | 260 | int main() 261 | { 262 | cout << "*** Testing String Performance (STL vs. Pool) ***\n\n"; 263 | cout << "by Giovanni Dicanio\n\n"; 264 | 265 | PrintTestConditions(); 266 | 267 | constexpr int kExitOk = 0; 268 | constexpr int kExitError = 1; 269 | 270 | try 271 | { 272 | Benchmark(); 273 | } 274 | catch (const exception& e) 275 | { 276 | cout << "\n\n*** ERROR: " << e.what() << "\n\n"; 277 | return kExitError; 278 | } 279 | 280 | return kExitOk; 281 | } 282 | 283 | -------------------------------------------------------------------------------- /StringPool/StringPool/StringPool.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GIOVANNI_DICANIO_STRINGPOOL_H 2 | #define INCLUDE_GIOVANNI_DICANIO_STRINGPOOL_H 3 | 4 | ////////////////////////////////////////////////////////////////////////////////////////// 5 | // 6 | // Implements a string pool allocator (StringPool::Allocator) and a string class 7 | // (StringPool::String) representing strings allocated with it. 8 | // 9 | // Copyright (C) by Giovanni Dicanio 10 | // 11 | ////////////////////////////////////////////////////////////////////////////////////////// 12 | 13 | 14 | #include // For portable int types like uint8_t 15 | #include // For std::bad_alloc 16 | #include // For std::wstring 17 | #include // For std::swap 18 | #include // For std::vector 19 | 20 | 21 | namespace StringPool 22 | { 23 | 24 | // Forward declarations 25 | class String; 26 | class Allocator; 27 | 28 | 29 | //======================================================================================== 30 | // String Class 31 | //======================================================================================== 32 | 33 | //---------------------------------------------------------------------------------------- 34 | // String returned by the allocator. 35 | // This is just made by a pointer to a C-style NUL-terminated string, 36 | // and the length of that string. 37 | // 38 | // Copying this class just means member-wise copy, and it's a cheap operation. 39 | // 40 | // Do *not* delete instances of this string class: the memory of this class is managed 41 | // by the StringPool::Allocator, which is responsible for deleting the allocated memory 42 | // blocks. 43 | //---------------------------------------------------------------------------------------- 44 | class String 45 | { 46 | public: 47 | 48 | // Creates an empty string. 49 | String() noexcept = default; 50 | 51 | // Default member-wise copy is fine. 52 | String(const String& other) noexcept = default; 53 | String& operator=(const String& other) noexcept = default; 54 | 55 | // Moves from the other string, leaving it empty. 56 | String(String&& other) noexcept 57 | : m_ptr{ other.m_ptr } 58 | , m_length{ other.m_length } 59 | { 60 | other.m_ptr = nullptr; 61 | other.m_length = 0; 62 | } 63 | 64 | // Moves from the other string, leaving it empty. 65 | String& operator=(String&& other) noexcept 66 | { 67 | if (&other != this) 68 | { 69 | m_ptr = other.m_ptr; 70 | other.m_ptr = nullptr; 71 | 72 | m_length = other.m_length; 73 | other.m_length = 0; 74 | } 75 | return *this; 76 | } 77 | 78 | // Returns C-style NUL-terminated string pointer. 79 | const wchar_t* Str() const noexcept 80 | { 81 | if (!IsEmpty()) 82 | { 83 | return m_ptr; 84 | } 85 | else 86 | { 87 | return &m_nul; 88 | } 89 | } 90 | 91 | // Number of wchar_ts in the string (excluding the terminating NUL). 92 | // An empty string has length 0. 93 | size_t Length() const noexcept 94 | { 95 | return m_length; 96 | } 97 | 98 | bool IsEmpty() const noexcept 99 | { 100 | return m_length == 0; 101 | } 102 | 103 | // Convert to std::wstring. 104 | std::wstring ToStdString() const 105 | { 106 | if (!IsEmpty()) 107 | { 108 | return std::wstring{ m_ptr, m_length }; 109 | } 110 | else 111 | { 112 | return std::wstring{}; 113 | } 114 | } 115 | 116 | // Compare this with other. 117 | // +1 : this > other 118 | // 0 : this == other 119 | // -1 : this < other 120 | int Compare(const String& other) const noexcept 121 | { 122 | const size_t minLength = m_length < other.m_length ? 123 | m_length : other.m_length; 124 | 125 | const int result = wmemcmp(m_ptr, other.m_ptr, minLength); 126 | 127 | if (result != 0) 128 | return result; 129 | 130 | if (m_length < other.m_length) 131 | return -1; 132 | 133 | if (m_length > other.m_length) 134 | return 1; 135 | 136 | return 0; 137 | } 138 | 139 | // StringPool::Allocator creates instances of this String class. 140 | friend class Allocator; 141 | 142 | // STL-style non-throwing swap 143 | friend void swap(String& a, String& b) noexcept 144 | { 145 | using std::swap; 146 | swap(a.m_ptr, b.m_ptr); 147 | swap(a.m_length, b.m_length); 148 | 149 | // No need to swap m_nul, as it's always NUL for each string instance. 150 | } 151 | 152 | 153 | private: 154 | const wchar_t* m_ptr{}; // C-style raw pointer to a NUL-terminated string 155 | size_t m_length{}; // Length, in wchar_ts, excluding the terminating NUL 156 | 157 | // The NUL character for empty strings 158 | const wchar_t m_nul{}; 159 | 160 | // Constructor is private, as only the friend StringPool::Allocator class 161 | // can allocate instances of this String class. 162 | String(const wchar_t* ptr, size_t length) noexcept 163 | : m_ptr{ ptr } 164 | , m_length{ length } 165 | {} 166 | }; 167 | 168 | 169 | // 170 | // Convenient overloaded relational operators for string comparisons 171 | // 172 | 173 | inline bool operator==(const String& a, const String& b) noexcept 174 | { 175 | return a.Compare(b) == 0; 176 | } 177 | 178 | inline bool operator!=(const String& a, const String& b) noexcept 179 | { 180 | return a.Compare(b) != 0; 181 | } 182 | 183 | inline bool operator<(const String& a, const String& b) noexcept 184 | { 185 | return a.Compare(b) < 0; 186 | } 187 | 188 | inline bool operator>(const String& a, const String& b) noexcept 189 | { 190 | return a.Compare(b) > 0; 191 | } 192 | 193 | inline bool operator<=(const String& a, const String& b) noexcept 194 | { 195 | return a.Compare(b) <= 0; 196 | } 197 | 198 | inline bool operator>=(const String& a, const String& b) noexcept 199 | { 200 | return a.Compare(b) >= 0; 201 | } 202 | 203 | 204 | //======================================================================================== 205 | // Allocator Class 206 | //======================================================================================== 207 | 208 | //---------------------------------------------------------------------------------------- 209 | // String Pool Allocator 210 | // 211 | // Preallocates chunks of memory, and serves memory just *increasing a pointer* 212 | // inside a chunk. 213 | //---------------------------------------------------------------------------------------- 214 | class Allocator 215 | { 216 | public: 217 | 218 | // Initialize an empty allocator. 219 | // Call AllocString when you need a new string. 220 | Allocator() = default; 221 | 222 | // Release all the allocated chunks (if any). 223 | ~Allocator() 224 | { 225 | Clear(); 226 | } 227 | 228 | // Ban copy 229 | Allocator(const Allocator&) = delete; 230 | Allocator& operator=(const Allocator&) = delete; 231 | 232 | // Release every allocated chunk. 233 | void Clear() 234 | { 235 | for (auto& pChunk : m_chunks) 236 | { 237 | Free(pChunk); 238 | pChunk = nullptr; 239 | } 240 | 241 | m_chunks.clear(); 242 | 243 | m_pNext = nullptr; 244 | m_pLimit = nullptr; 245 | } 246 | 247 | // Allocate a string using the pool allocator, deep-copying the string 248 | // from a C-style NUL-terminated string pointer. 249 | // Throws std::bad_alloc on allocation failure. 250 | String AllocString(const wchar_t* ptr) 251 | { 252 | return AllocString(ptr, ptr + wcslen(ptr)); 253 | } 254 | 255 | // Allocate a string using the pool allocator, deep-copying the string 256 | // from a [start, finish) "string view". 257 | // As per the STL convention: start is included, finish is excluded. 258 | // Throws std::bad_alloc on allocation failure. 259 | String AllocString(const wchar_t* start, const wchar_t* finish) 260 | { 261 | const size_t length = finish - start; 262 | const size_t lengthWithNul = length + 1; 263 | wchar_t* ptr = AllocMemory(lengthWithNul); 264 | wmemcpy(ptr, start, length); 265 | ptr[length] = L'\0'; // terminating NUL 266 | 267 | return String{ ptr, length }; 268 | } 269 | 270 | 271 | private: 272 | 273 | // A memory chunk is made by this header followed by the allocated wchar_ts. 274 | struct ChunkHeader 275 | { 276 | // Total chunk size, in bytes 277 | size_t SizeInBytes; 278 | 279 | // Followed by array of wchar_t characters 280 | wchar_t Chars[1]; 281 | }; 282 | 283 | enum 284 | { 285 | // An allocated chunk must be minimum this size, in bytes 286 | kMinChunkSizeInBytes = 600000, 287 | 288 | // Can't alloc strings larger than that (in wchar_ts) 289 | kMaxStringLength = 1024 * 1024 290 | }; 291 | 292 | 293 | wchar_t* m_pNext{}; // First available wchar_t slot in the current chunk 294 | wchar_t* m_pLimit{}; // One past last available wchar_t slot in the current chunk 295 | 296 | // Keep a list of all allocated chunks. 297 | // NOTE: ChunkHeader pointers are *owning* raw pointers, 298 | // that will be released by this class destructor. 299 | std::vector m_chunks{}; 300 | 301 | 302 | //------------------------------------------------------------------------------------ 303 | // Helper Methods 304 | //------------------------------------------------------------------------------------ 305 | 306 | 307 | // 308 | // Helper functions to allocate and free memory. 309 | // Those could be factored out as a trait class, to experiment with pool allocators 310 | // using different alloc/free techniques/APIs. 311 | // 312 | 313 | // Request a memory allocation. 314 | // Return nullptr on failure. 315 | static void* Allocate(size_t cbSize) noexcept 316 | { 317 | return malloc(cbSize); 318 | } 319 | 320 | // Free a previous allocation. 321 | static void Free(void* ptr) noexcept 322 | { 323 | return free(ptr); 324 | } 325 | 326 | // Helper function to allocate memory using the pool allocator. 327 | // 'length' is the number of wchar_ts requested. 328 | // 329 | // First tries to carve memory from the current chunk. 330 | // If there's not enough space, allocates a new chunk. 331 | // Throws std::bad_alloc on allocation errors. 332 | wchar_t* AllocMemory(size_t length) 333 | { 334 | // First let's try allocation in current chunk 335 | wchar_t* ptr = m_pNext; 336 | if (m_pNext + length <= m_pLimit) 337 | { 338 | // There's enough room in current chunk, so a simple pointer increase will do! 339 | m_pNext += length; 340 | return ptr; 341 | } 342 | 343 | // There's not enough room in current chunk. We need to allocate a new chunk. 344 | 345 | // Prevent request of too long strings 346 | if (length > kMaxStringLength) 347 | { 348 | throw std::bad_alloc(); 349 | } 350 | 351 | // Allocate a new chunk, not smaller than minimum chunk size 352 | size_t chunkSizeInBytes = (length * sizeof(wchar_t)) + sizeof(ChunkHeader); 353 | if (chunkSizeInBytes < kMinChunkSizeInBytes) 354 | { 355 | chunkSizeInBytes = kMinChunkSizeInBytes; 356 | } 357 | 358 | uint8_t* pChunkStart = static_cast(Allocate(chunkSizeInBytes)); 359 | if (pChunkStart == nullptr) 360 | { 361 | // Allocation failure: throw std::bad_alloc 362 | static std::bad_alloc outOfMemory; 363 | throw outOfMemory; 364 | } 365 | 366 | // Point one past the last available wchar_t in current chunk 367 | m_pLimit = reinterpret_cast(pChunkStart + chunkSizeInBytes); 368 | 369 | // Prepare the chunk header 370 | ChunkHeader* pNewChunk = reinterpret_cast(pChunkStart); 371 | pNewChunk->SizeInBytes = chunkSizeInBytes; 372 | 373 | // Set the pointer to point to the free bytes to serve the next allocation 374 | m_pNext = reinterpret_cast(pNewChunk + 1); 375 | 376 | // Keep track of the newly allocated chunk, 377 | // adding it to the chunk pointer vector 378 | m_chunks.push_back(pNewChunk); 379 | 380 | // Now that we have allocated a new chunk, 381 | // we can retry the allocation with a simple pointer increase 382 | return AllocMemory(length); 383 | } 384 | }; 385 | 386 | 387 | } // namespace StringPool 388 | 389 | 390 | #endif // INCLUDE_GIOVANNI_DICANIO_STRINGPOOL_H 391 | --------------------------------------------------------------------------------