btrfs-progs: print-tree: Enhance warning on tree block level mismatch and error handling
[platform/upstream/btrfs-progs.git] / kernel-shared / ulist.h
1 /*
2  * Copyright (C) 2011 STRATO AG
3  * written by Arne Jansen <sensille@gmx.net>
4  * Distributed under the GNU GPL license version 2.
5  *
6  */
7
8 #ifndef __ULIST_H__
9 #define __ULIST_H__
10
11 #include "kerncompat.h"
12 #include "list.h"
13 #include "rbtree.h"
14
15 /*
16  * ulist is a generic data structure to hold a collection of unique u64
17  * values. The only operations it supports is adding to the list and
18  * enumerating it.
19  * It is possible to store an auxiliary value along with the key.
20  *
21  */
22 struct ulist_iterator {
23         struct list_head *cur_list;  /* hint to start search */
24 };
25
26 /*
27  * element of the list
28  */
29 struct ulist_node {
30         u64 val;                /* value to store */
31         u64 aux;                /* auxiliary value saved along with the val */
32
33         struct list_head list;  /* used to link node */
34         struct rb_node rb_node; /* used to speed up search */
35 };
36
37 struct ulist {
38         /*
39          * number of elements stored in list
40          */
41         unsigned long nnodes;
42
43         struct list_head nodes;
44         struct rb_root root;
45 };
46
47 void ulist_init(struct ulist *ulist);
48 void ulist_reinit(struct ulist *ulist);
49 struct ulist *ulist_alloc(gfp_t gfp_mask);
50 void ulist_free(struct ulist *ulist);
51 int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
52 int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
53                     u64 *old_aux, gfp_t gfp_mask);
54 int ulist_del(struct ulist *ulist, u64 val, u64 aux);
55
56 /* just like ulist_add_merge() but take a pointer for the aux data */
57 static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
58                                       void **old_aux, gfp_t gfp_mask)
59 {
60 #if BITS_PER_LONG == 32
61         u64 old64 = (uintptr_t)*old_aux;
62         int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
63         *old_aux = (void *)((uintptr_t)old64);
64         return ret;
65 #else
66         return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
67 #endif
68 }
69
70 struct ulist_node *ulist_next(struct ulist *ulist,
71                               struct ulist_iterator *uiter);
72
73 #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
74
75 #endif