ADTF  3.18.4
freelist.h
Go to the documentation of this file.
1 
7 #ifndef _FREE_LIST_CLASS_HEADER_
8 #define _FREE_LIST_CLASS_HEADER_
9 
10 namespace A_UTILS_NS
11 {
12 
13 #ifdef A_UTILS_TAGGED_POINTER_AVALIABLE
14 
21 template <class T, class DataType>
22 class A_UTILS_CAS_ALIGNMENT free_list
23 {
24  private:
25  typedef tagged_ptr<T> tTaggedNodePtr;
26 
28  tTaggedNodePtr m_pFree;
29 
31  tBool m_bGrow;
32 
33  public:
34 
42  free_list(tUInt nReserve = 0, tBool bGrow = tTrue):
43  m_bGrow(bGrow)
44  {
45  // preallocate some nodes
46  for (tUInt nNode = 0; nNode < nReserve; ++nNode)
47  {
48  T* pTmp = new T;
49  pTmp->m_pNext = m_pFree;
50  m_pFree.Set(pTmp);
51  }
52  }
53 
57  ~free_list()
58  {
59  T* pTmp = m_pFree.GetPtr();
60  while (pTmp)
61  {
62  T* pNext = pTmp->m_pNext.GetPtr();
63  delete pTmp;
64  pTmp = pNext;
65  }
66  }
67 
73  T* GetFreeNode()
74  {
75  for (;;)
76  {
77  tTaggedNodePtr pOldTos;
78  pOldTos.Set(m_pFree);
79 
80  if (!pOldTos)
81  {
82  if (m_bGrow)
83  {
84  return new T;
85  }
86  else
87  {
88  return nullptr;
89  }
90  }
91 
92  T* pNewTos = pOldTos->m_pNext.GetPtr();
93 
94  if (m_pFree.CompareAndSwap(pOldTos, pNewTos))
95  {
96  return pOldTos.GetPtr();
97  }
98  }
99  }
100 
107  tVoid AddFreeNode(T* pNode)
108  {
109  pNode->m_oData = DataType(); // reset value
110 
111  tTaggedNodePtr pOld;
112  do
113  {
114  pOld.Set(m_pFree);
115  pNode->m_pNext.Set(pOld.GetPtr());
116  }
117  while (!m_pFree.CompareAndSwap(pOld, pNode)); // try insertion until we succeed
118  }
119 
120  private:
121  free_list(const free_list& oOther);
122 };
123 
124 #endif
125 
126 }
127 
128 #endif
unsigned int tUInt
type definition for unsigned integer value (platform and compiler dependent type).
void tVoid
The tVoid is always the definition for the void (non-type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
#define tTrue
Value for tBool.
Definition: constants.h:62
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
datamodel::DataType DataType
Reuse of datamodel datatype class - ddl::dd::datamodel::DataType.
Definition: dd.h:46