Diligent Engine  v.2.4.g
StringPool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include <cstring>
34 #include "../../Primitives/interface/BasicTypes.h"
35 #include "../../Primitives/interface/MemoryAllocator.h"
36 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
37 
38 namespace Diligent
39 {
40 
43 {
44 public:
46 
47  // clang-format off
48  StringPool (const StringPool&) = delete;
49  StringPool& operator=(const StringPool&) = delete;
50  // clang-format on
51 
53  // clang-format off
54  m_pBuffer {Pool.m_pBuffer },
55  m_pCurrPtr {Pool.m_pCurrPtr },
56  m_ReservedSize{Pool.m_ReservedSize},
57  m_pAllocator {Pool.m_pAllocator }
58  // clang-format on
59  {
60  Pool.m_pBuffer = nullptr;
61  Pool.m_pCurrPtr = nullptr;
62  Pool.m_ReservedSize = 0;
63  Pool.m_pAllocator = nullptr;
64  }
65 
67  {
68  Clear();
69  }
70 
71  void Reserve(size_t Size, IMemoryAllocator& Allocator)
72  {
73  Clear();
74 
75  VERIFY(m_ReservedSize == 0, "Pool is already initialized");
76  m_pAllocator = &Allocator;
77  m_ReservedSize = Size;
78  if (m_ReservedSize != 0)
79  {
80  m_pBuffer = reinterpret_cast<Char*>(m_pAllocator->Allocate(m_ReservedSize, "Memory for string pool", __FILE__, __LINE__));
81  }
82  m_pCurrPtr = m_pBuffer;
83  }
84 
85  void Clear()
86  {
87  if (m_pBuffer != nullptr && m_pAllocator != nullptr)
88  {
89  m_pAllocator->Free(m_pBuffer);
90  }
91 
92  m_pBuffer = nullptr;
93  m_pCurrPtr = nullptr;
94  m_ReservedSize = 0;
95  m_pAllocator = nullptr;
96  }
97 
98  static size_t GetRequiredReserveSize(const char* str)
99  {
100  return str != nullptr ? strlen(str) + 1 : 0;
101  }
102 
103  static size_t GetRequiredReserveSize(const std::string& str)
104  {
105  return str.length() + 1;
106  }
107 
108  void AssignMemory(Char* pBuffer, size_t Size)
109  {
110  VERIFY(m_ReservedSize == 0, "Pool is already initialized");
111  m_ReservedSize = Size;
112  m_pBuffer = pBuffer;
113  m_pCurrPtr = m_pBuffer;
114  }
115 
117  {
118  VERIFY(m_pCurrPtr + Length <= m_pBuffer + m_ReservedSize, "Not enough space in the buffer");
119  auto* Ptr = m_pCurrPtr;
120  m_pCurrPtr += Length;
121  return Ptr;
122  }
123 
124  Char* CopyString(const String& Str)
125  {
126  auto len = Str.length();
127  auto* str = Allocate(len + 1);
128  if (len != 0)
129  {
130  memcpy(str, Str.data(), len * sizeof(str[0]));
131  }
132  str[len] = 0;
133  return str;
134  }
135 
136  Char* CopyString(const char* Str)
137  {
138  if (Str == nullptr)
139  return nullptr;
140 
141  auto* Ptr = m_pCurrPtr;
142  while (*Str != 0 && m_pCurrPtr < m_pBuffer + m_ReservedSize)
143  {
144  *(m_pCurrPtr++) = *(Str++);
145  }
146  if (m_pCurrPtr < m_pBuffer + m_ReservedSize)
147  *(m_pCurrPtr++) = 0;
148  else
149  UNEXPECTED("Not enough space reserved in the string pool");
150  return Ptr;
151  }
152 
153 
154  size_t GetRemainingSize() const
155  {
156  VERIFY(m_pCurrPtr <= m_pBuffer + m_ReservedSize, "Buffer overflow");
157  return m_ReservedSize - (m_pCurrPtr - m_pBuffer);
158  }
159  size_t GetUsedSize() const
160  {
161  VERIFY(m_pCurrPtr <= m_pBuffer + m_ReservedSize, "Buffer overflow");
162  return m_pCurrPtr - m_pBuffer;
163  }
164  size_t GetReservedSize() const
165  {
166  return m_ReservedSize;
167  }
168 
169 private:
170  Char* m_pBuffer = nullptr;
171  Char* m_pCurrPtr = nullptr;
172  size_t m_ReservedSize = 0;
173  IMemoryAllocator* m_pAllocator = nullptr;
174 };
175 
176 } // namespace Diligent
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::StringPool::Clear
void Clear()
Definition: StringPool.hpp:85
Diligent::StringPool::operator=
StringPool & operator=(const StringPool &)=delete
Diligent::StringPool::GetRemainingSize
size_t GetRemainingSize() const
Definition: StringPool.hpp:154
Diligent::StringPool::AssignMemory
void AssignMemory(Char *pBuffer, size_t Size)
Definition: StringPool.hpp:108
Length
Uint32 Length
Definition: DXBCUtils.cpp:59
UNEXPECTED
#define UNEXPECTED(...)
Definition: DebugUtilities.hpp:77
Diligent::StringPool::CopyString
Char * CopyString(const char *Str)
Definition: StringPool.hpp:136
Diligent::StringPool::CopyString
Char * CopyString(const String &Str)
Definition: StringPool.hpp:124
Diligent::StringPool::StringPool
StringPool(StringPool &&Pool)
Definition: StringPool.hpp:52
Diligent::StringPool::GetRequiredReserveSize
static size_t GetRequiredReserveSize(const std::string &str)
Definition: StringPool.hpp:103
Diligent::StringPool::~StringPool
~StringPool()
Definition: StringPool.hpp:66
Diligent::StringPool::StringPool
StringPool()
Definition: StringPool.hpp:45
Diligent::StringPool::GetUsedSize
size_t GetUsedSize() const
Definition: StringPool.hpp:159
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::StringPool::Allocate
Char * Allocate(size_t Length)
Definition: StringPool.hpp:116
Diligent::String
std::basic_string< Char > String
String variable.
Definition: BasicTypes.h:66
Diligent::StringPool::Reserve
void Reserve(size_t Size, IMemoryAllocator &Allocator)
Definition: StringPool.hpp:71
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::StringPool
Implementation of a simple fixed-size string pool.
Definition: StringPool.hpp:42
Diligent::IMemoryAllocator::Free
virtual void Free(void *Ptr)=0
Releases memory.
Diligent::IMemoryAllocator::Allocate
virtual void * Allocate(size_t Size, const Char *dbgDescription, const char *dbgFileName, const Int32 dbgLineNumber)=0
Allocates block of memory.
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::StringPool::GetRequiredReserveSize
static size_t GetRequiredReserveSize(const char *str)
Definition: StringPool.hpp:98
Diligent::StringPool::GetReservedSize
size_t GetReservedSize() const
Definition: StringPool.hpp:164