3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] 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. This uses inlined functions
34 * for maximum speed. All buffers created with this library must be of the same size, however
35 * multiple independant buffers can be created.
37 * Note that for each buffer, insertion and removal operations may occur at the same time (via
38 * a multithreaded ISR based system) however the same kind of operation (two or more insertions
39 * or deletions) must not overlap. If there is possibility of two or more of the same kind of
40 * operating occuring at the same point in time, atomic (mutex) locking should be used.
43 #ifndef _ULW_RING_BUFF_H_
44 #define _ULW_RING_BUFF_H_
47 #include <util/atomic.h>
53 /** Size of each ring buffer, in data elements - must be between 1 and 255. */
54 #define BUFFER_SIZE 255
56 /** Type of data to store into the buffer. */
57 #define RingBuff_Data_t uint8_t
59 /** Datatype which may be used to store the count of data stored in a buffer, retrieved
60 * via a call to \ref RingBuffer_GetCount().
62 #if (BUFFER_SIZE <= 0xFF)
63 #define RingBuff_Count_t uint8_t
65 #define RingBuff_Count_t uint16_t
69 /** Type define for a new ring buffer object. Buffers should be initialized via a call to
70 * \ref RingBuffer_InitBuffer() before use.
74 RingBuff_Data_t Buffer
[BUFFER_SIZE
]; /**< Internal ring buffer data, referenced by the buffer pointers. */
75 RingBuff_Data_t
* In
; /**< Current storage location in the circular buffer */
76 RingBuff_Data_t
* Out
; /**< Current retrieval location in the circular buffer */
77 RingBuff_Count_t Count
;
80 /* Inline Functions: */
81 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function
82 * before any operations are called upon them. Already initialized buffers may be reset
83 * by re-initializing them using this function.
85 * \param[out] Buffer Pointer to a ring buffer structure to initialize
87 static inline void RingBuffer_InitBuffer(RingBuff_t
* const Buffer
)
89 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
91 Buffer
->In
= Buffer
->Buffer
;
92 Buffer
->Out
= Buffer
->Buffer
;
96 /** Retrieves the minimum number of bytes stored in a particular buffer. This value is computed
97 * by entering an atomic lock on the buffer while the IN and OUT locations are fetched, so that
98 * the buffer cannot be modified while the computation takes place. This value should be cached
99 * when reading out the contents of the buffer, so that as small a time as possible is spent
102 * \note The value returned by this function is guaranteed to only be the minimum number of bytes
103 * stored in the given buffer; this value may change as other threads write new data and so
104 * the returned number should be used only to determine how many successive reads may safely
105 * be performed on the buffer.
107 * \param[in] Buffer Pointer to a ring buffer structure whose count is to be computed
109 static inline RingBuff_Count_t
RingBuffer_GetCount(RingBuff_t
* const Buffer
)
111 RingBuff_Count_t Count
;
113 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
115 Count
= Buffer
->Count
;
121 /** Atomically determines if the specified ring buffer contains any free space. This should
122 * be tested before storing data to the buffer, to ensure that no data is lost due to a
125 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
127 * \return Boolean true if the buffer contains no free space, false otherwise
129 static inline bool RingBuffer_IsFull(RingBuff_t
* const Buffer
)
131 return (RingBuffer_GetCount(Buffer
) == BUFFER_SIZE
);
134 /** Atomically determines if the specified ring buffer contains any data. This should
135 * be tested before removing data from the buffer, to ensure that the buffer does not
138 * If the data is to be removed in a loop, store the total number of bytes stored in the
139 * buffer (via a call to the \ref RingBuffer_GetCount() function) in a temporary variable
140 * to reduce the time spent in atomicity locks.
142 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
144 * \return Boolean true if the buffer contains no free space, false otherwise
146 static inline bool RingBuffer_IsEmpty(RingBuff_t
* const Buffer
)
148 return (RingBuffer_GetCount(Buffer
) == 0);
151 /** Inserts an element into the ring buffer.
153 * \note Only one execution thread (main program thread or an ISR) may insert into a single buffer
154 * otherwise data corruption may occur. Insertion and removal may occur from different execution
157 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
158 * \param[in] Data Data element to insert into the buffer
160 static inline void RingBuffer_Insert(RingBuff_t
* const Buffer
,
161 const RingBuff_Data_t Data
)
165 if (++Buffer
->In
== &Buffer
->Buffer
[BUFFER_SIZE
])
166 Buffer
->In
= Buffer
->Buffer
;
168 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
174 /** Removes an element from the ring buffer.
176 * \note Only one execution thread (main program thread or an ISR) may remove from a single buffer
177 * otherwise data corruption may occur. Insertion and removal may occur from different execution
180 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
182 * \return Next data element stored in the buffer
184 static inline RingBuff_Data_t
RingBuffer_Remove(RingBuff_t
* const Buffer
)
186 RingBuff_Data_t Data
= *Buffer
->Out
;
188 if (++Buffer
->Out
== &Buffer
->Buffer
[BUFFER_SIZE
])
189 Buffer
->Out
= Buffer
->Buffer
;
191 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
199 /** Returns the next element stored in the ring buffer, without removing it.
201 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
203 * \return Next data element stored in the buffer
205 static inline RingBuff_Data_t
RingBuffer_Peek(RingBuff_t
* const Buffer
)