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

block_pool Class Reference

An abstract base class for pooled memory allocation. More...

#include <blockpool.h>

Inheritance diagram for block_pool:

Inheritance graph
[legend]
Collaboration diagram for block_pool:

Collaboration graph
[legend]
List of all members.

Public Types

typedef block_chain blk_chain
 This provides a publicly visible "type handle" that references a private type.


Public Member Functions

 block_pool (void)
void free_pool (void)
 Walk through the block chain and deallocate the blocks.

void * pool_alloc (unsigned int block_size)
 This function is called to allocate memory from the memory pool.

void print_block_pool_info (FILE *fp=stdout)
 Print information about the block pool.

virtual void getinfo (unsigned int &p_size, unsigned int &a_gran)=0
virtual void * MemAlloc (unsigned int n_bytes)=0
virtual void MemFree (void *addr)=0

Private Types

typedef block_pool::block_chain_struct block_chain
enum  bogus { max_block_multiple = 256 }
 the largest block of memory that can be allocated is the page_size * max_block_multiple More...


Private Member Functions

blk_chainnew_block (unsigned int block_size)
 The new_block function is the "root" memory allocator for the block_pool object.

void * add_block (unsigned int block_size)
 Add a new memory block to the memory pool.

void init_pool (void)

Private Attributes

unsigned int page_size
unsigned int alloc_gran
block_chainblock_list_start
 start of the block list for this pool

block_chaincurrent_block
 current block memory is being allocated from


Detailed Description

An abstract base class for pooled memory allocation.

Definition at line 95 of file blockpool.h.


Member Typedef Documentation

typedef block_chain block_pool::blk_chain
 

This provides a publicly visible "type handle" that references a private type.

Sun is quite permissive and does not require this sort of contortion, but HP does.

Definition at line 126 of file blockpool.h.

typedef struct block_pool::block_chain_struct block_pool::block_chain [private]
 


Member Enumeration Documentation

enum block_pool::bogus [private]
 

the largest block of memory that can be allocated is the page_size * max_block_multiple

Enumeration values:
max_block_multiple 

Definition at line 99 of file blockpool.h.

00099 { max_block_multiple = 256 } bogus;


Constructor & Destructor Documentation

block_pool::block_pool void   ) 
 

Definition at line 33 of file blockpool.C.

References alloc_gran, block_list_start, current_block, NULL, and page_size.

00034 {
00035   
00036   page_size = 0;
00037   alloc_gran = 0;
00038   current_block = NULL;
00039   block_list_start = NULL;
00040 } // block_pool constructor


Member Function Documentation

void * block_pool::add_block unsigned int  block_size  )  [private]
 

Add a new memory block to the memory pool.

This function is called when the amount of memory requested by pool_alloc will not fit in the current block.

Definition at line 133 of file blockpool.C.

References Chain_next, current_block, new_block(), and NULL.

Referenced by pool_alloc().

00134 {
00135   block_chain *block = NULL;
00136   block_chain *last_block;
00137 
00138   last_block = current_block;
00139   block = new_block( block_size );
00140   Chain_next(current_block) = block;
00141   current_block = block;
00142 
00143   return (void *)block;
00144 } // block_chain::add_block

void block_pool::free_pool void   ) 
 

Walk through the block chain and deallocate the blocks.

Note that the block chain structures and the allocatible memory is contained within a single allocated block. The block_chain structure is at the start of this block so passing its address to the memory deallocation function deallocates both the block chain structure and the allocatible memory

Definition at line 70 of file blockpool.C.

References block_list_start, Chain_next, MemFree(), and NULL.

Referenced by big_block_pool::~big_block_pool().

00071 {
00072   block_chain *tmp;
00073 
00074   while (block_list_start != NULL) {
00075     tmp = block_list_start;
00076     block_list_start = Chain_next(block_list_start);
00077     MemFree( (void *)tmp );
00078   }
00079 }

virtual void block_pool::getinfo unsigned int &  p_size,
unsigned int &  a_gran
[pure virtual]
 

Implemented in big_block_pool.

Referenced by init_pool().

void block_pool::init_pool void   )  [private]
 

Definition at line 47 of file blockpool.C.

References alloc_gran, block_list_start, current_block, getinfo(), new_block(), and page_size.

Referenced by pool_alloc().

00048 {
00049 
00050   block_chain *new_link;
00051 
00052   getinfo(page_size, alloc_gran);
00053 
00054   new_link = new_block( alloc_gran );
00055   block_list_start = new_link;
00056   current_block = new_link;
00057 } /* init_pool */

virtual void* block_pool::MemAlloc unsigned int  n_bytes  )  [pure virtual]
 

Implemented in big_block_pool.

Referenced by new_block().

virtual void block_pool::MemFree void *  addr  )  [pure virtual]
 

Implemented in big_block_pool.

Referenced by free_pool().

block_pool::blk_chain * block_pool::new_block unsigned int  block_size  )  [private]
 

The new_block function is the "root" memory allocator for the block_pool object.

The amount of memory allocated is rounded up to the next "block_size" boundary. Both the block_chain structure and the allocatible memory are allocated from a single block that is a multiple of the page size. This should avoid fragmentation in the system memory allocator.

