-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtb_fifo.c
More file actions
123 lines (114 loc) · 1.91 KB
/
tb_fifo.c
File metadata and controls
123 lines (114 loc) · 1.91 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "tb_all.h"
static inline void _elm_dtor(
tb_fifo *fifo,
tb_fifo_elm *elm
)
{
tb_list_rmu(&elm->elms);
(*(fifo->elm_dtor))(elm->impl);
free(elm->impl);
free(elm);
}
/*
* Construct a fifo.
*/
tb_fifo *tb_fifo_ctor(
size_t size,
void (*elm_dtor)(void *),
void (*elm_log)(void *)
)
{
assert(size);
assert(elm_dtor);
assert(elm_log);
malloc__(tb_fifo, fifo);
tb_list_init(&fifo->elms);
fifo->size = size;
fifo->elm_dtor = elm_dtor;
fifo->elm_log = elm_log;
fifo->nb = 0;
return fifo;
}
/*
* Destruct a fifo, delete all its elements.
*/
void tb_fifo_dtor(
tb_fifo *fifo
)
{
tb_fifo_elm *elm;
tb_list_fes(elm, &fifo->elms, elms) {
_elm_dtor(fifo, elm);
}
free(fifo);
}
/*
* Print the content of @fifo.
*/
void tb_fifo_log(
tb_fifo *fifo
)
{
tb_fifo_elm *elm;
tb_list_fes(elm, &fifo->elms, elms) {
(*(fifo->elm_log))(elm->impl);
}
}
/*
* Allocate a new element, initialize it with @init, and push it
* at the end of the fifo.
* @size must be @fifo->size.
*/
void tb_fifo_push(
tb_fifo *fifo,
void *init,
size_t size
)
{
malloc__(tb_fifo_elm, elm);
void *dst = malloc(fifo->size);
memcpy(dst, init, fifo->size);
tb_list_ib(&fifo->elms, &elm->elms);
elm->impl = dst;
SAFE_INCR(fifo->nb);
}
/*
* Read the first element of the fifo, return its pointer.
* @size must be @fifo->size.
* If @fifo is empty, return 0.
*/
void *tb_fifo_read(
tb_fifo *fifo,
uaddr size
)
{
tb_fifo_elm *elm = 0;
if (tb_list_emptyne(&fifo->elms, elm, tb_fifo_elm, elms)) return 0;
assert(elm);
assert(elm->impl);
return elm->impl;
}
/*
* Delete the first element of the fifo.
* It must exist.
*/
void tb_fifo_del(
tb_fifo *fifo
)
{
tb_fifo_elm *elm = 0;
u8 empty = tb_list_emptyne(&fifo->elms, elm, tb_fifo_elm, elms);
assert(!empty);
assert(elm);
_elm_dtor(fifo, elm);
SAFE_DECR(fifo->nb);
}
/*
* Return the number of elements in @fifo.
*/
uaddr tb_fifo_nb(
tb_fifo *fifo
)
{
return fifo->nb;
}