ScummVM API documentation
memorypool.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_MEMORYPOOL_H
23 #define COMMON_MEMORYPOOL_H
24 
25 #include "common/scummsys.h"
26 #include "common/array.h"
27 
28 
29 namespace Common {
30 
49 class MemoryPool {
50 protected:
51  MemoryPool(const MemoryPool&);
52  MemoryPool& operator=(const MemoryPool&);
53 
54  struct Page {
55  void *start;
56  size_t numChunks;
57  };
58 
59  const size_t _chunkSize;
60  Array<Page> _pages;
61  void *_next;
62  size_t _chunksPerPage;
63 
64  void allocPage();
65  void addPageToPool(const Page &page);
66  bool isPointerInPage(void *ptr, const Page &page);
67 
68 public:
73  explicit MemoryPool(size_t chunkSize);
74  ~MemoryPool();
75 
79  void *allocChunk();
88  void freeChunk(void *ptr);
89 
98  void freeUnusedPages();
99 
103  size_t getChunkSize() const { return _chunkSize; }
104 };
105 
111 template<size_t CHUNK_SIZE, size_t NUM_INTERNAL_CHUNKS = 32>
113 private:
114  enum {
115  REAL_CHUNK_SIZE = (CHUNK_SIZE + sizeof(void *) - 1) & (~(sizeof(void *) - 1))
116  };
117 
118  byte _storage[NUM_INTERNAL_CHUNKS * REAL_CHUNK_SIZE];
119 public:
120  FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {
121  assert(REAL_CHUNK_SIZE == _chunkSize);
122  // Insert some static storage
123  Page internalPage = { _storage, NUM_INTERNAL_CHUNKS };
124  addPageToPool(internalPage);
125  }
126 };
127 
128 // Ensure NUM_INTERNAL_CHUNKS == 0 results in a compile error
129 template<size_t CHUNK_SIZE>
130 class FixedSizeMemoryPool<CHUNK_SIZE,0> : public MemoryPool {
131 public:
132  FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {}
133 };
134 
138 template<class T, size_t NUM_INTERNAL_CHUNKS = 32>
139 class ObjectPool : public FixedSizeMemoryPool<sizeof(T), NUM_INTERNAL_CHUNKS> {
140 public:
145  void deleteChunk(T *ptr) {
146  ptr->~T();
147  this->freeChunk(ptr);
148  }
149 };
150 
153 } // End of namespace Common
154 
163 inline void *operator new(size_t nbytes, Common::MemoryPool &pool) {
164  assert(nbytes <= pool.getChunkSize());
165  return pool.allocChunk();
166 }
167 
168 inline void operator delete(void *p, Common::MemoryPool &pool) {
169  pool.freeChunk(p);
170 }
171 
172 #endif
void freeChunk(void *ptr)
Definition: memorypool.h:49
Definition: memorypool.h:112
size_t getChunkSize() const
Definition: memorypool.h:103
void deleteChunk(T *ptr)
Definition: memorypool.h:145
Definition: algorithm.h:29
Definition: memorypool.h:139
Definition: memorypool.h:54