xref: /linux/fs/bcachefs/darray.h (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_DARRAY_H
3 #define _BCACHEFS_DARRAY_H
4 
5 /*
6  * Dynamic arrays:
7  *
8  * Inspired by CCAN's darray
9  */
10 
11 #include <linux/slab.h>
12 
13 #define DARRAY_PREALLOCATED(_type, _nr)					\
14 struct {								\
15 	size_t nr, size;						\
16 	_type *data;							\
17 	_type preallocated[_nr];					\
18 }
19 
20 #define DARRAY(_type) DARRAY_PREALLOCATED(_type, 0)
21 
22 typedef DARRAY(char)	darray_char;
23 typedef DARRAY(char *) darray_str;
24 
25 int __bch2_darray_resize(darray_char *, size_t, size_t, gfp_t);
26 
27 static inline int __darray_resize(darray_char *d, size_t element_size,
28 				  size_t new_size, gfp_t gfp)
29 {
30 	return unlikely(new_size > d->size)
31 		? __bch2_darray_resize(d, element_size, new_size, gfp)
32 		: 0;
33 }
34 
35 #define darray_resize_gfp(_d, _new_size, _gfp)				\
36 	unlikely(__darray_resize((darray_char *) (_d), sizeof((_d)->data[0]), (_new_size), _gfp))
37 
38 #define darray_resize(_d, _new_size)					\
39 	darray_resize_gfp(_d, _new_size, GFP_KERNEL)
40 
41 static inline int __darray_make_room(darray_char *d, size_t t_size, size_t more, gfp_t gfp)
42 {
43 	return __darray_resize(d, t_size, d->nr + more, gfp);
44 }
45 
46 #define darray_make_room_gfp(_d, _more, _gfp)				\
47 	__darray_make_room((darray_char *) (_d), sizeof((_d)->data[0]), (_more), _gfp)
48 
49 #define darray_make_room(_d, _more)					\
50 	darray_make_room_gfp(_d, _more, GFP_KERNEL)
51 
52 #define darray_room(_d)		((_d).size - (_d).nr)
53 
54 #define darray_top(_d)		((_d).data[(_d).nr])
55 
56 #define darray_push_gfp(_d, _item, _gfp)				\
57 ({									\
58 	int _ret = darray_make_room_gfp((_d), 1, _gfp);			\
59 									\
60 	if (!_ret)							\
61 		(_d)->data[(_d)->nr++] = (_item);			\
62 	_ret;								\
63 })
64 
65 #define darray_push(_d, _item)	darray_push_gfp(_d, _item, GFP_KERNEL)
66 
67 #define darray_pop(_d)		((_d)->data[--(_d)->nr])
68 
69 #define darray_first(_d)	((_d).data[0])
70 #define darray_last(_d)		((_d).data[(_d).nr - 1])
71 
72 #define darray_insert_item(_d, pos, _item)				\
73 ({									\
74 	size_t _pos = (pos);						\
75 	int _ret = darray_make_room((_d), 1);				\
76 									\
77 	if (!_ret)							\
78 		array_insert_item((_d)->data, (_d)->nr, _pos, (_item));	\
79 	_ret;								\
80 })
81 
82 #define darray_remove_item(_d, _pos)					\
83 	array_remove_item((_d)->data, (_d)->nr, (_pos) - (_d)->data)
84 
85 #define __darray_for_each(_d, _i)						\
86 	for ((_i) = (_d).data; _i < (_d).data + (_d).nr; _i++)
87 
88 #define darray_for_each(_d, _i)						\
89 	for (typeof(&(_d).data[0]) _i = (_d).data; _i < (_d).data + (_d).nr; _i++)
90 
91 #define darray_for_each_reverse(_d, _i)					\
92 	for (typeof(&(_d).data[0]) _i = (_d).data + (_d).nr - 1; _i >= (_d).data; --_i)
93 
94 #define darray_init(_d)							\
95 do {									\
96 	(_d)->nr = 0;							\
97 	(_d)->size = ARRAY_SIZE((_d)->preallocated);			\
98 	(_d)->data = (_d)->size ? (_d)->preallocated : NULL;		\
99 } while (0)
100 
101 #define darray_exit(_d)							\
102 do {									\
103 	if (!ARRAY_SIZE((_d)->preallocated) ||				\
104 	    (_d)->data != (_d)->preallocated)				\
105 		kvfree((_d)->data);					\
106 	darray_init(_d);						\
107 } while (0)
108 
109 #endif /* _BCACHEFS_DARRAY_H */
110