/
CBuffer.cpp
347 lines (315 loc) · 6.47 KB
/
CBuffer.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "stdafx.h"
#include <list>
#include <boost/thread.hpp>
#define INITGUID
#include "CBuffer.h"
using namespace std;
// base class for the buffer
class CAbstractBuffer
{
public:
UInt32 read(void *data, UInt32 size) {assert(false); return 0;}
UInt32 write(void* data, UInt32 size) {assert(false); return 0;}
UInt32 size() {assert(false); return 0;}
};
// option implementation 1: a fixed-size circular buffer
class CCircularBuffer : public CAbstractBuffer
{
public:
const static size_t _capacity=1024*1024;
char m_buf[_capacity];
size_t m_size, m_start;
CCircularBuffer()
{
m_start = m_size = 0;
}
~CCircularBuffer()
{
assert(m_size==0);
}
UInt32 size()
{
return m_size;
}
UInt32 read(void *data, UInt32 size)
{
if (size>m_size) size=m_size;
if (m_start+size > _capacity)
{
size_t t = _capacity - m_start;
memcpy(data, m_buf+m_start, t);
memcpy((char*)data+t, m_buf, size-t);
m_start = size-t;
}
else
{
memcpy(data, m_buf+m_start, size);
m_start+=size;
}
m_size-=size;
return size;
}
UInt32 write(const void *data, UInt32 size)
{
size_t room=_capacity-m_size;
if (size>room) size=room;
size_t end = (m_start+m_size) % _capacity;
if (end+size>_capacity)
{
size_t t=_capacity-end;
memcpy(m_buf+end, data, t);
memcpy(m_buf, (const char*)data+t, size-t);
//end=size-t;
}
else
{
memcpy(m_buf+end, data, size);
//end+=size;
}
m_size+=size;
return size;
}
};
struct record
{
size_t size;
char* buf;
};
// a dynamic-sized queue buffer
class CQBuffer : public CAbstractBuffer
{
public:
std::list<record> m_queue;
size_t m_offset, m_size;
CQBuffer()
{
m_offset = m_size = 0;
}
UInt32 size()
{
return m_size;
}
UInt32 read(void *data, UInt32 size)
{
assert(m_queue.front().size > m_offset);
size_t left = m_queue.front().size - m_offset;
if (left>size)
{
memcpy(data, m_queue.front().buf+m_offset, size);
m_offset+=size;
m_size-=size;
return size;
}
else
{
memcpy(data, m_queue.front().buf+m_offset, left);
delete m_queue.front().buf;
m_queue.pop_front();
m_offset=0;
m_size-=left;
return left;
}
}
UInt32 write(const void* data, UInt32 size)
{
record rec;
rec.size=size;
rec.buf=new char[size];
memcpy(rec.buf, data, size);
m_queue.push_back(rec);
m_size += size;
return size;
}
};
// add synchornization
class CSynchornizedBuffer
{
public:
typedef CQBuffer CBuffer;
//typedef CCircularBuffer CBuffer;
typedef boost::mutex::scoped_lock Lock;
const static size_t _capacity=1024*1024;
bool m_reseting, m_closing, m_sealed;
Int64 m_total_size, m_written_size, m_read_size;
size_t m_ref_count;
CBuffer m_buffer;
CSynchornizedBuffer() : m_barrier(2)
{
m_ref_count = 1;
m_total_size = m_read_size = m_written_size = 0;
m_reseting = m_closing = false;
m_sealed = true;
}
~CSynchornizedBuffer()
{
}
size_t add_ref()
{
Lock lock(m_mutex);
return ++m_ref_count;
}
size_t dec_ref()
{
Lock lock(m_mutex);
return --m_ref_count;
}
HRESULT read(void *data, UInt32 size, UInt32 *processedSize)
{
Lock lock(m_mutex);
if (m_read_size==m_total_size)
{
*processedSize = 0;
m_sealed=true;
return S_OK;
}
while (m_buffer.size()==0)
m_buffer_not_empty.wait(lock);
try
{
*processedSize = m_buffer.read(data, size);
m_read_size += *processedSize;
//printf("CBuffer[%p]: %u bytes read\n", this, *processedSize);
}
catch(...)
{
assert(false);
}
m_buffer_not_full.notify_all();
lock.unlock();
//if (m_read_size==m_total_size)
// finalize();
return S_OK;
}
HRESULT write(const void *data, UInt32 size, UInt32 *processedSize)
{
Lock lock(m_mutex);
while (m_buffer.size()>_capacity)
m_buffer_not_full.wait(lock);
try
{
*processedSize = m_buffer.write(data, size);
m_written_size += *processedSize;
//printf("CBuffer[%p]: %u bytes written\n", this, *processedSize);
}
catch(...)
{
assert(false);
}
m_buffer_not_empty.notify_all();
lock.unlock();
//if (m_written_size==m_total_size)
// finalize();
return S_OK;
}
Int64 total_size()
{
return m_total_size;
}
void total_size(Int64 size)
{
m_total_size = size;
m_read_size = m_written_size = 0;
}
HRESULT flush()
{
Lock lock(m_mutex);
while (!m_sealed)
m_buffer_not_full.wait(lock);
m_sealed=false;
return S_OK;
}
void finalize()
{
m_barrier.wait();
}
void wait_for_read()
{
Lock lock(m_mutex);
size_t old_size=m_buffer.size();
while (old_size==m_buffer.size())
m_buffer_not_full.wait(lock);
}
void wait_for_write()
{
Lock lock(m_mutex);
size_t old_size=m_buffer.size();
while (old_size==m_buffer.size())
m_buffer_not_empty.wait(lock);
}
boost::condition_variable m_buffer_not_empty;
boost::condition_variable m_buffer_not_full;
boost::barrier m_barrier;
boost::mutex m_mutex;
};
// interface
class CTwiceBuffer : public IPipeBuffer
{
public:
CSynchornizedBuffer* m_buffer;
CTwiceBuffer()
{
m_buffer=new CSynchornizedBuffer;
}
CTwiceBuffer(CSynchornizedBuffer* buffer)
{
m_buffer=buffer;
m_buffer->add_ref();
}
virtual ~CTwiceBuffer()
{
//m_buffer->finalize();
if (m_buffer->dec_ref()==0)
delete m_buffer;
}
virtual void wait_for_read()
{
m_buffer->wait_for_read();
}
virtual void wait_for_write()
{
m_buffer->wait_for_write();
}
virtual IPipeBuffer* clone()
{
return new CTwiceBuffer(m_buffer);
}
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)
{
HRESULT hr=m_buffer->read(data, size, processedSize);
//m_buffer->wait_for_write();
return hr;
}
STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)
{
HRESULT hr=m_buffer->write(data, size, processedSize);
//m_buffer->wait_for_read();
return hr;
}
STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
{
assert(false);
return S_OK;
}
STDMETHOD(GetSize)(UInt64 *size)
{
*size = m_buffer->total_size();
return S_OK;
}
STDMETHOD(SetSize)(Int64 newSize)
{
m_buffer->total_size(newSize);
return S_OK;
}
STDMETHOD(Flush)()
{
return m_buffer->flush();
}
virtual void barrier()
{
m_buffer->finalize();
}
};
IPipeBuffer* new_pipe_buffer()
{
return new CTwiceBuffer;
}