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
22
25
28
29 int m_Size;
31
32 std::function<void(void *pCurrent)> m_PopCallback = nullptr;
33
34 CItem *NextBlock(CItem *pItem);
35 CItem *PrevBlock(CItem *pItem);
36 CItem *MergeBack(CItem *pItem);
37
38protected:
39 void *Allocate(int Size);
40
41 void *Prev(void *pCurrent);
42 void *Next(void *pCurrent);
43 void *First();
44 void *Last();
45
46 void Init(void *pMemory, int Size, int Flags);
47 int PopFirst();
48 void SetPopCallback(const std::function<void(void *pCurrent)> PopCallback);
49
50public:
51 enum
52 {
53 // Will start to destroy items to try to fit the next one
54 FLAG_RECYCLE = 1
55 };
56 static constexpr int ITEM_SIZE = sizeof(CItem);
57
58 void Clear();
59};
60
61template<typename T>
63{
64public:
65 T *Allocate(int Size) { return (T *)CRingBufferBase::Allocate(Size); }
67 void SetPopCallback(std::function<void(T *pCurrent)> PopCallback)
68 {
69 CRingBufferBase::SetPopCallback([PopCallback](void *pCurrent) {
70 PopCallback((T *)pCurrent);
71 });
72 }
73
74 T *Prev(T *pCurrent) { return (T *)CRingBufferBase::Prev(pCurrent); }
75 T *Next(T *pCurrent) { return (T *)CRingBufferBase::Next(pCurrent); }
76 T *First() { return (T *)CRingBufferBase::First(); }
77 T *Last() { return (T *)CRingBufferBase::Last(); }
78};
79
80template<typename T, int TSIZE, int TFLAGS = 0>
82{
83 unsigned char m_aBuffer[TSIZE];
84
85public:
87
88 void Init() { CRingBufferBase::Init(m_aBuffer, TSIZE, TFLAGS); }
89};
90
91template<typename T>
93{
94 unsigned char *m_pBuffer = nullptr;
95
96public:
97 CDynamicRingBuffer(int Size, int Flags = 0) { Init(Size, Flags); }
98
100 {
101 free(m_pBuffer);
102 }
103
104 void Init(int Size, int Flags)
105 {
106 free(m_pBuffer);
107 m_pBuffer = static_cast<unsigned char *>(malloc(Size));
108 CRingBufferBase::Init(m_pBuffer, Size, Flags);
109 }
110};
111
112#endif
Definition: ringbuffer.h:93
void Init(int Size, int Flags)
Definition: ringbuffer.h:104
virtual ~CDynamicRingBuffer()
Definition: ringbuffer.h:99
CDynamicRingBuffer(int Size, int Flags=0)
Definition: ringbuffer.h:97
unsigned char * m_pBuffer
Definition: ringbuffer.h:94
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:24
static constexpr int ITEM_SIZE
Definition: ringbuffer.h:56
int PopFirst()
Definition: ringbuffer.cpp:137
void * First()
Definition: ringbuffer.cpp:195
int m_Size
Definition: ringbuffer.h:29
void SetPopCallback(const std::function< void(void *pCurrent)> PopCallback)
Definition: ringbuffer.cpp:132
void * Allocate(int Size)
Definition: ringbuffer.cpp:65
void Clear()
Definition: ringbuffer.cpp:53
CItem * m_pLast
Definition: ringbuffer.h:27
void * Prev(void *pCurrent)
Definition: ringbuffer.cpp:167
@ FLAG_RECYCLE
Definition: ringbuffer.h:54
CItem * m_pProduce
Definition: ringbuffer.h:23
CItem * PrevBlock(CItem *pItem)
Definition: ringbuffer.cpp:12
CItem * MergeBack(CItem *pItem)
Definition: ringbuffer.cpp:19
CItem * m_pBuffer
Definition: ringbuffer.h:21
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:181
void * Last()
Definition: ringbuffer.cpp:202
int m_Flags
Definition: ringbuffer.h:30
CItem * m_pFirst
Definition: ringbuffer.h:26
std::function< void(void *pCurrent)> m_PopCallback
Definition: ringbuffer.h:32
Definition: ringbuffer.h:82
unsigned char m_aBuffer[TSIZE]
Definition: ringbuffer.h:83
CStaticRingBuffer()
Definition: ringbuffer.h:86
void Init()
Definition: ringbuffer.h:88
Definition: ringbuffer.h:63
T * Prev(T *pCurrent)
Definition: ringbuffer.h:74
int PopFirst()
Definition: ringbuffer.h:66
T * Allocate(int Size)
Definition: ringbuffer.h:65
T * Next(T *pCurrent)
Definition: ringbuffer.h:75
T * Last()
Definition: ringbuffer.h:77
T * First()
Definition: ringbuffer.h:76
void SetPopCallback(std::function< void(T *pCurrent)> PopCallback)
Definition: ringbuffer.h:67