Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

gust_pool.cpp

Go to the documentation of this file.
00001 /* LIBGUL - Geometry Utility Library
00002  * Copyright (C) 1998-1999 Norbert Irmer
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019  
00020 #include "stdafx.h"
00021 
00022 #include <iostream>
00023  
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 #include "gul_types.h"
00028 #include "gust_pool.h"
00029 
00030 using std::cout;
00031 
00032 GULAPI void gul_halt( void )
00033 {
00034 #ifdef POOLDBG
00035   cout << "this is a place for setting breakpoints\n";
00036 #endif
00037 }
00038 
00039 #ifdef _MSC_VER
00040 GULAPI void *test_gul_dll_malloc( size_t s )
00041 {
00042   return malloc(s);
00043 }
00044 GULAPI void test_gul_dll_free( void *s )
00045 {
00046   free(s);
00047 }
00048 #endif
00049 
00050 namespace gust {
00051 
00052 #ifdef POOLDBG
00053 GULAPI int pooldbg::freekey = 0;
00054 GULAPI int pooldbg::curmark = 0;
00055 GULAPI int pooldbg::count = 0;
00056 GULAPI pooldbg *pooldbg::head;
00057 #endif
00058 
00059 GULAPI
00060 extern PoolChunkList Pools[6] =
00061 {
00062   { 0, 0, 4,    256 },
00063   { 0, 0, 8,    128 },
00064   { 0, 0, 16,   64  },
00065   { 0, 0, 64,   32  },  // for testing
00066   { 0, 0, 256,  16  },
00067   { 0, 0, 2048, 4   }
00068 };
00069 }
00070 #include "gust_pooltbl.h" 
00071 
00072 namespace gust {
00073 
00074 GULAPI void PoolGrow( PoolChunkList *pool )
00075 {
00076   size_t nelems,elemsize;
00077   char *chunk,*start,*end,*p;
00078 
00079   nelems = pool->growrate;
00080   pool->growrate = nelems * 3/2;
00081   elemsize = pool->elemsize;
00082 
00083   /* important: sizeof(char) has to be ONE !!!!!!!!!!! */
00084  
00085   chunk = (char *)malloc( POOL_ALIGNMENT + nelems * elemsize );
00086   if( chunk == NULL ) return;
00087   
00088   start = chunk + POOL_ALIGNMENT;
00089   end = start + (nelems-1)*elemsize;
00090 
00091   for( p = start; p < end; p += elemsize )
00092     *((void **)p) = p + elemsize;
00093   
00094   *((void **)end) = NULL;
00095   
00096   pool->head = start;
00097   *((void **)chunk) = pool->chunks;
00098   pool->chunks = chunk;
00099 }
00100 
00101 GULAPI void PoolFreeAll( void )
00102 {
00103   void *ch, *next_ch;
00104   int nPools = sizeof(Pools)/sizeof(PoolChunkList), i;
00105   PoolChunkList *pool;
00106 
00107   for( i = 0; i < nPools; i++ )
00108   {
00109     pool = &Pools[i];
00110 
00111     ch = pool->chunks;
00112     while( ch )
00113     {
00114       next_ch = *((void **)ch);
00115       free( ch );
00116       ch = next_ch;
00117     }
00118     pool->head = 0;
00119     pool->chunks = 0;
00120   }
00121 }
00122 
00123 
00124 
00125 }
00126 
00127 #if 0
00128 /* -------------------------------------------------------------------------
00129   Program to create the Pool-Table
00130 --------------------------------------------------------------------------- */
00131 #include <stdio.h>
00132 
00133 typedef struct
00134 {
00135   void *head;
00136   void *chunks;
00137 
00138   size_t elemsize;
00139   size_t growrate;
00140 }
00141 PoolChunkList;
00142 
00143 PoolChunkList Pools[] =
00144 {
00145   { 0, 0, 4,    256 },
00146   { 0, 0, 8,    128 },
00147   { 0, 0, 16,   64  },
00148   { 0, 0, 64,   16  },
00149   { 0, 0, 256,  16  },
00150   { 0, 0, 2048, 4   }
00151 };
00152 
00153 #include <iostream>
00154 
00155 int main()
00156 {
00157   cout << "PoolChunkList *PoolTab[2049] = {\n";
00158   size_t pP = 0;
00159   for( size_t i = 0; i < 2049; i++ )
00160   {
00161     if( Pools[pP].elemsize < i ) pP++;
00162 
00163     if( pP > sizeof(Pools)/sizeof(PoolChunkList) ) break;
00164        
00165     cout << "  &Pools[" << pP << "], /* " << i << "*/\n"; 
00166   }
00167   cout << "};\n";
00168 }
00169 #endif
00170 
00171 
00172 
00173 
00174 

Generated on Mon Jan 21 04:17:42 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001