-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.c
More file actions
57 lines (51 loc) · 1.53 KB
/
window.c
File metadata and controls
57 lines (51 loc) · 1.53 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
#include "window.h"
void *CreateWindow(int32_t winSize, uint32_t seqNum) {
Window *window = (Window *) calloc(1, sizeof(Window));
window->segment = (Segment *) calloc(winSize, sizeof(Segment));
window->winSize = winSize;
window->start = seqNum % winSize;
window->startSeq = seqNum;
return window;
}
int isValidSeqNum(void *window, uint32_t seqNum) {
Window *w = (Window *) window;
uint32_t endSeq = w->startSeq + w->winSize;
if (w->startSeq <= seqNum && seqNum < endSeq) {
return 1;
}
return 0;
}
int isClosed(void *window) {
Window *w = (Window *) window;
if (w->winSize == w->curSize) {
return 1;
}
return 0;
}
void AddSegment(void *window, uint8_t *buf, uint32_t bufLen, uint32_t seqNum) {
Window *w = (Window *) window;
int i = seqNum % w->winSize;
if (!isClosed(w) && isValidSeqNum(w, seqNum)) {
(w->segment + i)->segSize = bufLen;
(w->segment + i)->seqNum = seqNum;
memcpy((w->segment + i)->buf, buf, bufLen);
w->curSize++;
(w->segment + i)->hasData = 1;
}
}
void MoveWindow(void *window, uint32_t seqNum) {
Window *w = (Window *) window;
if (seqNum <= w->startSeq + w->curSize) {
while(w->startSeq < seqNum) {
(w->segment + w->start)->hasData = 0;
w->start = (w->start + 1) % w->winSize;
w->startSeq++;
w->curSize--;
}
}
}
void FreeWindow(void *window) {
Window *w = (Window *) window;
free(w->segment);
free(w);
}