-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuffering.h
207 lines (200 loc) · 9.32 KB
/
Buffering.h
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
/*
* File: Buffering.h
* Author: Kai Niessen
*
* Created on January 31, 2016, 4:15 PM
*/
#ifndef BUFFERING_H
#define BUFFERING_H
/**
* Simple class extending an integer to roll over when reaching boundaries
*/
class rotor {
public:
rotor(int size, int initialValue = 0) {
fMax = size-1;
fValue = initialValue;
}
rotor(rotor& other) {
fMax = other.fMax;
fValue = other.fValue;
}
operator int() { return fValue; }
rotor& operator ++() { if (fValue==fMax) fValue=0; else fValue++; return *this;}
rotor& operator ++(int) { rotor* tmp=new rotor(*this); if (fValue==fMax) fValue=0; else fValue++; return *tmp;}
rotor& operator --() { if (fValue==0) fValue=fMax; else fValue--; return *this;}
rotor& operator --(int) { rotor* tmp=new rotor(*this); if (fValue==0) fValue=fMax; else fValue--; return *tmp;}
rotor& operator =(int i) { fValue = i % (fMax+1);}
int operator =(rotor& r) { return r.fValue;}
int operator -(rotor& other) {return (fValue-other.fValue + fMax + 1) % (fMax + 1);}
bool operator ==(int i) { return fValue==i;}
bool operator !=(int i) { return fValue!=i;}
bool operator <(int i) { return fValue<i;}
bool operator >(int i) { return fValue>i;}
private:
inline void setValue(int i) { fValue = i; }
inline int getValue() { return fValue; }
int fValue;
int fMax;
};
class Buffer : public BLocker {
public:
#define limit(requested, max) (requested=(requested > max) ? max : requested)
friend class BufferGroup;
typedef enum {
EMPTY,
FILLING,
FILLED,
READING
} bufferState;
Buffer(size_t size, int index)
: BLocker("buffer"),
fIndex(index),
fUsed(0),
fSize(size),
fData((char*)malloc(size)),
fState(EMPTY)
{ }
~Buffer() { free(fData); }
// Caller handles locking!
inline size_t fillable() { return fSize - fUsed; }
inline char* fillPos() { return fData + fUsed; }
inline bool isFull() { return fUsed == fSize; }
inline bufferState state() { return fState; }
void setState(bufferState state) {
if (state == EMPTY) fUsed = 0;
fState = state;
}
inline char* data() { return fData; }
inline size_t readable() { return fUsed; }
size_t fill(char* data, size_t size) {
if (limit(size, fillable())) {
memcpy(fillPos(), data, size);
fUsed += size;
}
return size;
}
size_t read(char* data, size_t size) {
if (limit(size, readable())) {
memcpy(data, fData, size);
};
return size;
}
protected:
bufferState fState;
size_t fSize;
size_t fUsed;
char* fData;
int fIndex;
};
typedef Buffer* pBuffer;
class BufferGroup : public BLocker {
public:
#define none -1
typedef bool (*LimitFunc)(void* cookie, float ratio, bool isOk);
BufferGroup(int numBuffers, size_t size)
: BLocker("buffers"),
firstEmpty(numBuffers, 0),
firstFilled(numBuffers, none),
fNumBuffers(numBuffers)
{
buffers = new pBuffer[numBuffers];
for (int i = 0; i < numBuffers; i++)
buffers[i] = new Buffer(size, i);
}
~BufferGroup() {
for (int i = 0; i < fNumBuffers; i++) {
delete buffers[i];
}
delete buffers;
}
inline int IndexOf(Buffer* buffer) {
return buffer->fIndex;
}
Buffer* RequestForFilling(Buffer* previous = NULL) {
Buffer* result = NULL;
Lock();
if (previous) {
if (previous->CountLocks() == 0) previous->Lock();
previous->fState = Buffer::FILLED;
if (firstFilled == none) {
firstFilled = IndexOf(previous);
}
previous->Unlock();
}
if (firstEmpty != none) {
result = buffers[firstEmpty++];
result->fState = Buffer::FILLING;
if (buffers[firstEmpty]->fState != Buffer::EMPTY)
firstEmpty = none;
}
Unlock();
return result;
}
Buffer* RequestForReading(Buffer* previous = NULL) {
Buffer* result = NULL;
Lock();
if (previous) {
if (previous->CountLocks() == 0) previous->Lock();
previous->setState(Buffer::EMPTY);
if (firstEmpty == none)
firstEmpty = IndexOf(previous);
previous->Unlock();
}
if (firstFilled != none) {
result = buffers[firstFilled++];
result->fState = Buffer::READING;
if (buffers[firstFilled]->fState != Buffer::FILLED)
firstFilled = none;
}
Unlock();
return result;
}
void ReturnBuffer(Buffer* previous) {
if (previous->CountLocks() == 0) previous->Lock();
if (previous->fState == Buffer::FILLING && previous->fUsed == 0)
previous->setState(Buffer::EMPTY);
Lock();
switch (previous->fState) {
case Buffer::READING :
case Buffer::EMPTY:
previous->setState(Buffer::EMPTY);
if (firstEmpty == none) firstEmpty = IndexOf(previous);
break;
case Buffer::FILLING:
previous->fState = Buffer::FILLED;
case Buffer::FILLED:
if (firstFilled == none) firstFilled = previous->fIndex;
break;
}
Unlock();
previous->Unlock();
}
size_t TotalUsed() {
size_t result = 0;
for (int i=0; i<fNumBuffers; i++) {
Buffer* b=buffers[i];
if (b->fState == Buffer::FILLED || b->fState == Buffer::FILLING) {
result += b->fUsed;
}
}
return result;
}
size_t TotalCapacity() {
return fNumBuffers * buffers[0]->fSize;
}
float FillRatio() {
if (firstFilled == none)
return 0.0f;
else if (firstEmpty == none)
return 1.0f;
else
return float(firstEmpty - firstFilled) / fNumBuffers;
}
private:
int fNumBuffers;
pBuffer* buffers;
rotor firstEmpty;
rotor firstFilled;
};
#endif /* BUFFERING_H */