Create FUNDING.yml
[wdl/wdl-ol.git] / WDL / circbuf.h
blob81046d3b359fc3f532bbece15306feff50f72184
1 /*
2 WDL - circbuf.h
3 Copyright (C) 2005 Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
25 This file provides a simple class for a circular FIFO queue of bytes.
29 #ifndef _WDL_CIRCBUF_H_
30 #define _WDL_CIRCBUF_H_
32 #include "heapbuf.h"
34 class WDL_CircBuf
36 public:
37 WDL_CircBuf() : m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_CircBuf"))
39 m_inbuf = m_wrptr = 0;
41 ~WDL_CircBuf()
44 void SetSize(int size)
46 m_inbuf = m_wrptr = 0;
47 m_hb.Resize(size,true);
49 void Reset() { m_inbuf = m_wrptr = 0; }
50 int Add(const void *buf, int l)
52 const int bf = m_hb.GetSize() - m_inbuf;
53 if (l > bf) l = bf;
54 if (l > 0)
56 const int wr1 = m_hb.GetSize()-m_wrptr;
57 if (wr1 < l)
59 memmove((char*)m_hb.Get() + m_wrptr, buf, wr1);
60 memmove(m_hb.Get(), (char*)buf + wr1, l-wr1);
61 m_wrptr = l-wr1;
63 else
65 memmove((char*)m_hb.Get() + m_wrptr, buf, l);
66 m_wrptr = wr1 == l ? 0 : m_wrptr+l;
68 m_inbuf += l;
70 return l;
72 int Peek(void *buf, int offs, int len) const
74 if (offs<0) return 0;
75 const int ibo = m_inbuf-offs;
76 if (len > ibo) len = ibo;
77 if (len > 0)
79 int rp = m_wrptr - ibo;
80 if (rp < 0) rp += m_hb.GetSize();
81 const int wr1 = m_hb.GetSize() - rp;
82 if (wr1 < len)
84 memmove(buf,(char*)m_hb.Get()+rp,wr1);
85 memmove((char*)buf+wr1,m_hb.Get(),len-wr1);
87 else
89 memmove(buf,(char*)m_hb.Get()+rp,len);
92 return len;
94 int Get(void *buf, int l)
96 const int amt = Peek(buf,0,l);
97 m_inbuf -= amt;
98 return amt;
100 int NbFree() const { return m_hb.GetSize() - m_inbuf; } // formerly Available()
101 int NbInBuf() const { return m_inbuf; }
103 private:
104 WDL_HeapBuf m_hb;
105 int m_inbuf, m_wrptr;
106 } WDL_FIXALIGN;
109 template <class T>
110 class WDL_TypedCircBuf
112 public:
114 WDL_TypedCircBuf() {}
115 ~WDL_TypedCircBuf() {}
117 void SetSize(int size)
119 mBuf.SetSize(size * sizeof(T));
122 void Reset()
124 mBuf.Reset();
127 int Add(const T* buf, int l)
129 return mBuf.Add(buf, l * sizeof(T)) / sizeof(T);
132 int Get(T* buf, int l)
134 return mBuf.Get(buf, l * sizeof(T)) / sizeof(T);
137 int NbFree() // formerly Available()
139 return mBuf.NbFree() / sizeof(T);
141 int NbInBuf()
143 return mBuf.NbInBuf() / sizeof(T);
146 private:
147 WDL_CircBuf mBuf;
148 } WDL_FIXALIGN;
150 #endif