Contiki-NG
ringbufindex.c
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 * ringbufindex library. Implements basic support for ring buffers
36 * of any type, as opposed to the os/lib/ringbuf module which
37 * is only for byte arrays. Simply returns index in the ringbuf
38 * rather than actual elements. The ringbuf size must be power of two.
39 * Like the original ringbuf, this module implements atomic put and get.
40 * \author
41 * Simon Duquennoy <simonduq@sics.se>
42 * based on Contiki's os/lib/ringbuf library by Adam Dunkels
43 */
44
45#include <string.h>
46#include "lib/ringbufindex.h"
47
48/* Initialize a ring buffer. The size must be a power of two */
49void
50ringbufindex_init(struct ringbufindex *r, uint8_t size)
51{
52 r->mask = size - 1;
53 r->put_ptr = 0;
54 r->get_ptr = 0;
55}
56/* Put one element to the ring buffer */
57int
58ringbufindex_put(struct ringbufindex *r)
59{
60 /* Check if buffer is full. If it is full, return 0 to indicate that
61 the element was not inserted.
62
63 XXX: there is a potential risk for a race condition here, because
64 the ->get_ptr field may be written concurrently by the
65 ringbufindex_get() function. To avoid this, access to ->get_ptr must
66 be atomic. We use an uint8_t type, which makes access atomic on
67 most platforms, but C does not guarantee this.
68 */
69 if(((r->put_ptr - r->get_ptr) & r->mask) == r->mask) {
70 return 0;
71 }
72 r->put_ptr = (r->put_ptr + 1) & r->mask;
73 return 1;
74}
75/* Check if there is space to put an element.
76 * Return the index where the next element is to be added */
77int
78ringbufindex_peek_put(const struct ringbufindex *r)
79{
80 /* Check if there are bytes in the buffer. If so, we return the
81 first one. If there are no bytes left, we return -1.
82 */
83 if(((r->put_ptr - r->get_ptr) & r->mask) == r->mask) {
84 return -1;
85 }
86 return r->put_ptr;
87}
88/* Remove the first element and return its index */
89int
90ringbufindex_get(struct ringbufindex *r)
91{
92 int get_ptr;
93
94 /* Check if there are bytes in the buffer. If so, we return the
95 first one and increase the pointer. If there are no bytes left, we
96 return -1.
97
98 XXX: there is a potential risk for a race condition here, because
99 the ->put_ptr field may be written concurrently by the
100 ringbufindex_put() function. To avoid this, access to ->get_ptr must
101 be atomic. We use an uint8_t type, which makes access atomic on
102 most platforms, but C does not guarantee this.
103 */
104 if(((r->put_ptr - r->get_ptr) & r->mask) > 0) {
105 get_ptr = r->get_ptr;
106 r->get_ptr = (r->get_ptr + 1) & r->mask;
107 return get_ptr;
108 } else {
109 return -1;
110 }
111}
112/* Return the index of the first element
113 * (which will be removed if calling ringbufindex_get) */
114int
115ringbufindex_peek_get(const struct ringbufindex *r)
116{
117 /* Check if there are bytes in the buffer. If so, we return the
118 first one. If there are no bytes left, we return -1.
119 */
120 if(((r->put_ptr - r->get_ptr) & r->mask) > 0) {
121 return r->get_ptr;
122 } else {
123 return -1;
124 }
125}
126/* Return the ring buffer size */
127int
128ringbufindex_size(const struct ringbufindex *r)
129{
130 return r->mask + 1;
131}
132/* Return the number of elements currently in the ring buffer */
133int
134ringbufindex_elements(const struct ringbufindex *r)
135{
136 return (r->put_ptr - r->get_ptr) & r->mask;
137}
138/* Is the ring buffer full? */
139int
140ringbufindex_full(const struct ringbufindex *r)
141{
142 return ((r->put_ptr - r->get_ptr) & r->mask) == r->mask;
143}
144/* Is the ring buffer empty? */
145int
146ringbufindex_empty(const struct ringbufindex *r)
147{
148 return ringbufindex_elements(r) == 0;
149}
int ringbufindex_elements(const struct ringbufindex *r)
Return the number of elements currently in the ring buffer.
Definition: ringbufindex.c:134
int ringbufindex_size(const struct ringbufindex *r)
Return the ring buffer size.
Definition: ringbufindex.c:128
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_full(const struct ringbufindex *r)
Is the ring buffer full?
Definition: ringbufindex.c:140
void ringbufindex_init(struct ringbufindex *r, uint8_t size)
Initialize a ring buffer.
Definition: ringbufindex.c:50
int ringbufindex_put(struct ringbufindex *r)
Put one element to the ring buffer.
Definition: ringbufindex.c:58
int ringbufindex_empty(const struct ringbufindex *r)
Is the ring buffer empty?
Definition: ringbufindex.c:146
int ringbufindex_get(struct ringbufindex *r)
Remove the first element and return its index.
Definition: ringbufindex.c:90
int ringbufindex_peek_put(const struct ringbufindex *r)
Check if there is space to put an element.
Definition: ringbufindex.c:78
Header file for the ringbufindex library.