00001
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
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
00021
00022
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
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
00055
00056
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
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
00076
00077
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
00085 if (this->mode_ != ACE_PURE_FREE_LIST && this->size_ <= this->lwm_)
00086 this->alloc (this->inc_);
00087
00088
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
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
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
00117 if (this->mode_ != ACE_PURE_FREE_LIST)
00118
00119 if (newsize < this->size_)
00120 this->dealloc (this->size_ - newsize);
00121 else
00122 this->alloc (newsize - this->size_);
00123 }
00124
00125
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
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