-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple_circular_buffer.c
100 lines (78 loc) · 1.99 KB
/
simple_circular_buffer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* LKM Sandbox::Rehearsal
* <https://github.com/tpiekarski/lkm-sandbox>
* ---
* Copyright 2020 Thomas Piekarski <[email protected]>
*
* This file is part of LKM Sandbox.
*
* LKM Sandbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* LKM Sandbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LKM Sandbox. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#define BUFFER_SIZE 8
static int buffer[BUFFER_SIZE];
static int buffer_length = 0;
static int index_read = 0;
static int index_write = 0;
int read(void)
{
if (buffer_length == 0) {
fprintf(stderr, "Can not read, buffer is empty, aborting.\n");
return -1;
}
int value = buffer[index_read++];
buffer_length--;
if (index_read == BUFFER_SIZE) {
index_read = 0;
}
return value;
}
void write(int value)
{
if (buffer_length == BUFFER_SIZE) {
fprintf(stderr, "Can not write, buffer is full, aborting\n");
return;
}
buffer[index_write++] = value;
buffer_length++;
if (index_write == BUFFER_SIZE) {
index_write = 0;
}
}
void print_buffer()
{
printf("Contents of buffer: ");
for (int i = 0; i < BUFFER_SIZE; i++)
printf("%i ", buffer[i]);
printf("\n");
}
int main()
{
int i = 0;
print_buffer();
printf("Writing %i values.\n", BUFFER_SIZE);
for (i = 1; i < BUFFER_SIZE + 1; i++)
write(i);
print_buffer();
printf("Reading %i values.\n", BUFFER_SIZE);
for (i = 0; i < BUFFER_SIZE; i++)
printf("%i ", read());
printf("\n");
print_buffer();
printf("Writing another single value.\n");
write(9);
print_buffer();
return 0;
}