-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorringbuffer.c
executable file
·59 lines (52 loc) · 1.02 KB
/
mirrorringbuffer.c
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
/*
* mirrorringbuffer.c
*
* Created on: 2013-7-17
*/
#include <stdio.h>
#include <stdlib.h>
#include "mirrorringbuffer.h"
void
cbInit(CircularBuffer *cb, int size)
{
cb->size = size;
cb->start = 0;
cb->end = 0;
cb->s_msb = 0;
cb->e_msb = 0;
cb->elems = (ElemType *) calloc(cb->size, sizeof(ElemType));
}
int
cbIsFull(CircularBuffer *cb)
{
return cb->end == cb->start && cb->e_msb != cb->s_msb;
}
int
cbIsEmpty(CircularBuffer *cb)
{
return cb->end == cb->start && cb->e_msb == cb->s_msb;
}
void
cbIncr(CircularBuffer *cb, int *p, int *msb)
{
*p = *p + 1;
if (*p == cb->size)
{
*msb ^= 1;
*p = 0;
}
}
void
cbWrite(CircularBuffer *cb, ElemType *elem)
{
cb->elems[cb->end] = *elem;
if (cbIsFull(cb)) /* full, overwrite moves start pointer */
cbIncr(cb, &cb->start, &cb->s_msb);
cbIncr(cb, &cb->end, &cb->e_msb);
}
void
cbRead(CircularBuffer *cb, ElemType *elem)
{
*elem = cb->elems[cb->start];
cbIncr(cb, &cb->start, &cb->s_msb);
}