-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbipbuffer.cc
80 lines (75 loc) · 1.36 KB
/
bipbuffer.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 "bipbuffer.h"
BipBuffer::BipBuffer(int size_) : size(size_) {
size_a = size_b = size_r = index_a = index_b = index_r = 0;
buffer = new char[size]();
}
char* BipBuffer::reserve(int n) {
if (size_r != 0) {
// previous reservation is not committed
return nullptr;
}
int a_free = size - (index_a + size_a);
int b_free = index_a - (index_b + size_b);
if (size_b) {
if (n > b_free) {
return nullptr;
}
index_r = index_b + size_b;
size_r = n;
} else {
if (a_free >= b_free) {
if (n > a_free) {
return nullptr;
}
index_r = index_a + size_a;
size_r = n;
} else {
if (n > b_free) {
return nullptr;
}
index_r = index_b + size_b;
size_r = n;
}
}
return buffer + index_r;
}
void BipBuffer::commit(int n) {
if (n == 0) {
index_r = size_r = 0;
return;
}
if (n > size_r) {
// just commit the size reserved before
n = size_r;
}
if (size_a == 0 && size_b == 0) {
index_a = index_r;
size_a = n;
index_r = size_r = 0;
return;
}
if (index_r == index_a + size_a) {
size_a += n;
} else {
size_b += n;
}
index_r = size_r = 0;
}
char* BipBuffer::get(int &n) {
if (size_a == 0) {
n = 0;
return nullptr;
}
n = size_a;
return buffer + index_a;
}
void BipBuffer::release(int n) {
if (n >= size_a) {
index_a = index_b;
size_a = size_b;
index_b = size_b = 0;
} else {
size_a -= n;
index_a += n;
}
}