Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Cached_Allocator Class Template Reference

A fixed-size allocator that caches items for quicker access. More...

#include <Malloc_T.h>

Inheritance diagram for ACE_Cached_Allocator:

Inheritance graph
[legend]
Collaboration diagram for ACE_Cached_Allocator:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACE_Cached_Allocator (size_t n_chunks)
 Create a cached memory pool with n_chunks chunks each with sizeof (TYPE) size. More...

 ~ACE_Cached_Allocator (void)
 Clear things up. More...

void * malloc (size_t nbytes=sizeof(T))
virtual void * calloc (size_t nbytes, char initial_value='\0')
virtual void * calloc (size_t n_elem, size_t elem_size, char initial_value='\0')
 This method is a no-op and just returns 0 since the free list only works with fixed sized entities. More...

void free (void *)
 Return a chunk of memory back to free list cache. More...


Private Attributes

char * pool_
 Remember how we allocate the memory in the first place so we can clear things up later. More...

ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node<
T >, ACE_LOCK > 
free_list_
 Maintain a cached memory free list. More...


Detailed Description

template<class T, class ACE_LOCK>
class ACE_Cached_Allocator< T, ACE_LOCK >

A fixed-size allocator that caches items for quicker access.

This class enables caching of dynamically allocated, fixed-sized classes. Notice that the sizeof (TYPE) must be greater than or equal to sizeof (void*) for this to work properly.

See also:
ACE_Dynamic_Cached_Allocator

Definition at line 78 of file Malloc_T.h.


Constructor & Destructor Documentation

template<class T, class ACE_LOCK>
ACE_Cached_Allocator< T, ACE_LOCK >::ACE_Cached_Allocator size_t    n_chunks
 

Create a cached memory pool with n_chunks chunks each with sizeof (TYPE) size.

Definition at line 22 of file Malloc_T.cpp.

References ACE_MALLOC_ALIGN, ACE_NEW, ACE_PURE_FREE_LIST, ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::add, free_list_, and pool_.

00023   : pool_ (0),
00024     free_list_ (ACE_PURE_FREE_LIST)
00025 {
00026   // To maintain alignment requirements, make sure that each element
00027   // inserted into the free list is aligned properly for the platform.
00028   // Since the memory is allocated as a char[], the compiler won't help.
00029   // To make sure enough room is allocated, round up the size so that
00030   // each element starts aligned.
00031   //
00032   // NOTE - this would probably be easier by defining pool_ as a pointer
00033   // to T and allocating an array of them (the compiler would probably
00034   // take care of the alignment for us), but then the ACE_NEW below would
00035   // require a default constructor on T - a requirement that is not in
00036   // previous versions of ACE
00037   size_t chunk_size = sizeof (T);
00038   if (chunk_size % ACE_MALLOC_ALIGN != 0)
00039     chunk_size += (ACE_MALLOC_ALIGN - (chunk_size % ACE_MALLOC_ALIGN));
00040   ACE_NEW (this->pool_,
00041            char[n_chunks * chunk_size]);
00042 
00043   for (size_t c = 0;
00044        c < n_chunks;
00045        c++)
00046     {
00047       void* placement = this->pool_ + c * chunk_size;
00048       this->free_list_.add (new (placement) ACE_Cached_Mem_Pool_Node<T>);
00049     }
00050   // Put into free list using placement contructor, no real memory
00051   // allocation in the above <new>.
00052 }

template<class T, class ACE_LOCK>
ACE_Cached_Allocator< T, ACE_LOCK >::~ACE_Cached_Allocator void   
 

Clear things up.

Definition at line 55 of file Malloc_T.cpp.

References pool_.

00056 {
00057   delete [] this->pool_;
00058 }


Member Function Documentation

template<class T, class ACE_LOCK>
ACE_INLINE void * ACE_Cached_Allocator< T, ACE_LOCK >::calloc size_t    n_elem,
size_t    elem_size,
char    initial_value = '\0'
[virtual]
 

This method is a no-op and just returns 0 since the free list only works with fixed sized entities.

Reimplemented from ACE_New_Allocator.

Definition at line 55 of file Malloc_T.i.

00058 {
00059   ACE_NOTSUP_RETURN (0);
00060 }

template<class T, class ACE_LOCK>
ACE_INLINE void * ACE_Cached_Allocator< T, ACE_LOCK >::calloc size_t    nbytes,
char    initial_value = '\0'
[virtual]
 

Get a chunk of memory from free list cache, giving them initial_value. Note that nbytes is only checked to make sure that it's less or equal to sizeof T, and is otherwise ignored since calloc() always returns a pointer to an item of sizeof (T).

Reimplemented from ACE_New_Allocator.

Definition at line 40 of file Malloc_T.i.

References free_list_, ACE_OS_String::memset, and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::remove.

00042 {
00043   // Check if size requested fits within pre-determined size.
00044   if (nbytes > sizeof (T))
00045     return 0;
00046 
00047   // addr() call is really not absolutely necessary because of the way
00048   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00049   void *ptr = this->free_list_.remove ()->addr ();
00050   ACE_OS::memset (ptr, initial_value, sizeof (T));
00051   return ptr;
00052 }

template<class T, class ACE_LOCK>
ACE_INLINE void ACE_Cached_Allocator< T, ACE_LOCK >::free void *    [virtual]
 

Return a chunk of memory back to free list cache.

Reimplemented from ACE_New_Allocator.

Definition at line 63 of file Malloc_T.i.

References ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::add, and free_list_.

00064 {
00065   if (ptr != 0)
00066     this->free_list_.add ((ACE_Cached_Mem_Pool_Node<T> *) ptr) ;
00067 }

template<class T, class ACE_LOCK>
ACE_INLINE void * ACE_Cached_Allocator< T, ACE_LOCK >::malloc size_t    nbytes = sizeof(T) [virtual]
 

Get a chunk of memory from free list cache. Note that nbytes is only checked to make sure that it's less or equal to sizeof T, and is otherwise ignored since malloc() always returns a pointer to an item of sizeof (T).

Reimplemented from ACE_New_Allocator.

Definition at line 28 of file Malloc_T.i.

References free_list_, and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::remove.

00029 {
00030   // Check if size requested fits within pre-determined size.
00031   if (nbytes > sizeof (T))
00032     return 0;
00033 
00034   // addr() call is really not absolutely necessary because of the way
00035   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00036   return this->free_list_.remove ()->addr ();
00037 }


Member Data Documentation

template<class T, class ACE_LOCK>
ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> ACE_Cached_Allocator::free_list_ [private]
 

Maintain a cached memory free list.

Definition at line 120 of file Malloc_T.h.

Referenced by ACE_Cached_Allocator, calloc, free, and malloc.

template<class T, class ACE_LOCK>
char* ACE_Cached_Allocator::pool_ [private]
 

Remember how we allocate the memory in the first place so we can clear things up later.

Definition at line 117 of file Malloc_T.h.

Referenced by ACE_Cached_Allocator, and ~ACE_Cached_Allocator.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:46:21 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002