ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

memorypool.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * 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 of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_MEMORYPOOL_H
00024 #define COMMON_MEMORYPOOL_H
00025 
00026 #include "common/scummsys.h"
00027 #include "common/array.h"
00028 
00029 
00030 namespace Common {
00031 
00042 class MemoryPool {
00043 protected:
00044     MemoryPool(const MemoryPool&);
00045     MemoryPool& operator=(const MemoryPool&);
00046 
00047     struct Page {
00048         void *start;
00049         size_t numChunks;
00050     };
00051 
00052     const size_t    _chunkSize;
00053     Array<Page>     _pages;
00054     void            *_next;
00055     size_t          _chunksPerPage;
00056 
00057     void    allocPage();
00058     void    addPageToPool(const Page &page);
00059     bool    isPointerInPage(void *ptr, const Page &page);
00060 
00061 public:
00066     explicit MemoryPool(size_t chunkSize);
00067     ~MemoryPool();
00068 
00072     void    *allocChunk();
00081     void    freeChunk(void *ptr);
00082 
00091     void    freeUnusedPages();
00092 
00096     size_t  getChunkSize() const { return _chunkSize; }
00097 };
00098 
00104 template<size_t CHUNK_SIZE, size_t NUM_INTERNAL_CHUNKS = 32>
00105 class FixedSizeMemoryPool : public MemoryPool {
00106 private:
00107     enum {
00108         REAL_CHUNK_SIZE = (CHUNK_SIZE + sizeof(void *) - 1) & (~(sizeof(void *) - 1))
00109     };
00110 
00111     byte    _storage[NUM_INTERNAL_CHUNKS * REAL_CHUNK_SIZE];
00112 public:
00113     FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {
00114         assert(REAL_CHUNK_SIZE == _chunkSize);
00115         // Insert some static storage
00116         Page internalPage = { _storage, NUM_INTERNAL_CHUNKS };
00117         addPageToPool(internalPage);
00118     }
00119 };
00120 
00121 // Ensure NUM_INTERNAL_CHUNKS == 0 results in a compile error
00122 template<size_t CHUNK_SIZE>
00123 class FixedSizeMemoryPool<CHUNK_SIZE,0> : public MemoryPool {
00124 public:
00125     FixedSizeMemoryPool() : MemoryPool(CHUNK_SIZE) {}
00126 };
00127 
00131 template<class T, size_t NUM_INTERNAL_CHUNKS = 32>
00132 class ObjectPool : public FixedSizeMemoryPool<sizeof(T), NUM_INTERNAL_CHUNKS> {
00133 public:
00138     void deleteChunk(T *ptr) {
00139         ptr->~T();
00140         this->freeChunk(ptr);
00141     }
00142 };
00143 
00144 } // End of namespace Common
00145 
00154 inline void *operator new(size_t nbytes, Common::MemoryPool &pool) {
00155     assert(nbytes <= pool.getChunkSize());
00156     return pool.allocChunk();
00157 }
00158 
00159 inline void operator delete(void *p, Common::MemoryPool &pool) {
00160     pool.freeChunk(p);
00161 }
00162 
00163 #endif


Generated on Sat Sep 14 2019 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge