Skip to content

Commit 179585e

Browse files
committed
Container code
1 parent 418f8c3 commit 179585e

File tree

2 files changed

+877
-0
lines changed

2 files changed

+877
-0
lines changed
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#ifndef __EmptyContainers_h__
5+
#define __EmptyContainers_h__
6+
7+
// This header file will contains minimal containers that are needed for EventPipe library implementation
8+
// @TODO - this will likely be replaced by the common container classes to be added to the EventPipe library
9+
// Hence initially, the bare boned implementation focus is on unblocking HW/Simple Trace bring up
10+
11+
#include "EmptyContainers2.h"
12+
13+
struct SLink_EP
14+
{
15+
SLink_EP* m_pNext;
16+
17+
SLink_EP()
18+
{
19+
m_pNext = NULL;
20+
}
21+
22+
// find pLink within the list starting at pHead
23+
// if found remove the link from the list and return the link
24+
// otherwise return NULL
25+
static SLink_EP* FindAndRemove(SLink_EP *pHead, SLink_EP* pLink, SLink_EP ** ppPrior)
26+
{
27+
28+
_ASSERTE(pHead != NULL);
29+
_ASSERTE(pLink != NULL);
30+
31+
SLink_EP* pFreeLink = NULL;
32+
*ppPrior = NULL;
33+
34+
while (pHead->m_pNext != NULL)
35+
{
36+
if (pHead->m_pNext == pLink)
37+
{
38+
pFreeLink = pLink;
39+
pHead->m_pNext = pLink->m_pNext;
40+
*ppPrior = pHead;
41+
break;
42+
}
43+
pHead = pHead->m_pNext;
44+
}
45+
46+
return pFreeLink;
47+
}
48+
};
49+
50+
51+
// template <class T, bool fHead = false, typename __PTR = T*, SLink_EP T::*LinkPtr = &T::m_Link>
52+
// Assumes fHead to be false
53+
template <class T>
54+
class SList_EP
55+
{
56+
protected:
57+
// used as sentinel
58+
SLink_EP m_link;
59+
SLink_EP* m_pHead;
60+
SLink_EP* m_pTail;
61+
62+
// get the list node within the object
63+
static SLink_EP* GetLink (T* pLink)
64+
{
65+
return &(pLink->m_Link);
66+
}
67+
68+
static T* GetObject (SLink_EP* pLink)
69+
{
70+
if (pLink == NULL)
71+
return NULL;
72+
return reinterpret_cast<T*>(reinterpret_cast<ptrdiff_t>(pLink) - offsetof(T, m_Link));
73+
}
74+
75+
public:
76+
77+
SList_EP()
78+
{
79+
m_pHead = &m_link;
80+
// NOTE :: fHead variable is template argument
81+
// the following code is a compiled in, only if the fHead flag
82+
// is set to false,
83+
m_pTail = &m_link;
84+
// TODO: Likely not needed now since SLink_EP has a ctor
85+
m_link.m_pNext = NULL;
86+
}
87+
88+
bool IsEmpty()
89+
{
90+
return m_pHead->m_pNext == NULL;
91+
}
92+
93+
void InsertTail(T *pObj)
94+
{
95+
_ASSERTE(pObj != NULL);
96+
SLink_EP *pLink = GetLink(pObj);
97+
98+
m_pTail->m_pNext = pLink;
99+
m_pTail = pLink;
100+
}
101+
102+
T* RemoveHead()
103+
{
104+
SLink_EP* pLink = m_pHead->m_pNext;
105+
if (pLink != NULL)
106+
{
107+
m_pHead->m_pNext = pLink->m_pNext;
108+
}
109+
110+
if(m_pTail == pLink)
111+
{
112+
m_pTail = m_pHead;
113+
}
114+
115+
return GetObject(pLink);
116+
}
117+
118+
T* GetHead()
119+
{
120+
return GetObject(m_pHead->m_pNext);
121+
}
122+
123+
static T *GetNext(T *pObj)
124+
{
125+
_ASSERTE(pObj != NULL);
126+
return GetObject(GetLink(pObj)->m_pNext);
127+
}
128+
129+
130+
T* FindAndRemove(T *pObj)
131+
{
132+
_ASSERTE(pObj != NULL);
133+
134+
SLink_EP *prior;
135+
SLink_EP *ret = SLink_EP::FindAndRemove(m_pHead, GetLink(pObj), &prior);
136+
137+
if (ret == m_pTail)
138+
m_pTail = prior;
139+
140+
return GetObject(ret);
141+
}
142+
143+
void InsertHead(T *pObj)
144+
{
145+
__debugbreak();
146+
}
147+
148+
149+
class Iterator
150+
{
151+
friend class SList_EP;
152+
//T* _t;
153+
154+
public:
155+
Iterator & operator++()
156+
{
157+
_ASSERTE(m_cur != NULL);
158+
m_cur = SList_EP::GetNext(m_cur);
159+
return *this;
160+
}
161+
162+
Iterator operator++(int)
163+
{
164+
Iterator it(m_cur);
165+
++(*this);
166+
return it;
167+
}
168+
169+
bool operator==(Iterator const & other) const
170+
{
171+
return m_cur == other.m_cur ||
172+
(m_cur != NULL && other.m_cur != NULL && *m_cur == *other.m_cur);
173+
}
174+
175+
T & operator*()
176+
{
177+
_ASSERTE(m_cur != NULL);
178+
return *m_cur;
179+
}
180+
181+
T * operator->() const
182+
{
183+
__debugbreak();
184+
return m_cur;
185+
}
186+
187+
private:
188+
// Iterator(SList * pList)
189+
// : m_cur(pList->GetHead())
190+
// { }
191+
192+
Iterator(T* pObj)
193+
: m_cur(pObj)
194+
{ }
195+
196+
Iterator()
197+
: m_cur(NULL)
198+
{ }
199+
200+
T* m_cur;
201+
202+
};
203+
204+
Iterator begin()
205+
{
206+
return Iterator(GetHead());
207+
}
208+
209+
Iterator end()
210+
{
211+
return Iterator();
212+
}
213+
214+
};
215+
216+
template <typename ElemT>
217+
struct SListElem_EP
218+
{
219+
SLink_EP m_Link;
220+
ElemT m_Value;
221+
222+
operator ElemT const &() const
223+
{
224+
return m_Value;
225+
}
226+
227+
operator ElemT &()
228+
{
229+
return m_Value;
230+
}
231+
232+
ElemT const & operator*() const
233+
{
234+
__debugbreak();
235+
return m_Value;
236+
}
237+
238+
ElemT & operator*()
239+
{
240+
__debugbreak();
241+
return m_Value;
242+
}
243+
244+
SListElem_EP()
245+
: m_Link()
246+
, m_Value()
247+
{
248+
}
249+
250+
template <typename T1>
251+
SListElem_EP(T1&& val)
252+
: m_Link()
253+
, m_Value(std::forward<T1>(val))
254+
{
255+
}
256+
257+
ElemT & GetValue()
258+
{
259+
return m_Value;
260+
}
261+
262+
};
263+
264+
// Bare boned implementation to unblock HW
265+
template <class T>
266+
class CQuickArrayList_EP
267+
{
268+
private:
269+
size_t m_curSize;
270+
T *m_array;
271+
size_t maxSize;
272+
public:
273+
CQuickArrayList_EP()
274+
: m_curSize(0), maxSize(100)
275+
{
276+
m_array = new T[maxSize];
277+
}
278+
279+
280+
T* AllocNoThrow(size_t iItems)
281+
{
282+
return new T[iItems];
283+
}
284+
285+
bool PushNoThrow(const T & value)
286+
{
287+
if(m_curSize >= maxSize)
288+
__debugbreak();
289+
m_array[m_curSize++] = value;
290+
return true;
291+
}
292+
293+
size_t Size() const
294+
{
295+
return m_curSize;
296+
}
297+
298+
T Pop()
299+
{
300+
T t = m_array[m_curSize];
301+
m_curSize--;
302+
return t;
303+
}
304+
305+
void Shrink()
306+
{
307+
}
308+
309+
T& operator[] (size_t ix)
310+
{
311+
return m_array[ix];
312+
}
313+
314+
T* Ptr()
315+
{
316+
return m_array;
317+
}
318+
};
319+
320+
#endif // __EmptyContainers_h__

0 commit comments

Comments
 (0)