summaryrefslogtreecommitdiff
path: root/ace/Free_List.cpp
blob: b8a25f07282c1323de4a0666e1d6adaa6793f9af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#if !defined (ACE_FREE_LIST_C)
#define ACE_FREE_LIST_C

#include "ace/Free_List.h"

#if !defined (__ACE_INLINE__)
#include "ace/Free_List.i"
#endif /* __ACE_INLINE__ */

// Empty constructor

template <class T>  
ACE_Free_List<T>::~ACE_Free_List (void)
{
  // Nothing
}

// Default constructor that takes in a preallocation number
// (<prealloc>), a low and high water mark (<lwm> and <hwm>) and an
// increment value (<inc>)

template <class T, class LOCK>  
ACE_Locked_Free_List<T, LOCK>::ACE_Locked_Free_List (size_t prealloc, 
                                                     size_t lwm, 
                                                     size_t hwm, 
                                                     size_t inc,
                                                     LOCK *mutex)
  : free_list_ (NULL),
    lwm_ (lwm),
    hwm_ (hwm),
    inc_ (inc),
    size_ (0),
    mutex_ (mutex == 0 ? new LOCK : mutex),
    delete_mutex_ (mutex == 0)
{
  this->alloc (prealloc);
}

// Destructor - removes all the elements from the free_list

template <class T, class LOCK>  
ACE_Locked_Free_List<T, LOCK>::~ACE_Locked_Free_List (void)
{
  while (this->free_list_ != NULL)
    {
      T *temp = this->free_list_;
      this->free_list_ = this->free_list_->get_next ();
      delete temp;
    }

  if (this->delete_mutex_)
    delete this->mutex_;
}

// Allocates <n> extra nodes for the freelist

template <class T, class LOCK> void 
ACE_Locked_Free_List<T, LOCK>::alloc (size_t n)
{
  ACE_MT (ACE_GUARD (LOCK, ace_mon, *this->mutex_));
  for (;n > 0; n--, this->size_++)
    {
      T *temp;
      ACE_NEW (temp, T);
      temp->set_next (this->free_list_);
      this->free_list_ = temp;
    }
}

// Removes and frees <n> nodes from the freelist

template <class T, class LOCK> void 
ACE_Locked_Free_List<T, LOCK>::dealloc (size_t n)
{
  ACE_MT (ACE_GUARD (LOCK, ace_mon, *this->mutex_));
  for (;this->free_list_ != NULL && n > 0; this->size_--, n--)
    {
      T *temp = this->free_list_;
      this->free_list_ = this->free_list_->get_next ();
      delete temp;
    }
}

// returns a reference to the mutex
template <class T, class LOCK> LOCK &
ACE_Locked_Free_List<T, LOCK>::get_mutex (void)
{
  return *this->mutex_;
}

template <class T, class LOCK> void 
ACE_Locked_Free_List<T, LOCK>::set_mutex (LOCK &mutex)
{
  if (this->delete_mutex_)
    delete this->mutex_;

  this->mutex_ = &mutex;
  this->delete_mutex_ = 0;
}

#endif /* ACE_FREE_LIST_C */