DDraceNetwork Docs
ringbuffer.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_RINGBUFFER_H
4#define ENGINE_SHARED_RINGBUFFER_H
5
6#include <base/system.h>
7
8#include <functional>
9
11{
12 class CItem
13 {
14 public:
17 int m_Free;
18 int m_Size;
19 };
20
23
26
27 int m_Size;
29
30 std::function<void(void *pCurrent)> m_PopCallback = nullptr;
31
32 CItem *NextBlock(CItem *pItem);
33 CItem *PrevBlock(CItem *pItem);
34 CItem *MergeBack(CItem *pItem);
35
36protected:
37 void *Allocate(int Size);
38
39 void *Prev(void *pCurrent);
40 void *Next(void *pCurrent);
41 void *First();
42 void *Last();
43
44 void Init(void *pMemory, int Size, int Flags);
45 int PopFirst();
46 void SetPopCallback(const std::function<void(void *pCurrent)> PopCallback);
47
48public:
49 enum
50 {
51 // Will start to destroy items to try to fit the next one
52 FLAG_RECYCLE = 1
53 };
54 static constexpr int ITEM_SIZE = sizeof(CItem);
55};
56
57template<typename T>
59{
60public:
61 T *Allocate(int Size) { return (T *)CRingBufferBase::Allocate(Size); }
63 void SetPopCallback(std::function<void(T *pCurrent)> PopCallback)
64 {
65 CRingBufferBase::SetPopCallback([PopCallback](void *pCurrent) {
66 PopCallback((T *)pCurrent);
67 });
68 }
69
70 T *Prev(T *pCurrent) { return (T *)CRingBufferBase::Prev(pCurrent); }
71 T *Next(T *pCurrent) { return (T *)CRingBufferBase::Next(pCurrent); }
72 T *First() { return (T *)CRingBufferBase::First(); }
73 T *Last() { return (T *)CRingBufferBase::Last(); }
74};
75
76template<typename T, int TSIZE, int TFLAGS = 0>
78{
79 unsigned char m_aBuffer[TSIZE];
80
81public:
83
84 void Init() { CRingBufferBase::Init(m_aBuffer, TSIZE, TFLAGS); }
85};
86
87template<typename T>
89{
90 unsigned char *m_pBuffer = nullptr;
91
92public:
93 CDynamicRingBuffer(int Size, int Flags = 0) { Init(Size, Flags); }
94
96 {
97 free(m_pBuffer);
98 }
99
100 void Init(int Size, int Flags)
101 {
102 free(m_pBuffer);
103 m_pBuffer = static_cast<unsigned char *>(malloc(Size));
104 CRingBufferBase::Init(m_pBuffer, Size, Flags);
105 }
106};
107
108#endif
Definition: ringbuffer.h:89
void Init(int Size, int Flags)
Definition: ringbuffer.h:100
virtual ~CDynamicRingBuffer()
Definition: ringbuffer.h:95
CDynamicRingBuffer(int Size, int Flags=0)
Definition: ringbuffer.h:93
unsigned char * m_pBuffer
Definition: ringbuffer.h:90
Definition: ringbuffer.h:13
CItem * m_pPrev
Definition: ringbuffer.h:15
CItem * m_pNext
Definition: ringbuffer.h:16
int m_Free
Definition: ringbuffer.h:17
int m_Size
Definition: ringbuffer.h:18
Definition: ringbuffer.h:11
CItem * m_pConsume
Definition: ringbuffer.h:22
static constexpr int ITEM_SIZE
Definition: ringbuffer.h:54
int PopFirst()
Definition: ringbuffer.cpp:131
void * First()
Definition: ringbuffer.cpp:189
int m_Size
Definition: ringbuffer.h:27
void SetPopCallback(const std::function< void(void *pCurrent)> PopCallback)
Definition: ringbuffer.cpp:126
void * Allocate(int Size)
Definition: ringbuffer.cpp:59
CItem * m_pLast
Definition: ringbuffer.h:25
void * Prev(void *pCurrent)
Definition: ringbuffer.cpp:161
CItem * m_pProduce
Definition: ringbuffer.h:21
CItem * PrevBlock(CItem *pItem)
Definition: ringbuffer.cpp:12
CItem * MergeBack(CItem *pItem)
Definition: ringbuffer.cpp:19
@ FLAG_RECYCLE
Definition: ringbuffer.h:52
void Init(void *pMemory, int Size, int Flags)
Definition: ringbuffer.cpp:45
CItem * NextBlock(CItem *pItem)
Definition: ringbuffer.cpp:5
void * Next(void *pCurrent)
Definition: ringbuffer.cpp:175
void * Last()
Definition: ringbuffer.cpp:196
int m_Flags
Definition: ringbuffer.h:28
CItem * m_pFirst
Definition: ringbuffer.h:24
std::function< void(void *pCurrent)> m_PopCallback
Definition: ringbuffer.h:30
Definition: ringbuffer.h:78
unsigned char m_aBuffer[TSIZE]
Definition: ringbuffer.h:79
CStaticRingBuffer()
Definition: ringbuffer.h:82
void Init()
Definition: ringbuffer.h:84
Definition: ringbuffer.h:59
T * Prev(T *pCurrent)
Definition: ringbuffer.h:70
int PopFirst()
Definition: ringbuffer.h:62
T * Allocate(int Size)
Definition: ringbuffer.h:61
T * Next(T *pCurrent)
Definition: ringbuffer.h:71
T * Last()
Definition: ringbuffer.h:73
T * First()
Definition: ringbuffer.h:72
void SetPopCallback(std::function< void(T *pCurrent)> PopCallback)
Definition: ringbuffer.h:63