Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



10 Commits

Repository files navigation


RingBuffer is an open-source, versatile, and optimized version of a ring buffer memory. It has been implemented in C to fit limited resource microcontrollers such as Microchip Inc. MCUs. This implementation allows both ring and linear access enabling standard functions to access the ring space. In addition, it is compatible with one-shot and cycling DMA modules.


Ring object creation

The code is re-entrant and allows multiple instances. The returned 'object' handles a single instance.

RING_DATA *ring1 = RING_InitBuffer(NULL, 16);
RING_DATA *ring2 = RING_InitBuffer(NULL, 123);

Ring object destroy

The user must care of unused instances destroying them when no more needed.


Linear access

Both read and write operations can be execute on linear space.

Read with linear access

This snippet reads readable bytes from the ring buffer memory. With this technique, memcpy can be changed with any user function.

uint8_t dest[16], *src;
size_t readable;

src = RING_GetBufferDirectly(ring, &readable, sizeof(dest));
memcpy(dest, src, readable);

Write with linear access

This snippet writes toWrite bytes into the ring buffer memory. With this technique, memcpy can be substituted with any user function.

uint8_t src[16], *dest;
size_t toWrite;

memset(src, 'L', sizeof(src));
dest = RING_AddBufferDirectly(ring, &toWrite, sizeof(src));
memcpy(dest, src, toWrite);

Get free and filled space

Two inline functions return the whole and linear accessible free space, respectively. Same for the filled space.

size_t freeSpace = RING_GetFreeSpace(ring);
size_t freeLinearSpace = RING_GetFreeLinearSpace(ring);

size_t fullSpace = RING_GetFullSpace(ring);
size_t fullLinearSpace = RING_GetFullLinearSpace(ring);


Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Sponsor: LP Systems Author: Luca Pascarella


Optimized version of a C ring buffer memory







No releases published


No packages published