xref: /linux/fs/bcachefs/darray.c (revision 91dae758bdb854367bf0811d97acb84e791764d9)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/log2.h>
4 #include <linux/slab.h>
5 #include "darray.h"
6 
7 int __bch2_darray_resize(darray_char *d, size_t element_size, size_t new_size, gfp_t gfp)
8 {
9 	if (new_size > d->size) {
10 		new_size = roundup_pow_of_two(new_size);
11 
12 		void *data = kvmalloc_array(new_size, element_size, gfp);
13 		if (!data)
14 			return -ENOMEM;
15 
16 		if (d->size)
17 			memcpy(data, d->data, d->size * element_size);
18 		if (d->data != d->preallocated)
19 			kvfree(d->data);
20 		d->data	= data;
21 		d->size = new_size;
22 	}
23 
24 	return 0;
25 }
26