Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1186 | savelij | 1 | #ifndef _DYN_ARRAY_H |
2 | #define _DYN_ARRAY_H |
||
3 | /* codevars.h */ |
||
4 | /*****************************************************************************/ |
||
5 | /* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only */ |
||
6 | /* */ |
||
7 | /* ASL */ |
||
8 | /* */ |
||
9 | /* Minimalistic Dynamic Array Handling */ |
||
10 | /* */ |
||
11 | /*****************************************************************************/ |
||
12 | |||
13 | #define DYN_ARRAY_INCR 16 |
||
14 | |||
15 | #define dyn_array_roundup(n) ((((n) + (DYN_ARRAY_INCR - 1)) / DYN_ARRAY_INCR) * DYN_ARRAY_INCR) |
||
16 | |||
17 | #define dyn_array_realloc(array, decl_type, old_cnt, new_cnt) \ |
||
18 | do { \ |
||
19 | size_t new_roundup_cnt = dyn_array_roundup(new_cnt); \ |
||
20 | size_t old_roundup_cnt = dyn_array_roundup(old_cnt); \ |
||
21 | if (new_roundup_cnt != old_roundup_cnt) { \ |
||
22 | size_t s = sizeof(*array) * new_roundup_cnt; \ |
||
23 | array = (decl_type*) (array ? realloc(array, s) : malloc(s)); \ |
||
24 | if (new_roundup_cnt > old_roundup_cnt) \ |
||
25 | memset(&array[old_roundup_cnt], 0, sizeof(*array) * (new_roundup_cnt - old_roundup_cnt)); \ |
||
26 | else if (new_roundup_cnt > (size_t)(new_cnt)) \ |
||
27 | memset(&array[new_cnt], 0, sizeof(*array) * (new_roundup_cnt - (new_cnt))); \ |
||
28 | } \ |
||
29 | } while (0) |
||
30 | |||
31 | #define dyn_array_rsv_end(array, decl_type, curr_cnt) \ |
||
32 | dyn_array_realloc(array, decl_type, curr_cnt, (curr_cnt) + 1) |
||
33 | |||
34 | #endif /* _DYN_ARRAY_H */ |