Contiki-NG
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 
45 struct 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  */
56 void 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  */
64 int 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  */
72 int 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  */
80 int 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  */
89 int 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  */
96 int ringbufindex_size(const struct ringbufindex *r);
97 
98 /**
99  * \brief Return the number of elements currently in the ring buffer.
100  * \param r Pinter to ringbufindex
101  * \return The number of elements in the ring buffer
102  */
103 int ringbufindex_elements(const struct ringbufindex *r);
104 
105 /**
106  * \brief Is the ring buffer full?
107  * \retval 0 Not full
108  * \retval 1 Full
109  */
110 int ringbufindex_full(const struct ringbufindex *r);
111 
112 /**
113  * \brief Is the ring buffer empty?
114  * \retval 0 Not empty
115  * \retval 1 Empty
116  */
117 int ringbufindex_empty(const struct ringbufindex *r);
118 
119 #endif /* __RINGBUFINDEX_H__ */
void ringbufindex_init(struct ringbufindex *r, uint8_t size)
Initialize a ring buffer.
Definition: ringbufindex.c:50
int ringbufindex_full(const struct ringbufindex *r)
Is the ring buffer full?
Definition: ringbufindex.c:140
int ringbufindex_peek_get(const struct ringbufindex *r)
Return the index of the first element which will be removed if calling ringbufindex_get.
Definition: ringbufindex.c:115
int ringbufindex_empty(const struct ringbufindex *r)
Is the ring buffer empty?
Definition: ringbufindex.c:146
int ringbufindex_elements(const struct ringbufindex *r)
Return the number of elements currently in the ring buffer.
Definition: ringbufindex.c:134
int ringbufindex_peek_put(const struct ringbufindex *r)
Check if there is space to put an element.
Definition: ringbufindex.c:78
int ringbufindex_put(struct ringbufindex *r)
Put one element to the ring buffer.
Definition: ringbufindex.c:58
int ringbufindex_get(struct ringbufindex *r)
Remove the first element and return its index.
Definition: ringbufindex.c:90
int ringbufindex_size(const struct ringbufindex *r)
Return the ring buffer size.
Definition: ringbufindex.c:128