3 Copyright (C) 2006 and later 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.
22 This file defines and implements a class which can queue arbitrary amounts of data.
23 It is optimized for lots of reads and writes with a significant queue (i.e. it doesnt
24 have to shuffle much memory around).
26 The downside is that you can't just ask for a pointer to specific bytes, it may have to peice
27 it together into a buffer of your choosing (or you can step through the buffers using GetPtr()).
32 #ifndef _WDL_FASTQUEUE_H_
33 #define _WDL_FASTQUEUE_H_
38 #define WDL_FASTQUEUE_ADD_NOZEROBUF ((void *)(INT_PTR)0xf0)
49 WDL_FastQueue(int bsize
=65536-64, int maxemptieskeep
=-1)
52 m_bsize
=bsize
<32?32:bsize
;
54 m_maxemptieskeep
=maxemptieskeep
;
58 m_queue
.Empty(true,free
);
59 m_empties
.Empty(true,free
);
62 void *Add(const void *buf
, int len
) // buf can be NULL to add zeroes
64 if (len
< 1) return NULL
;
66 fqBuf
*qb
=m_queue
.Get(m_queue
.GetSize()-1);
67 if (!qb
|| (qb
->used
+ len
) > qb
->alloc_size
)
69 const int esz
=m_empties
.GetSize()-1;
70 qb
=m_empties
.Get(esz
);
71 m_empties
.Delete(esz
);
72 if (qb
&& qb
->alloc_size
< len
) // spare buffer is not big enough, toss it
79 const int sz
=len
< m_bsize
? m_bsize
: len
;
80 qb
=(fqBuf
*)malloc(sz
+ sizeof(fqBuf
) - sizeof(qb
->data
));
88 void *ret
= qb
->data
+ qb
->used
;
91 if (buf
!= WDL_FASTQUEUE_ADD_NOZEROBUF
)
93 memcpy(ret
, buf
, len
);
106 void Clear(int limitmaxempties
=-1)
108 int x
=m_queue
.GetSize();
109 if (limitmaxempties
<0) limitmaxempties
= m_maxemptieskeep
;
112 if (limitmaxempties
<0 || m_empties
.GetSize()<limitmaxempties
)
114 m_empties
.Add(m_queue
.Get(--x
));
118 free(m_queue
.Get(--x
));
126 void Advance(int cnt
)
130 if (m_avail
<0)m_avail
=0;
133 while ((mq
=m_queue
.Get(0)))
135 const int sz
=mq
->used
;
136 if (m_offs
< sz
) break;
139 if (m_maxemptieskeep
<0 || m_empties
.GetSize()<m_maxemptieskeep
)
149 if (!mq
||m_offs
<0) m_offs
=0;
152 int Available() const // bytes available
158 int GetPtr(int offset
, void **buf
) const // returns bytes available in this block
164 while ((mq
=m_queue
.Get(x
)))
166 const int sz
=mq
->used
;
169 *buf
= (char *)mq
->data
+ offset
;
179 int SetFromBuf(int offs
, void *buf
, int len
) // returns length set
185 int l
=GetPtr(offs
+pos
,&p
);
188 memcpy(p
,(char *)buf
+ pos
,l
);
195 int GetToBuf(int offs
, void *buf
, int len
) const
201 int l
=GetPtr(offs
+pos
,&p
);
204 memcpy((char *)buf
+ pos
,p
,l
);
213 WDL_PtrList
<fqBuf
> m_queue
, m_empties
;
217 int m_maxemptieskeep
;
221 #endif //_WDL_FASTQUEUE_H_