-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
273 lines (226 loc) · 6.44 KB
/
queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
WDL - queue.h
Copyright (C) 2005 and later, Cockos Incorporated
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
This file provides a simple class for a FIFO queue of bytes. It uses a simple buffer,
so should not generally be used for large quantities of data (it can advance the queue
pointer, but Compact() needs to be called regularly to keep memory usage down, and when
it is called, there's a memcpy() penalty for the remaining data. oh well, is what it is).
You may also wish to look at fastqueue.h or circbuf.h if these limitations aren't acceptable.
*/
#ifndef _WDL_QUEUE_H_
#define _WDL_QUEUE_H_
#include "heapbuf.h"
class WDL_Queue
{
public:
WDL_Queue() : m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_Queue")), m_pos(0) { }
WDL_Queue(int hbgran) : m_hb(hbgran WDL_HEAPBUF_TRACEPARM("WDL_Queue")), m_pos(0) { }
~WDL_Queue() { }
template <class T> void* AddT(T* buf)
{
return Add(buf, sizeof(T));
}
void *Add(const void *buf, int len)
{
int olen=m_hb.GetSize();
if (m_pos >= olen) m_pos=olen=0; // if queue is empty then autoreset it
void *obuf=m_hb.Resize(olen+len,false);
if (m_hb.GetSize() != olen+len) return NULL;
char* newbuf = (char*) obuf + olen;
if (buf) memcpy(newbuf,buf,len);
return newbuf;
}
template <class T> T* GetT(T* val=0)
{
T* p = (T*) Get(sizeof(T));
if (val && p) *val = *p;
return p;
}
void* Get(int size)
{
void* p = Get();
if (p) Advance(size);
return p;
}
void *Get() const
{
void *buf=m_hb.Get();
if (buf && m_pos >= 0 && m_pos < m_hb.GetSize()) return (char *)buf+m_pos;
return NULL;
}
void* Rewind()
{
m_pos = 0;
return m_hb.Get();
}
int GetSize() const
{
return m_hb.GetSize()-m_pos;
}
int Available() const { return GetSize(); }
void Clear()
{
m_pos=0;
m_hb.Resize(0,false);
}
void Advance(int bytecnt)
{
m_pos+=bytecnt;
if (m_pos<0)m_pos=0;
else if (m_pos > m_hb.GetSize()) m_pos=m_hb.GetSize();
}
void Compact(bool allocdown=false, bool force=false)
{
int olen=m_hb.GetSize();
if (m_pos > (force ? 0 : olen/2))
{
if (m_pos < olen)
{
void *a=m_hb.Get();
if (a) memmove(a,(char*)a+m_pos,olen-m_pos);
m_hb.Resize(olen-m_pos,allocdown);
}
else m_hb.Resize(0,allocdown);
m_pos=0;
}
}
void SetGranul(int granul) { m_hb.SetGranul(granul); }
// endian-management stuff
static void WDL_Queue__bswap_buffer(void *buf, int len)
{
#ifdef __ppc__
char *p=(char *)buf;
char *ep=p+len;
while ((len-=2) >= 0)
{
char tmp=*p; *p++=*--ep; *ep=tmp;
}
#endif
}
// older API of static functions (that endedu p warning a bit anyway)
#define WDL_Queue__AddToLE(q, v) (q)->AddToLE(v)
#define WDL_Queue__AddDataToLE(q,d,ds,us) (q)->AddDataToLE(d,ds,us)
#define WDL_Queue__GetTFromLE(q,v) (q)->GetTFromLE(v)
#define WDL_Queue__GetDataFromLE(q,ds,us) (q)->GetDataFromLE(ds,us)
template<class T> void AddToLE(T *val)
{
WDL_Queue__bswap_buffer(AddT(val),sizeof(T));
}
void AddDataToLE(void *data, int datasize, int unitsize)
{
#ifdef __ppc__
char *dout = (char *)Add(data,datasize);
while (datasize >= unitsize)
{
WDL_Queue__bswap_buffer(dout,unitsize);
dout+=unitsize;
datasize-=unitsize;
}
#else
Add(data,datasize);
#endif
}
// NOTE: these thrash the contents of the queue if on LE systems. So for example if you are going to rewind it later or use it elsewhere,
// then get ready to get unhappy.
template<class T> T *GetTFromLE(T* val=0)
{
T *p = GetT(val);
if (p) {
WDL_Queue__bswap_buffer(p,sizeof(T));
if (val) *val = *p;
}
return p;
}
void *GetDataFromLE(int datasize, int unitsize)
{
void *data=Get(datasize);
#ifdef __ppc__
char *dout=(char *)data;
if (dout) while (datasize >= unitsize)
{
WDL_Queue__bswap_buffer(dout,unitsize);
dout+=unitsize;
datasize-=unitsize;
}
#endif
return data;
}
private:
WDL_HeapBuf m_hb;
int m_pos;
int __pad; // keep 8 byte aligned
};
template <class T> class WDL_TypedQueue
{
public:
WDL_TypedQueue() : m_pos(0), m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_TypedQueue")) { }
~WDL_TypedQueue() { }
T *Add(const T *buf, int len)
{
int olen=m_hb.GetSize();
if (m_pos >= olen) olen=m_pos=0;
len *= sizeof(T);
void *obuf=m_hb.Resize(olen+len,false);
if (!obuf||m_hb.GetSize()!=olen+len) return 0;
if (buf) memcpy((char*)obuf+olen,buf,len);
return (T*) ((char*)obuf+olen);
}
T *Get() const
{
void *buf=m_hb.Get();
if (buf && m_pos >= 0 && m_pos < m_hb.GetSize()) return (T*)((char *)buf+m_pos);
return NULL;
}
int GetSize() const
{
return (m_hb.GetSize()-m_pos)/sizeof(T);
}
int Available() const { return GetSize(); }
void Clear()
{
m_pos=0;
m_hb.Resize(0,false);
}
void Advance(int cnt)
{
m_pos+=cnt*sizeof(T);
if (m_pos<0)m_pos=0;
else if (m_pos > m_hb.GetSize()) m_pos=m_hb.GetSize();
}
void Compact(bool allocdown=false, bool force=false)
{
int olen=m_hb.GetSize();
if (m_pos > (force ? 0 : olen/2))
{
if (m_pos < olen)
{
void *a=m_hb.Get();
if (a) memmove(a,(char*)a+m_pos,olen-m_pos);
m_hb.Resize(olen-m_pos,allocdown);
}
else m_hb.Resize(0,allocdown);
m_pos=0;
}
}
void SetGranul(int granul) { m_hb.SetGranul(granul); }
private:
WDL_HeapBuf m_hb;
int m_pos;
int __pad; // keep 8 byte aligned
};
#endif