- switch from indent to clang-format, which is supported by Visual Studio Code

- reindent all sources to common code style
- fix include sort errors from clang-format
- update AGPL 3.0 license file
This commit is contained in:
Dirk Nehring
2021-12-27 20:02:45 +01:00
parent 987ff6ba12
commit 177e44de98
29 changed files with 4309 additions and 5337 deletions

View File

@@ -1,5 +1,5 @@
///
/// @file ringbuffer.c @brief Ringbuffer module
/// @file ringbuffer.c @brief Ringbuffer module
///
/// Copyright (c) 2009, 2011, 2014 by Johns. All Rights Reserved.
///
@@ -34,17 +34,16 @@
#include "ringbuffer.h"
/// ring buffer structure
struct _ring_buffer_
{
char *Buffer; ///< ring buffer data
const char *BufferEnd; ///< end of buffer
size_t Size; ///< bytes in buffer (for faster calc)
struct _ring_buffer_ {
char *Buffer; ///< ring buffer data
const char *BufferEnd; ///< end of buffer
size_t Size; ///< bytes in buffer (for faster calc)
const char *ReadPointer; ///< only used by reader
char *WritePointer; ///< only used by writer
const char *ReadPointer; ///< only used by reader
char *WritePointer; ///< only used by writer
/// The only thing modified by both
atomic_t Filled; ///< how many of the buffer is used
atomic_t Filled; ///< how many of the buffer is used
};
/**
@@ -52,8 +51,7 @@ struct _ring_buffer_
**
** @param rb Ring buffer to reset read/write pointers.
*/
void RingBufferReset(RingBuffer * rb)
{
void RingBufferReset(RingBuffer *rb) {
rb->ReadPointer = rb->Buffer;
rb->WritePointer = rb->Buffer;
atomic_set(&rb->Filled, 0);
@@ -67,11 +65,10 @@ void RingBufferReset(RingBuffer * rb)
** @returns Allocated ring buffer, must be freed with
** RingBufferDel(), NULL for out of memory.
*/
RingBuffer *RingBufferNew(size_t size)
{
RingBuffer *RingBufferNew(size_t size) {
RingBuffer *rb;
if (!(rb = malloc(sizeof(*rb)))) { // allocate structure
if (!(rb = malloc(sizeof(*rb)))) { // allocate structure
return rb;
}
if (!(rb->Buffer = malloc(size))) { // allocate buffer
@@ -89,8 +86,7 @@ RingBuffer *RingBufferNew(size_t size)
/**
** Free an allocated ring buffer.
*/
void RingBufferDel(RingBuffer * rb)
{
void RingBufferDel(RingBuffer *rb) {
free(rb->Buffer);
free(rb);
}
@@ -103,21 +99,20 @@ void RingBufferDel(RingBuffer * rb)
**
** @returns Number of bytes that could be advanced in ring buffer.
*/
size_t RingBufferWriteAdvance(RingBuffer * rb, size_t cnt)
{
size_t RingBufferWriteAdvance(RingBuffer *rb, size_t cnt) {
size_t n;
n = rb->Size - atomic_read(&rb->Filled);
if (cnt > n) { // not enough space
if (cnt > n) { // not enough space
cnt = n;
}
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n > cnt) { // don't cross the end
if (n > cnt) { // don't cross the end
rb->WritePointer += cnt;
} else { // reached or cross the end
} else { // reached or cross the end
rb->WritePointer = rb->Buffer;
if (n < cnt) {
n = cnt - n;
@@ -126,7 +121,7 @@ size_t RingBufferWriteAdvance(RingBuffer * rb, size_t cnt)
}
//
// Only atomic modification!
// Only atomic modification!
//
atomic_add(cnt, &rb->Filled);
return cnt;
@@ -142,22 +137,21 @@ size_t RingBufferWriteAdvance(RingBuffer * rb, size_t cnt)
** @returns The number of bytes that could be placed in the ring
** buffer.
*/
size_t RingBufferWrite(RingBuffer * rb, const void *buf, size_t cnt)
{
size_t RingBufferWrite(RingBuffer *rb, const void *buf, size_t cnt) {
size_t n;
n = rb->Size - atomic_read(&rb->Filled);
if (cnt > n) { // not enough space
if (cnt > n) { // not enough space
cnt = n;
}
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n > cnt) { // don't cross the end
if (n > cnt) { // don't cross the end
memcpy(rb->WritePointer, buf, cnt);
rb->WritePointer += cnt;
} else { // reached or cross the end
} else { // reached or cross the end
memcpy(rb->WritePointer, buf, n);
rb->WritePointer = rb->Buffer;
if (n < cnt) {
@@ -169,7 +163,7 @@ size_t RingBufferWrite(RingBuffer * rb, const void *buf, size_t cnt)
}
//
// Only atomic modification!
// Only atomic modification!
//
atomic_add(cnt, &rb->Filled);
return cnt;
@@ -184,21 +178,20 @@ size_t RingBufferWrite(RingBuffer * rb, const void *buf, size_t cnt)
** @returns The number of bytes that could be placed in the ring
** buffer at the write pointer.
*/
size_t RingBufferGetWritePointer(RingBuffer * rb, void **wp)
{
size_t RingBufferGetWritePointer(RingBuffer *rb, void **wp) {
size_t n;
size_t cnt;
// Total free bytes available in ring buffer
// Total free bytes available in ring buffer
cnt = rb->Size - atomic_read(&rb->Filled);
*wp = rb->WritePointer;
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n <= cnt) { // reached or cross the end
if (n <= cnt) { // reached or cross the end
return n;
}
return cnt;
@@ -212,21 +205,20 @@ size_t RingBufferGetWritePointer(RingBuffer * rb, void **wp)
**
** @returns Number of bytes that could be advanced in ring buffer.
*/
size_t RingBufferReadAdvance(RingBuffer * rb, size_t cnt)
{
size_t RingBufferReadAdvance(RingBuffer *rb, size_t cnt) {
size_t n;
n = atomic_read(&rb->Filled);
if (cnt > n) { // not enough filled
if (cnt > n) { // not enough filled
cnt = n;
}
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n > cnt) { // don't cross the end
if (n > cnt) { // don't cross the end
rb->ReadPointer += cnt;
} else { // reached or cross the end
} else { // reached or cross the end
rb->ReadPointer = rb->Buffer;
if (n < cnt) {
n = cnt - n;
@@ -235,7 +227,7 @@ size_t RingBufferReadAdvance(RingBuffer * rb, size_t cnt)
}
//
// Only atomic modification!
// Only atomic modification!
//
atomic_sub(cnt, &rb->Filled);
return cnt;
@@ -250,22 +242,21 @@ size_t RingBufferReadAdvance(RingBuffer * rb, size_t cnt)
**
** @returns Number of bytes that could be read from ring buffer.
*/
size_t RingBufferRead(RingBuffer * rb, void *buf, size_t cnt)
{
size_t RingBufferRead(RingBuffer *rb, void *buf, size_t cnt) {
size_t n;
n = atomic_read(&rb->Filled);
if (cnt > n) { // not enough filled
if (cnt > n) { // not enough filled
cnt = n;
}
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n > cnt) { // don't cross the end
if (n > cnt) { // don't cross the end
memcpy(buf, rb->ReadPointer, cnt);
rb->ReadPointer += cnt;
} else { // reached or cross the end
} else { // reached or cross the end
memcpy(buf, rb->ReadPointer, n);
rb->ReadPointer = rb->Buffer;
if (n < cnt) {
@@ -277,7 +268,7 @@ size_t RingBufferRead(RingBuffer * rb, void *buf, size_t cnt)
}
//
// Only atomic modification!
// Only atomic modification!
//
atomic_sub(cnt, &rb->Filled);
return cnt;
@@ -292,21 +283,20 @@ size_t RingBufferRead(RingBuffer * rb, void *buf, size_t cnt)
** @returns The number of bytes that could be read from the ring
** buffer at the read pointer.
*/
size_t RingBufferGetReadPointer(RingBuffer * rb, const void **rp)
{
size_t RingBufferGetReadPointer(RingBuffer *rb, const void **rp) {
size_t n;
size_t cnt;
// Total used bytes in ring buffer
// Total used bytes in ring buffer
cnt = atomic_read(&rb->Filled);
*rp = rb->ReadPointer;
//
// Hitting end of buffer?
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n <= cnt) { // reached or cross the end
if (n <= cnt) { // reached or cross the end
return n;
}
return cnt;
@@ -319,10 +309,7 @@ size_t RingBufferGetReadPointer(RingBuffer * rb, const void **rp)
**
** @returns Number of bytes free in buffer.
*/
size_t RingBufferFreeBytes(RingBuffer * rb)
{
return rb->Size - atomic_read(&rb->Filled);
}
size_t RingBufferFreeBytes(RingBuffer *rb) { return rb->Size - atomic_read(&rb->Filled); }
/**
** Get used bytes in ring buffer.
@@ -331,7 +318,4 @@ size_t RingBufferFreeBytes(RingBuffer * rb)
**
** @returns Number of bytes used in buffer.
*/
size_t RingBufferUsedBytes(RingBuffer * rb)
{
return atomic_read(&rb->Filled);
}
size_t RingBufferUsedBytes(RingBuffer *rb) { return atomic_read(&rb->Filled); }