libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_ring_buffer.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblRingBuffer container and related functions
3 * \ingroup containers
4 * \copydoc GblRingBuffer
5 *
6 * \todo
7 * - since nothing is public, make it an opaque struct
8 * with its buffer appended to the end of the same
9 * allocation
10 *
11 * \author Falco Girgis
12 */
13
14#ifndef GIMBAL_RING_BUFFER_H
15#define GIMBAL_RING_BUFFER_H
16
17#include "../core/gimbal_typedefs.h"
18#include "../core/gimbal_ctx.h"
19#include <stdlib.h>
20
21#define GBL_SELF_TYPE GblRingBuffer
22
24
25/*! \brief Contiguous, array-based circular/ring buffer with queue semantics
26 *
27 * GblRingBuffer is a generic container of fixed capacity,
28 * to which elements may be enqueued or dequeued without requiring any
29 * shuffling around in memory. Rather than resizing when the capacity is
30 * reached, any new values simply overrwrite the oldest values.
31 * Insertions and removals are performed semantically like a queue:
32 * pushing to the back and popping from the front; however, since the
33 * container is implemented as a contiguous array, it is also randomly
34 * accessible.
35 *
36 * Operation |Time Complexity
37 * ----------------------------------|------------------
38 * iteration | O(N)
39 * insertion (back) | O(1)
40 * removal (front) | O(1)
41 * access (front or back) | O(1)
42 * random access (middle) | O(1)
43 *
44 * These properties make this structure well-suited for FIFO or queue
45 * style structures of fixed sizes as well as for representing stream
46 * buffers in memory, where a producer is pushing new entries while a
47 * consumer pops them.
48 *
49 * \ingroup containers
50 * \sa GblDeque, GblArrayList
51 */
52typedef struct GblRingBuffer { // Size (32/64-bit)
54 GblContext* pCtx; // 4/8 bytes
55 uint8_t* pData; // 4/8 bytes
56 size_t size; // 4/8 bytes
57 size_t capacity; // 4/8 bytes
58 size_t frontPos; // 4/8 bytes
59 uint16_t elementSize; // 2 bytes
61} GblRingBuffer; // 22/42 total
62
63GBL_EXPORT GBL_RESULT GblRingBuffer_construct_6(GBL_SELF,
64 uint16_t elementSize,
65 size_t capacity,
66 size_t initialSize,
67 const void* pInitialData,
68 GblContext* pCtx) GBL_NOEXCEPT;
69
70GBL_EXPORT GBL_RESULT GblRingBuffer_construct_5(GBL_SELF,
71 uint16_t elementSize,
72 size_t capacity,
73 size_t initialSize,
74 const void* pInitialData) GBL_NOEXCEPT;
75
76GBL_EXPORT GBL_RESULT GblRingBuffer_construct_4(GBL_SELF,
77 uint16_t elementSize,
78 size_t capacity,
79 size_t initialSize) GBL_NOEXCEPT;
80
81GBL_EXPORT GBL_RESULT GblRingBuffer_construct_3(GBL_SELF,
82 uint16_t elementSize,
83 size_t capacity) GBL_NOEXCEPT;
84
85#define GblRingBuffer_construct(...) GBL_VA_OVERLOAD_CALL(GblRingBuffer_construct,
87 __VA_ARGS__)
88
89GBL_EXPORT GBL_RESULT GblRingBuffer_destruct (GBL_SELF) GBL_NOEXCEPT;
90GBL_EXPORT GBL_RESULT GblRingBuffer_copy (GBL_SELF, const GblRingBuffer* pOther) GBL_NOEXCEPT;
91GBL_EXPORT GBL_RESULT GblRingBuffer_move (GBL_SELF, GblRingBuffer* pOther) GBL_NOEXCEPT;
92
93GBL_EXPORT GblContext* GblRingBuffer_context (GBL_CSELF) GBL_NOEXCEPT;
94GBL_EXPORT size_t GblRingBuffer_capacity (GBL_CSELF) GBL_NOEXCEPT;
95GBL_EXPORT size_t GblRingBuffer_size (GBL_CSELF) GBL_NOEXCEPT;
96GBL_EXPORT size_t GblRingBuffer_elementSize (GBL_CSELF) GBL_NOEXCEPT;
97
98GBL_EXPORT GblBool GblRingBuffer_empty (GBL_CSELF) GBL_NOEXCEPT;
99GBL_EXPORT GblBool GblRingBuffer_full (GBL_CSELF) GBL_NOEXCEPT;
100
101GBL_EXPORT void* GblRingBuffer_at (GBL_CSELF, size_t index) GBL_NOEXCEPT;
102GBL_EXPORT void* GblRingBuffer_front (GBL_CSELF) GBL_NOEXCEPT;
103GBL_EXPORT void* GblRingBuffer_back (GBL_CSELF) GBL_NOEXCEPT;
104
105GBL_EXPORT GBL_RESULT GblRingBuffer_pushBack (GBL_SELF, const void* pData) GBL_NOEXCEPT;
106GBL_EXPORT void* GblRingBuffer_emplaceBack (GBL_SELF) GBL_NOEXCEPT;
107
108GBL_EXPORT void* GblRingBuffer_popFront (GBL_SELF) GBL_NOEXCEPT;
109GBL_EXPORT void GblRingBuffer_clear (GBL_SELF) GBL_NOEXCEPT;
110
112
113#undef GBL_SELF_TYPE
114
115#endif // GIMBAL_RING_BUFFER_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_PRIVATE_BEGIN
#define GBL_EXPORT
#define GBL_PRIVATE_END
Private data structure.
#define GBL_VA_OVERLOAD_CALL(BASE, SUFFIXER,...)
#define GBL_VA_OVERLOAD_SUFFIXER_ARGC(...)
#define GblRingBuffer_construct(...)
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
Contiguous, array-based circular/ring buffer with queue semantics.