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)
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
,
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 ();
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
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
;
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
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_
);
86 T
*temp
= this->free_list_
;
90 this->free_list_
= this->free_list_
->get_next ();
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 ()
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
);
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
)
137 temp
->set_next (this->free_list_
);
138 this->free_list_
= temp
;
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;
151 T
*temp
= this->free_list_
;
152 this->free_list_
= this->free_list_
->get_next ();
158 ACE_ALLOC_HOOK_DEFINE_Tcc(ACE_Locked_Free_List
)
160 ACE_END_VERSIONED_NAMESPACE_DECL
162 #endif /* ACE_FREE_LIST_CPP */