Contiki-NG
Loading...
Searching...
No Matches
ringbufindex.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, SICS Swedish ICT.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * Header file for the ringbufindex library
36 * \author
37 * Simon Duquennoy <simonduq@sics.se>
38 */
39
40#ifndef RINGBUFINDEX_H_
41#define RINGBUFINDEX_H_
42
43#include "contiki.h"
44
45struct ringbufindex {
46 uint8_t mask;
47 /* These must be 8-bit quantities to avoid race conditions. */
48 uint8_t put_ptr, get_ptr;
49};
50
51/**
52 * \brief Initialize a ring buffer. The size must be a power of two
53 * \param r Pointer to ringbufindex
54 * \param size Size of ring buffer
55 */
56void ringbufindex_init(struct ringbufindex *r, uint8_t size);
57
58/**
59 * \brief Put one element to the ring buffer
60 * \param r Pointer to ringbufindex
61 * \retval 0 Failure; the ring buffer is full
62 * \retval 1 Success; an element is added
63 */
64int ringbufindex_put(struct ringbufindex *r);
65
66/**
67 * \brief Check if there is space to put an element.
68 * \param r Pinter to ringbufindex
69 * \retval >= 0 The index where the next element is to be added.
70 * \retval -1 Failure; the ring buffer is full
71 */
72int ringbufindex_peek_put(const struct ringbufindex *r);
73
74/**
75 * \brief Remove the first element and return its index
76 * \param r Pinter to ringbufindex
77 * \retval >= 0 The index of the first element
78 * \retval -1 No element in the ring buffer
79 */
80int ringbufindex_get(struct ringbufindex *r);
81
82/**
83 * \brief Return the index of the first element which will be removed if calling
84 * ringbufindex_get.
85 * \param r Pinter to ringbufindex
86 * \retval >= 0 The index of the first element
87 * \retval -1 No element in the ring buffer
88 */
89int ringbufindex_peek_get(const struct ringbufindex *r);
90
91/**
92 * \brief Return the ring buffer size
93 * \param r Pinter to ringbufindex
94 * \return The size of the ring buffer
95 */
96static inline int
97ringbufindex_size(const struct ringbufindex *r)
98{
99 return r->mask + 1;
100}
101
102/**
103 * \brief Return the number of elements currently in the ring buffer.
104 * \param r Pinter to ringbufindex
105 * \return The number of elements in the ring buffer
106 */
107static inline int
108ringbufindex_elements(const struct ringbufindex *r)
109{
110 return (r->put_ptr - r->get_ptr) & r->mask;
111}
112
113/**
114 * \brief Is the ring buffer full?
115 * \retval 0 Not full
116 * \retval 1 Full
117 */
118static inline int
119ringbufindex_full(const struct ringbufindex *r)
120{
121 return ((r->put_ptr - r->get_ptr) & r->mask) == r->mask;
122}
123
124/**
125 * \brief Is the ring buffer empty?
126 * \retval 0 Not empty
127 * \retval 1 Empty
128 */
129static inline int
130ringbufindex_empty(const struct ringbufindex *r)
131{
132 return ringbufindex_elements(r) == 0;
133}
134
135#endif /* RINGBUFINDEX_H_ */
static int ringbufindex_size(const struct ringbufindex *r)
Return the ring buffer size.
int ringbufindex_peek_get(const struct ringbufindex *r)
Return the index of the first element which will be removed if calling ringbufindex_get.
static int ringbufindex_elements(const struct ringbufindex *r)
Return the number of elements currently in the ring buffer.
void ringbufindex_init(struct ringbufindex *r, uint8_t size)
Initialize a ring buffer.
int ringbufindex_put(struct ringbufindex *r)
Put one element to the ring buffer.
int ringbufindex_get(struct ringbufindex *r)
Remove the first element and return its index.
static int ringbufindex_full(const struct ringbufindex *r)
Is the ring buffer full?
int ringbufindex_peek_put(const struct ringbufindex *r)
Check if there is space to put an element.
static int ringbufindex_empty(const struct ringbufindex *r)
Is the ring buffer empty?