OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Public Attributes | Private Attributes | List of all members
OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK > Class Template Reference

A size-based allocator that caches blocks for quicker access, but if the pool is exhausted it will use the heap. More...

#include <Dynamic_Cached_Allocator_With_Overflow_T.h>

Inheritance diagram for OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >:
Inheritance graph
[legend]
Collaboration diagram for OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >:
Collaboration graph
[legend]

Public Member Functions

 Dynamic_Cached_Allocator_With_Overflow (size_t n_chunks, size_t chunk_size)
 
 ~Dynamic_Cached_Allocator_With_Overflow ()
 Clear things up. More...
 
void * malloc (size_t nbytes=0)
 
virtual void * calloc (size_t, char='\0')
 
virtual void * calloc (size_t, size_t, char='\0')
 
void free (void *ptr)
 Return a chunk of memory back to free list cache. More...
 
size_t pool_depth ()
 Return the number of chunks available in the cache. More...
 
size_t available ()
 
- Public Member Functions inherited from ACE_New_Allocator
virtual int remove (void)
 
virtual int bind (const char *name, void *pointer, int duplicates=0)
 
virtual int trybind (const char *name, void *&pointer)
 
virtual int find (const char *name, void *&pointer)
 
virtual int find (const char *name)
 
virtual int unbind (const char *name)
 
virtual int unbind (const char *name, void *&pointer)
 
virtual int sync (ssize_t len=-1, int flags=MS_SYNC)
 
virtual int sync (void *addr, size_t len, int flags=MS_SYNC)
 
virtual int protect (ssize_t len=-1, int prot=PROT_RDWR)
 
virtual int protect (void *addr, size_t len, int prot=PROT_RDWR)
 
virtual void dump (void) const
 
- Public Member Functions inherited from ACE_Allocator
 ACE_Allocator (void)
 
virtual ~ACE_Allocator (void)
 

Public Attributes

Atomic< unsigned long > allocs_from_heap_
 number of allocations from the heap. More...
 
Atomic< unsigned long > allocs_from_pool_
 number of allocations from the pool. More...
 
Atomic< unsigned long > frees_to_heap_
 number of frees returned to the heap More...
 
Atomic< unsigned long > frees_to_pool_
 number of frees returned to the pool More...
 

Private Attributes

unsigned char * begin_
 
unsigned char * end_
 The end of the pool. More...
 
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK > free_list_
 
size_t chunk_size_
 Remember the size of our chunks. More...
 

Additional Inherited Members

- Public Types inherited from ACE_Allocator
typedef size_t size_type
 
- Static Public Member Functions inherited from ACE_Allocator
static ACE_Allocatorinstance (void)
 
static ACE_Allocatorinstance (ACE_Allocator *)
 
static void close_singleton (void)
 

Detailed Description

template<class ACE_LOCK>
class OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >

A size-based allocator that caches blocks for quicker access, but if the pool is exhausted it will use the heap.

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 45 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

Constructor & Destructor Documentation

◆ Dynamic_Cached_Allocator_With_Overflow()

template<class ACE_LOCK>
OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::Dynamic_Cached_Allocator_With_Overflow ( size_t  n_chunks,
size_t  chunk_size 
)
inline

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

Definition at line 49 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

50  : allocs_from_heap_(0),
52  frees_to_heap_(0),
53  frees_to_pool_(0),
54  free_list_(ACE_PURE_FREE_LIST)
55  {
56  chunk_size_ = ACE_MALLOC_ROUNDUP(chunk_size, ACE_MALLOC_ALIGN);
57  begin_ = static_cast<unsigned char*> (ACE_Allocator::instance()->malloc(n_chunks * chunk_size_));
58  // Remember end of the pool.
59  end_ = begin_ + n_chunks * chunk_size_;
60 
61  // Put into free list using placement contructor, no real memory
62  // allocation in the <new> below.
63  for (size_t c = 0;
64  c < n_chunks;
65  c++) {
66  void* placement = begin_ + c * chunk_size_;
67 
69  }
70  }
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK > free_list_
#define ACE_MALLOC_ROUNDUP(X, Y)
Atomic< unsigned long > allocs_from_pool_
number of allocations from the pool.
static ACE_Allocator * instance(void)
Atomic< unsigned long > allocs_from_heap_
number of allocations from the heap.
virtual void add(ACE_Cached_Mem_Pool_Node< char > *element)
Atomic< unsigned long > frees_to_pool_
number of frees returned to the pool
virtual void * malloc(size_type nbytes)=0
Atomic< unsigned long > frees_to_heap_
number of frees returned to the heap

◆ ~Dynamic_Cached_Allocator_With_Overflow()

Clear things up.

Definition at line 73 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

73  {
75  begin_ = 0;
76  chunk_size_ = 0;
77  }
virtual void free(void *ptr)=0
static ACE_Allocator * instance(void)

Member Function Documentation

◆ available()

template<class ACE_LOCK>
size_t OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::available ( void  )
inline

◆ calloc() [1/2]

template<class ACE_LOCK>
virtual void* OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::calloc ( size_t  ,
char  = '\0' 
)
inlinevirtual

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 137 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

138  {
140  }
#define ACE_NOTSUP_RETURN(FAILVALUE)

◆ calloc() [2/2]

template<class ACE_LOCK>
virtual void* OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::calloc ( size_t  ,
size_t  ,
char  = '\0' 
)
inlinevirtual

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 144 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

146  {
148  }
#define ACE_NOTSUP_RETURN(FAILVALUE)

◆ free()

template<class ACE_LOCK>
void OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::free ( void *  ptr)
inlinevirtual

Return a chunk of memory back to free list cache.

Reimplemented from ACE_New_Allocator.

Definition at line 151 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

