]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_on_stack.h
f607a0cb37ed74b8ae39b0804a43fcdbee7bb040
[bcachefs-tools-debian] / libbcachefs / bkey_on_stack.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_ON_STACK_H
3 #define _BCACHEFS_BKEY_ON_STACK_H
4
5 #include "bcachefs.h"
6
7 struct bkey_on_stack {
8         struct bkey_i   *k;
9         u64             onstack[12];
10 };
11
12 static inline void bkey_on_stack_realloc(struct bkey_on_stack *s,
13                                          struct bch_fs *c, unsigned u64s)
14 {
15         if (s->k == (void *) s->onstack &&
16             u64s > ARRAY_SIZE(s->onstack)) {
17                 s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS);
18                 memcpy(s->k, s->onstack, sizeof(s->onstack));
19         }
20 }
21
22 static inline void bkey_on_stack_reassemble(struct bkey_on_stack *s,
23                                             struct bch_fs *c,
24                                             struct bkey_s_c k)
25 {
26         bkey_on_stack_realloc(s, c, k.k->u64s);
27         bkey_reassemble(s->k, k);
28 }
29
30 static inline void bkey_on_stack_init(struct bkey_on_stack *s)
31 {
32         s->k = (void *) s->onstack;
33 }
34
35 static inline void bkey_on_stack_exit(struct bkey_on_stack *s,
36                                       struct bch_fs *c)
37 {
38         if (s->k != (void *) s->onstack)
39                 mempool_free(s->k, &c->large_bkey_pool);
40         s->k = NULL;
41 }
42
43 #endif /* _BCACHEFS_BKEY_ON_STACK_H */