-
Notifications
You must be signed in to change notification settings - Fork 12
/
ringbuf.h
132 lines (117 loc) · 4.69 KB
/
ringbuf.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
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
/*
* Copyright (c) 2022, smartmx - [email protected]
* Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
*
* The ring buffer library implements ring (circular) buffer where
* bytes can be read and written independently. A ring buffer is
* particularly useful in device drivers where data can come in
* through interrupts.
*
*/
#ifndef _RINGBUF_H_
#define _RINGBUF_H_
/* add typedefinitions of your chips. */
#include "stdint.h"
/* access to RINGBUF_INDEX_CONF_TYPE must be atomic. */
#ifndef RINGBUF_INDEX_CONF_TYPE
#define RINGBUF_INDEX_CONF_TYPE uint8_t
#endif
#ifndef CC_ACCESS_NOW
#define CC_ACCESS_NOW(type, variable) (*(volatile type *)&(variable))
#endif
/**
* \brief Structure that holds the state of a ring buffer.
*
* This structure holds the state of a ring buffer. The
* actual buffer needs to be defined separately. This
* struct is an opaque structure with no user-visible
* elements.
*
*/
struct ringbuf
{
uint8_t *data;
RINGBUF_INDEX_CONF_TYPE mask;
/* XXX these must be 8-bit quantities to avoid race conditions. */
RINGBUF_INDEX_CONF_TYPE put_ptr, get_ptr;
};
/**
* \brief Initialize a ring buffer
* \param r A pointer to a struct ringbuf to hold the state of the ring buffer
* \param a A pointer to an array to hold the data in the buffer
* \param size_power_of_two The size of the ring buffer, which must be a power of two
*
* This function initiates a ring buffer. The data in the
* buffer is stored in an external array, to which a
* pointer must be supplied. The size of the ring buffer
* must be a power of two and cannot be larger than 128
* bytes.
*
*/
void ringbuf_init(struct ringbuf *r, uint8_t *a, RINGBUF_INDEX_CONF_TYPE size_power_of_two);
/**
* \brief Insert a byte into the ring buffer
* \param r A pointer to a struct ringbuf to hold the state of the ring buffer
* \param c The byte to be written to the buffer
* \return Non-zero if there data could be written, or zero if the buffer was full.
*
* This function inserts a byte into the ring buffer. It
* is safe to call this function from an interrupt
* handler.
*
*/
int ringbuf_put(struct ringbuf *r, uint8_t c);
/**
* \brief Get a byte from the ring buffer
* \param r A pointer to a struct ringbuf to hold the state of the ring buffer
* \return The data from the buffer, or -1 if the buffer was empty
*
* This function removes a byte from the ring buffer. It
* is safe to call this function from an interrupt
* handler.
*
*/
int ringbuf_get(struct ringbuf *r);
/**
* \brief Get the size of a ring buffer
* \param r A pointer to a struct ringbuf to hold the state of the ring buffer
* \return The size of the buffer.
*/
int ringbuf_size(struct ringbuf *r);
/**
* \brief Get the number of elements currently in the ring buffer
* \param r A pointer to a struct ringbuf to hold the state of the ring buffer
* \return The number of elements in the buffer.
*/
int ringbuf_elements(struct ringbuf *r);
#endif /* RINGBUF_H_ */
/** @}*/
/** @}*/