70 lines
No EOL
2.6 KiB
C++
70 lines
No EOL
2.6 KiB
C++
/*
|
|
* ringbuffer.h
|
|
*
|
|
* Created on: Sep 14, 2021
|
|
* Author: Attila Body
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
namespace f1ll {
|
|
|
|
class ringbuffer
|
|
{
|
|
public:
|
|
ringbuffer(uint8_t *buffer, uint16_t size);
|
|
|
|
/// @brief Copies data to the ring buffer (without committing it) The amount of the committed data in the buffer
|
|
/// should not exceed the size of the buffer.
|
|
/// @param data Pointer to the data to copy
|
|
/// @param len Length of the data to copy
|
|
uint16_t put(uint8_t const *data_buffer, uint16_t len);
|
|
|
|
/// @brief Commits the data already placed into the buffer and notifies the consumer about it's availability
|
|
void commit();
|
|
|
|
/// @brief Waits until all the data from the ring buffer gets consumed.
|
|
// void flush();
|
|
|
|
/// @brief Gets a pointer to the next chunk of committed data in the buffer without registering the consumption.
|
|
/// The caller should also call report_consumption using the returned chunk length after
|
|
/// it finished processing the data.
|
|
/// @param[in] len_requested Length of the data requested from the buffer. The length of the actual data provided
|
|
/// might be actually smaller (because either reaching the end of the buffer or not enough data in the
|
|
/// buffer).
|
|
/// @param[out] data Receives a pointer to the first byte of the available data in the buffer
|
|
/// @param[out] len Receives the length of the chunk available in the buffer. Will not exceed len_requested.
|
|
/// @retval true if the buffer has more available data, false otherwise
|
|
bool get_chunk(uint16_t len_requested, uint8_t **data, uint16_t *len);
|
|
|
|
/// @brief Marks the chunk returned by ringbuffer_GetChunk as available
|
|
/// @param consumed The length of the chunk as returned by ringbuffer_GetChunk(..., len)
|
|
void report_consumption(uint16_t consumed);
|
|
|
|
/// @brief Returns the number of uncommited bytes in the ring buffer
|
|
uint16_t uncommited() const;
|
|
|
|
/// @brief Returns the number of commited bytes in the ring buffer
|
|
uint16_t commited() const;
|
|
|
|
/// @brief Discards the uncommited data in the ring buffer
|
|
void discard();
|
|
|
|
private:
|
|
uint8_t *m_buffer; //!< Pointer to the phisical memory bufer
|
|
uint16_t m_size; //!< Size of the memory buffer in bytes
|
|
uint16_t m_head; //!< Write position
|
|
uint16_t m_head_shadow; //!< Shadowed write position for collecting data before committing it
|
|
uint16_t m_tail; //!< Read position
|
|
|
|
static uint16_t buffer_used(uint16_t size, uint16_t head, uint16_t tail);
|
|
static uint16_t buffer_free(uint16_t size, uint16_t head, uint16_t tail);
|
|
|
|
uint16_t buffer_used() const;
|
|
uint16_t buffer_free() const;
|
|
};
|
|
|
|
} // namespace
|