Skip to content
Proyek

Tentang

Pustaka ringan dan generik untuk mengelola buffer cincin

Alat pengembangan

Cara angka ini diukur

1,566 bintang359 Fork +2 dalam 24 jam

Yang dapat Anda lakukan

  • Written in C (C11), compatible with stdint.h data types
  • Platform independent, no architecture specific code
  • No dynamic memory allocation, data is stored in a user-provided static array
  • Uses optimized memory copy instead of loops to read/write data from/to memory

Pertumbuhan bintang terukur

dalam 24 jam
+2
dalam 7 hari
+5
dalam 15 hari
+10
dalam 1 bulan
+16

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

LwRB — Lightweight ring buffer for embedded systems

Build & Test Release PlatformIO Registry License: MIT Documentation

LwRB is a generic, platform independent FIFO (First In, First Out) ring buffer written in C11, optimized for embedded systems. It uses no dynamic memory, is lock-free for a single writer and a single reader (for example an interrupt and the main loop), and gives direct, zero-copy access to its memory, which makes it a natural fit for UART, USB and DMA data streams.

📖 Documentation · Changelog · Releases

Features

  • Written in C (C11), compatible with stdint.h data types
  • Platform independent, no architecture specific code
  • No dynamic memory allocation, data is stored in a user-provided static array
  • Uses optimized memory copy instead of loops to read/write data from/to memory
  • Thread and interrupt safe when used as a pipe with a single write and a single read context
  • Uses C11 atomic operations by default to keep reads and writes race-free, even on architectures whose native word size is smaller than size_t (AVR for instance); can be disabled with LWRB_DISABLE_ATOMIC
  • Suitable for DMA transfers from and to memory with zero-copy overhead between buffer and application memory
  • Supports data peek, skip for read and advance for write
  • Byte-sequence search within buffered data
  • Optional all-or-nothing read and write operations to avoid partial transfers
  • Optional per-buffer custom user argument storage
  • Event notifications on read, write and reset
  • Optimized single byte read/write API
  • User friendly MIT license

Quick start

#include <stdio.h>
#include "lwrb/lwrb.h"

/* Buffer instance and its raw memory */
lwrb_t buff;
uint8_t buff_data[8];

int
main(void) {
    uint8_t data[8];
    size_t len;

    /* Initialize buffer */
    lwrb_init(&buff, buff_data, sizeof(buff_data));

    /* Write 4 bytes of data */
    lwrb_write(&buff, "0123", 4);
    printf("Bytes in buffer: %d\r\n", (int)lwrb_get_full(&buff)); /* Prints "4" */

    /* Read data back, returns number of bytes read */
    len = lwrb_read(&buff, data, sizeof(data));
    printf("Number of bytes read: %d\r\n", (int)len); /* Prints "4" */
    return 0;
}

Note: A buffer of size N holds at most N - 1 bytes. One byte is always kept free to tell a full buffer apart from an empty one.

Zero-copy DMA transmit

Instead of copying data out of the buffer, hand the buffer memory directly to the DMA and release it once the transfer completes:

volatile size_t len;

void
send_data(void) {
    if (len > 0) {
        return; /* DMA transfer already in progress */
    }
    /* Get the longest contiguous block available for reading */
    len = lwrb_get_linear_block_read_length(&buff);
    if (len > 0) {
        start_dma_transfer(lwrb_get_linear_block_read_address(&buff), len);
    }
}

/* Called when the DMA transfer finishes */
void
DMA_Interrupt_handler(void) {
    lwrb_skip(&buff, len); /* Mark transmitted data as read */
    len = 0;
    send_data();           /* Start the next transfer, if any */
}

More examples are in docs/examples_src and in the documentation.

Thread safety

The library is lock-free for exactly one write context and one read context (a context is one thread or one interrupt). Write functions (lwrb_write, lwrb_advance, ...) only modify the write pointer, and read functions (lwrb_read, lwrb_skip, ...) only modify the read pointer, so a producer and a consumer can run in parallel without locks.

Multiple writers or multiple readers, as well as lwrb_init and lwrb_reset, require application-level locking. See Thread safety in the documentation for details.

Add to your project

CMake

add_subdirectory(path/to/lwrb/lwrb)
target_link_libraries(${PROJECT_NAME} PRIVATE lwrb)

Link lwrb_ex instead to also get the extended functions (lwrb_overwrite, lwrb_move). Optional compiler options and definitions can be set with LWRB_COMPILE_OPTIONS and LWRB_COMPILE_DEFINITIONS, see lwrb/library.cmake.

Any other build system

  1. Copy the lwrb folder to your project
  2. Add lwrb/src/include to the include path
  3. Add the source files from lwrb/src/lwrb/ to the build

PlatformIO

LwRB is available in the PlatformIO Registry. Add it to your platformio.ini:

lib_deps =
    majerle/LwRB

Contribute

Fresh contributions are always welcome. Simple instructions to proceed:

  1. Fork Github repository
  2. Follow C style & coding rules and use clang-format to format the code
  3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:

  1. Report a bug
  2. Ask for a feature request

License

LwRB is released under the MIT license.

Jelajahi proyek terkait

Memuat…