-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer.h
54 lines (45 loc) · 1.07 KB
/
buffer.h
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
/*******************************************************************************
* This file is part of AvsVCEh264.
* Contains declaration & functions for circularBuffer
*
* Copyright (C) 2013 David González García <[email protected]>
*******************************************************************************/
#include <malloc.h>
typedef void* BufferType;
typedef struct
{
BYTE write;
BYTE read;
BufferType keys[0];
} Buffer;
Buffer* newBuffer()
{
int sz = 256 * sizeof(BufferType) + sizeof(Buffer);
Buffer *que = (Buffer*) malloc(sz);
que->write = que->read = 0;
return que;
}
inline bool BufferIsFull(Buffer *que)
{
return que->read == (BYTE)(que->write + 1);
}
inline bool BufferIsEmpty(Buffer *que)
{
return que->read == que->write;
}
inline bool BufferWrite(Buffer *que, BufferType k)
{
if (BufferIsFull(que))
return false;
que->keys[que->write] = k;
que->write++;
return true;
}
inline int BufferRead(Buffer *que, BufferType *pK)
{
if (BufferIsEmpty(que))
return 0;
*pK = que->keys[que->read];
que->read++;
return true;
}