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

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

#include <Cached_Allocator_With_Overflow_T.h>

Inheritance diagram for OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >:
Inheritance graph
[legend]
Collaboration diagram for OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >:
Collaboration graph
[legend]

Public Member Functions

 Cached_Allocator_With_Overflow (size_t n_chunks)
 
 ~Cached_Allocator_With_Overflow ()
 Clear things up. More...
 
void * malloc (size_t nbytes=sizeof(T))
 
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 available ()
 
size_t n_chunks () const
 
- 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)
 

Private Attributes

unsigned char * begin_
 
unsigned char * end_
 The end of the pool. More...
 
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK > free_list_
 Maintain a cached memory free list. More...
 
const size_t n_chunks_
 

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 T, class ACE_LOCK>
class OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >

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

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. If the free list is empty then memory is allocated from the heap. This way the allocations will not fail but may be slower.

Definition at line 46 of file Cached_Allocator_With_Overflow_T.h.

Constructor & Destructor Documentation

◆ Cached_Allocator_With_Overflow()

template<class T, class ACE_LOCK>
OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::Cached_Allocator_With_Overflow ( size_t  n_chunks)
inlineexplicit

Create a cached memory pool with n_chunks chunks each with sizeof (TYPE) size.

Definition at line 50 of file Cached_Allocator_With_Overflow_T.h.

51  : free_list_(ACE_PURE_FREE_LIST)
53  {
54  // To maintain alignment requirements, make sure that each element
55  // inserted into the free list is aligned properly for the platform.
56  // Since the memory is allocated as a char[], the compiler won't help.
57  // To make sure enough room is allocated, round up the size so that
58  // each element starts aligned.
59  //
60  // NOTE - this would probably be easier by defining begin_ as a pointer
61  // to T and allocating an array of them (the compiler would probably
62  // take care of the alignment for us), but then the ACE_NEW below would
63  // require a default constructor on T - a requirement that is not in
64  // previous versions of ACE
65  size_t chunk_size = sizeof(T);
66  chunk_size = ACE_MALLOC_ROUNDUP(chunk_size, ACE_MALLOC_ALIGN);
67  begin_ = static_cast<unsigned char*> (ACE_Allocator::instance()->malloc(n_chunks * chunk_size));
68 
69  // Remember end of the pool.
70  end_ = begin_ + n_chunks * chunk_size;
71 
72  // Put into free list using placement contructor, no real memory
73  // allocation in the <new> below.
74  for (size_t c = 0; c < n_chunks; c++) {
75  void* placement = begin_ + c * chunk_size;
76  this->free_list_.add(new(placement) ACE_Cached_Mem_Pool_Node<T>);
77  }
78  }
#define ACE_MALLOC_ROUNDUP(X, Y)
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK > free_list_
Maintain a cached memory free list.
static ACE_Allocator * instance(void)
virtual void add(ACE_Cached_Mem_Pool_Node< T > *element)
virtual void * malloc(size_type nbytes)=0

◆ ~Cached_Allocator_With_Overflow()

template<class T, class ACE_LOCK>
OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::~Cached_Allocator_With_Overflow ( )
inline

Clear things up.

Definition at line 81 of file Cached_Allocator_With_Overflow_T.h.

82  {
84  }
virtual void free(void *ptr)=0
static ACE_Allocator * instance(void)

Member Function Documentation

◆ available()

template<class T, class ACE_LOCK>
size_t OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::available ( void  )
inline

How many chunks are available at this time.

Definition at line 153 of file Cached_Allocator_With_Overflow_T.h.

Referenced by OpenDDS::DCPS::Cached_Allocator_With_Overflow< ACE_Message_Block, RECEIVE_SYNCH >::free(), and OpenDDS::DCPS::Cached_Allocator_With_Overflow< ACE_Message_Block, RECEIVE_SYNCH >::malloc().

153 { return free_list_.size(); }
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK > free_list_
Maintain a cached memory free list.

◆ calloc() [1/2]

template<class T, class ACE_LOCK>
virtual void* OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, 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 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 117 of file Cached_Allocator_With_Overflow_T.h.

