blob: fb36731074b5fa6ed5e27be3cca08caf329631ee [file] [log] [blame]
Arne Jansenda5c8132011-09-13 12:29:12 +02001/*
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
Wang Shilongf7f82b82013-04-12 12:12:17 +000011#include <linux/list.h>
12#include <linux/rbtree.h>
13
Arne Jansenda5c8132011-09-13 12:29:12 +020014/*
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 * The implementation is preliminary and can probably be sped up
21 * significantly. A first step would be to store the values in an rbtree
22 * as soon as ULIST_SIZE is exceeded.
23 */
24
25/*
26 * number of elements statically allocated inside struct ulist
27 */
28#define ULIST_SIZE 16
29
Jan Schmidtcd1b4132012-05-22 14:56:50 +020030struct ulist_iterator {
31 int i;
32};
33
Arne Jansenda5c8132011-09-13 12:29:12 +020034/*
35 * element of the list
36 */
37struct ulist_node {
38 u64 val; /* value to store */
Alexander Block34d73f52012-07-28 16:18:58 +020039 u64 aux; /* auxiliary value saved along with the val */
Wang Shilongf7f82b82013-04-12 12:12:17 +000040 struct rb_node rb_node; /* used to speed up search */
Arne Jansenda5c8132011-09-13 12:29:12 +020041};
42
43struct ulist {
44 /*
45 * number of elements stored in list
46 */
47 unsigned long nnodes;
48
49 /*
50 * number of nodes we already have room for
51 */
52 unsigned long nodes_alloced;
53
54 /*
55 * pointer to the array storing the elements. The first ULIST_SIZE
56 * elements are stored inline. In this case the it points to int_nodes.
57 * After exceeding ULIST_SIZE, dynamic memory is allocated.
58 */
59 struct ulist_node *nodes;
60
Wang Shilongf7f82b82013-04-12 12:12:17 +000061 struct rb_root root;
62
Arne Jansenda5c8132011-09-13 12:29:12 +020063 /*
64 * inline storage space for the first ULIST_SIZE entries
65 */
66 struct ulist_node int_nodes[ULIST_SIZE];
67};
68
69void ulist_init(struct ulist *ulist);
70void ulist_fini(struct ulist *ulist);
71void ulist_reinit(struct ulist *ulist);
Daniel J Blueman2eec6c82012-04-26 00:37:14 +080072struct ulist *ulist_alloc(gfp_t gfp_mask);
Arne Jansenda5c8132011-09-13 12:29:12 +020073void ulist_free(struct ulist *ulist);
Alexander Block34d73f52012-07-28 16:18:58 +020074int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
75int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
76 u64 *old_aux, gfp_t gfp_mask);
Jan Schmidtcd1b4132012-05-22 14:56:50 +020077struct ulist_node *ulist_next(struct ulist *ulist,
78 struct ulist_iterator *uiter);
79
80#define ULIST_ITER_INIT(uiter) ((uiter)->i = 0)
Arne Jansenda5c8132011-09-13 12:29:12 +020081
82#endif