2018-08-19 11:45:46 +02:00
|
|
|
///
|
2021-12-27 20:02:45 +01:00
|
|
|
/// @file ringbuffer.c @brief Ringbuffer module
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Copyright (c) 2009, 2011, 2014 by Johns. All Rights Reserved.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Contributor(s):
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// License: AGPLv3
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// This program is free software: you can redistribute it and/or modify
|
|
|
|
/// it under the terms of the GNU Affero General Public License as
|
|
|
|
/// published by the Free Software Foundation, either version 3 of the
|
|
|
|
/// License.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// This program is distributed in the hope that it will be useful,
|
|
|
|
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
/// GNU Affero General Public License for more details.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// $Id: c9497b197ce7e7a6ba397944edc7ccb161152efd $
|
2018-08-19 11:45:46 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// @defgroup Ringbuffer The ring buffer module.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Lock free ring buffer with only one writer and one reader.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "iatomic.h"
|
|
|
|
#include "ringbuffer.h"
|
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
/// ring buffer structure
|
2021-12-27 20:02:45 +01:00
|
|
|
struct _ring_buffer_ {
|
|
|
|
char *Buffer; ///< ring buffer data
|
|
|
|
const char *BufferEnd; ///< end of buffer
|
|
|
|
size_t Size; ///< bytes in buffer (for faster calc)
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
const char *ReadPointer; ///< only used by reader
|
|
|
|
char *WritePointer; ///< only used by writer
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/// The only thing modified by both
|
2021-12-27 20:02:45 +01:00
|
|
|
atomic_t Filled; ///< how many of the buffer is used
|
2018-08-19 11:45:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Reset ring buffer pointers.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to reset read/write pointers.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void RingBufferReset(RingBuffer *rb) {
|
2018-08-19 11:45:46 +02:00
|
|
|
rb->ReadPointer = rb->Buffer;
|
|
|
|
rb->WritePointer = rb->Buffer;
|
|
|
|
atomic_set(&rb->Filled, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Allocate a new ring buffer.
|
|
|
|
**
|
|
|
|
** @param size Size of the ring buffer.
|
|
|
|
**
|
|
|
|
** @returns Allocated ring buffer, must be freed with
|
|
|
|
** RingBufferDel(), NULL for out of memory.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
RingBuffer *RingBufferNew(size_t size) {
|
2018-08-19 11:45:46 +02:00
|
|
|
RingBuffer *rb;
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
if (!(rb = malloc(sizeof(*rb)))) { // allocate structure
|
2019-10-26 18:42:19 +02:00
|
|
|
return rb;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-10-26 18:42:19 +02:00
|
|
|
if (!(rb->Buffer = malloc(size))) { // allocate buffer
|
|
|
|
free(rb);
|
|
|
|
return NULL;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rb->Size = size;
|
|
|
|
rb->BufferEnd = rb->Buffer + size;
|
|
|
|
RingBufferReset(rb);
|
|
|
|
|
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Free an allocated ring buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void RingBufferDel(RingBuffer *rb) {
|
2018-08-19 11:45:46 +02:00
|
|
|
free(rb->Buffer);
|
|
|
|
free(rb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Advance write pointer in ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to advance write pointer.
|
|
|
|
** @param cnt Number of bytes to be adavanced.
|
|
|
|
**
|
|
|
|
** @returns Number of bytes that could be advanced in ring buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferWriteAdvance(RingBuffer *rb, size_t cnt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = rb->Size - atomic_read(&rb->Filled);
|
2021-12-27 20:02:45 +01:00
|
|
|
if (cnt > n) { // not enough space
|
2019-10-26 18:42:19 +02:00
|
|
|
cnt = n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->WritePointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n > cnt) { // don't cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
rb->WritePointer += cnt;
|
2021-12-27 20:02:45 +01:00
|
|
|
} else { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
rb->WritePointer = rb->Buffer;
|
|
|
|
if (n < cnt) {
|
|
|
|
n = cnt - n;
|
|
|
|
rb->WritePointer += n;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Only atomic modification!
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
atomic_add(cnt, &rb->Filled);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Write to a ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to write to.
|
|
|
|
** @param buf Buffer of @p cnt bytes.
|
|
|
|
** @param cnt Number of bytes in buffer.
|
|
|
|
**
|
|
|
|
** @returns The number of bytes that could be placed in the ring
|
|
|
|
** buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferWrite(RingBuffer *rb, const void *buf, size_t cnt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = rb->Size - atomic_read(&rb->Filled);
|
2021-12-27 20:02:45 +01:00
|
|
|
if (cnt > n) { // not enough space
|
2019-10-26 18:42:19 +02:00
|
|
|
cnt = n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->WritePointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n > cnt) { // don't cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
memcpy(rb->WritePointer, buf, cnt);
|
|
|
|
rb->WritePointer += cnt;
|
2021-12-27 20:02:45 +01:00
|
|
|
} else { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
memcpy(rb->WritePointer, buf, n);
|
|
|
|
rb->WritePointer = rb->Buffer;
|
|
|
|
if (n < cnt) {
|
|
|
|
buf += n;
|
|
|
|
n = cnt - n;
|
|
|
|
memcpy(rb->WritePointer, buf, n);
|
|
|
|
rb->WritePointer += n;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Only atomic modification!
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
atomic_add(cnt, &rb->Filled);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Get write pointer and free bytes at this position of ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to write to.
|
|
|
|
** @param[out] wp Write pointer is placed here
|
|
|
|
**
|
|
|
|
** @returns The number of bytes that could be placed in the ring
|
|
|
|
** buffer at the write pointer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferGetWritePointer(RingBuffer *rb, void **wp) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
size_t cnt;
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// Total free bytes available in ring buffer
|
2018-08-19 11:45:46 +02:00
|
|
|
cnt = rb->Size - atomic_read(&rb->Filled);
|
|
|
|
|
|
|
|
*wp = rb->WritePointer;
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->WritePointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n <= cnt) { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
return n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Advance read pointer in ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to advance read pointer.
|
|
|
|
** @param cnt Number of bytes to be advanced.
|
|
|
|
**
|
|
|
|
** @returns Number of bytes that could be advanced in ring buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferReadAdvance(RingBuffer *rb, size_t cnt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = atomic_read(&rb->Filled);
|
2021-12-27 20:02:45 +01:00
|
|
|
if (cnt > n) { // not enough filled
|
2019-10-26 18:42:19 +02:00
|
|
|
cnt = n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->ReadPointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n > cnt) { // don't cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
rb->ReadPointer += cnt;
|
2021-12-27 20:02:45 +01:00
|
|
|
} else { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
rb->ReadPointer = rb->Buffer;
|
|
|
|
if (n < cnt) {
|
|
|
|
n = cnt - n;
|
|
|
|
rb->ReadPointer += n;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Only atomic modification!
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
atomic_sub(cnt, &rb->Filled);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Read from a ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to read from.
|
|
|
|
** @param buf Buffer of @p cnt bytes.
|
|
|
|
** @param cnt Number of bytes to be read.
|
|
|
|
**
|
|
|
|
** @returns Number of bytes that could be read from ring buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferRead(RingBuffer *rb, void *buf, size_t cnt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = atomic_read(&rb->Filled);
|
2021-12-27 20:02:45 +01:00
|
|
|
if (cnt > n) { // not enough filled
|
2019-10-26 18:42:19 +02:00
|
|
|
cnt = n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->ReadPointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n > cnt) { // don't cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
memcpy(buf, rb->ReadPointer, cnt);
|
|
|
|
rb->ReadPointer += cnt;
|
2021-12-27 20:02:45 +01:00
|
|
|
} else { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
memcpy(buf, rb->ReadPointer, n);
|
|
|
|
rb->ReadPointer = rb->Buffer;
|
|
|
|
if (n < cnt) {
|
|
|
|
buf += n;
|
|
|
|
n = cnt - n;
|
|
|
|
memcpy(buf, rb->ReadPointer, n);
|
|
|
|
rb->ReadPointer += n;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Only atomic modification!
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
atomic_sub(cnt, &rb->Filled);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Get read pointer and used bytes at this position of ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer to read from.
|
|
|
|
** @param[out] rp Read pointer is placed here
|
|
|
|
**
|
|
|
|
** @returns The number of bytes that could be read from the ring
|
|
|
|
** buffer at the read pointer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferGetReadPointer(RingBuffer *rb, const void **rp) {
|
2018-08-19 11:45:46 +02:00
|
|
|
size_t n;
|
|
|
|
size_t cnt;
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// Total used bytes in ring buffer
|
2018-08-19 11:45:46 +02:00
|
|
|
cnt = atomic_read(&rb->Filled);
|
|
|
|
|
|
|
|
*rp = rb->ReadPointer;
|
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Hitting end of buffer?
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
|
|
|
n = rb->BufferEnd - rb->ReadPointer;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (n <= cnt) { // reached or cross the end
|
2019-10-26 18:42:19 +02:00
|
|
|
return n;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** Get free bytes in ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer.
|
|
|
|
**
|
|
|
|
** @returns Number of bytes free in buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferFreeBytes(RingBuffer *rb) { return rb->Size - atomic_read(&rb->Filled); }
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get used bytes in ring buffer.
|
|
|
|
**
|
|
|
|
** @param rb Ring buffer.
|
|
|
|
**
|
|
|
|
** @returns Number of bytes used in buffer.
|
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
size_t RingBufferUsedBytes(RingBuffer *rb) { return atomic_read(&rb->Filled); }
|