00001 /* ScummVM - Graphic Adventure Engine00002 *00003 * ScummVM is the legal property of its developers, whose names00004 * are too numerous to list here. Please refer to the COPYRIGHT00005 * file distributed with this source distribution.00006 *00007 * This program is free software; you can redistribute it and/or00008 * modify it under the terms of the GNU General Public License00009 * as published by the Free Software Foundation; either version 200010 * of the License, or (at your option) any later version.00011 *00012 * This program is distributed in the hope that it will be useful,00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the00015 * GNU General Public License for more details.00016 *00017 * You should have received a copy of the GNU General Public License00018 * along with this program; if not, write to the Free Software00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.00020 *00021 */00022
00023 #ifndef COMMON_MEMORYPOOL_H00024 #define COMMON_MEMORYPOOL_H00025
00026 #include "common/scummsys.h"00027 #include "common/array.h"00028
00029
00030 namespace Common {
00031
00042class MemoryPool {
00043 protected:
00044 MemoryPool(constMemoryPool&);
00045 MemoryPool& operator=(constMemoryPool&);
00046
00047struct Page {
00048void *start;
00049size_tnumChunks;
00050 };
00051
00052constsize_t_chunkSize;
00053Array<Page>_pages;
00054void *_next;
00055size_t_chunksPerPage;
00056
00057 voidallocPage();
00058 voidaddPageToPool(constPage &page);
00059 boolisPointerInPage(void *ptr, constPage &page);
00060
00061 public:
00066 explicitMemoryPool(size_t chunkSize);
00067 ~MemoryPool();
00068
00072 void *allocChunk();
00081 voidfreeChunk(void *ptr);
00082
00091 voidfreeUnusedPages();
00092
00096size_tgetChunkSize() const { return_chunkSize; }
00097 };
00098
00104 template<size_t CHUNK_SIZE, size_t NUM_INTERNAL_CHUNKS = 32>
00105class FixedSizeMemoryPool : publicMemoryPool {
00106 private:
00107 enum {
00108REAL_CHUNK_SIZE = (CHUNK_SIZE + sizeof(void *) - 1) & (~(sizeof(void *) - 1))
00109 };
00110
00111byte_storage[NUM_INTERNAL_CHUNKS * REAL_CHUNK_SIZE];
00112 public:
00113FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {
00114 assert(REAL_CHUNK_SIZE == _chunkSize);
00115 // Insert some static storage00116 Page internalPage = { _storage, NUM_INTERNAL_CHUNKS };
00117 addPageToPool(internalPage);
00118 }
00119 };
00120
00121 // Ensure NUM_INTERNAL_CHUNKS == 0 results in a compile error00122 template<size_t CHUNK_SIZE>
00123class FixedSizeMemoryPool<CHUNK_SIZE,0> : publicMemoryPool {
00124 public:
00125FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {}
00126 };
00127
00131 template<class T, size_t NUM_INTERNAL_CHUNKS = 32>
00132class ObjectPool : publicFixedSizeMemoryPool<sizeof(T), NUM_INTERNAL_CHUNKS> {
00133 public:
00138voiddeleteChunk(T *ptr) {
00139 ptr->~T();
00140 this->freeChunk(ptr);
00141 }
00142 };
00143
00144 } // End of namespace Common00145
00154inlinevoid *operator new(size_t nbytes, Common::MemoryPool &pool) {
00155 assert(nbytes <= pool.getChunkSize());
00156 return pool.allocChunk();
00157 }
00158
00159inlinevoidoperator delete(void *p, Common::MemoryPool &pool) {
00160 pool.freeChunk(p);
00161 }
00162
00163 #endif
Generated on Sat Dec 14 2019 05:00:33 for ResidualVM by 1.7.1