Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
pools.c File Reference

Go to the source code of this file.

Data Structures

struct  free_atom_t
 

Macros

#define DEFAULT_BLOCK_SIZE   8192
 

Functions

void init_pool (pool_t *pool, int atom_size)
 initializes a pool More...
 
void expand_pool (pool_t *pool)
 expands a pool when requested More...
 
void return_to_pool (pool_t *pool, void *a)
 returns atom back to its pool More...
 
voidtake_from_pool (pool_t *pool)
 takes new atom from a pool More...
 

Variables

pool_t data_pool
 pool for data atoms More...
 
pool_t data_pool_ex
 pool for extended data atoms More...
 

Macro Definition Documentation

#define DEFAULT_BLOCK_SIZE   8192

Definition at line 56 of file pools.c.

Function Documentation

void init_pool ( pool_t pool,
int  atom_size 
)
Parameters
poolpool to initialize
atom_sizesize of atom in bytes

Initializes a pool by setting size of atoms and maximal size of the pool. Initially the pool has no any allocated atoms. Aom will be allocated during the first request to take an atom.

Definition at line 79 of file pools.c.

80 {
81  static int id = 0;
82  pool->id = ++id;
83  pool->atom_size = atom_size;
85  pool->free_block_atoms = 0;
86  pool->free_atom = NULL;
87  pool->free_block = NULL;
88  pool->first_atom = NULL;
89 
90  #ifdef DEBUG_POOL
91  printf( "<POOL> Create pool %d (size=%d)\n",
92  pool->id, atom_size );
93  #endif //DEBUG_POOL
94 }
void expand_pool ( pool_t pool)
Parameters
poolpool to be expanded

Assumes that both the list and the block of free free atoms are empty and allocates a new block.

Definition at line 107 of file pools.c.

108 {
109  pool->first_atom = ALLOC( pool->atom_size * pool->block_size );
110  pool->free_block = pool->first_atom;
111 
112 #ifdef DEBUG_CLEAR_FREED_MEM
113  memset( pool->first_atom, 0xFF, pool->atom_size * pool->block_size );
114 #endif //DEBUG_CLEAR_FREE_MEM
115 
116 
117  #ifdef SAFEMODE
118  assert( pool->free_block );
119  #endif //SAFEMODE
120 
121  pool->free_block_atoms = pool->block_size;
122 
123  #ifdef DEBUG_POOL
124  printf( "<POOL> Expand pool %d by %d bytes (%d atoms)\n",
125  pool->id, pool->atom_size*pool->block_size,pool->block_size );
126  #endif //DEBUG_POOL
127 
128  #ifdef ADVANCED
129  stats_free += pool->block_size;
130  stats_allocs--; // do not count pool allocs
131  #endif //ADVANCED
132 }
void return_to_pool ( pool_t pool,
void a 
)
Parameters
poolpool that will accept the atom
aatom to be returned

Returns atom to the pool. The function assumes that the atom size is the same as the pool's atom size. Returned atoms are attached to the list and never to the block.

Definition at line 148 of file pools.c.

149 {
150  //printf("!");
151  #ifdef DEBUG_ATOM
152  printf("<ATOM> -[%08x]\n",(int)a);
153  #endif //DEBUG_ATOM
154 
155  #ifdef DEBUG_RUNTIME_ATOMS
157  {
158  outter( TEXT("<RUNTIME> free "), -1 );
159  dump_atom_address( a );
160  //dump_atom( a, 1 );
161  outter( TEXT("\n"), -1 );
162  }
163  #endif
164  #ifdef DEBUG_COMPILETIME_ATOMS
165  if( compiling_code )
166  {
167  outter( TEXT("<COMPILETIME> free "), -1 );
168  dump_atom_address( a );
169  //dump_atom( a, 1 );
170  outter( TEXT("\n"), -1 );
171  }
172  #endif
173 
174 #ifdef DEBUG_CLEAR_FREED_MEM
175  memset( a, 0xFF, pool->atom_size );
176 #endif //DEBUG_CLEAR_FREE_MEM
177 
178  free_atom_t* b = a;
179  b->next = pool->free_atom;
180  pool->free_atom = b;
181 
182  //printf("delete %x\n",(int)b);
183 
184  //*((void**)a) = pool->free_atom;
185  //pool->free_atom = a;
186 
187 }
void * take_from_pool ( pool_t pool)
Parameters
poolpool from which to allocate atom
Returns
new atom from the pool

Takes atom from the pool. First attempts to take it from the list. If empty it takes from the block. If it is empty too then expands the pool with a new fresh block and takes atom from it.

Definition at line 203 of file pools.c.

204 {
205  // take atom from the list
206  free_atom_t* new = pool->free_atom;
207  //printf(",");
208  if( new )
209  { // there is free atom in the list
210  //printf("alloc %x (next free is %x)\n",(int)new,(int)new->next);
211  pool->free_atom = new->next;
212  }
213  else
214  { // take atom from the block
215  if( !pool->free_block ) expand_pool( pool );
216 
217  new = (free_atom_t*)((char*)pool->free_block + (-- pool->free_block_atoms)*pool->atom_size);
218  if( pool->free_block_atoms == 0 )
219  pool->free_block = NULL;
220  }
221 
222  #ifdef DEBUG_ATOM
223  printf( "<ATOM> +[%08x]\n",(int)new );
224  #endif //ATOM
225 
226  #ifdef DEBUG_RUNTIME_ATOMS
228  {
229  outter( TEXT("<RUNTIME> alloc"), -1 );
230  dump_atom_address( (atom_t)new );
231  outter( TEXT("\n"), -1 );
232  }
233  #endif
234  #ifdef DEBUG_COMPILETIME_ATOMS
235  if( compiling_code )
236  {
237  outter( TEXT("<COMPILETIME> alloc"), -1 );
238  dump_atom_address( (atom_t)new );
239  outter( TEXT("\n"), -1 );
240  }
241  #endif
242 
243  return new;
244 }

Variable Documentation

pool_t data_pool

Definition at line 58 of file pools.c.

pool_t data_pool_ex

Definition at line 59 of file pools.c.


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013