forked from ashishc/Http-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounded_buffer.c
52 lines (45 loc) · 897 Bytes
/
bounded_buffer.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
#include "bounded_buffer.h"
bounded_buffer_t bb;
void init_buffer(int buffer_max)
{
bb.buffer = (int*)malloc(sizeof(int)*buffer_max);
bb.buffer_max =buffer_max;
}
int buffer_add(int item)
{
if(bb.buf_in == INT_MAX) bb.buf_in = 0;
if((bb.buf_in - bb.buf_out) >= 0 && abs(bb.buf_in - bb.buf_out) < bb.buffer_max)
{
bb.buffer[ bb.buf_in % bb.buffer_max ] = item;
bb.buf_in ++;
return 1;
}
return 0;
}
int buffer_get()
{
if(bb.buf_out == INT_MAX) bb.buf_out = 0;
if((bb.buf_in - bb.buf_out) > 0 && abs(bb.buf_in - bb.buf_out) < bb.buffer_max)
{
int peer_sfd = bb.buffer[ bb.buf_out % bb.buffer_max ];
bb.buf_out++;
return peer_sfd;
}
return -1;
}
int is_buffer_full()
{
if(abs(bb.buf_out - bb.buf_in) == bb.buffer_max-1)
return 1;
return 0;
}
int is_buffer_empty()
{
if(bb.buf_in == bb.buf_out)
return 1;
return 0;
}
void destroy_buffer()
{
free(bb.buffer);
}