-
Notifications
You must be signed in to change notification settings - Fork 1
/
circular_buffer.c
137 lines (103 loc) · 2.73 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright (C) 2013
Fabien Gaud <fgaud@sfu.ca>, Baptiste Lepers <baptiste.lepers@inria.fr>,
Fabien Mottet <fabien.mottet@inria.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 or later, as published by the Free Software Foundation.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "circular_buffer.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
/******************open_circ********************/
circularBuffer * open_circ(circularBuffer *cb, int size){
if(cb == NULL){
return NULL;
}
cb->buffer = (int *)malloc((size+1) * sizeof(int));
if(cb->buffer == NULL)
{
return NULL;
}
else
{
bzero(cb->buffer, (size+1) * sizeof(int));
cb->readPos = 0;
cb->writePos = 0;
cb->size = size + 1;
}
return(cb);
}
void close_circ(circularBuffer *cb){
if(cb == NULL){
return;
}
free(cb->buffer);
cb->readPos = 0;
cb->writePos = 0;
cb->size = 0;
}
/******************get_circ********************/
int get_circ(circularBuffer *cb){
int retValue;
//empty buffer
if(cb->readPos == cb->writePos)
return(CIRC_BUF_ERROR);
retValue = cb->buffer[cb->readPos];
//Increment readPos and return the value pointed by it.
if(++(cb->readPos)==cb->size)
cb->readPos = 0;
return retValue;
}
/******************put_circ********************/
int put_circ(circularBuffer *cb, int e){
int p;
//get where we will write
p = cb->writePos + 1;
//check if it's a valid position to write to
if (p == cb->size)
{
p = 0;
}
if (p == cb->readPos)
{
return(CIRC_BUF_ERROR);
}
//If position is ok, we write the data and writePos goes to the next step.
cb->buffer[cb->writePos] = e;
cb->writePos = p;
return (e);
}
/******************printfCircularBuffer********************/
void printfCircularBuffer(circularBuffer* cb ){
int i;
int size = (int)cb->size;
printf("circBuffer-> size:%d, read_pos:%d, write_pos:%d\n", size, cb->readPos, cb->writePos);
for(i=0 ; i< (size); i++){
printf("\t|%d", cb->buffer[i]);
}
printf("\t|\n");
for(i=0 ; i< (size); i++){
printf("\t");
if( i == cb->readPos && i == cb->writePos){
printf(" rw");
}
else if( i == cb->writePos){
printf(" w");
}
else if( i == cb->readPos){
printf(" r");
}
}
printf("\n");
}
//EOF