-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtb_fifo.h
More file actions
173 lines (150 loc) · 2.49 KB
/
tb_fifo.h
File metadata and controls
173 lines (150 loc) · 2.49 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef TB_UTIL_FIFO_H
#define TB_UTIL_FIFO_H
/**************
* Structures *
**************/
/*
* TB fifo header.
*/
typedef struct tb_fifo_elm {
/* Elements of the same fifo. */
tb_list elms;
/* Implementation struct. */
void *impl;
} tb_fifo_elm;
/*
* The tb fifo stores internally allocated structs.
*/
typedef struct tb_fifo {
/* Elements. */
tb_list elms;
/* Number of elements. */
uaddr nb;
/* Size of elements implementation. */
size_t size;
/*
* Element destructor.
* Does not free memory.
*/
void (*elm_dtor)(
void *impl
);
/*
* Log.
*/
void (*elm_log)(
void *elm
);
} tb_fifo;
/*******
* API *
*******/
/*
* Construct a fifo.
*/
tb_fifo *tb_fifo_ctor(
size_t size,
void (*elm_dtor)(void *),
void (*elm_log)(void *)
);
/*
* Destruct a fifo, delete all its elements.
*/
void tb_fifo_dtor(
tb_fifo *fifo
);
/*
* Print the content of @fifo.
*/
void tb_fifo_log(
tb_fifo *fifo
);
/*
* 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
);
/*
* 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
);
/*
* Delete the first element of the fifo.
* It must exist.
*/
void tb_fifo_del(
tb_fifo *fifo
);
/*
* Return the number of elements in @fifo.
*/
uaddr tb_fifo_nb(
tb_fifo *fifo
);
/**********************
* Implementation API *
**********************/
/*
* Generate an API for a fifo managing instances of type '@type'.
*/
#define TB_FIFO_API(type, type_dtor, type_log) \
typedef tb_fifo type##_fifo; \
static inline type##_fifo *type##_fifo_ctor( \
void \
) { \
return tb_fifo_ctor( \
sizeof(type), \
(void (*)(void *)) type_dtor, \
(void (*)(void *)) type_log \
); \
} \
static inline void type##_fifo_dtor( \
type##_fifo *fifo \
) { \
return tb_fifo_dtor( \
fifo \
); \
} \
static inline void type##_fifo_log( \
type##_fifo *fifo \
) { \
return tb_fifo_log( \
fifo \
); \
} \
static inline void type##_fifo_push( \
type##_fifo *fifo, \
type *init \
) { \
return tb_fifo_push( \
fifo, \
init, \
sizeof(type) \
); \
} \
static inline type *type##_fifo_read( \
type##_fifo *fifo \
) { \
return tb_fifo_read( \
fifo, \
sizeof(type) \
); \
} \
static inline void type##_fifo_del( \
type##_fifo *fifo \
) { \
return tb_fifo_del( \
fifo \
); \
}
#endif /* TB_UTIL_FIFO_H */