-
Notifications
You must be signed in to change notification settings - Fork 0
/
circBufT.c
executable file
·74 lines (67 loc) · 2.07 KB
/
circBufT.c
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
// *******************************************************
//
// circBufT.c
//
// Support for a circular buffer of uint32_t values on the
// Tiva processor.
// P.J. Bones UCECE
// Last modified: 8.3.2017
//
// *******************************************************
#include <stdint.h>
#include "stdlib.h"
#include "circBufT.h"
// *******************************************************
// initCircBuf: Initialise the circBuf instance. Reset both indices to
// the start of the buffer. Dynamically allocate and clear the the
// memory and return a pointer for the data. Return NULL if
// allocation fails.
uint32_t *
initCircBuf (circBuf_t *buffer, uint32_t size)
{
buffer->windex = 0;
buffer->rindex = 0;
buffer->size = size;
buffer->data =
(uint32_t *) calloc (size, sizeof(uint32_t));
return buffer->data;
}
// Note use of calloc() to clear contents.
// *******************************************************
// writeCircBuf: insert entry at the current windex location,
// advance windex, modulo (buffer size).
void
writeCircBuf (circBuf_t *buffer, uint32_t entry)
{
buffer->data[buffer->windex] = entry;
buffer->windex++;
if (buffer->windex >= buffer->size)
buffer->windex = 0;
}
// *******************************************************
// readCircBuf: return entry at the current rindex location,
// advance rindex, modulo (buffer size). The function deos not check
// if reading has advanced ahead of writing.
uint32_t
readCircBuf (circBuf_t *buffer)
{
uint32_t entry;
entry = buffer->data[buffer->rindex];
buffer->rindex++;
if (buffer->rindex >= buffer->size)
buffer->rindex = 0;
return entry;
}
// *******************************************************
// freeCircBuf: Releases the memory allocated to the buffer data,
// sets pointer to NULL and ohter fields to 0. The buffer can
// re-initialised by another call to initCircBuf().
void
freeCircBuf (circBuf_t * buffer)
{
buffer->windex = 0;
buffer->rindex = 0;
buffer->size = 0;
free (buffer->data);
buffer->data = NULL;
}