00001 00002 /* 00003 TEDDY - General graphics application library 00004 Copyright (C) 1999-2002 Timo Suoranta 00005 tksuoran@cc.helsinki.fi 00006 00007 Adapted from 00008 00009 The Universe Development Kit 00010 Copyright (C) 2000 Sean O'Neil 00011 soneil@home.com 00012 00013 This library is free software; you can redistribute it and/or 00014 modify it under the terms of the GNU Lesser General Public 00015 License as published by the Free Software Foundation; either 00016 version 2.1 of the License, or (at your option) any later version. 00017 00018 This library is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 Lesser General Public License for more details. 00022 00023 You should have received a copy of the GNU Lesser General Public 00024 License along with this library; if not, write to the Free Software 00025 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 00027 $Id: MemoryBlock.h,v 1.4 2002/01/11 14:35:03 tksuoran Exp $ 00028 */ 00029 00030 00031 #ifndef TEDDY__SYS_SUPPORT__MEMORY_BLOCK__H 00032 #define TEDDY__SYS_SUPPORT__MEMORY_BLOCK__H 00033 00034 00035 #include <cstring> 00036 00037 00038 namespace Teddy { 00039 namespace SysSupport { 00040 00041 00046 class MemoryBlock { 00047 public: 00048 MemoryBlock(){ 00049 elements = 0; 00050 element_size = 0; 00051 pool = NULL; 00052 free_stack = NULL; 00053 free_position = 0; 00054 } 00055 00056 MemoryBlock( int elements, int element_size ){ 00057 pool = NULL; 00058 free_stack = NULL; 00059 init( elements, element_size ); 00060 } 00061 00062 ~MemoryBlock(){ 00063 cleanup(); 00064 } 00065 00066 void cleanup(){ 00067 if( free_stack != NULL ){ 00068 delete (char*)(free_stack); 00069 free_stack = NULL; 00070 } 00071 00072 if( pool != NULL ){ 00073 delete (char*)(pool); 00074 pool = NULL; 00075 } 00076 } 00077 00078 void init( int elements, int element_size ){ 00079 cleanup(); 00080 elements = elements; 00081 element_size = element_size; 00082 pool = new char[elements * element_size]; 00083 // TRACE("CMemoryBlock::Init() allocating %d bytes.\n", nElements * nElementSize); 00084 free_stack = new void *[elements]; 00085 for( free_position = 0; free_position < elements; free_position++ ){ 00086 free_stack[free_position] = (void *)( (int)pool + free_position * element_size ); 00087 } 00088 } 00089 00090 int getElementSize (){ return element_size; } 00091 int getFreeElementCount(){ return free_position; } 00092 00093 void *getElement(){ 00094 void *block = NULL; 00095 if( free_position ){ 00096 block = free_stack[--free_position]; 00097 } 00098 return block; 00099 } 00100 00101 void releaseElement( void *block ){ 00102 free_stack[free_position++] = block; 00103 } 00104 00105 protected: 00106 int elements; 00107 int element_size; 00108 void *pool; 00109 void **free_stack; 00110 int free_position; 00111 }; 00112 00113 00114 }; // namespace SysSupport 00115 }; // namespace Teddy 00116 00117 00118 #endif // TEDDY__SYS_SUPPORT__MEMORY_BLOCK__H 00119