-
Notifications
You must be signed in to change notification settings - Fork 1
/
fifo.cpp
60 lines (54 loc) · 796 Bytes
/
fifo.cpp
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
#include "fifo.hpp"
#include "heap.hpp"
#include "task.hpp"
void FIFO::init(int size){
this->size=size;
this->task=0;
buf=(int *)malloc(size*sizeof(int));
free=size;
flags=0;
w=0;
r=0;
return;
}
void FIFO::init(int size,Task *task){
this->size=size;
this->task=task;
buf=(int *)malloc(size*sizeof(int));
free=size;
flags=0;
w=0;
r=0;
return;
}
void FIFO::remove(){
mfree((uintptr_t)buf,size*4);
destroyed=true;
return;
}
int FIFO::put(int data){
if(free==0){
flags|=FLAGS_OVERRUN;
return -1;
}
buf[w]=data;
w=(w+1)%size;
free--;
if(task!=0)
if(task->stat==SLEEPING)
task_run(task,-1,0);
return 0;
}
int FIFO::get(){
int data;
if(free==size){
return -1;
}
data=buf[r];
r=(r+1)%size;
free++;
return data;
}
int FIFO::status(){
return size-free;
}