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
29namespace Common {
30
50protected:
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
68public:
73 explicit MemoryPool(size_t chunkSize);
75
79 void *allocChunk();
88 void freeChunk(void *ptr);
89
99
103 size_t getChunkSize() const { return _chunkSize; }
104};
105
111template<size_t CHUNK_SIZE, size_t NUM_INTERNAL_CHUNKS = 32>
113private:
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];
119public:
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/*
129 * This is the degenerate case of FixedSizeMemoryPool with 0 chunks.
130 * It's a MemoryPool with static chunk size.
131 * This is useful for ObjectPool users (HashMap)
132 */
133template<size_t CHUNK_SIZE>
134class FixedSizeMemoryPool<CHUNK_SIZE, 0> : public MemoryPool {
135public:
136 FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {}
137};
138
142template<class T, size_t NUM_INTERNAL_CHUNKS = 32>
143class ObjectPool : public FixedSizeMemoryPool<sizeof(T), NUM_INTERNAL_CHUNKS> {
144public:
149 void deleteChunk(T *ptr) {
150 ptr->~T();
151 this->freeChunk(ptr);
152 }
153};
154
157} // End of namespace Common
158
167inline void *operator new(size_t nbytes, Common::MemoryPool &pool) {
168 assert(nbytes <= pool.getChunkSize());
169 return pool.allocChunk();
170}
171
172inline void operator delete(void *p, Common::MemoryPool &pool) {
173 pool.freeChunk(p);
174}
175
176#endif
Definition: memorypool.h:112
Definition: memorypool.h:49
void freeChunk(void *ptr)
MemoryPool(size_t chunkSize)
size_t getChunkSize() const
Definition: memorypool.h:103
Definition: memorypool.h:143
void deleteChunk(T *ptr)
Definition: memorypool.h:149
Definition: algorithm.h:29
Definition: memorypool.h:54