-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.h
More file actions
79 lines (62 loc) · 2.13 KB
/
buffer.h
File metadata and controls
79 lines (62 loc) · 2.13 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
/*
* buffer.h
*
* Created: 24/2/2018 4:03:06 PM
* Author: dcstanc
*/
#ifndef BUFFER_H_
#define BUFFER_H_
typedef struct tb
{
// The actual buffer
unsigned char *buffer;
// Front and back pointers
unsigned int front, back;
// Our buffer size
unsigned int size;
// Number of characters in buffer
unsigned int count;
// This is for atomicity control
unsigned char csreg;
} TBuffer;
typedef enum
{
BUFFER_OK,
BUFFER_FULL,
BUFFER_EMPTY,
BUFFER_INVALID
} TBufferResult;
// Intialize the buffer. We must call this before using writeBuffer or readBuffer.
// PRE:
// Buffer to use is specified in "buffer", size of buffer in characters is specified in "size"
// POST:
// Buffer is initialized to accept up to "size" characters.
void initBuffer(TBuffer *buffer, unsigned int size);
// Write to the buffer.
// PRE:
// Buffer specified in "buffer" argument must be initialized using initBuffer. "data" is character to
// write to buffer.
// POST:
// "data" is written to "buffer" and writeBuffer returns BUFFER_OK if space is available.
// "data" is discarded and writeBuffer returns BUFFER_FULL if space is not available.
// "data" is discarded and writeBuffer returns BUFFER_INVALID if "buffer" was not initialized using initBuffer.
TBufferResult writeBuffer(TBuffer *buffer, unsigned char data);
// Read from the buffer.
// PRE:
// Buffer specified in "buffer" argument must be initialized using initBuffer. "data" is a pointer
// to a variable of type unsigned char.
// POST:
// Variable pointed to by "data" contains character read from head of the queue, and readBuffer returns
// BUFFER_OK if data is available for reading.
// Variable pointed to by "data" is unmodified, and readBuffer returns BUFFER_EMTPY if no data is available.
// Variable pointed to by "data" is unmodified, and readBuffer returns BUFFER_INVALID if "buffer" was
// not initialized using initBuffer.
TBufferResult readBuffer(TBuffer *buffer, unsigned char *data);
// Frees buffer
// PRE:
// "buffer" points to buffer to free
// POST:
// "buffer" is deallocated.
void freeBuffer(TBuffer *buffer);
int dataAvailable(TBuffer *buffer);
#endif /* BUFFER_H_ */