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

ACE_Dynamic_Cached_Allocator Class Template Reference

A size-based allocator that caches blocks for quicker access. More...

#include <Malloc_T.h>

Inheritance diagram for ACE_Dynamic_Cached_Allocator:

Inheritance graph
[legend]
Collaboration diagram for ACE_Dynamic_Cached_Allocator:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACE_Dynamic_Cached_Allocator (size_t n_chunks, size_t chunk_size)
 Create a cached memory pool with n_chunks chunks each with chunk_size size. More...

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

void * malloc (size_t nbytes=0)
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<
char >, ACE_LOCK > 
free_list_
 Maintain a cached memory free list. We use char as template parameter, although sizeof(char) is usually less than sizeof(void*). Really important is that chunk_size must be greater or equal to sizeof(void*). More...

size_t chunk_size_
 Remember the size of our chunks. More...


Detailed Description

template<class ACE_LOCK>
class ACE_Dynamic_Cached_Allocator< ACE_LOCK >

A size-based allocator that caches blocks for quicker access.

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

See also:
ACE_Cached_Allocator

Definition at line 136 of file Malloc_T.h.


Constructor & Destructor Documentation

template<class ACE_LOCK>
ACE_Dynamic_Cached_Allocator< ACE_LOCK >::ACE_Dynamic_Cached_Allocator size_t    n_chunks,
size_t    chunk_size
 

Create a cached memory pool with n_chunks chunks each with chunk_size size.

Definition at line 62 of file Malloc_T.cpp.

References ACE_NEW, and ACE_PURE_FREE_LIST.

00063     : pool_ (0),
00064       free_list_ (ACE_PURE_FREE_LIST),
00065       chunk_size_(chunk_size)
00066 {
00067   ACE_NEW (this->pool_, char[n_chunks * chunk_size_]);
00068 
00069   for (size_t c = 0;
00070        c < n_chunks;
00071        c++)
00072     {
00073       void* placement = this->pool_ + c * chunk_size_;
00074 
00075       this->free_list_.add (new (placement) ACE_Cached_Mem_Pool_Node<char>);
00076     }
00077   // Put into free list using placement contructor, no real memory
00078   // allocation in the above <new>.
00079 }

template<class ACE_LOCK>
ACE_Dynamic_Cached_Allocator< ACE_LOCK >::~ACE_Dynamic_Cached_Allocator void   
 

Clear things up.

Definition at line 82 of file Malloc_T.cpp.

References chunk_size_, and pool_.

00083 {
00084   delete [] this->pool_;
00085   this->pool_ = 0;
00086   chunk_size_ = 0;
00087 }


Member Function Documentation

template<class ACE_LOCK>
ACE_INLINE void * ACE_Dynamic_Cached_Allocator< 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 97 of file Malloc_T.i.

00098 {
00099   ACE_NOTSUP_RETURN (0);
00100 }

template<class ACE_LOCK>
ACE_INLINE void * ACE_Dynamic_Cached_Allocator< 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 chunk_size, and is otherwise ignored since calloc() always returns a pointer to an item of chunk_size.

Reimplemented from ACE_New_Allocator.

Definition at line 82 of file Malloc_T.i.

References ACE_Cached_Mem_Pool_Node< char >::addr, chunk_size_, free_list_, ACE_OS_String::memset, and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::remove.

00084 {
00085   // Check if size requested fits within pre-determined size.
00086   if (nbytes > chunk_size_)
00087     return 0;
00088 
00089   // addr() call is really not absolutely necessary because of the way
00090   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00091   void *ptr = this->free_list_.remove ()->addr ();
00092   ACE_OS::memset (ptr, initial_value, chunk_size_);
00093   return ptr;
00094 }

template<class ACE_LOCK>
ACE_INLINE void ACE_Dynamic_Cached_Allocator< ACE_LOCK >::free void *    [virtual]
 

Return a chunk of memory back to free list cache.

Reimplemented from ACE_New_Allocator.

Definition at line 103 of file Malloc_T.i.

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

00104 {
00105   this->free_list_.add ((ACE_Cached_Mem_Pool_Node<char> *) ptr);
00106 }

template<class ACE_LOCK>
ACE_INLINE void * ACE_Dynamic_Cached_Allocator< ACE_LOCK >::malloc size_t    nbytes = 0 [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 chunk_size, and is otherwise ignored since malloc() always returns a pointer to an item of chunk_size size.

Reimplemented from ACE_New_Allocator.

Definition at line 70 of file Malloc_T.i.

References ACE_Cached_Mem_Pool_Node< char >::addr, chunk_size_, free_list_, and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::remove.

00071 {
00072   // Check if size requested fits within pre-determined size.
00073   if (nbytes > chunk_size_)
00074     return 0;
00075 
00076   // addr() call is really not absolutely necessary because of the way
00077   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00078   return this->free_list_.remove ()->addr ();
00079 }


Member Data Documentation

template<class ACE_LOCK>
size_t ACE_Dynamic_Cached_Allocator::chunk_size_ [private]
 

Remember the size of our chunks.

Definition at line 184 of file Malloc_T.h.

Referenced by calloc, malloc, and ~ACE_Dynamic_Cached_Allocator.

template<class ACE_LOCK>
ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>, ACE_LOCK> ACE_Dynamic_Cached_Allocator::free_list_ [private]
 

Maintain a cached memory free list. We use char as template parameter, although sizeof(char) is usually less than sizeof(void*). Really important is that chunk_size must be greater or equal to sizeof(void*).

Definition at line 181 of file Malloc_T.h.

Referenced by calloc, free, and malloc.

template<class ACE_LOCK>
char* ACE_Dynamic_Cached_Allocator::pool_ [private]
 

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

Definition at line 175 of file Malloc_T.h.

Referenced by ~ACE_Dynamic_Cached_Allocator.


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