]>
Commit | Line | Data |
---|---|---|
da5c8135 AJ |
1 | /* |
2 | * Copyright (C) 2011 STRATO AG | |
3 | * written by Arne Jansen <[email protected]> | |
4 | * Distributed under the GNU GPL license version 2. | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef __ULIST__ | |
9 | #define __ULIST__ | |
10 | ||
f7f82b81 WS |
11 | #include <linux/list.h> |
12 | #include <linux/rbtree.h> | |
13 | ||
da5c8135 AJ |
14 | /* |
15 | * ulist is a generic data structure to hold a collection of unique u64 | |
16 | * values. The only operations it supports is adding to the list and | |
17 | * enumerating it. | |
18 | * It is possible to store an auxiliary value along with the key. | |
19 | * | |
da5c8135 | 20 | */ |
cd1b413c | 21 | struct ulist_iterator { |
4c7a6f74 | 22 | #ifdef CONFIG_BTRFS_DEBUG |
cd1b413c | 23 | int i; |
4c7a6f74 WS |
24 | #endif |
25 | struct list_head *cur_list; /* hint to start search */ | |
cd1b413c JS |
26 | }; |
27 | ||
da5c8135 AJ |
28 | /* |
29 | * element of the list | |
30 | */ | |
31 | struct ulist_node { | |
32 | u64 val; /* value to store */ | |
34d73f54 | 33 | u64 aux; /* auxiliary value saved along with the val */ |
4c7a6f74 WS |
34 | |
35 | #ifdef CONFIG_BTRFS_DEBUG | |
36 | int seqnum; /* sequence number this node is added */ | |
37 | #endif | |
38 | ||
39 | struct list_head list; /* used to link node */ | |
f7f82b81 | 40 | struct rb_node rb_node; /* used to speed up search */ |
da5c8135 AJ |
41 | }; |
42 | ||
43 | struct ulist { | |
44 | /* | |
45 | * number of elements stored in list | |
46 | */ | |
47 | unsigned long nnodes; | |
48 | ||
4c7a6f74 | 49 | struct list_head nodes; |
f7f82b81 | 50 | struct rb_root root; |
da5c8135 AJ |
51 | }; |
52 | ||
53 | void ulist_init(struct ulist *ulist); | |
da5c8135 | 54 | void ulist_reinit(struct ulist *ulist); |
2eec6c81 | 55 | struct ulist *ulist_alloc(gfp_t gfp_mask); |
da5c8135 | 56 | void ulist_free(struct ulist *ulist); |
34d73f54 AB |
57 | int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask); |
58 | int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux, | |
59 | u64 *old_aux, gfp_t gfp_mask); | |
d4b80404 | 60 | int ulist_del(struct ulist *ulist, u64 val, u64 aux); |
4eb1f66d TI |
61 | |
62 | /* just like ulist_add_merge() but take a pointer for the aux data */ | |
63 | static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux, | |
64 | void **old_aux, gfp_t gfp_mask) | |
65 | { | |
66 | #if BITS_PER_LONG == 32 | |
67 | u64 old64 = (uintptr_t)*old_aux; | |
68 | int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask); | |
69 | *old_aux = (void *)((uintptr_t)old64); | |
70 | return ret; | |
71 | #else | |
72 | return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask); | |
73 | #endif | |
74 | } | |
75 | ||
cd1b413c JS |
76 | struct ulist_node *ulist_next(struct ulist *ulist, |
77 | struct ulist_iterator *uiter); | |
78 | ||
4c7a6f74 | 79 | #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL) |
da5c8135 AJ |
80 | |
81 | #endif |