Lhogho  0.0.027
Data Structures | Defines | Functions | Variables
pools.c File Reference

Data Structures

struct  free_atom_t

Defines

#define DEFAULT_BLOCK_SIZE   8192

Functions

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

Variables

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

Define Documentation

#define DEFAULT_BLOCK_SIZE   8192

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 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.

{
  pool->first_atom = ALLOC( pool->atom_size * pool->block_size );
  pool->free_block = pool->first_atom;

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


  #ifdef SAFEMODE
    assert( pool->free_block );
  #endif //SAFEMODE

  pool->free_block_atoms = pool->block_size;

  #ifdef DEBUG_POOL
    printf( "<POOL> Expand pool %d by %d bytes (%d atoms)\n",
       pool->id, pool->atom_size*pool->block_size,pool->block_size );
  #endif //DEBUG_POOL

  #ifdef ADVANCED
    stats_free += pool->block_size;
    stats_allocs--; // do not count pool allocs
  #endif //ADVANCED
}
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;

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

Variable Documentation


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