ACE_Locked_Free_List< T, ACE_LOCK > Class Template Reference

Implements a free list. More...

#include <Free_List.h>

Inheritance diagram for ACE_Locked_Free_List< T, ACE_LOCK >:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Locked_Free_List (int mode=ACE_FREE_LIST_WITH_POOL, size_t prealloc=ACE_DEFAULT_FREE_LIST_PREALLOC, size_t lwm=ACE_DEFAULT_FREE_LIST_LWM, size_t hwm=ACE_DEFAULT_FREE_LIST_HWM, size_t inc=ACE_DEFAULT_FREE_LIST_INC)
virtual ~ACE_Locked_Free_List (void)
 Destructor - removes all the elements from the free_list.
virtual void add (T *element)
virtual T * remove (void)
virtual size_t size (void)
 Returns the current size of the free list.
virtual void resize (size_t newsize)
 Resizes the free list to newsize.

Protected Member Functions

virtual void alloc (size_t n)
 Allocates n extra nodes for the freelist.
virtual void dealloc (size_t n)
 Removes and frees n nodes from the freelist.

Protected Attributes

int mode_
T * free_list_
 Pointer to the first node in the freelist.
size_t lwm_
 Low water mark.
size_t hwm_
 High water mark.
size_t inc_
 Increment value.
size_t size_
 Keeps track of the size of the list.
ACE_LOCK mutex_
 Synchronization variable for ACE_Timer_Queue.

Private Member Functions

 ACE_Locked_Free_List (const ACE_Locked_Free_List< T, ACE_LOCK > &)
void operator= (const ACE_Locked_Free_List< T, ACE_LOCK > &)

Detailed Description

template<class T, class ACE_LOCK>
class ACE_Locked_Free_List< T, ACE_LOCK >

Implements a free list.

This class maintains a free list of nodes of type T. It depends on the type T having a <get_next> and <set_next> method. It maintains a mutex so the freelist can be used in a multithreaded program .

Definition at line 69 of file Free_List.h.


Constructor & Destructor Documentation

template<class T, class ACE_LOCK>
ACE_Locked_Free_List< T, ACE_LOCK >::ACE_Locked_Free_List ( int  mode = ACE_FREE_LIST_WITH_POOL,
size_t  prealloc = ACE_DEFAULT_FREE_LIST_PREALLOC,
size_t  lwm = ACE_DEFAULT_FREE_LIST_LWM,
size_t  hwm = ACE_DEFAULT_FREE_LIST_HWM,
size_t  inc = ACE_DEFAULT_FREE_LIST_INC 
)

Constructor takes a mode (i.e., ACE_FREE_LIST_WITH_POOL or ACE_PURE_FREE_LIST), a count of the number of nodes to <prealloc>, a low and high water mark (<lwm> and <hwm>) that indicate when to allocate more nodes, an increment value (<inc>) that indicates how many nodes to allocate when the list must grow.

Definition at line 25 of file Free_List.cpp.

References ACE_Locked_Free_List< T, ACE_LOCK >::alloc().

00030   : mode_ (mode),
00031     free_list_ (0),
00032     lwm_ (lwm),
00033     hwm_ (hwm),
00034     inc_ (inc),
00035     size_ (0)
00036 {
00037   this->alloc (prealloc);
00038 }

template<class T, class ACE_LOCK>
ACE_Locked_Free_List< T, ACE_LOCK >::~ACE_Locked_Free_List ( void   )  [virtual]

Destructor - removes all the elements from the free_list.

Definition at line 43 of file Free_List.cpp.

References ACE_PURE_FREE_LIST, and ACE_Locked_Free_List< T, ACE_LOCK >::free_list_.

00044 {
00045   if (this->mode_ != ACE_PURE_FREE_LIST)
00046     while (this->free_list_ != 0)
00047       {
00048         T *temp = this->free_list_;
00049         this->free_list_ = this->free_list_->get_next ();
00050         delete temp;
00051       }
00052 }

template<class T, class ACE_LOCK>
ACE_Locked_Free_List< T, ACE_LOCK >::ACE_Locked_Free_List ( const ACE_Locked_Free_List< T, ACE_LOCK > &   )  [private]


Member Function Documentation

template<class T, class ACE_LOCK>
void ACE_Locked_Free_List< T, ACE_LOCK >::add ( T *  element  )  [virtual]