119  {
121  }
#define ACE_NOTSUP_RETURN(FAILVALUE)

◆ calloc() [2/2]

template<class T, class ACE_LOCK>
virtual void* OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, 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 125 of file Cached_Allocator_With_Overflow_T.h.

128  {
130  }
#define ACE_NOTSUP_RETURN(FAILVALUE)

◆ free()

template<class T, class ACE_LOCK>
void OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::free ( void *  ptr)
inlinevirtual

Return a chunk of memory back to free list cache.

Reimplemented from ACE_New_Allocator.

Definition at line 133 of file Cached_Allocator_With_Overflow_T.h.

Referenced by OpenDDS::DCPS::RtpsUdpReceiveStrategy::handle_input(), and OpenDDS::DCPS::WriteDataContainer::release_buffer().

134  {
135  unsigned char* tmp = static_cast<unsigned char*>(ptr);
136 
137  if (tmp < begin_ || tmp >= end_) {
139  } else if (ptr != 0) {
141 
142  if (DCPS_debug_level >= 6 && this->available() % 512 == 0) {
143  ACE_DEBUG((LM_DEBUG, "(%P|%t) Cached_Allocator_With_Overflow::free %@"
144  " %Lu available from pool\n", this, this->available()));
145  }
146  }
147  }
#define ACE_DEBUG(X)
virtual void free(void *ptr)=0
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK > free_list_
Maintain a cached memory free list.
static ACE_Allocator * instance(void)
virtual void add(ACE_Cached_Mem_Pool_Node< T > *element)
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30

◆ malloc()

template<class T, class ACE_LOCK>
void* OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::malloc ( size_t  nbytes = sizeof(T))
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 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 91 of file Cached_Allocator_With_Overflow_T.h.

Referenced by OpenDDS::DCPS::RtpsUdpDataLink::alloc_msgblock(), OpenDDS::DCPS::TransportQueueElement::clone_mb(), OpenDDS::DCPS::WriteDataContainer::copy_and_prepend(), OpenDDS::DCPS::RtpsUdpReceiveStrategy::handle_input(), OpenDDS::DCPS::WriteDataContainer::obtain_buffer(), OpenDDS::DCPS::WriteDataContainer::obtain_buffer_for_control(), and OpenDDS::DCPS::RtpsUdpReceiveStrategy::RtpsUdpReceiveStrategy().

92  {
93  // Check if size requested fits within pre-determined size.
94  if (nbytes > sizeof(T))
95  return 0;
96 
97  // addr() call is really not absolutely necessary because of the way
98  // ACE_Cached_Mem_Pool_Node's internal structure arranged.
99  void* rtn = this->free_list_.remove()->addr();
100  if (0 == rtn) {
101  return ACE_Allocator::instance()->malloc(sizeof(T));
102  }
103 
104  if (DCPS_debug_level >= 6 && this->available() % 512 == 0) {
105  ACE_DEBUG((LM_DEBUG, "(%P|%t) Cached_Allocator_With_Overflow::malloc %@"
106  " %Lu available from pool\n", this, this->available()));
107  }
108  return rtn;
109  }
#define ACE_DEBUG(X)
virtual ACE_Cached_Mem_Pool_Node< T > * remove(void)
ACE_Locked_Free_List< ACE_Cached_Mem_Pool_Node< T >, ACE_LOCK > free_list_
Maintain a cached memory free list.
static ACE_Allocator * instance(void)
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30
virtual void * malloc(size_type nbytes)=0

◆ n_chunks()

template<class T, class ACE_LOCK>
size_t OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::n_chunks ( ) const
inline

Member Data Documentation

◆ begin_

template<class T, class ACE_LOCK>
unsigned char* OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::begin_
private

◆ end_

template<class T, class ACE_LOCK>
unsigned char* OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::end_
private

◆ free_list_

template<class T, class ACE_LOCK>
ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::free_list_
private

◆ n_chunks_

template<class T, class ACE_LOCK>
const size_t OpenDDS::DCPS::Cached_Allocator_With_Overflow< T, ACE_LOCK >::n_chunks_
private

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