-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircular_buffer.h
88 lines (65 loc) · 2.76 KB
/
circular_buffer.h
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
/*
Copyright (C) 2013
Fabien Gaud <[email protected]>, Baptiste Lepers <[email protected]>,
Fabien Mottet <[email protected]>
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.
*/
#ifndef _CIRCULAR_BUFFER_H_
#define _CIRCULAR_BUFFER_H_
#define CIRC_BUF_ERROR -1
/*****************************************************************
*****************************************************************
* A "thread-safe lock free" circular buffer (See Note).
* Note: only with ONE producer and ONE consumer
*
* inspired from:
* -http://www.cppfrance.com/codes/BUFFER-CIRCULAIRE_41402.aspx
*
* **************************************************************
* **************************************************************/
/* **************************************************
* The structure used to manage a circular buffer.
* **************************************************/
typedef struct
{
int *buffer;
int size;
int writePos;
int readPos;
} circularBuffer;
/**************************************
* Create a new circular buffer.
*
* WARNING 1: In case of an error get and put functions returns CIRC_ERROR
* which is -1. So you can't have -1 value in aa circular buffer.
*
* WARNING 2: the size of the circular buffer is the amount of data you can effeectivelly read.
* But, notice that the circular buffer allocates size+1 cases to works fine.
*****************************************/
circularBuffer * open_circ(circularBuffer *cb, int size);
/**************************************************
* free a circular buffer.
* *************************************************/
void close_circ(circularBuffer *cb);
/**************************************************
* Get the head element from the circular buffer.
* *************************************************/
int get_circ(circularBuffer *cb);
/**************************************************
* Put an element in the circular buffer.
* ***********************************************/
int put_circ(circularBuffer *cb, int e);
/******************************************
* Printf a circular buffer.
* ****************************************/
void printfCircularBuffer(circularBuffer* cb);
#endif /*_CIRCULAR_BUFFER_H_*/