Main Page   Class Hierarchy   Compound List   File List   Compound Members  

glmemorypool.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 
00070 
00071 #ifndef GL_MEMORY_POOL
00072 #define GL_MEMORY_POOL
00073 
00074 /*      A memory pool that will dynamically grow as memory is needed.
00075 */
00076 class GlMemoryPool
00077 {
00078   public:
00079         GlMemoryPool( unsigned objectSize, unsigned blockSize = 4096 );
00080         ~GlMemoryPool();
00081 
00082         void* Alloc();
00083         void Free( void* mem );
00084 
00085   private:
00086         struct Block;
00087 
00088         struct Chunk
00089         {
00090                 Chunk* next;
00091         };
00092 
00093         struct BlockHeader
00094         {
00095                 Block* nextBlock;       // pointer to the next block
00096         };
00097 
00098         struct Block
00099         {
00100                 BlockHeader header;
00101                 Chunk* chunk;           // treated as an array of chunks.
00102         };
00103 
00104         void NewBlock();
00105 
00106         unsigned chunkSize;
00107         unsigned blockSize;
00108         unsigned chunksPerBlock;
00109         
00110         unsigned numBlocks;
00111         unsigned numChunks;
00112         
00113         Block* rootBlock;
00114         Chunk* head;
00115 };
00116 
00117 /*      A memory pool that has a fixed allocation.
00118         Essentially a cross between a linked list and an array.
00119 */
00120 template < class T, int COUNT >
00121 class GlFixedMemoryPool
00122 {
00123   private:
00124         struct Chunk
00125         {
00126                 Chunk* next;
00127         };
00128 
00129   public:
00130         GlFixedMemoryPool()
00131         {
00132                 GLASSERT( sizeof( T ) >= sizeof( Chunk* ) );
00133                 for( int i=0; i<COUNT-1; ++i )
00134                 {
00135                         ( (Chunk*)(&memory[i]) )->next = (Chunk*)(&memory[i+1]);
00136                 }
00137                 ( (Chunk*)(&memory[COUNT-1]) )->next = 0;
00138                 root = ( (Chunk*)(&memory[0]) );
00139                 inUse = 0;
00140         }
00141 
00142         ~GlFixedMemoryPool()    {}
00143 
00144         T* Alloc()
00145         {
00146                 T* ret = 0;
00147                 if ( root )
00148                 {
00149                         ret = (T*) root;
00150                         root = root->next;
00151                         ++inUse;
00152                 }
00153                 return ret;
00154         }
00155 
00156         void Free( T* _mem )
00157         {
00158                 if ( _mem )
00159                 {
00160                         Chunk* mem = (Chunk*) _mem;
00161                         mem->next = root;
00162                         root = mem;
00163                         --inUse;
00164                 }
00165         }
00166 
00167         unsigned InUse()        {       return inUse; }
00168         unsigned Remain()       {       return COUNT - inUse; }
00169 
00170   private:
00171         T memory[ COUNT ];
00172         unsigned inUse;
00173         Chunk* root;
00174 };
00175 
00176 
00177 /*      This is memory allocation for when you know exactly how much 
00178         memory is going to be needed. So it's a way to pre-allocate 
00179         while still using the new and delete operators.
00180 */
00181 
00182 class GlLinearMemoryPool
00183 {
00184   public:
00186         GlLinearMemoryPool( unsigned totalMemory );
00187 
00188         ~GlLinearMemoryPool();
00189 
00191         void* Alloc( unsigned allocate );
00192 
00195         void Free( void* mem )          {}
00196 
00198         bool OutOfMemory()                      { return current == end; }
00199 
00200   public:
00201         char*   base;
00202         char*   current;
00203         char*   end;
00204 };
00205 
00206 #endif

Generated on Fri Feb 7 20:44:20 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001