blob: 79e2cc34332ee74b8a5bfedff783c698691d6b13 (
plain)
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
|
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
#include <gras/buffer_queue.hpp>
#include <gras_impl/debug.hpp>
#include <boost/circular_buffer.hpp>
using namespace gras;
struct BufferQueuePool : BufferQueue
{
BufferQueuePool(const SBufferConfig &config, const size_t num):
_token(config.token), //save config, its holds token
_queue(boost::circular_buffer<SBuffer>(num))
{
//NOP
}
~BufferQueuePool(void)
{
_token.reset();
_queue.clear();
}
SBuffer &front(void)
{
ASSERT(not _queue.empty());
ASSERT(_queue.front());
return _queue.front();
}
void pop(void)
{
ASSERT(not _queue.empty());
SBuffer &buff = _queue.front();
//This little half consumed metric lets us keep using
//the same buffer if its only been partially consumed.
//Input buffer stitching will rejoin contiguous memory.
if (buff.offset > buff.get_actual_length()/2)
{
buff.reset(); //dont hold ref
_queue.pop_front();
}
else
{
//enables buffer stitching on pool buffers
buff.last = buff.get();
}
}
void push(const SBuffer &buff)
{
//is it my buffer? otherwise dont keep it
if GRAS_UNLIKELY(buff->config.token.lock() != _token) return;
//should never get a buffer from a circ queue
ASSERT(buff.get_user_index() == size_t(~0));
_queue.push_back(buff);
}
bool empty(void) const
{
return _queue.empty();
}
SBufferToken _token;
boost::circular_buffer<SBuffer> _queue;
};
BufferQueueSptr BufferQueue::make_pool(
const SBufferConfig &config,
const size_t num_buffs
){
BufferQueueSptr queue(new BufferQueuePool(config, num_buffs));
for (size_t i = 0; i < num_buffs; i++)
{
SBuffer buff(config);
std::memset(buff.get_actual_memory(), 0, buff.get_actual_length());
//buffer derefs and returns to this queue thru token callback
}
return queue;
}
|