Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

mem_pool< pool_type, max_handle > Class Template Reference

This is a template class that implements arrays of memory pools. More...

#include <pools.h>

Inheritance diagram for mem_pool< pool_type, max_handle >:

Inheritance graph
[legend]
Collaboration diagram for mem_pool< pool_type, max_handle >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef unsigned int pool_handle

Public Member Functions

void pool_init (void)
pool_handle get_new_pool (void)
 Find the first free pool slot and allocate a pool.

void release_pool (pool_handle handle)
void * GetMem (pool_handle handle, unsigned int num_bytes)
void pr (FILE *fp, pool_handle handle)

Private Attributes

pool_type * pools [max_handle]

Detailed Description

template<class pool_type, int max_handle>
class mem_pool< pool_type, max_handle >

This is a template class that implements arrays of memory pools.

This allows the user to request a new memory pool (get_new_pool), allocate memory from this pool (GetMem) and delete the pool (release_pool).

The mem_pool class should be instantiated with a memory pool allocation object. For example:

mem_pool<big_block_pool, 128> big_pool;

The constant argument (e.g., 128, in the above example), defines the maximum number of pools that can be active at once. Each pool has its own block chain and can be allocated and deallocated independently of the other pools.

The class functions are in-lined because some C++ compilers (notably Sun's) only support templates with in-lined class functions.

Definition at line 48 of file pools.h.


Member Typedef Documentation

template<class pool_type, int max_handle>
typedef unsigned int mem_pool< pool_type, max_handle >::pool_handle
 

Definition at line 54 of file pools.h.

Referenced by mem_pool< big_block_pool, MAX_POOLS >::get_new_pool().


Member Function Documentation

template<class pool_type, int max_handle>
pool_handle mem_pool< pool_type, max_handle >::get_new_pool void   )  [inline]
 

Find the first free pool slot and allocate a pool.

Definition at line 67 of file pools.h.

00068     {
00069         pool_handle handle;
00070 
00071         for (handle = min_handle; handle < max_handle; handle++) {
00072             if (pools[handle] == NULL) {
00073                 pools[handle] = new pool_type;
00074                 break;
00075             }
00076         }
00077         if (handle > max_handle) {
00078             Fatal("big_pool::get_new_pool: no memory pools available");
00079         }
00080 
00081         return handle;
00082     } // get_new_pool

template<class pool_type, int max_handle>
void* mem_pool< pool_type, max_handle >::GetMem pool_handle  handle,
unsigned int  num_bytes
[inline]
 

Definition at line 95 of file pools.h.

00096     {
00097         void *mem;
00098 
00099         mem = NULL;
00100         if (handle < max_handle && pools[ handle ] != NULL) {
00101             // mem = pools[ handle ]->MemAlloc( num_bytes );
00102             mem = pools[ handle ]->pool_alloc( num_bytes );
00103         }
00104         else {
00105             Fatal("big_pool::GetMem: bad pool handle");
00106         }
00107         return mem;
00108     } // GetMem

template<class pool_type, int max_handle>
void mem_pool< pool_type, max_handle >::pool_init void   )  [inline]
 

Definition at line 57 of file pools.h.

00058     {
00059         int i;
00060         
00061         for (i = min_handle; i < max_handle; i++) {
00062             pools[ i ] = NULL;
00063         }
00064     }  // pool_init 

template<class pool_type, int max_handle>
void mem_pool< pool_type, max_handle >::pr FILE *  fp,
pool_handle  handle
[inline]
 

Definition at line 110 of file pools.h.

00111     {
00112         if (handle < max_handle && pools[ handle ] != NULL) {
00113             pools[ handle ]->print_block_pool_info();
00114         }
00115         else {
00116             fprintf(fp, "big_pool::pr: bad pool handle");
00117         }
00118     } // pr

template<class pool_type, int max_handle>
void mem_pool< pool_type, max_handle >::release_pool pool_handle  handle  )  [inline]
 

Definition at line 84 of file pools.h.

00085     {
00086         if (handle < max_handle && pools[ handle ] != NULL) {
00087             delete pools[ handle ];
00088             pools[ handle ] = NULL;
00089         }
00090         else {
00091             Fatal("big_pool::release_pool: bad pool handle");
00092         }
00093     } // release_pool


Member Data Documentation

template<class pool_type, int max_handle>
pool_type* mem_pool< pool_type, max_handle >::pools[ max_handle ] [private]
 

Definition at line 51 of file pools.h.

Referenced by mem_pool< big_block_pool, MAX_POOLS >::get_new_pool(), mem_pool< big_block_pool, MAX_POOLS >::GetMem(), mem_pool< big_block_pool, MAX_POOLS >::pool_init(), mem_pool< big_block_pool, MAX_POOLS >::pr(), and mem_pool< big_block_pool, MAX_POOLS >::release_pool().


The documentation for this class was generated from the following file:
Generated on Wed Mar 31 21:16:07 2004 for Data Structures for a VHDL Compiler by doxygen 1.3.3