-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircbuf.c
75 lines (62 loc) · 1.59 KB
/
circbuf.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Circular buffer example, keeps one slot open */
#include <stdlib.h>
#include <stdio.h>
#include "circbuf.h"
/* Инициализация буфера, если его нет! */
int cb_init(CircularBuffer * cb, int size)
{
cb->size = size + 1; /* include empty elem */
cb->start = 0;
cb->end = 0;
if (cb->elems == NULL) {
cb->elems = (ElemType *) calloc(cb->size, sizeof(ElemType));
if (cb->elems == NULL) /* Не смог выделить память */
return -1;
}
return 0;
}
/* Удалить буфер, если он существует! */
void cb_free(CircularBuffer * cb)
{
if (cb->elems != NULL) {
free(cb->elems); /* OK if null */
cb->elems = 0;
}
}
/**
* Буфер полный?
*/
int cb_is_full(CircularBuffer * cb)
{
return (cb->end + 1) % cb->size == cb->start;
}
/**
* Буфер пустой?
*/
int cb_is_empty(CircularBuffer * cb)
{
return cb->end == cb->start;
}
/**
* Очистить буфер
*/
void cb_clear(CircularBuffer * cb)
{
cb->start = 0;
cb->end = 0;
}
/* Write an element, overwriting oldest element if buffer is full. App can
choose to avoid the overwrite by checking cbIsFull(). */
void cb_write(CircularBuffer * cb, ElemType * elem)
{
cb->elems[cb->end] = *elem;
cb->end = (cb->end + 1) % cb->size;
if (cb->end == cb->start)
cb->start = (cb->start + 1) % cb->size; /* full, overwrite */
}
/* Read oldest element. App must ensure !cbIsEmpty() first. */
void cb_read(CircularBuffer * cb, ElemType * elem)
{
*elem = cb->elems[cb->start];
cb->start = (cb->start + 1) % cb->size;
}