summaryrefslogtreecommitdiffstats
path: root/fs/bcachefs/darray.h
blob: 519ab9b96e67fd6e2257b3a63e3becde7711cbca (plain)
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
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_DARRAY_H
#define _BCACHEFS_DARRAY_H

/*
 * Dynamic arrays:
 *
 * Inspired by CCAN's darray
 */

#include "util.h"
#include <linux/slab.h>

#define DARRAY(type)							\
struct {								\
	size_t nr, size;						\
	type *data;							\
}

typedef DARRAY(void) darray_void;

static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more)
{
	if (d->nr + more > d->size) {
		size_t new_size = roundup_pow_of_two(d->nr + more);
		void *data = krealloc_array(d->data, new_size, t_size, GFP_KERNEL);

		if (!data)
			return -ENOMEM;

		d->data	= data;
		d->size = new_size;
	}

	return 0;
}

#define darray_make_room(_d, _more)					\
	__darray_make_room((darray_void *) (_d), sizeof((_d)->data[0]), (_more))

#define darray_top(_d)		((_d).data[(_d).nr])

#define darray_push(_d, _item)						\
({									\
	int _ret = darray_make_room((_d), 1);				\
									\
	if (!_ret)							\
		(_d)->data[(_d)->nr++] = (_item);			\
	_ret;								\
})

#define darray_insert_item(_d, _pos, _item)				\
({									\
	size_t pos = (_pos);						\
	int _ret = darray_make_room((_d), 1);				\
									\
	if (!_ret)							\
		array_insert_item((_d)->data, (_d)->nr, pos, (_item));	\
	_ret;								\
})

#define darray_for_each(_d, _i)						\
	for (_i = (_d).data; _i < (_d).data + (_d).nr; _i++)

#define darray_init(_d)							\
do {									\
	(_d)->data = NULL;						\
	(_d)->nr = (_d)->size = 0;					\
} while (0)

#define darray_exit(_d)							\
do {									\
	kfree((_d)->data);						\
	darray_init(_d);						\
} while (0)

#endif /* _BCACHEFS_DARRAY_H */