xref: /linux/fs/bcachefs/darray.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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 		memcpy(data, d->data, d->size * element_size);
17 		if (d->data != d->preallocated)
18 			kvfree(d->data);
19 		d->data	= data;
20 		d->size = new_size;
21 	}
22 
23 	return 0;
24 }
25