-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_buffer.h
More file actions
98 lines (83 loc) · 2.9 KB
/
ring_buffer.h
File metadata and controls
98 lines (83 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* @file ring_buffer.h
* @author Lukasz Krzak
* @modified by Michal Bogon
* @date 16'th Mar 2021
* @brief File containing ring buffer functions declarations and structure RingBuffer.
* @ver 1.0
*/
#ifndef _RING_BUFFER_
#define _RING_BUFFER_
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
/** Structure describing the ring buffer. */
typedef struct {
/// Compose your structure here!
int head;
int tail;
char* buffer;
size_t capacity;
bool isFull;
} RingBuffer;
/**
* Initializes the given ring buffer structure.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @param dataBuffer pointer to a location in memory, where the ring buffer data will be stored
* @param dataBufferSize size in bytes of the dataBuffer
* @return true if all arguments are valid and the ring buffer is initialized successfully, false otherwise
*/
bool RingBuffer_Init(RingBuffer *ringBuffer, char *dataBuffer, size_t dataBufferSize);
/**
* Clears contents of the given ring buffer.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return true if the ring buffer is cleared successfully, false otherwise
*/
bool RingBuffer_Clear(RingBuffer *ringBuffer);
/**
* Checks if the given ring buffer is empty.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return true if the ring buffer holds no data, false otherwise
*/
bool RingBuffer_IsEmpty(const RingBuffer *ringBuffer);
/**
* Gets the length (in bytes) of the data stored in the given ring buffer.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return length (in bytes) of the data stored in the ring buffer
*/
size_t RingBuffer_GetLen(const RingBuffer *ringBuffer);
/**
* Returns the capacity (in bytes) of the given buffer.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return capacity (in bytes) of the ring buffer (how much characters can it store)
*/
size_t RingBuffer_GetCapacity(const RingBuffer *ringBuffer);
/**
* Appends a single character to the ring buffer. The stored data length will be
* increased by 1.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return true if the character was added successfully, false otherwise
*/
bool RingBuffer_PutChar(RingBuffer *ringBuffer, char c);
/**
* Pulls out a single character from the ring buffer. The stored data length will be
* decreased by 1.
*
* @param ringBuffer pointer to a \ref RingBuffer structure
* @return true if the character was pulled out successfully, false otherwise
*/
bool RingBuffer_GetChar(RingBuffer *ringBuffer, char *c);
/**
* Clears the whole buffer within its capacity, putting NULL signs,
* and setting flag isFull = false, and integers head = 0, tail = 0
*
* @param ringBuffer pointer to a \ref RingBuffer structure
*/
void ClearBuffer(RingBuffer *ringBuffer);
#endif //_RING_BUFFER_