webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
allocators.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_ALLOCATORS_H_
22 #define RAPIDJSON_ALLOCATORS_H_
23 
24 #include "rapidjson.h"
25 
27 
29 // Allocator
30 
61 // CrtAllocator
63 
65 
68 class CrtAllocator {
69 public:
70  static const bool kNeedFree = true;
71  void* Malloc(size_t size) { return std::malloc(size); }
72  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return std::realloc(originalPtr, newSize); }
73  static void Free(void *ptr) { std::free(ptr); }
74 };
75 
77 // MemoryPoolAllocator
78 
80 
95 template <typename BaseAllocator = CrtAllocator>
97 public:
98  static const bool kNeedFree = false;
99 
101 
104  MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
105  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
106  {
107  }
108 
110 
119  MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
120  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
121  {
122  RAPIDJSON_ASSERT(buffer != 0);
123  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
124  chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
125  chunkHead_->capacity = size - sizeof(ChunkHeader);
126  chunkHead_->size = 0;
127  chunkHead_->next = 0;
128  }
129 
131 
134  Clear();
135  RAPIDJSON_DELETE(ownBaseAllocator_);
136  }
137 
139  void Clear() {
140  while(chunkHead_ != 0 && chunkHead_ != userBuffer_) {
141  ChunkHeader* next = chunkHead_->next;
142  baseAllocator_->Free(chunkHead_);
143  chunkHead_ = next;
144  }
145  }
146 
148 
150  size_t Capacity() const {
151  size_t capacity = 0;
152  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
153  capacity += c->capacity;
154  return capacity;
155  }
156 
158 
160  size_t Size() const {
161  size_t size = 0;
162  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
163  size += c->size;
164  return size;
165  }
166 
168  void* Malloc(size_t size) {
169  size = RAPIDJSON_ALIGN(size);
170  if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
171  AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
172 
173  void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size;
174  chunkHead_->size += size;
175  return buffer;
176  }
177 
179  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
180  if (originalPtr == 0)
181  return Malloc(newSize);
182 
183  // Do not shrink if new size is smaller than original
184  if (originalSize >= newSize)
185  return originalPtr;
186 
187  // Simply expand it if it is the last allocation and there is sufficient space
188  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
189  size_t increment = static_cast<size_t>(newSize - originalSize);
190  increment = RAPIDJSON_ALIGN(increment);
191  if (chunkHead_->size + increment <= chunkHead_->capacity) {
192  chunkHead_->size += increment;
193  return originalPtr;
194  }
195  }
196 
197  // Realloc process: allocate and copy memory, do not free original buffer.
198  void* newBuffer = Malloc(newSize);
199  RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
200  return std::memcpy(newBuffer, originalPtr, originalSize);
201  }
202 
204  static void Free(void *ptr) { (void)ptr; } // Do nothing
205 
206 private:
208  MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
210  MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
211 
213 
215  void AddChunk(size_t capacity) {
216  if (!baseAllocator_)
217  ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
218  ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity));
219  chunk->capacity = capacity;
220  chunk->size = 0;
221  chunk->next = chunkHead_;
222  chunkHead_ = chunk;
223  }
224 
225  static const int kDefaultChunkCapacity = 64 * 1024;
226 
228 
230  struct ChunkHeader {
231  size_t capacity;
232  size_t size;
233  ChunkHeader *next;
234  };
235 
236  ChunkHeader *chunkHead_;
237  size_t chunk_capacity_;
238  void *userBuffer_;
239  BaseAllocator* baseAllocator_;
240  BaseAllocator* ownBaseAllocator_;
241 };
242 
244 
245 #endif // RAPIDJSON_ENCODINGS_H_
#define size
Definition: float-mm.c:27
EGLStreamKHR EGLint EGLint EGLint size
Definition: eglext.h:984
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Definition: allocators.h:72
def chunk(list, chunkSize)
Definition: make-js-file-arrays.py:38
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:168
int c
Definition: cpp_unittests.cpp:275
static void Free(void *ptr)
Frees a memory block (concept Allocator)
Definition: allocators.h:204
C-runtime library allocator.
Definition: allocators.h:68
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:160
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:139
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:179
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:119
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:133
Clear
Definition: python_message.py:1326
void
Definition: AVFoundationCFSoftLinking.h:81
Definition: interfaces.idl:172
void * Malloc(size_t size)
Definition: allocators.h:71
Default memory allocator used by the parser and DOM.
Definition: allocators.h:96
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:454
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:219
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:458
#define malloc
Definition: mbmalloc.h:49
static const bool kNeedFree
Definition: allocators.h:70
#define buffer
Definition: xmlparse.c:622
static void Free(void *ptr)
Definition: allocators.h:73
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:104
common definitions and configuration
#define free
Definition: mbmalloc.h:50
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:192
#define realloc
Definition: mbmalloc.h:51
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:150