Contiki 3.x
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 /*---------------------------------------------------------------------------*/
42 void
43 ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
44 {
45  r->data = dataptr;
46  r->mask = size - 1;
47  r->put_ptr = 0;
48  r->get_ptr = 0;
49 }
50 /*---------------------------------------------------------------------------*/
51 int
52 ringbuf_put(struct ringbuf *r, uint8_t c)
53 {
54  /* Check if buffer is full. If it is full, return 0 to indicate that
55  the element was not inserted into the buffer.
56 
57  XXX: there is a potential risk for a race condition here, because
58  the ->get_ptr field may be written concurrently by the
59  ringbuf_get() function. To avoid this, access to ->get_ptr must
60  be atomic. We use an uint8_t type, which makes access atomic on
61  most platforms, but C does not guarantee this.
62  */
63  if(((r->put_ptr - r->get_ptr) & r->mask) == r->mask) {
64  return 0;
65  }
66  r->data[r->put_ptr] = c;
67  r->put_ptr = (r->put_ptr + 1) & r->mask;
68  return 1;
69 }
70 /*---------------------------------------------------------------------------*/
71 int
72 ringbuf_get(struct ringbuf *r)
73 {
74  uint8_t c;
75 
76  /* Check if there are bytes in the buffer. If so, we return the
77  first one and increase the pointer. If there are no bytes left, we
78  return -1.
79 
80  XXX: there is a potential risk for a race condition here, because
81  the ->put_ptr field may be written concurrently by the
82  ringbuf_put() function. To avoid this, access to ->get_ptr must
83  be atomic. We use an uint8_t type, which makes access atomic on
84  most platforms, but C does not guarantee this.
85  */
86  if(((r->put_ptr - r->get_ptr) & r->mask) > 0) {
87  c = r->data[r->get_ptr];
88  r->get_ptr = (r->get_ptr + 1) & r->mask;
89  return c;
90  } else {
91  return -1;
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 int
97 {
98  return r->mask + 1;
99 }
100 /*---------------------------------------------------------------------------*/
101 int
103 {
104  return (r->put_ptr - r->get_ptr) & r->mask;
105 }
106 /*---------------------------------------------------------------------------*/
Header file for the ring buffer library
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:72
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:43
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition: ringbuf.c:102
int ringbuf_size(struct ringbuf *r)
Get the size of a ring buffer.
Definition: ringbuf.c:96
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:52