Inserts an element onto the free list (if it isn't past the high water mark).

Implements ACE_Free_List< T >.

Definition at line 59 of file Free_List.cpp.

References ACE_GUARD, ACE_PURE_FREE_LIST, ACE_Locked_Free_List< T, ACE_LOCK >::free_list_, and ACE_Locked_Free_List< T, ACE_LOCK >::size_.

Referenced by ACE_Timer_Hash_T< TYPE, FUNCTOR, ACE_LOCK, BUCKET >::cancel(), ACE_Dynamic_Cached_Allocator< ACE_LOCK >::free(), ACE_Cached_Allocator< T, ACE_LOCK >::free(), ACE_Timer_Hash_T< TYPE, FUNCTOR, ACE_LOCK, BUCKET >::free_node(), and ACE_Thread_Manager::remove_thr().

00060 {
00061   ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));
00062 
00063   // Check to see that we not at the high water mark.
00064   if (this->mode_ == ACE_PURE_FREE_LIST
00065       || this->size_ < this->hwm_)
00066     {
00067       element->set_next (this->free_list_);
00068       this->free_list_ = element;
00069       this->size_++;
00070     }
00071   else
00072     delete element;
00073 }

template<class T, class ACE_LOCK>
void ACE_Locked_Free_List< T, ACE_LOCK >::alloc ( size_t  n  )  [protected, virtual]

Allocates n extra nodes for the freelist.

Definition at line 134 of file Free_List.cpp.

References ACE_NEW, ACE_Locked_Free_List< T, ACE_LOCK >::free_list_, and ACE_Locked_Free_List< T, ACE_LOCK >::size_.

Referenced by ACE_Locked_Free_List< T, ACE_LOCK >::ACE_Locked_Free_List(), ACE_Locked_Free_List< T, ACE_LOCK >::remove(), and ACE_Locked_Free_List< T, ACE_LOCK >::resize().

00135 {
00136   for (; n > 0; n--)
00137     {
00138       T *temp = 0;
00139       ACE_NEW (temp, T);
00140       temp->set_next (this->free_list_);
00141       this->free_list_ = temp;
00142       this->size_++;
00143     }
00144 }

template<class T, class ACE_LOCK>
void ACE_Locked_Free_List< T, ACE_LOCK >::dealloc ( size_t  n  )  [protected, virtual]

Removes and frees n nodes from the freelist.

Definition at line 149 of file Free_List.cpp.

References ACE_Locked_Free_List< T, ACE_LOCK >::free_list_, and ACE_Locked_Free_List< T, ACE_LOCK >::size_.

Referenced by ACE_Locked_Free_List< T, ACE_LOCK >::resize().

00150 {
00151   for (; this->free_list_ != 0 && n > 0;
00152        n--)
00153     {
00154       T *temp = this->free_list_;
00155       this->free_list_ = this->free_list_->get_next ();
00156       delete temp;
00157       this->size_--;
00158     }
00159 }

template<class T, class ACE_LOCK>
void ACE_Locked_Free_List< T, ACE_LOCK >::operator= ( const ACE_Locked_Free_List< T, ACE_LOCK > &   )  [private]

template<class T, class ACE_LOCK>
T * ACE_Locked_Free_List< T, ACE_LOCK >::remove ( void   )  [virtual]

Takes a element off the freelist and returns it. It creates <inc> new elements if the size is at or below the low water mark.

Implements ACE_Free_List< T >.

Definition at line 80 of file Free_List.cpp.

References ACE_GUARD_RETURN, ACE_PURE_FREE_LIST, ACE_Locked_Free_List< T, ACE_LOCK >::alloc(), ACE_Locked_Free_List< T, ACE_LOCK >::free_list_, and ACE_Locked_Free_List< T, ACE_LOCK >::size_.

Referenced by ACE_Dynamic_Cached_Allocator< ACE_LOCK >::calloc(), ACE_Cached_Allocator< T, ACE_LOCK >::calloc(), ACE_Dynamic_Cached_Allocator< ACE_LOCK >::malloc(), ACE_Cached_Allocator< T, ACE_LOCK >::malloc(), and ACE_Timer_Hash_T< TYPE, FUNCTOR, ACE_LOCK, BUCKET >::schedule_i().

