A size-based allocator that caches blocks for quicker access. More...
#include <Malloc_T.h>
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. |
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.
Definition at line 150 of file Malloc_T.h.
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.
: pool_ (0), free_list_ (ACE_PURE_FREE_LIST), chunk_size_(chunk_size) { 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<char>); } // Put into free list using placement contructor, no real memory // allocation in the above <new>. }
ACE_Dynamic_Cached_Allocator< ACE_LOCK >::~ACE_Dynamic_Cached_Allocator | ( | void | ) |
Clear things up.
Definition at line 126 of file Malloc_T.cpp.
{ delete [] this->pool_; this->pool_ = 0; chunk_size_ = 0; }
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.
{ // Check if size requested fits within pre-determined size. if (nbytes > chunk_size_) 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, chunk_size_); return ptr; }
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.
{ ACE_NOTSUP_RETURN (0); }
void ACE_Dynamic_Cached_Allocator< ACE_LOCK >::free | ( | void * | ptr | ) | [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.
{ if (ptr != 0) this->free_list_.add ((ACE_Cached_Mem_Pool_Node<char> *) ptr); }
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.
{ // Check if size requested fits within pre-determined size. if (nbytes > chunk_size_) 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_Dynamic_Cached_Allocator< ACE_LOCK >::pool_depth | ( | void | ) | [inline] |
Return the number of chunks available in the cache.
Definition at line 37 of file Malloc_T.inl.
{ return this->free_list_.size (); }
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.
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.
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.