Contiki 2.5
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  * $Id: ringbuf.c,v 1.2 2010/06/15 13:31:22 nifi Exp $
32  */
33 
34 /**
35  * \file
36  * Ring buffer library implementation
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "lib/ringbuf.h"
42 /*---------------------------------------------------------------------------*/
43 void
44 ringbuf_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 /*---------------------------------------------------------------------------*/
52 int
53 ringbuf_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  r->data[r->put_ptr] = c;
68  r->put_ptr = (r->put_ptr + 1) & r->mask;
69  return 1;
70 }
71 /*---------------------------------------------------------------------------*/
72 int
73 ringbuf_get(struct ringbuf *r)
74 {
75  uint8_t c;
76 
77  /* Check if there are bytes in the buffer. If so, we return the
78  first one and increase the pointer. If there are no bytes left, we
79  return -1.
80 
81  XXX: there is a potential risk for a race condition here, because
82  the ->put_ptr field may be written concurrently by the
83  ringbuf_put() function. To avoid this, access to ->get_ptr must
84  be atomic. We use an uint8_t type, which makes access atomic on
85  most platforms, but C does not guarantee this.
86  */
87  if(((r->put_ptr - r->get_ptr) & r->mask) > 0) {
88  c = r->data[r->get_ptr];
89  r->get_ptr = (r->get_ptr + 1) & r->mask;
90  return c;
91  } else {
92  return -1;
93  }
94 }
95 /*---------------------------------------------------------------------------*/
96 int
98 {
99  return r->mask + 1;
100 }
101 /*---------------------------------------------------------------------------*/
102 int
104 {
105  return (r->put_ptr - r->get_ptr) & r->mask;
106 }
107 /*---------------------------------------------------------------------------*/