summaryrefslogtreecommitdiffstats
path: root/WDL/queue.h
blob: e04e697ad24f82fe5c3c09e248802547c2489d61 (plain) (blame)
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
/*
    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(int hbgran=4096) : m_hb(hbgran), 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

    char *newbuf=(char *)m_hb.ResizeOK(olen+len,false);
    if (newbuf)
    {
      newbuf += 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
  {
    if (m_pos >= 0 && m_pos < m_hb.GetSize()) return (char *)m_hb.Get()+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))
    {
      olen -= m_pos;
      if (olen > 0)
      {
        char *a=(char*)m_hb.Get();
        memmove(a,a+m_pos,olen);
      }
      else 
      {
        olen = 0;
      }
      m_hb.Resize(olen,allocdown);
      m_pos=0;
    }
  }

  void SetGranul(int granul) { m_hb.SetGranul(granul); }

  void Prealloc(int sz) { m_hb.Prealloc(sz); }



  // endian-management stuff 

  static void WDL_Queue__bswap_buffer(void *buf, int len) // poorly named! only bswaps on BE, deprecated, use wdl_memcpy_le()
  {
    wdl_memcpy_le(buf,buf,1,len);
  }

  // older API of static functions (that ended up 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)
  {
    void *w = Add(NULL, sizeof(T));
    if (WDL_NORMALLY(w != NULL) && val != NULL)
      wdl_memcpy_le(w, val, 1, sizeof(T));
  }
  void AddDataToLE(const void *data, int datasize, int unitsize)
  {
    char *w = (char *)Add(NULL,datasize);
    if (WDL_NOT_NORMALLY(w == NULL)) return;

    if (WDL_NOT_NORMALLY(unitsize<1)) unitsize=1;
    WDL_ASSERT((datasize % unitsize) == 0);
    if (data) wdl_memcpy_le(w,data,datasize/unitsize,unitsize);
  }


  // NOTE: these thrash the contents of the queue if on BE systems, do not use if you
  // will rewind etc. better to use Get() and wdl_memcpy_le().
  template<class T> T *GetTFromLE(T* val=0)
  {
    T *p = GetT(val);
    if (p)
    {
      wdl_memcpy_le(p,p,1,sizeof(T));
      if (val) *val = *p;
    }
    return p;
  }

  void *GetDataFromLE(int datasize, int unitsize)
  {
    void *data=Get(datasize);
    WDL_ASSERT((datasize % unitsize) == 0);
    if (data && WDL_NORMALLY(unitsize>0)) wdl_memcpy_le(data,data,datasize/unitsize,unitsize);
    return data;
  }


private:
  WDL_HeapBuf m_hb;
  int m_pos;
public:
  int __pad; // keep 8 byte aligned
};

template <class T> class WDL_TypedQueue
{
public:
  WDL_TypedQueue() : m_hb(4096), m_pos(0) { }
  ~WDL_TypedQueue() { }

  T *Add(const T *buf, int len)
  {
    int olen=m_hb.GetSize();
    if (m_pos >= olen) olen=m_pos=0;
    len *= (int)sizeof(T);

    char *newbuf=(char*)m_hb.ResizeOK(olen+len,false);
    if (newbuf)
    {
      newbuf += olen;
      if (buf) memcpy(newbuf,buf,len);
    }
    return (T*) newbuf;
  }

  T *Get() const
  {
    if (m_pos >= 0 && m_pos < m_hb.GetSize()) return (T*)((char *)m_hb.Get()+m_pos);
    return NULL;
  }

  int GetSize() const
  {
    return m_pos < m_hb.GetSize() ? (m_hb.GetSize()-m_pos)/sizeof(T) : 0;
  }
  int Available() const { return GetSize(); }

  void Clear()
  {
    m_pos=0;
    m_hb.Resize(0,false);
  }

  void Advance(int cnt) 
  { 
    m_pos+=cnt*(int)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))
    {
      olen -= m_pos;
      if (olen > 0)
      {
        char *a=(char*)m_hb.Get();
        memmove(a,a+m_pos,olen);
      }
      else
      {
        olen = 0;
      }
      m_hb.Resize(olen,allocdown);
      m_pos=0;
    }
  }

  void SetGranul(int granul) { m_hb.SetGranul(granul); }
  void Prealloc(int sz) { m_hb.Prealloc(sz * sizeof(T)); }

private:
  WDL_HeapBuf m_hb;
  int m_pos;
public:
  int __pad; // keep 8 byte aligned
};

#endif