00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 },
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
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
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