Definition at line 90 of file blockpool.C.

References alloc_gran, Chain_block, Chain_block_size, Chain_bytes_used, Chain_next, max_block_multiple, MemAlloc(), NULL, and page_size.

Referenced by add_block(), and init_pool().

00091 {
00092   const unsigned int max_block_size = max_block_multiple * page_size;
00093   block_chain *new_link = NULL;
00094   unsigned int alloc_amt, total_alloc;
00095 
00096   // add in the memory needed for the block_chain structure
00097   total_alloc = block_size + sizeof(block_chain);
00098   if (total_alloc < alloc_gran)
00099       alloc_amt = alloc_gran;
00100   else { 
00101       // its larger than the allocation granularity, so round
00102       // up the the nearest page.
00103       alloc_amt = ((total_alloc + (page_size-1))/page_size) * page_size;
00104   }
00105 
00106   if (alloc_amt <= max_block_size) {
00107 
00108     /* allocate memory for both the block_chain structure and the memory block */
00109     new_link = (block_chain *)MemAlloc( alloc_amt );
00110 
00111     // The new memory block starts after the block_chain structure
00112     Chain_block(new_link) = (void *)(((unsigned int)new_link) + sizeof(block_chain));
00113 
00114     assert( alloc_amt >= block_size );
00115 
00116     Chain_bytes_used(new_link) = 0;
00117     Chain_block_size(new_link) = alloc_amt - sizeof(block_chain);
00118     Chain_next(new_link) = NULL;
00119   }
00120   else {
00121     printf("block_pool::new_block: allocation request too large\n");
00122   }
00123 
00124   return new_link;
00125 } // block_chain::new_block

void * block_pool::pool_alloc unsigned int  num_bytes  ) 
 

This function is called to allocate memory from the memory pool.

If there is enough free memory in the current block to satisify the memory request, memory is allocated from the current block and the amount of free memory is updated. If the current block does not have enough memory, add_block is called to allocate a new memory block which will be large enough.

Definition at line 156 of file blockpool.C.

References add_block(), Chain_block, Chain_block_size, Chain_bytes_used, current_block, init_pool(), and NULL.

00157 {
00158   const unsigned int align = sizeof( int );
00159   void *addr = NULL;
00160   unsigned int amt_free;
00161 
00162   /* the number of bytes allocated must be a multiple of the align
00163      size */
00164   num_bytes = ((num_bytes + (align-1))/align) * align;
00165 
00166   if (current_block == NULL) {
00167     init_pool();
00168   }
00169 
00170   amt_free = Chain_block_size(current_block) - Chain_bytes_used(current_block);
00171   
00172   if (num_bytes > amt_free) {
00173     if (add_block( num_bytes ) != NULL) {
00174       amt_free = Chain_block_size(current_block);
00175     }
00176   }
00177 
00178   if (amt_free >= num_bytes) {
00179     addr = (void *)((unsigned int)Chain_block(current_block) + Chain_bytes_used(current_block));
00180     Chain_bytes_used(current_block) += num_bytes;
00181   }
00182   else {
00183     printf("block_pool::block_alloc: allocation error\n");
00184     exit(1);
00185   }
00186   return addr;
00187 } // block_pool::pool_alloc

void block_pool::print_block_pool_info FILE *  fp = stdout  ) 
 

Print information about the block pool.

Definition at line 194 of file blockpool.C.

References block_list_start, Chain_block_size, Chain_bytes_used, Chain_next, and NULL.

00195 {
00196   int total_allocated = 0;
00197   int total_unused = 0;
00198   block_chain *ptr = block_list_start;
00199 
00200   fprintf(fp, "[block size, bytes_used]\n");
00201   while (ptr != NULL) {
00202     fprintf(fp, "[%4d, %4d]", Chain_block_size(ptr), Chain_bytes_used(ptr));
00203     total_allocated += Chain_bytes_used(ptr);
00204     total_unused += (Chain_block_size(ptr) - Chain_bytes_used(ptr));
00205     if (Chain_next(ptr) != NULL) {
00206       fprintf(fp, ", ");
00207     }
00208     else {
00209       fprintf(fp, "\n");
00210     }
00211     ptr = Chain_next(ptr);
00212   } // while
00213   fprintf(fp, "Total allocated = %5d, total unused = %3d\n", total_allocated,
00214          total_unused );
00215 }


Member Data Documentation

unsigned int block_pool::alloc_gran [private]
 

Definition at line 113 of file blockpool.h.

Referenced by block_pool(), init_pool(), and new_block().

block_chain* block_pool::block_list_start [private]
 

start of the block list for this pool

Definition at line 116 of file blockpool.h.

Referenced by block_pool(), free_pool(), init_pool(), and print_block_pool_info().

block_chain* block_pool::current_block [private]
 

current block memory is being allocated from

Definition at line 118 of file blockpool.h.

Referenced by add_block(), block_pool(), init_pool(), and pool_alloc().

unsigned int block_pool::page_size [private]
 

Definition at line 112 of file blockpool.h.

Referenced by block_pool(), init_pool(), and new_block().


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