LMMS
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
1/*
2 WDL - queue.h
3 Copyright (C) 2005 and later, Cockos Incorporated
4
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.
8
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:
12
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.
20
21*/
22
23/*
24
25 This file provides a simple class for a FIFO queue of bytes. It uses a simple buffer,
26 so should not generally be used for large quantities of data (it can advance the queue
27 pointer, but Compact() needs to be called regularly to keep memory usage down, and when
28 it is called, there's a memcpy() penalty for the remaining data. oh well, is what it is).
29
30 You may also wish to look at fastqueue.h or circbuf.h if these limitations aren't acceptable.
31
32*/
33
34#ifndef _WDL_QUEUE_H_
35#define _WDL_QUEUE_H_
36
37#include "heapbuf.h"
38
39
41{
42public:
43 WDL_Queue() : m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_Queue")), m_pos(0) { }
44 WDL_Queue(int hbgran) : m_hb(hbgran WDL_HEAPBUF_TRACEPARM("WDL_Queue")), m_pos(0) { }
46
47 template <class T> void* AddT(T* buf)
48 {
49 return Add(buf, sizeof(T));
50 }
51
52 void *Add(const void *buf, int len)
53 {
54 int olen=m_hb.GetSize();
55 if (m_pos >= olen) m_pos=olen=0; // if queue is empty then autoreset it
56
57 char *newbuf=(char *)m_hb.ResizeOK(olen+len,false);
58 if (newbuf)
59 {
60 newbuf += olen;
61 if (buf) memcpy(newbuf,buf,len);
62 }
63 return newbuf;
64 }
65
66 template <class T> T* GetT(T* val=0)
67 {
68 T* p = (T*) Get(sizeof(T));
69 if (val && p) *val = *p;
70 return p;
71 }
72
73 void* Get(int size)
74 {
75 void* p = Get();
76 if (p) Advance(size);
77 return p;
78 }
79
80 void *Get() const
81 {
82 if (m_pos >= 0 && m_pos < m_hb.GetSize()) return (char *)m_hb.Get()+m_pos;
83 return NULL;
84 }
85
86 void* Rewind()
87 {
88 m_pos = 0;
89 return m_hb.Get();
90 }
91
92 int GetSize() const
93 {
94 return m_hb.GetSize()-m_pos;
95 }
96 int Available() const { return GetSize(); }
97
98 void Clear()
99 {
100 m_pos=0;
101 m_hb.Resize(0,false);
102 }
103
104 void Advance(int bytecnt)
105 {
106 m_pos+=bytecnt;
107 if (m_pos<0)m_pos=0;
108 else if (m_pos > m_hb.GetSize()) m_pos=m_hb.GetSize();
109 }
110
111 void Compact(bool allocdown=false, bool force=false)
112 {
113 int olen=m_hb.GetSize();
114 if (m_pos > (force ? 0 : olen/2))
115 {
116 olen -= m_pos;
117 if (olen > 0)
118 {
119 char *a=(char*)m_hb.Get();
120 memmove(a,a+m_pos,olen);
121 }
122 else
123 {
124 olen = 0;
125 }
126 m_hb.Resize(olen,allocdown);
127 m_pos=0;
128 }
129 }
130
131 void SetGranul(int granul) { m_hb.SetGranul(granul); }
132
133
134
135
136 // endian-management stuff
137
138 static void WDL_Queue__bswap_buffer(void *buf, int len)
139 {
140 #ifdef __ppc__
141 char *p=(char *)buf;
142 char *ep=p+len;
143 while ((len-=2) >= 0)
144 {
145 char tmp=*p; *p++=*--ep; *ep=tmp;
146 }
147 #endif
148 }
149
150 // older API of static functions (that endedu p warning a bit anyway)
151#define WDL_Queue__AddToLE(q, v) (q)->AddToLE(v)
152#define WDL_Queue__AddDataToLE(q,d,ds,us) (q)->AddDataToLE(d,ds,us)
153#define WDL_Queue__GetTFromLE(q,v) (q)->GetTFromLE(v)
154#define WDL_Queue__GetDataFromLE(q,ds,us) (q)->GetDataFromLE(ds,us)
155
156 template<class T> void AddToLE(T *val)
157 {
158 WDL_Queue__bswap_buffer(AddT(val),sizeof(T));
159 }
160 void AddDataToLE(void *data, int datasize, int unitsize)
161 {
162 #ifdef __ppc__
163 char *dout = (char *)Add(data,datasize);
164 while (datasize >= unitsize)
165 {
166 WDL_Queue__bswap_buffer(dout,unitsize);
167 dout+=unitsize;
168 datasize-=unitsize;
169 }
170 #else
171 Add(data,datasize);
172 #endif
173 }
174
175
176 // 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,
177 // then get ready to get unhappy.
178 template<class T> T *GetTFromLE(T* val=0)
179 {
180 T *p = GetT(val);
181 if (p) {
182 WDL_Queue__bswap_buffer(p,sizeof(T));
183 if (val) *val = *p;
184 }
185 return p;
186 }
187
188 void *GetDataFromLE(int datasize, int unitsize)
189 {
190 void *data=Get(datasize);
191 #ifdef __ppc__
192 char *dout=(char *)data;
193 if (dout) while (datasize >= unitsize)
194 {
195 WDL_Queue__bswap_buffer(dout,unitsize);
196 dout+=unitsize;
197 datasize-=unitsize;
198 }
199 #endif
200 return data;
201 }
202
203
204private:
206 int m_pos;
207public:
208 int __pad; // keep 8 byte aligned
209};
210
211template <class T> class WDL_TypedQueue
212{
213public:
214 WDL_TypedQueue() : m_hb(4096 WDL_HEAPBUF_TRACEPARM("WDL_TypedQueue")), m_pos(0) { }
216
217 T *Add(const T *buf, int len)
218 {
219 int olen=m_hb.GetSize();
220 if (m_pos >= olen) olen=m_pos=0;
221 len *= (int)sizeof(T);
222
223 char *newbuf=(char*)m_hb.ResizeOK(olen+len,false);
224 if (newbuf)
225 {
226 newbuf += olen;
227 if (buf) memcpy(newbuf,buf,len);
228 }
229 return (T*) newbuf;
230 }
231
232 T *Get() const
233 {
234 if (m_pos >= 0 && m_pos < m_hb.GetSize()) return (T*)((char *)m_hb.Get()+m_pos);
235 return NULL;
236 }
237
238 int GetSize() const
239 {
240 return m_pos < m_hb.GetSize() ? (m_hb.GetSize()-m_pos)/sizeof(T) : 0;
241 }
242 int Available() const { return GetSize(); }
243
244 void Clear()
245 {
246 m_pos=0;
247 m_hb.Resize(0,false);
248 }
249
250 void Advance(int cnt)
251 {
252 m_pos+=cnt*(int)sizeof(T);
253 if (m_pos<0)m_pos=0;
254 else if (m_pos > m_hb.GetSize()) m_pos=m_hb.GetSize();
255 }
256
257 void Compact(bool allocdown=false, bool force=false)
258 {
259 int olen=m_hb.GetSize();
260 if (m_pos >= (force ? 0 : olen/2))
261 {
262 olen -= m_pos;
263 if (olen > 0)
264 {
265 char *a=(char*)m_hb.Get();
266 memmove(a,a+m_pos,olen);
267 }
268 else
269 {
270 olen = 0;
271 }
272 m_hb.Resize(olen,allocdown);
273 m_pos=0;
274 }
275 }
276
277 void SetGranul(int granul) { m_hb.SetGranul(granul); }
278
279private:
281 int m_pos;
282public:
283 int __pad; // keep 8 byte aligned
284};
285
286#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
uint8_t a
Definition Spc_Cpu.h:141
Definition heapbuf.h:48
void SetGranul(int granul)
Definition queue.h:131
void * Add(const void *buf, int len)
Definition queue.h:52
int Available() const
Definition queue.h:96
void * Rewind()
Definition queue.h:86
void * Get() const
Definition queue.h:80
WDL_HeapBuf m_hb
Definition queue.h:205
int GetSize() const
Definition queue.h:92
void * AddT(T *buf)
Definition queue.h:47
void AddToLE(T *val)
Definition queue.h:156
void AddDataToLE(void *data, int datasize, int unitsize)
Definition queue.h:160
void * Get(int size)
Definition queue.h:73
T * GetTFromLE(T *val=0)
Definition queue.h:178
WDL_Queue()
Definition queue.h:43
void Compact(bool allocdown=false, bool force=false)
Definition queue.h:111
WDL_Queue(int hbgran)
Definition queue.h:44
int __pad
Definition queue.h:208
void * GetDataFromLE(int datasize, int unitsize)
Definition queue.h:188
int m_pos
Definition queue.h:206
T * GetT(T *val=0)
Definition queue.h:66
static void WDL_Queue__bswap_buffer(void *buf, int len)
Definition queue.h:138
~WDL_Queue()
Definition queue.h:45
void Clear()
Definition queue.h:98
void Advance(int bytecnt)
Definition queue.h:104
int GetSize() const
Definition queue.h:238
T * Add(const T *buf, int len)
Definition queue.h:217
WDL_HeapBuf m_hb
Definition queue.h:280
void SetGranul(int granul)
Definition queue.h:277
void Clear()
Definition queue.h:244
void Compact(bool allocdown=false, bool force=false)
Definition queue.h:257
~WDL_TypedQueue()
Definition queue.h:215
T * Get() const
Definition queue.h:232
WDL_TypedQueue()
Definition queue.h:214
int Available() const
Definition queue.h:242
void Advance(int cnt)
Definition queue.h:250
int m_pos
Definition queue.h:281
int __pad
Definition queue.h:283
#define WDL_HEAPBUF_TRACEPARM(x)
Definition heapbuf.h:42
int val
Definition jpeglib.h:956
JSAMPIMAGE data
Definition jpeglib.h:945
uch * p
Definition crypt.c:594
memcpy(hh, h, RAND_HEAD_LEN)
ulg size
Definition extract.c:2350
typedef int(UZ_EXP MsgFn)()