A fixed-size allocator that caches items for quicker access. More...
#include <Malloc_T.h>
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. |
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.
Definition at line 85 of file Malloc_T.h.
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.
: pool_ (0), free_list_ (ACE_PURE_FREE_LIST) { // To maintain alignment requirements, make sure that each element // inserted into the free list is aligned properly for the platform. // Since the memory is allocated as a char[], the compiler won't help. // To make sure enough room is allocated, round up the size so that // each element starts aligned. // // NOTE - this would probably be easier by defining pool_ as a pointer // to T and allocating an array of them (the compiler would probably // take care of the alignment for us), but then the ACE_NEW below would // require a default constructor on T - a requirement that is not in // previous versions of ACE size_t chunk_size = sizeof (T); chunk_size = ACE_MALLOC_ROUNDUP (chunk_size, ACE_MALLOC_ALIGN); ACE_NEW (this->pool_, char[n_chunks * chunk_size]); for (size_t c = 0; c < n_chunks; c++) { void* placement = this->pool_ + c * chunk_size; this->free_list_.add (new (placement) ACE_Cached_Mem_Pool_Node<T>); } // Put into free list using placement contructor, no real memory // allocation in the above <new>. }
ACE_Cached_Allocator< T, ACE_LOCK >::~ACE_Cached_Allocator | ( | void | ) |
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.
{ // Check if size requested fits within pre-determined size. if (nbytes > sizeof (T)) return 0; // addr() call is really not absolutely necessary because of the way // ACE_Cached_Mem_Pool_Node's internal structure arranged. void *ptr = this->free_list_.remove ()->addr (); if (ptr != 0) ACE_OS::memset (ptr, initial_value, sizeof (T)); return ptr; }
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.
{ ACE_NOTSUP_RETURN (0); }
void ACE_Cached_Allocator< T, ACE_LOCK >::free | ( | void * | ptr | ) | [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.
{ if (ptr != 0) this->free_list_.add ((ACE_Cached_Mem_Pool_Node<T> *) ptr) ; }
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.
{ // Check if size requested fits within pre-determined size. if (nbytes > sizeof (T)) return 0; // addr() call is really not absolutely necessary because of the way // ACE_Cached_Mem_Pool_Node's internal structure arranged. return this->free_list_.remove ()->addr (); }
size_t ACE_Cached_Allocator< T, ACE_LOCK >::pool_depth | ( | void | ) | [inline] |
Return the number of chunks available in the cache.
Definition at line 31 of file Malloc_T.inl.
{ return this->free_list_.size (); }
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.
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.