]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/keylist.c
New upstream release
[bcachefs-tools-debian] / libbcachefs / keylist.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey.h"
5 #include "keylist.h"
6
7 int bch2_keylist_realloc(struct keylist *l, u64 *inline_u64s,
8                         size_t nr_inline_u64s, size_t new_u64s)
9 {
10         size_t oldsize = bch2_keylist_u64s(l);
11         size_t newsize = oldsize + new_u64s;
12         u64 *old_buf = l->keys_p == inline_u64s ? NULL : l->keys_p;
13         u64 *new_keys;
14
15         newsize = roundup_pow_of_two(newsize);
16
17         if (newsize <= nr_inline_u64s ||
18             (old_buf && roundup_pow_of_two(oldsize) == newsize))
19                 return 0;
20
21         new_keys = krealloc(old_buf, sizeof(u64) * newsize, GFP_NOIO);
22         if (!new_keys)
23                 return -ENOMEM;
24
25         if (!old_buf)
26                 memcpy_u64s(new_keys, inline_u64s, oldsize);
27
28         l->keys_p = new_keys;
29         l->top_p = new_keys + oldsize;
30
31         return 0;
32 }
33
34 void bch2_keylist_add_in_order(struct keylist *l, struct bkey_i *insert)
35 {
36         struct bkey_i *where;
37
38         for_each_keylist_key(l, where)
39                 if (bkey_cmp(insert->k.p, where->k.p) < 0)
40                         break;
41
42         memmove_u64s_up((u64 *) where + insert->k.u64s,
43                         where,
44                         ((u64 *) l->top) - ((u64 *) where));
45
46         l->top_p += insert->k.u64s;
47         bkey_copy(where, insert);
48 }
49
50 void bch2_keylist_pop_front(struct keylist *l)
51 {
52         l->top_p -= bch2_keylist_front(l)->k.u64s;
53
54         memmove_u64s_down(l->keys,
55                           bkey_next(l->keys),
56                           bch2_keylist_u64s(l));
57 }
58
59 #ifdef CONFIG_BCACHEFS_DEBUG
60 void bch2_verify_keylist_sorted(struct keylist *l)
61 {
62         struct bkey_i *k;
63
64         for_each_keylist_key(l, k)
65                 BUG_ON(bkey_next(k) != l->top &&
66                        bpos_cmp(k->k.p, bkey_next(k)->k.p) >= 0);
67 }
68 #endif