-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfl-lib.h
494 lines (426 loc) · 13.5 KB
/
fl-lib.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#ifndef _FL_LIB_H_
#define _FL_LIB_H_
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#define fl_typeof(___zarg) typeof(___zarg)
#define fl_auto_type(___zcxzzarg) fl_typeof(___zcxzzarg)
#include <stddef.h>
#define fl_offsetof(___j1h3oi1bob,___j1h3oi1bob2) offsetof(___j1h3oi1bob,___j1h3oi1bob2)
#define fl_container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - fl_offsetof(type,member) );})
#define FL_BUILD_BUG_ON_ZERO(e) \
(sizeof(struct { int:-!!(e)*1234; }))
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#define FL_GNUC_VERSION \
(__GNUC__ << 16) + __GNUC_MINOR__
#define FL_GNUC_PREREQ(maj, min) \
(FL_GNUC_VERSION >= ((maj) << 16) + (min))
#else
#define FL_GNUC_PREREQ(maj, min) 0
#endif
#include <stddef.h>
#define fl_sizeof(___j1h32314141) sizeof(___j1h32314141)
#if FL_GNUC_PREREQ(3, 1)
#define FL_SAME_TYPE(a, b) \
__builtin_types_compatible_p(fl_typeof(a), fl_typeof(b))
#define FL_MUST_BE_ARRAY(a) \
FL_BUILD_BUG_ON_ZERO(FL_SAME_TYPE((a), &(*a)))
#else
#define FL_MUST_BE_ARRAY(a) \
FL_BUILD_BUG_ON_ZERO(fl_sizeof(a) % fl_sizeof(*a))
#endif
#define FL_ARRAY_SIZE(a) ( \
(fl_sizeof(a) / fl_sizeof(*a)) \
+ FL_MUST_BE_ARRAY(a))
struct fl_struct_array_private
{
size_t max;
size_t top;
};
typedef struct fl_struct_array_private fl_array_t;
static inline void fl_array_init(fl_array_t * obj, size_t max_length)
{
obj->max = max_length;
obj->top = 0;
}
static inline void fl_array_deinit(fl_array_t * obj)
{
obj->max = 0;
obj->top = 0;
}
static inline void fl_array_clear(fl_array_t * obj)
{
obj->top = 0;
}
static inline size_t fl_array_length(fl_array_t * obj)
{
return obj->top;
}
static inline bool fl_array_full(fl_array_t * obj)
{
return obj->top == obj->max;
}
static inline size_t _fl_array_insert_prepare(fl_array_t * obj)
{
if (fl_array_full(obj))
obj->top--;
return obj->top;
}
static inline void _fl_array_insert_end(fl_array_t * obj)
{
obj->top ++;
}
#define fl_array_insert(obj, buffer, data) \
({ \
fl_array_t * _o = (obj); \
buffer[_fl_array_insert_prepare(_o)] = data; \
_fl_array_insert_end(_o); \
})
static inline size_t _fl_array_remove_prepare(fl_array_t * obj, size_t pos)
{
if (pos >= obj->top)
return 0;
return obj->top - 1 - pos;
}
static inline void _fl_array_remove_end(fl_array_t * obj)
{
obj->top --;
}
#define fl_array_remove(obj, buffer, pos) \
({ \
fl_array_t * _o = (obj); \
size_t _p = (pos); \
fl_auto_type (buffer) _b = (buffer); \
size_t n = _fl_array_remove_prepare(_o, _p); \
for (size_t i = 0; i < n; i++) \
_b[_p + i] = _b[_p + i + 1]; \
if (n) _fl_array_remove_end(_o); \
})
#define fl_array_free(obj, buffer, pos, freefunc) \
({ \
fl_array_t * _o = (obj); \
size_t _p = (pos); \
fl_auto_type (buffer) _b = (buffer); \
fl_auto_type (freefunc) _f = (freefunc); \
_f(_b[_p]); \
for (size_t i = 0; i < _fl_array_remove_prepare(_o, _p); i++) \
_b[_p + i] = _b[_p + i + 1]; \
_fl_array_remove_end(_o); \
})
#define fl_array_find(obj, buffer, comparefunc, seed) \
({ \
int _r = -1; \
fl_array_t * _o = (obj); \
fl_auto_type (buffer) _b = (buffer); \
fl_auto_type (seed) _s = (seed); \
fl_auto_type (comparefunc) _f = (comparefunc); \
for (size_t i = 0; i < fl_array_length(_o); i++) \
if (_f(_b[i], _s)) { _r = (int)i; break; } \
_r; \
})
/*
fl_ring_fifo
- Single consumer / single Producer safe
- FIFO's available slots will be rounded down to a power of two integer,
it is a requirement of the SCSP safety algorithm. Use power of two
integer in buffer sizes and `num_of_slots` init parameter to avoid
rounding.
- size_t must be atomic in the platform
*/
typedef struct fl_struct_ring_fifo_private {
size_t mask;
size_t wrIdx;
size_t rdIdx;
} fl_ring_fifo_t;
/*
* num_of_slots must be base of two, i.e. 2, 4, 8, 16, 32, 64, ...
*/
static inline int fl_ring_fifo_init(fl_ring_fifo_t * obj, size_t num_of_slots)
{
size_t pre_mask = (SIZE_MAX >> 1) + 1;
while (pre_mask)
{
if (num_of_slots & pre_mask) {
break;
}
pre_mask >>= 1;
}
if (pre_mask <= 1) {
obj->mask = 0;
return -1;
}
obj->mask = pre_mask - 1;
obj->rdIdx = 0;
obj->wrIdx = 0;
return (int)pre_mask;
}
static inline size_t fl_ring_fifo_num_of_slots(fl_ring_fifo_t * obj)
{
return obj->mask + 1;
}
static inline size_t fl_ring_fifo_write_index(fl_ring_fifo_t * obj)
{
return obj->mask & obj->wrIdx;
}
static inline size_t fl_ring_fifo_read_index(fl_ring_fifo_t * obj)
{
return obj->mask & obj->rdIdx;
}
static inline void fl_ring_fifo_write_increment(fl_ring_fifo_t * obj)
{
obj->wrIdx++;
}
static inline void fl_ring_fifo_read_increment(fl_ring_fifo_t * obj)
{
obj->rdIdx++;
}
static inline void fl_ring_fifo_read_reset(fl_ring_fifo_t * obj)
{
obj->rdIdx = 0;
obj->wrIdx = 0;
}
static inline bool fl_ring_fifo_empty(fl_ring_fifo_t * obj)
{
return obj->rdIdx == obj->wrIdx;
}
static inline size_t fl_ring_fifo_count(fl_ring_fifo_t * obj)
{
return obj->wrIdx - obj->rdIdx;
}
static inline bool fl_ring_fifo_full(fl_ring_fifo_t * obj)
{
return fl_ring_fifo_count (obj) == fl_ring_fifo_num_of_slots (obj);
}
struct fl_struct_linked_list_head
{
struct fl_struct_linked_list_head * next;
struct fl_struct_linked_list_head * prev;
};
typedef struct fl_struct_linked_list_head fl_linked_list_t;
/**
* Initialize a fl_linked_list_t within structure
* @param
*/
#define fl_linked_list_init(structure, field) \
({ \
fl_auto_type(structure) ___str = structure; \
if (___str) {\
___str->field.next = (fl_linked_list_t *)0;\
___str->field.prev = (fl_linked_list_t *)0;\
} \
})
/**
* Get the number of nodes of the list.
*
* @param ptr_to_current
* @return Pointer to the last node of the list, including ptr_to_current.
*/
#define fl_linked_list_count(ptr_to_current, field) \
({ \
int ___ret = 0; \
fl_auto_type(ptr_to_current) ___cur = fl_linked_list_first(ptr_to_current, field); fl_linked_list_t * ___head;\
if (___cur) ___ret++; \
if (___ret) \
{ \
___head = &___cur->field; \
if (___head->next) { while (___head->next){ ___head = ___head->next; ___ret++;}} \
} \
___ret; \
})
/**
* Get the next node.
* @param ptr_to_current
* @return Pointer to the next node.
*/
#define fl_linked_list_next(ptr_to_current, field) \
({ \
fl_auto_type(ptr_to_current) ___ret = ptr_to_current; fl_linked_list_t * ___head;\
if (___ret) \
{ \
___head = ___ret->field.next; \
if (___head) { ___ret = fl_container_of(___head, fl_typeof(*(ptr_to_current)), field); }\
else {___ret = 0; } \
} \
___ret; \
})
/**
* Get the previous node.
* @param ptr_to_current
* @return Pointer to the previous node.
*/
#define fl_linked_list_previous(ptr_to_current, field) \
({ \
fl_auto_type(ptr_to_current) ___ret = ptr_to_current; fl_linked_list_t * ___head;\
if (___ret) \
{ \
___head = ___ret->field.prev; \
if (___head) { ___ret = fl_container_of(___head, fl_typeof(*(ptr_to_current)), field); }\
else {___ret = 0; } \
} \
___ret; \
})
/**
* Get the last node of the list, including ptr_to_current.
*
* @param ptr_to_current
* @return Pointer to the last node of the list, including ptr_to_current.
*/
#define fl_linked_list_last(ptr_to_current, field) \
({ \
fl_auto_type(ptr_to_current) ___ret = ptr_to_current; fl_linked_list_t * ___head;\
if (___ret) \
{ \
___head = &___ret->field; \
if (___head->next) { while (___head->next) ___head = ___head->next;} \
if (___head) { ___ret = fl_container_of(___head, fl_typeof(*(ptr_to_current)), field); } \
else {___ret = 0; } \
} \
___ret; \
})
/**
* Get the first node of the list, including ptr_to_current.
*
* @param ptr_to_current
* @return Pointer to the first node of the list, including ptr_to_current.
*/
#define fl_linked_list_first(ptr_to_current, field) \
({ \
fl_auto_type(ptr_to_current) ___ret2 = ptr_to_current; fl_linked_list_t * ___head;\
if (___ret2) \
{ \
___head = &___ret2->field; \
if (___head->prev) { while (___head->prev) ___head = ___head->prev;} \
if (___head) { ___ret2 = fl_container_of(___head, fl_typeof(*(ptr_to_current)), field); } \
else {___ret2 = 0; } \
} \
___ret2; \
})
/**
* Insert new node between current and current's next.
*
* @param ptr_to_current
* @param ptr_to_new
*/
#define fl_linked_list_insert_after(ptr_to_current, ptr_to_new, field) \
({ \
fl_auto_type(ptr_to_current) ___curr = ptr_to_current; \
fl_auto_type(ptr_to_new) ___new = ptr_to_new; \
if (___curr && ___new) \
{ \
___new->field.next = ___curr->field.next; \
___new->field.prev = &___curr->field; \
if (___curr->field.next) ___curr->field.next->prev = &___new->field; \
___curr->field.next = &___new->field; \
} \
})
/**
* Insert new node between current and current's previous.
*
* @param ptr_to_current
* @param ptr_to_new
*/
#define fl_linked_list_insert_before(ptr_to_current, ptr_to_new, field) \
({ \
fl_auto_type(ptr_to_current) ___curr = ptr_to_current; \
fl_auto_type(ptr_to_new) ___new = ptr_to_new; \
if (___curr && ___new) \
{ \
___new->field.prev = ___curr->field.prev; \
___new->field.next = &___curr->field; \
if(___curr->field.prev) ___curr->field.prev->next = &___new->field; \
___curr->field.prev = &___new->field; \
} \
})
/**
* Remove the current node and return it.
*
* @param current
* @return Return the new or old beginning of the list.
*/
#define fl_linked_list_remove(ptr_to_current, field) \
({ \
fl_auto_type(ptr_to_current) ___cur = ptr_to_current; \
fl_auto_type(ptr_to_current) ___ret = (fl_typeof(ptr_to_current))0; \
fl_linked_list_t * ___head_next = (fl_linked_list_t*)0; \
fl_linked_list_t * ___head_prev = (fl_linked_list_t*)0; \
if (___cur) \
{ \
___head_next = ___cur->field.next; \
___head_prev = ___cur->field.prev; \
if (___head_prev) ___head_prev->next = ___head_next; \
if (___head_next) ___head_next->prev = ___head_prev; \
fl_linked_list_init(___cur, field); \
if (___head_prev) ___ret = fl_linked_list_first(fl_container_of(___head_prev, fl_typeof(*(ptr_to_current)), field), field); \
else if(___head_next) ___ret = fl_linked_list_first(fl_container_of(___head_next, fl_typeof(*(ptr_to_current)), field), field); \
} \
___ret; \
})
/**
* Free all list members,
* but instead calling free itself, this function calls
* user free function passed on free_callback.
*
* @param ptr_to_current
*
* @warning
* Any pointer to the a list node will be invalid.
*/
#define fl_linked_list_free(ptr_to_current, field, free_callback) \
({ \
fl_auto_type(ptr_to_current) ___item = fl_linked_list_first(ptr_to_current, field); \
fl_typeof(___item) ___item_to_delete; \
while (___item) \
{ \
___item_to_delete = ___item; \
___item = fl_linked_list_next(___item_to_delete, field); \
free_callback(___item_to_delete); \
} \
})
/**
*/
#define fl_linked_list_find(ptr_to_current, field, comparator_func, comparator_seed) \
({ \
fl_auto_type(ptr_to_current) ___item = ptr_to_current; \
while (___item) \
{ \
if (comparator_func(comparator_seed, ___item) == true) break; \
___item = fl_linked_list_next(___item, field); \
} \
___item; \
})
#ifdef FL_ENABLE_TIME_MODULE
#ifndef FL_TIME_TYPE
#error "You must define `FL_TIME_TYPE` with a unsigned integer type such as `uint32_t`, `uin64_t`, etc. "
#endif
typedef FL_TIME_TYPE fl_time_t;
/* This is a elapsed time checker that protects against wrap around.
Now must always be (physically) bigger than before.*/
static inline bool fl_time_check_elapsed(fl_time_t now, fl_time_t before, fl_time_t desired_wait)
{
return (now - before) >= desired_wait;
}
#ifdef FL_TIME_INT_TYPE
/* Return the remaining time to timeout expiration.*/
static inline FL_TIME_INT_TYPE fl_time_remaining(fl_time_t now, fl_time_t before, fl_time_t desired_wait)
{
return (FL_TIME_INT_TYPE)desired_wait - (FL_TIME_INT_TYPE)(now - before);
}
#endif
/* This is a reached time checker that protects against wrap around.
It checks if now has reached timestamp. Timestamp must always be bigger than
now when first stored. */
static inline bool fl_time_check_reached(fl_time_t timestamp, fl_time_t now)
{
/* First we subtract from timestamp a stupid_big_number,
to create a "before" number.
Then we use a stupid_big_number as the elapsed variable
to the check_elapsed function. */
const fl_time_t max = (fl_time_t)-1;
const fl_time_t stupid_big_number = (max/2) + 1;
fl_time_t before = timestamp - stupid_big_number;
return fl_time_check_elapsed(now, before, stupid_big_number);
}
#endif /* FL_ENABLE_TIME_MODULE */
#endif /* _FL_LIB_H_ */