Contiki-NG
ringbuf.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008, Swedish Institute of Computer Science.
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 * Ring buffer library implementation
36 * \author
37 * Adam Dunkels <adam@sics.se>
38 */
39
40#include "lib/ringbuf.h"
41#include <sys/cc.h>
42/*---------------------------------------------------------------------------*/
43void
44ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
45{
46 r->data = dataptr;
47 r->mask = size - 1;
48 r->put_ptr = 0;
49 r->get_ptr = 0;
50}
51/*---------------------------------------------------------------------------*/
52int
53ringbuf_put(struct ringbuf *r, uint8_t c)
54{
55 /* Check if buffer is full. If it is full, return 0 to indicate that
56 the element was not inserted into the buffer.
57
58 XXX: there is a potential risk for a race condition here, because
59 the ->get_ptr field may be written concurrently by the
60 ringbuf_get() function. To avoid this, access to ->get_ptr must
61 be atomic. We use an uint8_t type, which makes access atomic on
62 most platforms, but C does not guarantee this.
63 */
64 if(((r->put_ptr - r->get_ptr) & r->mask) == r->mask) {
65 return 0;
66 }
67 /*
68 * CC_ACCESS_NOW is used because the compiler is allowed to reorder
69 * the access to non-volatile variables.
70 * In this case a reader might read from the moved index/ptr before
71 * its value (c) is written. Reordering makes little sense, but
72 * better safe than sorry.
73 */
74 CC_ACCESS_NOW(uint8_t, r->data[r->put_ptr]) = c;
75 CC_ACCESS_NOW(uint8_t, r->put_ptr) = (r->put_ptr + 1) & r->mask;
76 return 1;
77}
78/*---------------------------------------------------------------------------*/
79int
81{
82 uint8_t c;
83
84 /* Check if there are bytes in the buffer. If so, we return the
85 first one and increase the pointer. If there are no bytes left, we
86 return -1.
87
88 XXX: there is a potential risk for a race condition here, because
89 the ->put_ptr field may be written concurrently by the
90 ringbuf_put() function. To avoid this, access to ->get_ptr must
91 be atomic. We use an uint8_t type, which makes access atomic on
92 most platforms, but C does not guarantee this.
93 */
94 if(((r->put_ptr - r->get_ptr) & r->mask) > 0) {
95 /*
96 * CC_ACCESS_NOW is used because the compiler is allowed to reorder
97 * the access to non-volatile variables.
98 * In this case the memory might be freed and overwritten by
99 * increasing get_ptr before the value was copied to c.
100 * Opposed to the put-operation this would even make sense,
101 * because the register used for mask can be reused to save c
102 * (on some architectures).
103 */
104 c = CC_ACCESS_NOW(uint8_t, r->data[r->get_ptr]);
105 CC_ACCESS_NOW(uint8_t, r->get_ptr) = (r->get_ptr + 1) & r->mask;
106 return c;
107 } else {
108 return -1;
109 }
110}
111/*---------------------------------------------------------------------------*/
112int
114{
115 return r->mask + 1;
116}
117/*---------------------------------------------------------------------------*/
118int
120{
121 return (r->put_ptr - r->get_ptr) & r->mask;
122}
123/*---------------------------------------------------------------------------*/
Default definitions of C compiler quirk work-arounds.
#define CC_ACCESS_NOW(type, variable)
This macro ensures that the access to a non-volatile variable can not be reordered or optimized by th...
Definition: cc.h:94
int ringbuf_size(struct ringbuf *r)
Get the size of a ring buffer.
Definition: ringbuf.c:113
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:80
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:53
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:44
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition: ringbuf.c:119
Header file for the ring buffer library.
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68