Added module for storing variable length packets in a ring buffer. Implementation assumes multiple producing contexts and single consumer. API provides zero copy functionality with alloc, commit, claim, free scheme. Additionally, there are functions optimized for storing single word packets and packets consisting of a word and a pointer. Buffer can work in two modes: saturation or overwriting the oldest packets when buffer has no space to allocate for a new buffer. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
64 lines
1.3 KiB
C
64 lines
1.3 KiB
C
/*
|
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
#ifndef ZEPHYR_INCLUDE_SYS_MPSC_PACKET_H_
|
|
#define ZEPHYR_INCLUDE_SYS_MPSC_PACKET_H_
|
|
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/**
|
|
* @brief Multi producer, single consumer packet header
|
|
* @defgroup mpsc_packet MPSC (Multi producer, single consumer) packet header
|
|
* @ingroup mpsc_buf
|
|
* @{
|
|
*/
|
|
|
|
/** @brief Number of bits in the first word which are used by the buffer. */
|
|
#define MPSC_PBUF_HDR_BITS 2
|
|
|
|
/** @brief Header that must be added to the first word in each packet.
|
|
*
|
|
* This fields are controlled by the packet buffer and unless specified must
|
|
* not be used. Fields must be added at the top of the packet header structure.
|
|
*/
|
|
#define MPSC_PBUF_HDR \
|
|
uint32_t valid: 1; \
|
|
uint32_t busy: 1
|
|
|
|
/** @brief Generic packet header. */
|
|
struct mpsc_pbuf_hdr {
|
|
MPSC_PBUF_HDR;
|
|
uint32_t data: 32 - MPSC_PBUF_HDR_BITS;
|
|
};
|
|
|
|
/** @brief Skip packet used internally by the packet buffer. */
|
|
struct mpsc_pbuf_skip {
|
|
MPSC_PBUF_HDR;
|
|
uint32_t len: 32 - MPSC_PBUF_HDR_BITS;
|
|
};
|
|
|
|
/** @brief Generic packet header. */
|
|
union mpsc_pbuf_generic {
|
|
struct mpsc_pbuf_hdr hdr;
|
|
struct mpsc_pbuf_skip skip;
|
|
uint32_t raw;
|
|
};
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* ZEPHYR_INCLUDE_SYS_MPSC_PACKET_H_ */
|