3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Ultra lightweight ring buffer, for fast insertion/deletion.
36 #ifndef _ULW_RING_BUFF_H_
37 #define _ULW_RING_BUFF_H_
40 #include <util/atomic.h>
46 /** Size of each ring buffer, in data elements - must be between 1 and 255. */
47 #define BUFFER_SIZE 255
49 /** Type of data to store into the buffer. */
50 #define RingBuff_Data_t uint8_t
52 /** Datatype which may be used to store the count of data stored in a buffer, retrieved
53 * via a call to \ref RingBuffer_GetCount().
55 #if (BUFFER_SIZE <= 0xFF)
56 #define RingBuff_Count_t uint8_t
58 #define RingBuff_Count_t uint16_t
62 /** Type define for a new ring buffer object. Buffers should be initialized via a call to
63 * \ref RingBuffer_InitBuffer() before use.
67 RingBuff_Data_t Buffer
[BUFFER_SIZE
]; /**< Internal ring buffer data, referenced by the buffer pointers. */
68 RingBuff_Data_t
* In
; /**< Current storage location in the circular buffer */
69 RingBuff_Data_t
* Out
; /**< Current retrieval location in the circular buffer */
70 RingBuff_Count_t Count
;
73 /* Inline Functions: */
74 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function
75 * before any operations are called upon them. Already initialized buffers may be reset
76 * by re-initializing them using this function.
78 * \param[out] Buffer Pointer to a ring buffer structure to initialize
80 static inline void RingBuffer_InitBuffer(RingBuff_t
* const Buffer
)
82 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
84 Buffer
->In
= Buffer
->Buffer
;
85 Buffer
->Out
= Buffer
->Buffer
;
89 /** Retrieves the minimum number of bytes stored in a particular buffer. This value is computed
90 * by entering an atomic lock on the buffer while the IN and OUT locations are fetched, so that
91 * the buffer cannot be modified while the computation takes place. This value should be cached
92 * when reading out the contents of the buffer, so that as small a time as possible is spent
95 * \note The value returned by this function is guaranteed to only be the minimum number of bytes
96 * stored in the given buffer; this value may change as other threads write new data and so
97 * the returned number should be used only to determine how many successive reads may safely
98 * be performed on the buffer.
100 * \param[in] Buffer Pointer to a ring buffer structure whose count is to be computed
102 static inline RingBuff_Count_t
RingBuffer_GetCount(RingBuff_t
* const Buffer
)
104 RingBuff_Count_t Count
;
106 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
108 Count
= Buffer
->Count
;
114 /** Atomically determines if the specified ring buffer contains any free space. This should
115 * be tested before storing data to the buffer, to ensure that no data is lost due to a
118 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
120 * \return Boolean true if the buffer contains no free space, false otherwise
122 static inline bool RingBuffer_IsFull(RingBuff_t
* const Buffer
)
124 return (RingBuffer_GetCount(Buffer
) == BUFFER_SIZE
);
127 /** Atomically determines if the specified ring buffer contains any data. This should
128 * be tested before removing data from the buffer, to ensure that the buffer does not
131 * If the data is to be removed in a loop, store the total number of bytes stored in the
132 * buffer (via a call to the \ref RingBuffer_GetCount() function) in a temporary variable
133 * to reduce the time spent in atomicity locks.
135 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
137 * \return Boolean true if the buffer contains no free space, false otherwise
139 static inline bool RingBuffer_IsEmpty(RingBuff_t
* const Buffer
)
141 return (RingBuffer_GetCount(Buffer
) == 0);
144 /** Inserts an element into the ring buffer.
146 * \note Only one execution thread (main program thread or an ISR) may insert into a single buffer
147 * otherwise data corruption may occur. Insertion and removal may occur from different execution
150 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
151 * \param[in] Data Data element to insert into the buffer
153 static inline void RingBuffer_Insert(RingBuff_t
* const Buffer
,
154 const RingBuff_Data_t Data
)
158 if (++Buffer
->In
== &Buffer
->Buffer
[BUFFER_SIZE
])
159 Buffer
->In
= Buffer
->Buffer
;
161 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
167 /** Removes an element from the ring buffer.
169 * \note Only one execution thread (main program thread or an ISR) may remove from a single buffer
170 * otherwise data corruption may occur. Insertion and removal may occur from different execution
173 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
175 * \return Next data element stored in the buffer
177 static inline RingBuff_Data_t
RingBuffer_Remove(RingBuff_t
* const Buffer
)
179 RingBuff_Data_t Data
= *Buffer
->Out
;
181 if (++Buffer
->Out
== &Buffer
->Buffer
[BUFFER_SIZE
])
182 Buffer
->Out
= Buffer
->Buffer
;
184 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)