Free_List.cpp

Go to the documentation of this file.
00001 // Free_List.cpp,v 1.22 2005/10/28 16:14:52 ossama Exp
00002 
00003 #ifndef ACE_FREE_LIST_CPP
00004 #define ACE_FREE_LIST_CPP
00005 
00006 #include "ace/Free_List.h"
00007 #include "ace/Guard_T.h"
00008 
00009 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00010 # pragma once
00011 #endif /* ACE_LACKS_PRAGMA_ONCE */
00012 
00013 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 template <class T>
00016 ACE_Free_List<T>::~ACE_Free_List (void)
00017 {
00018 }
00019 
00020 // Default constructor that takes in a preallocation number
00021 // (<prealloc>), a low and high water mark (<lwm> and <hwm>) and an
00022 // increment value (<inc>)
00023 
00024 template <class T, class ACE_LOCK>
00025 ACE_Locked_Free_List<T, ACE_LOCK>::ACE_Locked_Free_List (int mode,
00026                                                      size_t prealloc,
00027                                                      size_t lwm,
00028                                                      size_t hwm,
00029                                                      size_t inc)
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 }
00039 
00040 // Destructor - removes all the elements from the free_list
00041 
00042 template <class T, class ACE_LOCK>
00043 ACE_Locked_Free_List<T, ACE_LOCK>::~ACE_Locked_Free_List (void)
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 }
00053 
00054 // Inserts an element onto the free list (if we are allowed to manage
00055 // elements withing and it pasts the high water mark, delete the
00056 // element)
00057 
00058 template <class T, class ACE_LOCK> void
00059 ACE_Locked_Free_List<T, ACE_LOCK>::add (T *element)
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 }
00074 
00075 // Takes a element off the freelist and returns it.  It creates <inc>
00076 // new elements if we are allowed to do it and the size is at the low
00077 // water mark.
00078 
00079 template <class T, class ACE_LOCK> T *
00080 ACE_Locked_Free_List<T, ACE_LOCK>::remove (void)
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 }
00099 
00100 
00101 // Returns the current size of the free list
00102 
00103 template <class T, class ACE_LOCK> size_t
00104 ACE_Locked_Free_List<T, ACE_LOCK>::size (void)
00105 {
00106   return this->size_;
00107 }
00108 
00109 // Resizes the free list to <newsize>
00110 
00111 template <class T, class ACE_LOCK> void
00112 ACE_Locked_Free_List<T, ACE_LOCK>::resize (size_t newsize)
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     // Check to see if we grow or shrink
00119     if (newsize < this->size_)
00120       this->dealloc (this->size_ - newsize);
00121     else
00122       this->alloc (newsize - this->size_);
00123 }
00124 
00125 // Allocates <n> extra nodes for the freelist
00126 
00127 template <class T, class ACE_LOCK> void
00128 ACE_Locked_Free_List<T, ACE_LOCK>::alloc (size_t n)
00129 {
00130   for (; n > 0; n--)
00131     {
00132       T *temp = 0;
00133       ACE_NEW (temp, T);
00134       temp->set_next (this->free_list_);
00135       this->free_list_ = temp;
00136       this->size_++;
00137     }
00138 }
00139 
00140 // Removes and frees <n> nodes from the freelist.
00141 
00142 template <class T, class ACE_LOCK> void
00143 ACE_Locked_Free_List<T, ACE_LOCK>::dealloc (size_t n)
00144 {
00145   for (; this->free_list_ != 0 && n > 0;
00146        n--)
00147     {
00148       T *temp = this->free_list_;
00149       this->free_list_ = this->free_list_->get_next ();
00150       delete temp;
00151       this->size_--;
00152     }
00153 }
00154 
00155 ACE_END_VERSIONED_NAMESPACE_DECL
00156 
00157 #endif /* ACE_FREE_LIST_CPP */

Generated on Thu Nov 9 09:41:51 2006 for ACE by doxygen 1.3.6