Commit 099dc5c2 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: DARRAY_PREALLOCATED()

Add support to darray for preallocating some number of elements.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent a58a6a58
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include "darray.h" #include "darray.h"
int __bch2_darray_resize(darray_void *d, size_t element_size, size_t new_size, gfp_t gfp) int __bch2_darray_resize(darray_char *d, size_t element_size, size_t new_size, gfp_t gfp)
{ {
if (new_size > d->size) { if (new_size > d->size) {
new_size = roundup_pow_of_two(new_size); new_size = roundup_pow_of_two(new_size);
...@@ -14,6 +14,7 @@ int __bch2_darray_resize(darray_void *d, size_t element_size, size_t new_size, g ...@@ -14,6 +14,7 @@ int __bch2_darray_resize(darray_void *d, size_t element_size, size_t new_size, g
return -ENOMEM; return -ENOMEM;
memcpy(data, d->data, d->size * element_size); memcpy(data, d->data, d->size * element_size);
if (d->data != d->preallocated)
kvfree(d->data); kvfree(d->data);
d->data = data; d->data = data;
d->size = new_size; d->size = new_size;
......
...@@ -10,17 +10,20 @@ ...@@ -10,17 +10,20 @@
#include <linux/slab.h> #include <linux/slab.h>
#define DARRAY(type) \ #define DARRAY_PREALLOCATED(_type, _nr) \
struct { \ struct { \
size_t nr, size; \ size_t nr, size; \
type *data; \ _type *data; \
_type preallocated[_nr]; \
} }
typedef DARRAY(void) darray_void; #define DARRAY(_type) DARRAY_PREALLOCATED(_type, 0)
int __bch2_darray_resize(darray_void *, size_t, size_t, gfp_t); typedef DARRAY(char) darray_char;
static inline int __darray_resize(darray_void *d, size_t element_size, int __bch2_darray_resize(darray_char *, size_t, size_t, gfp_t);
static inline int __darray_resize(darray_char *d, size_t element_size,
size_t new_size, gfp_t gfp) size_t new_size, gfp_t gfp)
{ {
return unlikely(new_size > d->size) return unlikely(new_size > d->size)
...@@ -29,18 +32,18 @@ static inline int __darray_resize(darray_void *d, size_t element_size, ...@@ -29,18 +32,18 @@ static inline int __darray_resize(darray_void *d, size_t element_size,
} }
#define darray_resize_gfp(_d, _new_size, _gfp) \ #define darray_resize_gfp(_d, _new_size, _gfp) \
__darray_resize((darray_void *) (_d), sizeof((_d)->data[0]), (_new_size), _gfp) unlikely(__darray_resize((darray_char *) (_d), sizeof((_d)->data[0]), (_new_size), _gfp))
#define darray_resize(_d, _new_size) \ #define darray_resize(_d, _new_size) \
darray_resize_gfp(_d, _new_size, GFP_KERNEL) darray_resize_gfp(_d, _new_size, GFP_KERNEL)
static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more, gfp_t gfp) static inline int __darray_make_room(darray_char *d, size_t t_size, size_t more, gfp_t gfp)
{ {
return __darray_resize(d, t_size, d->nr + more, gfp); return __darray_resize(d, t_size, d->nr + more, gfp);
} }
#define darray_make_room_gfp(_d, _more, _gfp) \ #define darray_make_room_gfp(_d, _more, _gfp) \
__darray_make_room((darray_void *) (_d), sizeof((_d)->data[0]), (_more), _gfp) __darray_make_room((darray_char *) (_d), sizeof((_d)->data[0]), (_more), _gfp)
#define darray_make_room(_d, _more) \ #define darray_make_room(_d, _more) \
darray_make_room_gfp(_d, _more, GFP_KERNEL) darray_make_room_gfp(_d, _more, GFP_KERNEL)
...@@ -86,12 +89,15 @@ static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more, ...@@ -86,12 +89,15 @@ static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more,
#define darray_init(_d) \ #define darray_init(_d) \
do { \ do { \
(_d)->data = NULL; \ (_d)->nr = 0; \
(_d)->nr = (_d)->size = 0; \ (_d)->size = ARRAY_SIZE((_d)->preallocated); \
(_d)->data = (_d)->size ? (_d)->preallocated : NULL; \
} while (0) } while (0)
#define darray_exit(_d) \ #define darray_exit(_d) \
do { \ do { \
if (!ARRAY_SIZE((_d)->preallocated) || \
(_d)->data != (_d)->preallocated) \
kvfree((_d)->data); \ kvfree((_d)->data); \
darray_init(_d); \ darray_init(_d); \
} while (0) } while (0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment