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_
37 WDL_CircBuf() : m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_CircBuf"))
39 m_inbuf
= m_wrptr
= 0;
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
;
56 const int wr1
= m_hb
.GetSize()-m_wrptr
;
59 memmove((char*)m_hb
.Get() + m_wrptr
, buf
, wr1
);
60 memmove(m_hb
.Get(), (char*)buf
+ wr1
, l
-wr1
);
65 memmove((char*)m_hb
.Get() + m_wrptr
, buf
, l
);
66 m_wrptr
= wr1
== l
? 0 : m_wrptr
+l
;
72 int Peek(void *buf
, int offs
, int len
) const
75 const int ibo
= m_inbuf
-offs
;
76 if (len
> ibo
) len
= ibo
;
79 int rp
= m_wrptr
- ibo
;
80 if (rp
< 0) rp
+= m_hb
.GetSize();
81 const int wr1
= m_hb
.GetSize() - rp
;
84 memmove(buf
,(char*)m_hb
.Get()+rp
,wr1
);
85 memmove((char*)buf
+wr1
,m_hb
.Get(),len
-wr1
);
89 memmove(buf
,(char*)m_hb
.Get()+rp
,len
);
94 int Get(void *buf
, int l
)
96 const int amt
= Peek(buf
,0,l
);
100 int NbFree() const { return m_hb
.GetSize() - m_inbuf
; } // formerly Available()
101 int NbInBuf() const { return m_inbuf
; }
105 int m_inbuf
, m_wrptr
;
110 class WDL_TypedCircBuf
114 WDL_TypedCircBuf() {}
115 ~WDL_TypedCircBuf() {}
117 void SetSize(int size
)
119 mBuf
.SetSize(size
* sizeof(T
));
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
);
143 return mBuf
.NbInBuf() / sizeof(T
);