ACE_Dynamic_Cached_Allocator< ACE_LOCK > 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< ACE_LOCK >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Dynamic_Cached_Allocator< ACE_LOCK >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Dynamic_Cached_Allocator (size_t n_chunks, size_t chunk_size)
 ~ACE_Dynamic_Cached_Allocator (void)
 Clear things up.

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')
void free (void *)
 Return a chunk of memory back to free list cache.

size_t pool_depth (void)
 Return the number of chunks available in the cache.


Private Attributes

char * pool_
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node<
char >, ACE_LOCK > 
free_list_
size_t chunk_size_
 Remember the size of our chunks.


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.

This class can be configured flexibly with different types of ACE_LOCK strategies that support the ACE_Thread_Mutex and ACE_Process_Mutex constructor API.

See also:
ACE_Cached_Allocator

Definition at line 150 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 105 of file Malloc_T.cpp.

References ACE_MALLOC_ALIGN, ACE_MALLOC_ROUNDUP, and ACE_NEW.

00106     : pool_ (0),
00107       free_list_ (ACE_PURE_FREE_LIST),
00108       chunk_size_(chunk_size)
00109 {
00110   chunk_size = ACE_MALLOC_ROUNDUP (chunk_size, ACE_MALLOC_ALIGN);
00111   ACE_NEW (this->pool_, char[n_chunks * chunk_size_]);
00112 
00113   for (size_t c = 0;
00114        c < n_chunks;
00115        c++)
00116     {
00117       void* placement = this->pool_ + c * chunk_size_;
00118 
00119       this->free_list_.add (new (placement) ACE_Cached_Mem_Pool_Node<char>);
00120     }
00121   // Put into free list using placement contructor, no real memory
00122   // allocation in the above <new>.
00123 }

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

Clear things up.

Definition at line 126 of file Malloc_T.cpp.

References ACE_Dynamic_Cached_Allocator< ACE_LOCK >::chunk_size_.

00127 {
00128   delete [] this->pool_;
00129   this->pool_ = 0;
00130   chunk_size_ = 0;
00131 }


Member Function Documentation

template<class ACE_LOCK>
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 162 of file Malloc_T.cpp.

References ACE_NOTSUP_RETURN.

00163 {
00164   ACE_NOTSUP_RETURN (0);
00165 }

template<class ACE_LOCK>
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 146 of file Malloc_T.cpp.

References ACE_Cached_Mem_Pool_Node< char >::addr(), ACE_Dynamic_Cached_Allocator< ACE_LOCK >::chunk_size_, ACE_OS::memset(), and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::remove().

00148 {
00149   // Check if size requested fits within pre-determined size.
00150   if (nbytes > chunk_size_)
00151     return 0;
00152 
00153   // addr() call is really not absolutely necessary because of the way
00154   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00155   void *ptr = this->free_list_.remove ()->addr ();
00156   if (ptr != 0)
00157     ACE_OS::memset (ptr, initial_value, chunk_size_);
00158   return ptr;
00159 }

template<class ACE_LOCK>
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 168 of file Malloc_T.cpp.

References ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::add().

00169 {
00170   if (ptr != 0)
00171     this->free_list_.add ((ACE_Cached_Mem_Pool_Node<char> *) ptr);
00172 }

template<class ACE_LOCK>
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 134 of file Malloc_T.cpp.

References ACE_Cached_Mem_Pool_Node< char >::addr(), ACE_Dynamic_Cached_Allocator< ACE_LOCK >::chunk_size_, and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::remove().

00135 {
00136   // Check if size requested fits within pre-determined size.
00137   if (nbytes > chunk_size_)
00138     return 0;
00139 
00140   // addr() call is really not absolutely necessary because of the way
00141   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00142   return this->free_list_.remove ()->addr ();
00143 }

template<class ACE_LOCK>
ACE_INLINE size_t ACE_Dynamic_Cached_Allocator< ACE_LOCK >::pool_depth void   ) 
 

Return the number of chunks available in the cache.

Definition at line 37 of file Malloc_T.inl.

References ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK >::size().

00038 {
00039   return this->free_list_.size ();
00040 }


Member Data Documentation

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

Remember the size of our chunks.

Definition at line 201 of file Malloc_T.h.

Referenced by ACE_Dynamic_Cached_Allocator< ACE_LOCK >::calloc(), ACE_Dynamic_Cached_Allocator< ACE_LOCK >::malloc(), and ACE_Dynamic_Cached_Allocator< ACE_LOCK >::~ACE_Dynamic_Cached_Allocator().

template<class ACE_LOCK>
ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>, ACE_LOCK> ACE_Dynamic_Cached_Allocator< ACE_LOCK >::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 198 of file Malloc_T.h.

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

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

Definition at line 192 of file Malloc_T.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:21:54 2006 for ACE by doxygen 1.3.6