PX4 Firmware
PX4 Autopilot Software http://px4.io
ringbuffer.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2013-2015 PX4 Development Team. 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  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file ringbuffer.h
36  *
37  * A flexible ringbuffer class.
38  */
39 
40 #pragma once
41 
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 
46 namespace ringbuffer __EXPORT
47 {
48 
50 {
51 public:
52  RingBuffer(unsigned num_items, size_t item_size);
53  virtual ~RingBuffer();
54 
55  /**
56  * Put an item into the buffer.
57  *
58  * @param val Item to put
59  * @return true if the item was put, false if the buffer is full
60  */
61  bool put(const void *val, size_t val_size = 0);
62 
63  bool put(int8_t val);
64  bool put(uint8_t val);
65  bool put(int16_t val);
66  bool put(uint16_t val);
67  bool put(int32_t val);
68  bool put(uint32_t val);
69  bool put(int64_t val);
70  bool put(uint64_t val);
71  bool put(float val);
72  bool put(double val);
73 
74  /**
75  * Force an item into the buffer, discarding an older item if there is not space.
76  *
77  * @param val Item to put
78  * @return true if an item was discarded to make space
79  */
80  bool force(const void *val, size_t val_size = 0);
81 
82  bool force(int8_t val);
83  bool force(uint8_t val);
84  bool force(int16_t val);
85  bool force(uint16_t val);
86  bool force(int32_t val);
87  bool force(uint32_t val);
88  bool force(int64_t val);
89  bool force(uint64_t val);
90  bool force(float val);
91  bool force(double val);
92 
93  /**
94  * Get an item from the buffer.
95  *
96  * @param val Item that was gotten
97  * @return true if an item was got, false if the buffer was empty.
98  */
99  bool get(void *val, size_t val_size = 0);
100 
101  bool get(int8_t &val);
102  bool get(uint8_t &val);
103  bool get(int16_t &val);
104  bool get(uint16_t &val);
105  bool get(int32_t &val);
106  bool get(uint32_t &val);
107  bool get(int64_t &val);
108  bool get(uint64_t &val);
109  bool get(float &val);
110  bool get(double &val);
111 
112  /*
113  * Get the number of slots free in the buffer.
114  *
115  * @return The number of items that can be put into the buffer before
116  * it becomes full.
117  */
118  unsigned space(void);
119 
120  /*
121  * Get the number of items in the buffer.
122  *
123  * @return The number of items that can be got from the buffer before
124  * it becomes empty.
125  */
126  unsigned count(void);
127 
128  /*
129  * Returns true if the buffer is empty.
130  */
131  bool empty();
132 
133  /*
134  * Returns true if the buffer is full.
135  */
136  bool full();
137 
138  /*
139  * Returns the capacity of the buffer, or zero if the buffer could
140  * not be allocated.
141  */
142  unsigned size();
143 
144  /*
145  * Empties the buffer.
146  */
147  void flush();
148 
149  /*
150  * resize the buffer. This is unsafe to be called while
151  * a producer or consuming is running. Caller is responsible
152  * for any locking needed
153  *
154  * @param new_size new size for buffer
155  * @return true if the resize succeeds, false if
156  * not (allocation error)
157  */
158  bool resize(unsigned new_size);
159 
160  /*
161  * printf() some info on the buffer
162  */
163  void print_info(const char *name);
164 
165 private:
166  unsigned _num_items;
167  const size_t _item_size;
168  char *_buf;
169  volatile unsigned _head; /**< insertion point in _item_size units */
170  volatile unsigned _tail; /**< removal point in _item_size units */
171 
172  unsigned _next(unsigned index);
173 
174  /* we don't want this class to be copied */
175  RingBuffer(const RingBuffer &);
176  RingBuffer operator=(const RingBuffer &);
177 };
178 
179 } // namespace ringbuffer
volatile unsigned _tail
removal point in _item_size units
Definition: ringbuffer.h:170
Definition: I2C.hpp:51
volatile unsigned _head
insertion point in _item_size units
Definition: ringbuffer.h:169
const char * name
Definition: tests_main.c:58
const size_t _item_size
Definition: ringbuffer.h:167