mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-25 22:14:37 +00:00
api: Add documentation to RingBuffer class
This commit is contained in:
parent
88c87172ba
commit
d9517a5cab
@ -21,6 +21,11 @@
|
|||||||
#include <types.h>
|
#include <types.h>
|
||||||
#include <lock.hpp>
|
#include <lock.hpp>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A generic ring buffer implementation with thread safety.
|
||||||
|
*
|
||||||
|
* @tparam T Type of the data stored in the buffer.
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class RingBuffer
|
class RingBuffer
|
||||||
{
|
{
|
||||||
@ -34,6 +39,13 @@ private:
|
|||||||
size_t Tail;
|
size_t Tail;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Construct a new RingBuffer object.
|
||||||
|
*
|
||||||
|
* Initializes the buffer and the spin lock. The default buffer size is 16.
|
||||||
|
*
|
||||||
|
* @param Size Initial size (capacity) of the buffer.
|
||||||
|
*/
|
||||||
RingBuffer(size_t Size = 16)
|
RingBuffer(size_t Size = 16)
|
||||||
: Lock(new spin_lock()),
|
: Lock(new spin_lock()),
|
||||||
Buffer(new T[Size]),
|
Buffer(new T[Size]),
|
||||||
@ -42,18 +54,33 @@ public:
|
|||||||
Head(0),
|
Head(0),
|
||||||
Tail(0) {}
|
Tail(0) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destroy the RingBuffer object.
|
||||||
|
*
|
||||||
|
* Releases the allocated memory for the buffer and the spin lock.
|
||||||
|
*/
|
||||||
~RingBuffer()
|
~RingBuffer()
|
||||||
{
|
{
|
||||||
delete Lock;
|
delete Lock;
|
||||||
delete[] Buffer;
|
delete[] Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Write(const T *Data, size_t Size)
|
/**
|
||||||
|
* @brief Write data to the ring buffer.
|
||||||
|
*
|
||||||
|
* Writes up to `nReads` elements from `Data` into the buffer. If the buffer
|
||||||
|
* is full, the function stops writing.
|
||||||
|
*
|
||||||
|
* @param Data Pointer to the data to be written.
|
||||||
|
* @param nReads Number of elements to write.
|
||||||
|
* @return size_t Number of elements actually written.
|
||||||
|
*/
|
||||||
|
size_t Write(const T *Data, size_t nReads)
|
||||||
{
|
{
|
||||||
sl_guard(*Lock);
|
sl_guard(*Lock);
|
||||||
|
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
while (Size > 0)
|
while (nReads > 0)
|
||||||
{
|
{
|
||||||
if (BufferCount == BufferSize)
|
if (BufferCount == BufferSize)
|
||||||
break;
|
break;
|
||||||
@ -62,18 +89,28 @@ public:
|
|||||||
Head = (Head + 1) % BufferSize;
|
Head = (Head + 1) % BufferSize;
|
||||||
BufferCount++;
|
BufferCount++;
|
||||||
written++;
|
written++;
|
||||||
Size--;
|
nReads--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Read(T *Data, size_t Size)
|
/**
|
||||||
|
* @brief Read data from the ring buffer.
|
||||||
|
*
|
||||||
|
* Reads up to `nReads` elements from the buffer into `Data`. If the buffer
|
||||||
|
* is empty, the function stops reading.
|
||||||
|
*
|
||||||
|
* @param Data Pointer to the memory where read elements will be stored.
|
||||||
|
* @param nReads Number of elements to read.
|
||||||
|
* @return size_t Number of elements actually read.
|
||||||
|
*/
|
||||||
|
size_t Read(T *Data, size_t nReads)
|
||||||
{
|
{
|
||||||
sl_guard(*Lock);
|
sl_guard(*Lock);
|
||||||
|
|
||||||
size_t read = 0;
|
size_t read = 0;
|
||||||
while (Size > 0)
|
while (nReads > 0)
|
||||||
{
|
{
|
||||||
if (BufferCount == 0)
|
if (BufferCount == 0)
|
||||||
break;
|
break;
|
||||||
@ -82,19 +119,29 @@ public:
|
|||||||
Tail = (Tail + 1) % BufferSize;
|
Tail = (Tail + 1) % BufferSize;
|
||||||
BufferCount--;
|
BufferCount--;
|
||||||
read++;
|
read++;
|
||||||
Size--;
|
nReads--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Peek(T *Data, size_t Size)
|
/**
|
||||||
|
* @brief Peek at data in the ring buffer without removing it.
|
||||||
|
*
|
||||||
|
* Peeks up to `nReads` elements from the buffer into `Data` without
|
||||||
|
* modifying the buffer state. If the buffer is empty, the function stops peeking.
|
||||||
|
*
|
||||||
|
* @param Data Pointer to the memory where peeked elements will be stored.
|
||||||
|
* @param nReads Number of elements to peek.
|
||||||
|
* @return size_t Number of elements actually peeked.
|
||||||
|
*/
|
||||||
|
size_t Peek(T *Data, size_t nReads)
|
||||||
{
|
{
|
||||||
sl_guard(*Lock);
|
sl_guard(*Lock);
|
||||||
|
|
||||||
size_t read = 0;
|
size_t read = 0;
|
||||||
size_t tail = Tail;
|
size_t tail = Tail;
|
||||||
while (Size > 0)
|
while (nReads > 0)
|
||||||
{
|
{
|
||||||
if (BufferCount == 0)
|
if (BufferCount == 0)
|
||||||
break;
|
break;
|
||||||
@ -102,18 +149,28 @@ public:
|
|||||||
*Data++ = Buffer[tail];
|
*Data++ = Buffer[tail];
|
||||||
tail = (tail + 1) % BufferSize;
|
tail = (tail + 1) % BufferSize;
|
||||||
read++;
|
read++;
|
||||||
Size--;
|
nReads--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the current number of elements in the buffer.
|
||||||
|
*
|
||||||
|
* @return size_t The number of elements in the buffer.
|
||||||
|
*/
|
||||||
size_t Count()
|
size_t Count()
|
||||||
{
|
{
|
||||||
sl_guard(*Lock);
|
sl_guard(*Lock);
|
||||||
return BufferCount;
|
return BufferCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the available space left in the buffer.
|
||||||
|
*
|
||||||
|
* @return size_t The number of free slots in the buffer.
|
||||||
|
*/
|
||||||
size_t Free()
|
size_t Free()
|
||||||
{
|
{
|
||||||
sl_guard(*Lock);
|
sl_guard(*Lock);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user