blob: 515652fb0cee20e6285cbf935648c63955d89688 (
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
|
// Copyright (c) 2000-2001 Brad Hughes <bhughes@trolltech.com>
//
// Use, modification and distribution is allowed without limitation,
// warranty, or liability of any kind.
//
#include "recycler.h"
#include "constants.h"
#include "buffer.h"
Recycler::Recycler ( unsigned int sz )
: add_index ( 0 ), done_index ( 0 ), current_count ( 0 )
{
buffer_count = ( sz / Buffer::size() );
if ( buffer_count < 1 )
{
buffer_count = 1;
}
buffers = new Buffer*[buffer_count];
for ( unsigned int i = 0; i < buffer_count; i ++ )
{
buffers[i] = new Buffer;
}
}
Recycler::~Recycler()
{
for ( unsigned int i = 0; i < buffer_count; i++ )
{
delete buffers[i];
buffers[i] = 0;
}
delete [] buffers;
}
bool Recycler::full() const
{
return current_count == buffer_count;
}
bool Recycler::empty() const
{
return current_count == 0;
}
int Recycler::available() const
{
return buffer_count - current_count;
}
int Recycler::used() const
{
return current_count;
}
Buffer *Recycler::get()
{
if ( full() )
return 0;
return buffers[add_index];
}
void Recycler::add()
{
add_index = ++add_index % buffer_count;
current_count++;
}
Buffer *Recycler::next()
{
return buffers[done_index];
}
void Recycler::done()
{
done_index = ++done_index % buffer_count;
current_count--;
}
void Recycler::clear()
{
add_index = done_index = current_count = 0;
}
unsigned int Recycler::size() const
{
return buffer_count * Buffer::size();
}
|