Lhogho  0.0.027
Data Structures | Functions | Variables
pools.h File Reference

Data Structures

struct  pool_t
 internal structure of a pool More...

Functions

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

Variables

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

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.

{
  static int id = 0;
  pool->id      = ++id;
  pool->atom_size  = atom_size;
  pool->block_size    = DEFAULT_BLOCK_SIZE;
  pool->free_block_atoms = 0;
  pool->free_atom  = NULL;
  pool->free_block    = NULL;
  pool->first_atom    = NULL;

  #ifdef DEBUG_POOL
    printf( "<POOL> Create pool %d (size=%d)\n",
       pool->id, atom_size );
  #endif //DEBUG_POOL
}
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.

{
  // take atom from the list
  free_atom_t* new = pool->free_atom;
  //printf(",");
  if( new )
    { // there is free atom in the list
      //printf("alloc  %x (next free is %x)\n",(int)new,(int)new->next);
      pool->free_atom = new->next;
    }
  else
    { // take atom from the block
      if( !pool->free_block ) expand_pool( pool );

      new = (free_atom_t*)((char*)pool->free_block + (-- pool->free_block_atoms)*pool->atom_size);
      if( pool->free_block_atoms == 0 )
   pool->free_block = NULL;
    }

  #ifdef DEBUG_ATOM
  printf( "<ATOM> +[%08x]\n",(int)new );
  #endif //ATOM

  #ifdef DEBUG_RUNTIME_ATOMS
  if( running_compiled_code )
    {
      outter( TEXT("<RUNTIME> alloc"), -1 );
      dump_atom_address( (atom_t)new );
      outter( TEXT("\n"), -1 );
    }
  #endif
  #ifdef DEBUG_COMPILETIME_ATOMS
  if( compiling_code )
    {
      outter( TEXT("<COMPILETIME> alloc"), -1 );
      dump_atom_address( (atom_t)new );
      outter( TEXT("\n"), -1 );
    }
  #endif

  return new;
}
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.

{
  //printf("!");
  #ifdef DEBUG_ATOM
  printf("<ATOM> -[%08x]\n",(int)a);
  #endif //DEBUG_ATOM

  #ifdef DEBUG_RUNTIME_ATOMS
  if( running_compiled_code )
    {
      outter( TEXT("<RUNTIME> free "), -1 );
      dump_atom_address( a );
      //dump_atom( a, 1 );
      outter( TEXT("\n"), -1 );
    }
  #endif
  #ifdef DEBUG_COMPILETIME_ATOMS
  if( compiling_code )
    {
      outter( TEXT("<COMPILETIME> free "), -1 );
      dump_atom_address( a );
      //dump_atom( a, 1 );
      outter( TEXT("\n"), -1 );
    }
  #endif

#ifdef DEBUG_CLEAR_FREED_MEM
  memset( a, 0xFF, pool->atom_size );
#endif //DEBUG_CLEAR_FREE_MEM

  free_atom_t* b = a;
  b->next = pool->free_atom;
  pool->free_atom = b;

  //printf("delete %x\n",(int)b);

  //*((void**)a) = pool->free_atom;
  //pool->free_atom = a;

}

Variable Documentation


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Tue Feb 7 2012