151  {
152  unsigned char* tmp = static_cast<unsigned char*> (ptr);
153  if (tmp < begin_ ||
154  tmp >= end_) {
156  frees_to_heap_ ++;
157 
159  ACE_ERROR((LM_ERROR,
160  "(%P|%t) ERROR: Dynamic_Cached_Allocator_With_Overflow::free %x"
161  " more deletes %d than allocs %d to the heap\n",
162  this,
165  }
166 
167  if (DCPS_debug_level >= 6) {
168  if (frees_to_heap_ % 500 == 0) {
169  ACE_DEBUG((LM_DEBUG,
170  "(%P|%t) Dynamic_Cached_Allocator_With_Overflow::free %@"
171  " %Lu heap allocs with %Lu outstanding\n",
172  this, allocs_from_heap_.load(),
174  }
175  }
176 
177  return;
178 
179  } else if (ptr != 0) {
180  frees_to_pool_ ++;
181 
183  ACE_ERROR((LM_ERROR,
184  "(%P|%t) ERROR: Dynamic_Cached_Allocator_With_Overflow::free %x"
185  " more deletes %d than allocs %d from the pool\n",
186  this,
189  }
190 
192 
193  if (DCPS_debug_level >= 6)
194  if (available() % 500 == 0)
195  ACE_DEBUG((LM_DEBUG,
196  "(%P|%t) Dynamic_Cached_Allocator_With_Overflow::malloc %x"
197  " %d pool allocs %d pool frees with %d available\n",
199  available()));
200  }
201  }
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK > free_list_
#define ACE_DEBUG(X)
#define ACE_ERROR(X)
virtual void free(void *ptr)=0
Atomic< unsigned long > allocs_from_pool_
number of allocations from the pool.
static ACE_Allocator * instance(void)
Atomic< unsigned long > allocs_from_heap_
number of allocations from the heap.
T load() const
Definition: Atomic.h:33
virtual void add(ACE_Cached_Mem_Pool_Node< char > *element)
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30
Atomic< unsigned long > frees_to_pool_
number of frees returned to the pool
Atomic< unsigned long > frees_to_heap_
number of frees returned to the heap

◆ malloc()

template<class ACE_LOCK>
void* OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::malloc ( size_t  nbytes = 0)
inlinevirtual

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 85 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

85  {
86  // Check if size requested fits within pre-determined size.
87  if (nbytes > chunk_size_)
88  return 0;
89 
90  // addr() call is really not absolutely necessary because of the way
91  // ACE_Cached_Mem_Pool_Node's internal structure arranged.
92  void* rtn = free_list_.remove()->addr();
93 
94  if (0 == rtn) {
97 
98  if (DCPS_debug_level >= 2) {
99  if (allocs_from_heap_ == 1 && DCPS_debug_level >= 2)
100  ACE_DEBUG((LM_DEBUG,
101  "(%P|%t) Dynamic_Cached_Allocator_With_Overflow::malloc %x"
102  " %d heap allocs with %d outstanding\n",
103  this, allocs_from_heap_.load(),
105 
106  if (DCPS_debug_level >= 6)
107  if (allocs_from_heap_ % 500 == 0)
108  ACE_DEBUG((LM_DEBUG,
109  "(%P|%t) Dynamic_Cached_Allocator_With_Overflow::malloc %@"
110  " %Lu heap allocs with %Lu outstanding\n",
111  this, allocs_from_heap_.load(),
113  }
114 
115  } else {
117 
118  if (DCPS_debug_level >= 6)
119  if (allocs_from_pool_ % 500 == 0)
120  ACE_DEBUG((LM_DEBUG,
121  "(%P|%t) Dynamic_Cached_Allocator_With_Overflow::malloc %x"
122  " %d pool allocs %d pool free with %d available\n",
123  this, allocs_from_pool_.load(),
125  available()));
126  }
127 
128  return rtn;
129  }
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK > free_list_
#define ACE_DEBUG(X)
virtual ACE_Cached_Mem_Pool_Node< char > * remove(void)
Atomic< unsigned long > allocs_from_pool_
number of allocations from the pool.
static ACE_Allocator * instance(void)
Atomic< unsigned long > allocs_from_heap_
number of allocations from the heap.
T load() const
Definition: Atomic.h:33
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30
Atomic< unsigned long > frees_to_pool_
number of frees returned to the pool
virtual void * malloc(size_type nbytes)=0
Atomic< unsigned long > frees_to_heap_
number of frees returned to the heap

◆ pool_depth()

template<class ACE_LOCK>
size_t OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::pool_depth ( )
inline

Return the number of chunks available in the cache.

Definition at line 204 of file Dynamic_Cached_Allocator_With_Overflow_T.h.

204  {
205  return free_list_.size() ;
206  }
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< char >, ACE_LOCK > free_list_

Member Data Documentation

◆ allocs_from_heap_

template<class ACE_LOCK>
Atomic<unsigned long> OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::allocs_from_heap_

◆ allocs_from_pool_

template<class ACE_LOCK>
Atomic<unsigned long> OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::allocs_from_pool_

◆ begin_

template<class ACE_LOCK>
unsigned char* OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::begin_
private

◆ chunk_size_

template<class ACE_LOCK>
size_t OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::chunk_size_
private

◆ end_

template<class ACE_LOCK>
unsigned char* OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::end_
private

◆ free_list_

template<class ACE_LOCK>
ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>, ACE_LOCK> OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::free_list_
private

◆ frees_to_heap_

template<class ACE_LOCK>
Atomic<unsigned long> OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::frees_to_heap_

◆ frees_to_pool_

template<class ACE_LOCK>
Atomic<unsigned long> OpenDDS::DCPS::Dynamic_Cached_Allocator_With_Overflow< ACE_LOCK >::frees_to_pool_

The documentation for this class was generated from the following file: