shm_allocator Class Reference

Shared memory allocator for use with template containers. More...

#include <shm_allocator.h>

Collaboration diagram for shm_allocator:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 shm_allocator (shm_segment &s)
shm_allocator_blocksplit_block (shm_allocator_block *b, size_t s)
shm_allocator_blockinsert_block (shm_allocator_block *b)
shm_allocator_blockremove_block (shm_allocator_block *b)
void * alloc (size_t nbytes)
void free (void *addr)
const shm_segmentget_segment () const
size_t get_overhead ()

Private Member Functions

void init ()
void init_allocator_header ()
void init_allocator_header_lock (shm_allocator_header *head)

Private Attributes

shm_segmentseg
shm_allocator_headerheader

Classes

struct  shm_allocator_block
 shared memory allocation block More...
struct  shm_allocator_header
 shared memory allocator header structure More...

Detailed Description

Shared memory allocator for use with template containers.

The shared allocator uses a sorted circular doubly linked list for the free block list and tries to merge contiguous blocks on free. Currently the allocator initializes the shm_allocator_header in the constructor.

Note : use the following to provide new and delete to work with shm_allocator *


   void* operator new(size_t s, shm_allocator& a)
   {
     return a.alloc(s);
   }
  
   void operator delete(void* p, shm_allocator& a)
   {
     a.free(p);
   }

   

Definition at line 44 of file shm_allocator.h.


Constructor & Destructor Documentation

shm_allocator::shm_allocator ( shm_segment s  )  [inline]

construct a shared memory allocator with a shm_segment.

The allocator will use the entire segment for allocation. The allocator header structure will be at offset zero. Access to the segment is protected using a process shared POSIX mutex.

Parameters:
s shared memory segment as source for memory allocation.
TODO : fix up free for merging contiguous free blocks adjusting sorted list

Definition at line 148 of file shm_allocator.h.

References init().

Here is the call graph for this function:


Member Function Documentation

void shm_allocator::init (  )  [inline, private]

initialize the shared memory allocator

The initialization does a simple check for the shared memory segment having already been initialized. This consists of a check of the memory at the base of the shared segment for the shared memory key. If there is a situation where this check fails, the initialization of the shared memory segment to all zero's may be required.

Definition at line 92 of file shm_allocator.h.

References shm_segment::get_base(), shm_segment::get_key(), header, init_allocator_header(), shm_allocator::shm_allocator_header::key, and seg.

Referenced by shm_allocator().

Here is the call graph for this function:

void shm_allocator::init_allocator_header (  )  [inline, private]

initialize allocator header structure

The initialization creates the first allocator block with a size of the shared memory segement minus the size of the header information. The allocator header mutex lock initialization method is also invoked.

Definition at line 108 of file shm_allocator.h.

References shm_allocator::shm_allocator_header::freelist, shm_segment::get_base(), shm_segment::get_key(), shm_segment::get_size(), header, init_allocator_header_lock(), shm_allocator::shm_allocator_header::key, shm_allocator::shm_allocator_block::next, overhead, shm_allocator::shm_allocator_block::prev, seg, and shm_allocator::shm_allocator_block::size.

Referenced by init().

Here is the call graph for this function:

void shm_allocator::init_allocator_header_lock ( shm_allocator_header head  )  [inline, private]

initialize allocator header mutex lock with all process scope

Parameters:
head allocator header block containing lock to be initialized

Definition at line 125 of file shm_allocator.h.

References shm_allocator::shm_allocator_header::lock.

Referenced by init_allocator_header().

shm_allocator::shm_allocator_block * shm_allocator::split_block ( shm_allocator::shm_allocator_block b,
size_t  s 
)

split an allocator block into two blocks given a request size.

Parameters:
b pointer to the current block to split
s request size already rounded up for alignment
Returns:
remainder block with a size of the original block size minus the requested size s, with prev and next set to zero.
The original block b size is adjusted to be the size_t s

Definition at line 289 of file shm_allocator.h.

References shm_allocator::shm_allocator_block::next, shm_allocator::shm_allocator_block::prev, and shm_allocator::shm_allocator_block::size.

Referenced by alloc().

shm_allocator::shm_allocator_block * shm_allocator::insert_block ( shm_allocator::shm_allocator_block b  ) 

insert an allocator block to the free list

Parameters:
b pointer to the block to insert
Returns:
c which is the next chunk from b (after insertion)

Definition at line 250 of file shm_allocator.h.

References block_size, get_freelist, next_block, prev_block, set_freelist, set_next_block, and set_prev_block.

Referenced by alloc(), and free().

shm_allocator::shm_allocator_block * shm_allocator::remove_block ( shm_allocator::shm_allocator_block b  ) 

remove an allocator block from the free list

Get the first block from the freelist. If the block to remove is the first block, fix up the freelist reference.

Parameters:
b pointer to the current block to remove
WARNING : if the block to be removed is the only block, bad things may happen to the original first free block !

Definition at line 228 of file shm_allocator.h.

References shm_segment::get_base(), get_freelist, next_block, prev_block, seg, set_freelist, set_next_block, and set_prev_block.

Referenced by alloc().

Here is the call graph for this function:

void * shm_allocator::alloc ( size_t  bytes  ) 

allocate a block of memory

The allocation of a block of memory, that is appropriate given the requested size, scans the free list of memory blocks picking the first block that is large enough to service the request, removing it from the free list. If the free block to be used is larger than required, the block is split with the remainder being placed back into the free list.

Definition at line 176 of file shm_allocator.h.

References shm_ptr< T >::get(), get_freelist, header, insert_block(), shm_allocator::shm_allocator_header::lock, shm_allocator::shm_allocator_block::next, next_block, shm_allocator::shm_allocator_block::prev, remove_block(), seg, shm_allocator::shm_allocator_block::size, and split_block().

Referenced by main(), shm_queue< T >::operator new(), shm_queue_header< T >::operator new(), shm_queue_node< T >::operator new(), shm_message::operator new(), and shm_message::setText().

Here is the call graph for this function:

void shm_allocator::free ( void *  addr  ) 

Definition at line 210 of file shm_allocator.h.

References header, insert_block(), and shm_allocator::shm_allocator_header::lock.

Referenced by shm_queue< T >::operator delete(), shm_queue_header< T >::operator delete(), shm_queue_node< T >::operator delete(), and shm_message::operator delete().

Here is the call graph for this function:

const shm_segment& shm_allocator::get_segment (  )  const [inline]

Definition at line 158 of file shm_allocator.h.

References seg.

size_t shm_allocator::get_overhead (  )  [inline]

Definition at line 159 of file shm_allocator.h.

Referenced by shm_queue< T >::pop(), shm_queue< T >::push(), and shm_queue< T >::shm_queue().


Member Data Documentation

shm_segment& shm_allocator::seg [private]

Definition at line 134 of file shm_allocator.h.

Referenced by alloc(), get_segment(), init(), init_allocator_header(), and remove_block().

shm_allocator_header* shm_allocator::header [private]

Definition at line 135 of file shm_allocator.h.

Referenced by alloc(), free(), init(), and init_allocator_header().


The documentation for this class was generated from the following file:
Project shmq hosted by   SourceForge.net
Documentation generated on Sat Sep 2 10:07:40 2006 for shmq by   doxygen.org 1.4.6