Document return values
[ACE_TAO.git] / ACE / ace / Free_List.cpp
blobb576432c56fb488ce72de0052991e91b756982a4
1 #ifndef ACE_FREE_LIST_CPP
2 #define ACE_FREE_LIST_CPP
4 #include "ace/Free_List.h"
5 #include "ace/Guard_T.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (ACE_LACKS_PRAGMA_ONCE)
12 # pragma once
13 #endif /* ACE_LACKS_PRAGMA_ONCE */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 // Default constructor that takes in a preallocation number
18 // (<prealloc>), a low and high water mark (<lwm> and <hwm>) and an
19 // increment value (<inc>)
21 template <class T, class ACE_LOCK>
22 ACE_Locked_Free_List<T, ACE_LOCK>::ACE_Locked_Free_List (int mode,
23 size_t prealloc,
24 size_t lwm,
25 size_t hwm,
26 size_t inc)
27 : mode_ (mode),
28 free_list_ (0),
29 lwm_ (lwm),
30 hwm_ (hwm),
31 inc_ (inc),
32 size_ (0)
34 this->alloc (prealloc);
37 // Destructor - removes all the elements from the free_list
39 template <class T, class ACE_LOCK>
40 ACE_Locked_Free_List<T, ACE_LOCK>::~ACE_Locked_Free_List ()
42 if (this->mode_ != ACE_PURE_FREE_LIST)
43 while (this->free_list_ != 0)
45 T *temp = this->free_list_;
46 this->free_list_ = this->free_list_->get_next ();
47 delete temp;
51 // Inserts an element onto the free list (if we are allowed to manage
52 // elements withing and it pasts the high water mark, delete the
53 // element)
55 template <class T, class ACE_LOCK> void
56 ACE_Locked_Free_List<T, ACE_LOCK>::add (T *element)
58 ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));
60 // Check to see that we not at the high water mark.
61 if (this->mode_ == ACE_PURE_FREE_LIST
62 || this->size_ < this->hwm_)
64 element->set_next (this->free_list_);
65 this->free_list_ = element;
66 this->size_++;
68 else
69 delete element;
72 // Takes a element off the freelist and returns it. It creates <inc>
73 // new elements if we are allowed to do it and the size is at the low
74 // water mark.
76 template <class T, class ACE_LOCK> T *
77 ACE_Locked_Free_List<T, ACE_LOCK>::remove ()
79 ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0));
81 // If we are at the low water mark, add some nodes
82 if (this->mode_ != ACE_PURE_FREE_LIST && this->size_ <= this->lwm_)
83 this->alloc (this->inc_);
85 // Remove a node
86 T *temp = this->free_list_;
88 if (temp != 0)
90 this->free_list_ = this->free_list_->get_next ();
91 this->size_--;
94 return temp;
98 // Returns the current size of the free list
100 template <class T, class ACE_LOCK> size_t
101 ACE_Locked_Free_List<T, ACE_LOCK>::size ()
103 return this->size_;
106 // Resizes the free list to <newsize>
108 template <class T, class ACE_LOCK> void
109 ACE_Locked_Free_List<T, ACE_LOCK>::resize (size_t newsize)
111 ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));
113 // Check if we are allowed to resize
114 if (this->mode_ != ACE_PURE_FREE_LIST)
116 // Check to see if we grow or shrink
117 if (newsize < this->size_)
119 this->dealloc (this->size_ - newsize);
121 else
123 this->alloc (newsize - this->size_);
128 // Allocates <n> extra nodes for the freelist
130 template <class T, class ACE_LOCK> void
131 ACE_Locked_Free_List<T, ACE_LOCK>::alloc (size_t n)
133 for (; n > 0; n--)
135 T *temp = 0;
136 ACE_NEW (temp, T);
137 temp->set_next (this->free_list_);
138 this->free_list_ = temp;
139 this->size_++;
143 // Removes and frees <n> nodes from the freelist.
145 template <class T, class ACE_LOCK> void
146 ACE_Locked_Free_List<T, ACE_LOCK>::dealloc (size_t n)
148 for (; this->free_list_ != 0 && n > 0;
149 n--)
151 T *temp = this->free_list_;
152 this->free_list_ = this->free_list_->get_next ();
153 delete temp;
154 this->size_--;
158 ACE_ALLOC_HOOK_DEFINE_Tcc(ACE_Locked_Free_List)
160 ACE_END_VERSIONED_NAMESPACE_DECL
162 #endif /* ACE_FREE_LIST_CPP */