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