ddnet/src/engine/shared/ringbuffer.h

69 lines
1.5 KiB
C
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#ifndef ENGINE_SHARED_RINGBUFFER_H
#define ENGINE_SHARED_RINGBUFFER_H
2008-02-03 22:42:03 +00:00
2009-10-27 14:38:53 +00:00
class CRingBufferBase
2008-02-03 22:42:03 +00:00
{
2009-10-27 14:38:53 +00:00
class CItem
{
public:
CItem *m_pPrev;
CItem *m_pNext;
int m_Free;
int m_Size;
};
2009-10-27 14:38:53 +00:00
CItem *m_pProduce;
CItem *m_pConsume;
2009-10-27 14:38:53 +00:00
CItem *m_pFirst;
CItem *m_pLast;
2009-10-27 14:38:53 +00:00
int m_Size;
int m_Flags;
2009-10-27 14:38:53 +00:00
CItem *NextBlock(CItem *pItem);
CItem *PrevBlock(CItem *pItem);
CItem *MergeBack(CItem *pItem);
2009-10-27 14:38:53 +00:00
protected:
void *Allocate(int Size);
2009-10-27 14:38:53 +00:00
void *Prev(void *pCurrent);
void *Next(void *pCurrent);
void *First();
void *Last();
2009-10-27 14:38:53 +00:00
void Init(void *pMemory, int Size, int Flags);
int PopFirst();
2009-10-27 14:38:53 +00:00
public:
enum
{
2010-05-29 07:25:38 +00:00
// Will start to destroy items to try to fit the next one
FLAG_RECYCLE = 1
2009-10-27 14:38:53 +00:00
};
};
2008-02-03 22:42:03 +00:00
template<typename T, int TSIZE, int TFLAGS = 0>
2020-10-27 17:57:14 +00:00
class CStaticRingBuffer : public CRingBufferBase
2009-10-27 14:38:53 +00:00
{
unsigned char m_aBuffer[TSIZE];
2009-10-27 14:38:53 +00:00
public:
2020-10-27 17:57:14 +00:00
CStaticRingBuffer() { Init(); }
2009-10-27 14:38:53 +00:00
void Init() { CRingBufferBase::Init(m_aBuffer, TSIZE, TFLAGS); }
T *Allocate(int Size) { return (T *)CRingBufferBase::Allocate(Size); }
2009-10-27 14:38:53 +00:00
int PopFirst() { return CRingBufferBase::PopFirst(); }
T *Prev(T *pCurrent) { return (T *)CRingBufferBase::Prev(pCurrent); }
T *Next(T *pCurrent) { return (T *)CRingBufferBase::Next(pCurrent); }
T *First() { return (T *)CRingBufferBase::First(); }
T *Last() { return (T *)CRingBufferBase::Last(); }
2009-10-27 14:38:53 +00:00
};
#endif