ACE_Cached_Allocator< T, ACE_LOCK > Class Template Reference

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

#include <Malloc_T.h>

Inheritance diagram for ACE_Cached_Allocator< T, ACE_LOCK >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Cached_Allocator< T, ACE_LOCK >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Cached_Allocator (size_t n_chunks)
 ~ACE_Cached_Allocator (void)
 Clear things up.

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')
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<
T >, ACE_LOCK > 
free_list_
 Maintain a cached memory free list.


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.

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

See also:
ACE_Dynamic_Cached_Allocator

Definition at line 85 of file Malloc_T.h.


Constructor & Destructor Documentation

template<class T, class ACE_LOCK>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL 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 23 of file Malloc_T.cpp.

References ACE_MALLOC_ALIGN, ACE_MALLOC_ROUNDUP, ACE_NEW, ACE_PURE_FREE_LIST, ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::add(), and ACE_Cached_Allocator< T, ACE_LOCK >::pool_.

00024   : pool_ (0),
00025     free_list_ (ACE_PURE_FREE_LIST)
00026 {
00027   // To maintain alignment requirements, make sure that each element
00028   // inserted into the free list is aligned properly for the platform.
00029   // Since the memory is allocated as a char[], the compiler won't help.
00030   // To make sure enough room is allocated, round up the size so that
00031   // each element starts aligned.
00032   //
00033   // NOTE - this would probably be easier by defining pool_ as a pointer
00034   // to T and allocating an array of them (the compiler would probably
00035   // take care of the alignment for us), but then the ACE_NEW below would
00036   // require a default constructor on T - a requirement that is not in
00037   // previous versions of ACE
00038   size_t chunk_size = sizeof (T);
00039   chunk_size = ACE_MALLOC_ROUNDUP (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 ACE_Cached_Allocator< T, ACE_LOCK >::pool_.

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


Member Function Documentation

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

References ACE_NOTSUP_RETURN.

00092 {
00093   ACE_NOTSUP_RETURN (0);
00094 }

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

References ACE_OS::memset(), and ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::remove().

00075 {
00076   // Check if size requested fits within pre-determined size.
00077   if (nbytes > sizeof (T))
00078     return 0;
00079 
00080   // addr() call is really not absolutely necessary because of the way
00081   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00082   void *ptr = this->free_list_.remove ()->addr ();
00083   if (ptr != 0)
00084     ACE_OS::memset (ptr, initial_value, sizeof (T));
00085   return ptr;
00086 }

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

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

00098 {
00099   if (ptr != 0)
00100     this->free_list_.add ((ACE_Cached_Mem_Pool_Node<T> *) ptr) ;
00101 }

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

References ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK >::remove().

00062 {
00063   // Check if size requested fits within pre-determined size.
00064   if (nbytes > sizeof (T))
00065     return 0;
00066 
00067   // addr() call is really not absolutely necessary because of the way
00068   // ACE_Cached_Mem_Pool_Node's internal structure arranged.
00069   return this->free_list_.remove ()->addr ();
00070 }

template<class T, class ACE_LOCK>
ACE_INLINE size_t ACE_Cached_Allocator< T, ACE_LOCK >::pool_depth void   ) 
 

Return the number of chunks available in the cache.

Definition at line 31 of file Malloc_T.inl.

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

00032 {
00033   return this->free_list_.size ();
00034 }


Member Data Documentation

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

Maintain a cached memory free list.

Definition at line 130 of file Malloc_T.h.

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

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

Definition at line 127 of file Malloc_T.h.

Referenced by ACE_Cached_Allocator< T, ACE_LOCK >::ACE_Cached_Allocator(), and ACE_Cached_Allocator< T, ACE_LOCK >::~ACE_Cached_Allocator().


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