forked from nitsanw/javanetperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifobuffer.cc
80 lines (66 loc) · 2.42 KB
/
fifobuffer.cc
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
#include "buffer.h"
#include <cassert>
#include <unistd.h>
using std::vector;
FifoBuffer::FifoBuffer() : read_position_(0), write_position_(0) {
blocks_.push_back(new Block());
}
FifoBuffer::~FifoBuffer() {
for (size_t i = 0; i < blocks_.size(); ++i) {
delete blocks_[i];
}
}
void* FifoBuffer::getWritePosition(size_t* max_length) {
Block* current = blocks_.back();
int remaining = static_cast<int>(sizeof(current->data)) - write_position_;
assert(remaining >= 0);
if (remaining == 0) {
current = new Block();
blocks_.push_back(current);
write_position_ = 0;
remaining = sizeof(current->data);
}
char* end = current->data + write_position_;
*max_length = remaining;
return end;
}
void FifoBuffer::advanceWritePosition(size_t length) {
write_position_ += length;
assert(write_position_ <= static_cast<int>(sizeof(blocks_.back()->data)));
}
int FifoBuffer::read(void* buffer, size_t length) {
int bytes_copied = 0;
char* current = reinterpret_cast<char*>(buffer);
while (bytes_copied < static_cast<int>(length) && hasBytesAvailable()) {
// Get the number of bytes remaining in the next block
int remaining_in_block;
if (blocks_.size() == 1) {
remaining_in_block = write_position_ - read_position_;
} else {
assert(blocks_.size() > 1);
remaining_in_block = sizeof(blocks_.front()->data) - read_position_;
}
assert(remaining_in_block > 0);
int to_copy = std::min(static_cast<int>(length) - bytes_copied, remaining_in_block);
memcpy(current, blocks_.front()->data + read_position_, to_copy);
current += to_copy;
read_position_ += to_copy;
bytes_copied += to_copy;
if (to_copy == remaining_in_block) {
// Consumed an entire block: remove it
if (blocks_.size() > 1) {
assert(read_position_ == sizeof(blocks_.front()->data));
delete blocks_.front();
blocks_.erase(blocks_.begin());
} else {
// Last block: just reset the counter
assert(blocks_.size() == 1);
assert(read_position_ == write_position_);
write_position_ = 0;
}
read_position_ = 0;
}
}
assert(0 <= bytes_copied && bytes_copied <= static_cast<int>(length));
return bytes_copied;
}