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
53 /** Type define for a new ring buffer object. Buffers should be initialized via a call to
54 * \ref RingBuffer_InitBuffer() before use.
58 RingBuff_Data_t Buffer
[BUFFER_SIZE
]; /**< Internal ring buffer data, referenced by the buffer pointers. */
59 RingBuff_Data_t
* In
; /**< Current storage location in the circular buffer */
60 RingBuff_Data_t
* Out
; /**< Current retrieval location in the circular buffer */
61 uint8_t Count
; /**< Total number of bytes stored in the circular buffer */
64 /* Inline Functions: */
65 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function
66 * before any operations are called upon them. Already initialized buffers may be reset
67 * by re-initializing them using this function.
69 * \param[out] Buffer Pointer to a ring buffer structure to initialize
71 static inline void RingBuffer_InitBuffer(RingBuff_t
* const Buffer
)
73 Buffer
->In
= Buffer
->Buffer
;
74 Buffer
->Out
= Buffer
->Buffer
;
78 /** Atomically inserts an element into the ring buffer.
80 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
81 * \param[in] Data Data element to insert into the buffer
83 static inline void RingBuffer_AtomicInsert(RingBuff_t
* const Buffer
, RingBuff_Data_t Data
)
85 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
89 if (++Buffer
->In
== &Buffer
->Buffer
[BUFFER_SIZE
])
90 Buffer
->In
= Buffer
->Buffer
;
96 /** Atomically retrieves an element from the ring buffer.
98 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
100 * \return Next data element stored in the buffer
102 static inline RingBuff_Data_t
RingBuffer_AtomicRemove(RingBuff_t
* const Buffer
)
104 RingBuff_Data_t Data
;
106 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
110 if (++Buffer
->Out
== &Buffer
->Buffer
[BUFFER_SIZE
])
111 Buffer
->Out
= Buffer
->Buffer
;
119 /** Inserts an element into the ring buffer.
121 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
122 * \param[in] Data Data element to insert into the buffer
124 static inline void RingBuffer_Insert(RingBuff_t
* const Buffer
, RingBuff_Data_t Data
)
128 if (++Buffer
->In
== &Buffer
->Buffer
[BUFFER_SIZE
])
129 Buffer
->In
= Buffer
->Buffer
;
134 /** Retrieves an element from the ring buffer.
136 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
138 * \return Next data element stored in the buffer
140 static inline RingBuff_Data_t
RingBuffer_Remove(RingBuff_t
* const Buffer
)
142 RingBuff_Data_t Data
= *Buffer
->Out
;
144 if (++Buffer
->Out
== &Buffer
->Buffer
[BUFFER_SIZE
])
145 Buffer
->Out
= Buffer
->Buffer
;