00081 {
00082   ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0));
00083 
00084   // If we are at the low water mark, add some nodes
00085   if (this->mode_ != ACE_PURE_FREE_LIST && this->size_ <= this->lwm_)
00086     this->alloc (this->inc_);
00087 
00088   // Remove a node
00089   T *temp = this->free_list_;
00090 
00091   if (temp != 0)
00092     {
00093       this->free_list_ = this->free_list_->get_next ();
00094       this->size_--;
00095     }
00096 
00097   return temp;
00098 }

template<class T, class ACE_LOCK>
void ACE_Locked_Free_List< T, ACE_LOCK >::resize ( size_t  newsize  )  [virtual]

Resizes the free list to newsize.

Implements ACE_Free_List< T >.

Definition at line 112 of file Free_List.cpp.

References ACE_GUARD, ACE_PURE_FREE_LIST, ACE_Locked_Free_List< T, ACE_LOCK >::alloc(), ACE_Locked_Free_List< T, ACE_LOCK >::dealloc(), and ACE_Locked_Free_List< T, ACE_LOCK >::size_.

00113 {
00114   ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));
00115 
00116   // Check if we are allowed to resize
00117   if (this->mode_ != ACE_PURE_FREE_LIST)
00118     {
00119       // Check to see if we grow or shrink
00120       if (newsize < this->size_)
00121         {
00122           this->dealloc (this->size_ - newsize);
00123         }
00124       else
00125         {
00126           this->alloc (newsize - this->size_);
00127         }
00128     }
00129 }

template<class T, class ACE_LOCK>
size_t ACE_Locked_Free_List< T, ACE_LOCK >::size ( void   )  [virtual]

Returns the current size of the free list.

Implements ACE_Free_List< T >.

Definition at line 104 of file Free_List.cpp.

References ACE_Locked_Free_List< T, ACE_LOCK >::size_.

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

00105 {
00106   return this->size_;
00107 }


Member Data Documentation

template<class T, class ACE_LOCK>
T* ACE_Locked_Free_List< T, ACE_LOCK >::free_list_ [protected]

Pointer to the first node in the freelist.

Definition at line 116 of file Free_List.h.

Referenced by ACE_Locked_Free_List< T, ACE_LOCK >::add(), ACE_Locked_Free_List< T, ACE_LOCK >::alloc(), ACE_Locked_Free_List< T, ACE_LOCK >::dealloc(), ACE_Locked_Free_List< T, ACE_LOCK >::remove(), and ACE_Locked_Free_List< T, ACE_LOCK >::~ACE_Locked_Free_List().

template<class T, class ACE_LOCK>
size_t ACE_Locked_Free_List< T, ACE_LOCK >::hwm_ [protected]

High water mark.

Definition at line 122 of file Free_List.h.

template<class T, class ACE_LOCK>
size_t ACE_Locked_Free_List< T, ACE_LOCK >::inc_ [protected]

Increment value.

Definition at line 125 of file Free_List.h.

template<class T, class ACE_LOCK>
size_t ACE_Locked_Free_List< T, ACE_LOCK >::lwm_ [protected]

Low water mark.

Definition at line 119 of file Free_List.h.

template<class T, class ACE_LOCK>
int ACE_Locked_Free_List< T, ACE_LOCK >::mode_ [protected]

Free list operation mode, either ACE_FREE_LIST_WITH_POOL or ACE_PURE_FREE_LIST.

Definition at line 113 of file Free_List.h.

template<class T, class ACE_LOCK>
ACE_LOCK ACE_Locked_Free_List< T, ACE_LOCK >::mutex_ [protected]

Synchronization variable for ACE_Timer_Queue.

Definition at line 131 of file Free_List.h.

template<class T, class ACE_LOCK>
size_t ACE_Locked_Free_List< T, ACE_LOCK >::size_ [protected]

Keeps track of the size of the list.

Definition at line 128 of file Free_List.h.

Referenced by ACE_Locked_Free_List< T, ACE_LOCK >::add(), ACE_Locked_Free_List< T, ACE_LOCK >::alloc(), ACE_Locked_Free_List< T, ACE_LOCK >::dealloc(), ACE_Locked_Free_List< T, ACE_LOCK >::remove(), ACE_Locked_Free_List< T, ACE_LOCK >::resize(), and ACE_Locked_Free_List< T, ACE_LOCK >::size().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:15 2010 for ACE by  doxygen 1.4.7