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

Go to the source code of this file.

Data Structures

struct  pool_t
 internal structure of a pool More...
 

Functions

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

Variables

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

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* 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 }
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 }

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