-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_buffer.c
More file actions
88 lines (77 loc) · 1.96 KB
/
Copy pathtcp_buffer.c
File metadata and controls
88 lines (77 loc) · 1.96 KB
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
#include "tcp_buffer.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
uint16_t buffer[depth][width] __attribute__((aligned(32)));
int wb = 0;
int rb = 0;
int in_use_index = 0;
int bytes_blocked_in_read = 0;
void buffer_reset() {
wb = 0;
rb = 0;
in_use_index = 0;
bytes_blocked_in_read = 0;
}
uint16_t *buffer_reserve_wr() {
if ((wb + 1) % depth == in_use_index) {
return NULL;
}
return buffer[wb];
}
uint16_t *buffer_done_wr() {
if ((wb + 1) % depth == in_use_index) {
return NULL;
}
uint16_t *buf = buffer[wb];
wb = (wb + 1) % depth;
return buf;
}
uint16_t *buffer_read() {
if (rb != wb) {
uint16_t *buf = buffer[rb];
rb = (rb + 1) % depth;
bytes_blocked_in_read += width;
return buf;
}
return NULL;
}
void buffer_release_no_blocked_update() { // TODO remove duplicate code
if (in_use_index == rb) {
return;
}
in_use_index = (in_use_index + 1) % depth;
}
void buffer_release() {
if (in_use_index == rb) {
return;
}
in_use_index = (in_use_index + 1) % depth;
bytes_blocked_in_read -= width;
if (bytes_blocked_in_read < 0) {
bytes_blocked_in_read = 0;
}
}
void buffer_release_bytes(int byte_count) {
if (byte_count <= 0 || bytes_blocked_in_read <= 0) {
bytes_blocked_in_read = 0;
return;
}
while(byte_count >= width) {
byte_count -= width;
buffer_release();
}
if ((bytes_blocked_in_read - byte_count) % width == 0 && bytes_blocked_in_read > 0) {
bytes_blocked_in_read -= byte_count;
buffer_release_no_blocked_update();
}
else {
bytes_blocked_in_read -= byte_count;
}
}
bool buffer_is_empty() { return wb == rb; }
bool buffer_is_full() { return (wb + 1) % depth == rb; }
int buffer_get_write_index() { return wb; }
int buffer_get_read_index() { return rb; }
int buffer_get_busy_index() { return in_use_index; }
int buffer_get_blocked_bytes() {return bytes_blocked_in_read; }