xref: /haiku/src/add-ons/media/plugins/ape_reader/MAClib/CircleBuffer.h (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 #ifndef APE_CIRCLEBUFFER_H
2 #define APE_CIRCLEBUFFER_H
3 
4 class CCircleBuffer
5 {
6 public:
7 
8     // construction / destruction
9     CCircleBuffer();
10     virtual ~CCircleBuffer();
11 
12     // create the buffer
13     void CreateBuffer(int nBytes, int nMaxDirectWriteBytes);
14 
15     // query
16     int MaxAdd();
17     int MaxGet();
18 
19     // direct writing
20     inline unsigned char * GetDirectWritePointer()
21     {
22         // return a pointer to the tail -- note that it will always be safe to write
23         // at least m_nMaxDirectWriteBytes since we use an end cap region
24         return &m_pBuffer[m_nTail];
25     }
26 
27     inline void UpdateAfterDirectWrite(int nBytes)
28     {
29         // update the tail
30         m_nTail += nBytes;
31 
32         // if the tail enters the "end cap" area, set the end cap and loop around
33         if (m_nTail >= (m_nTotal - m_nMaxDirectWriteBytes))
34         {
35             m_nEndCap = m_nTail;
36             m_nTail = 0;
37         }
38     }
39 
40     // get data
41     int Get(unsigned char * pBuffer, int nBytes);
42 
43     // remove / empty
44     void Empty();
45     int RemoveHead(int nBytes);
46     int RemoveTail(int nBytes);
47 
48 private:
49 
50     int m_nTotal;
51     int m_nMaxDirectWriteBytes;
52     int m_nEndCap;
53     int m_nHead;
54     int m_nTail;
55     unsigned char * m_pBuffer;
56 };
57 
58 
59 #endif // #ifndef APE_CIRCLEBUFFER_H
60