-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_buffer.c
More file actions
125 lines (105 loc) · 3 KB
/
circular_buffer.c
File metadata and controls
125 lines (105 loc) · 3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "circular_buffer.h"
#include <stdlib.h>
#include <string.h>
struct CircularBuffer {
void* buffer;
size_t head;
size_t tail;
size_t length;
size_t unitsize;
bool full;
};
CircularBufferHandle CircularBufInit(void* buffer, size_t length, size_t unitsize){
if ((buffer == NULL)||(length == 0)||(unitsize == 0)){
return NULL;
}
CircularBufferHandle c_buf = (CircularBuffer*) malloc(sizeof(CircularBuffer));
if (c_buf == NULL){
return NULL;
}
c_buf->buffer = buffer;
CircularBufReset(c_buf);
c_buf->unitsize = unitsize;
c_buf->length = length;
return c_buf;
}
C_Buffer_Status CircularBufReset(CircularBufferHandle c_buf){
if(c_buf == NULL)
return ERR_HANDLE_NULL;
c_buf->head = 0;
c_buf->tail = 0;
c_buf->full = false;
return SUCC_BUF_RESET;
}
C_Buffer_Status CircularBufFree(CircularBufferHandle c_buf){
if(c_buf == NULL)
return ERR_HANDLE_NULL;
free(c_buf);
return SUCC_BUF_FREE;
}
bool CircularBufFull(CircularBufferHandle c_buf){
return c_buf->full;
}
bool CircularBufEmpty(CircularBufferHandle c_buf){
return (!c_buf->full)&&(c_buf->tail==c_buf->head);
}
size_t CircularBufCapacity(CircularBufferHandle c_buf){
return c_buf->length;
}
size_t CircularBufUnitSize(CircularBufferHandle c_buf){
return c_buf->unitsize;
}
size_t CircularBufSize(CircularBufferHandle c_buf){
if(c_buf->full)
return c_buf->length;
if(c_buf->head >= c_buf->tail)
return c_buf->head-c_buf->tail;
return c_buf->length + c_buf->head - c_buf->tail;
}
static void _advancepointer(CircularBufferHandle c_buf){
if(c_buf->full)
c_buf->tail = (c_buf->tail + c_buf->unitsize)%c_buf->length;
c_buf->head = (c_buf->head + c_buf->unitsize)%c_buf->length;
c_buf->full = (c_buf->head == c_buf->tail);
}
static void _recedepointer(CircularBufferHandle c_buf){
c_buf->full = false;
c_buf->tail = (c_buf->tail + c_buf->unitsize)%c_buf->length;
}
C_Buffer_Status CircularBufInsert(CircularBufferHandle c_buf, void* data, size_t datasize){
if(c_buf == NULL)
return ERR_HANDLE_NULL;
if (data == NULL)
return ERR_DATA_NULL;
if (datasize != c_buf->unitsize)
return ERR_DATASIZE_UNEQ;
memcpy((void*)((char*)c_buf->buffer+c_buf->head), data, c_buf->unitsize);
_advancepointer(c_buf);
return SUCC_BUF_INS;
}
C_Buffer_Status CircularBufInsert_V2(CircularBufferHandle c_buf, void* data, size_t datasize){
if(c_buf == NULL)
return ERR_HANDLE_NULL;
if (data == NULL)
return ERR_DATA_NULL;
if(c_buf->full)
return ERR_BUF_FULL;
if (datasize != c_buf->unitsize)
return ERR_DATASIZE_UNEQ;
memcpy((void*)((char*)c_buf->buffer+c_buf->head), data, c_buf->unitsize);
_advancepointer(c_buf);
return SUCC_BUF_INS;
}
C_Buffer_Status CircularBufGet(CircularBufferHandle c_buf, void* data, size_t datasize){
if(c_buf == NULL)
return ERR_HANDLE_NULL;
if (data == NULL)
return ERR_DATA_NULL;
if(CircularBufEmpty(c_buf))
return ERR_BUF_EMPTY;
if (datasize != c_buf->unitsize)
return ERR_DATASIZE_UNEQ;
memcpy(data, (void*)((char*)c_buf->buffer+c_buf->tail), c_buf->unitsize);
_recedepointer(c_buf);
return SUCC_BUF_INS;
}