Loading...
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__
9#define __ULIST__
10
11#include <linux/list.h>
12#include <linux/rbtree.h>
13
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 *
20 */
21struct ulist_iterator {
22#ifdef CONFIG_BTRFS_DEBUG
23 int i;
24#endif
25 struct list_head *cur_list; /* hint to start search */
26};
27
28/*
29 * element of the list
30 */
31struct ulist_node {
32 u64 val; /* value to store */
33 u64 aux; /* auxiliary value saved along with the val */
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 */
40 struct rb_node rb_node; /* used to speed up search */
41};
42
43struct ulist {
44 /*
45 * number of elements stored in list
46 */
47 unsigned long nnodes;
48
49 struct list_head nodes;
50 struct rb_root root;
51};
52
53void ulist_init(struct ulist *ulist);
54void ulist_reinit(struct ulist *ulist);
55struct ulist *ulist_alloc(gfp_t gfp_mask);
56void ulist_free(struct ulist *ulist);
57int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
58int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
59 u64 *old_aux, gfp_t gfp_mask);
60int ulist_del(struct ulist *ulist, u64 val, u64 aux);
61
62/* just like ulist_add_merge() but take a pointer for the aux data */
63static 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
76struct ulist_node *ulist_next(struct ulist *ulist,
77 struct ulist_iterator *uiter);
78
79#define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
80
81#endif
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__
9#define __ULIST__
10
11/*
12 * ulist is a generic data structure to hold a collection of unique u64
13 * values. The only operations it supports is adding to the list and
14 * enumerating it.
15 * It is possible to store an auxiliary value along with the key.
16 *
17 * The implementation is preliminary and can probably be sped up
18 * significantly. A first step would be to store the values in an rbtree
19 * as soon as ULIST_SIZE is exceeded.
20 */
21
22/*
23 * number of elements statically allocated inside struct ulist
24 */
25#define ULIST_SIZE 16
26
27struct ulist_iterator {
28 int i;
29};
30
31/*
32 * element of the list
33 */
34struct ulist_node {
35 u64 val; /* value to store */
36 unsigned long aux; /* auxiliary value saved along with the val */
37};
38
39struct ulist {
40 /*
41 * number of elements stored in list
42 */
43 unsigned long nnodes;
44
45 /*
46 * number of nodes we already have room for
47 */
48 unsigned long nodes_alloced;
49
50 /*
51 * pointer to the array storing the elements. The first ULIST_SIZE
52 * elements are stored inline. In this case the it points to int_nodes.
53 * After exceeding ULIST_SIZE, dynamic memory is allocated.
54 */
55 struct ulist_node *nodes;
56
57 /*
58 * inline storage space for the first ULIST_SIZE entries
59 */
60 struct ulist_node int_nodes[ULIST_SIZE];
61};
62
63void ulist_init(struct ulist *ulist);
64void ulist_fini(struct ulist *ulist);
65void ulist_reinit(struct ulist *ulist);
66struct ulist *ulist_alloc(gfp_t gfp_mask);
67void ulist_free(struct ulist *ulist);
68int ulist_add(struct ulist *ulist, u64 val, unsigned long aux,
69 gfp_t gfp_mask);
70int ulist_add_merge(struct ulist *ulist, u64 val, unsigned long aux,
71 unsigned long *old_aux, gfp_t gfp_mask);
72struct ulist_node *ulist_next(struct ulist *ulist,
73 struct ulist_iterator *uiter);
74
75#define ULIST_ITER_INIT(uiter) ((uiter)->i = 0)
76
77#endif