blob: 53c9136327337f0732e42e8e276e2335d02ac92d [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 *
Arne Jansenda5c8132011-09-13 12:29:12 +020020 */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020021struct ulist_iterator {
Wang Shilong4c7a6f72014-01-29 00:25:34 +080022 struct list_head *cur_list; /* hint to start search */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020023};
24
Arne Jansenda5c8132011-09-13 12:29:12 +020025/*
26 * element of the list
27 */
28struct ulist_node {
29 u64 val; /* value to store */
Alexander Block34d73f52012-07-28 16:18:58 +020030 u64 aux; /* auxiliary value saved along with the val */
Wang Shilong4c7a6f72014-01-29 00:25:34 +080031
Wang Shilong4c7a6f72014-01-29 00:25:34 +080032 struct list_head list; /* used to link node */
Wang Shilongf7f82b82013-04-12 12:12:17 +000033 struct rb_node rb_node; /* used to speed up search */
Arne Jansenda5c8132011-09-13 12:29:12 +020034};
35
36struct ulist {
37 /*
38 * number of elements stored in list
39 */
40 unsigned long nnodes;
41
Wang Shilong4c7a6f72014-01-29 00:25:34 +080042 struct list_head nodes;
Wang Shilongf7f82b82013-04-12 12:12:17 +000043 struct rb_root root;
Arne Jansenda5c8132011-09-13 12:29:12 +020044};
45
46void ulist_init(struct ulist *ulist);
David Sterba6655bc32017-02-15 16:47:36 +010047void ulist_release(struct ulist *ulist);
Arne Jansenda5c8132011-09-13 12:29:12 +020048void ulist_reinit(struct ulist *ulist);
Daniel J Blueman2eec6c82012-04-26 00:37:14 +080049struct ulist *ulist_alloc(gfp_t gfp_mask);
Arne Jansenda5c8132011-09-13 12:29:12 +020050void ulist_free(struct ulist *ulist);
Alexander Block34d73f52012-07-28 16:18:58 +020051int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
52int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
53 u64 *old_aux, gfp_t gfp_mask);
Qu Wenruod4b80402015-04-20 09:26:02 +080054int ulist_del(struct ulist *ulist, u64 val, u64 aux);
Takashi Iwai4eb1f662014-07-28 10:57:04 +020055
56/* just like ulist_add_merge() but take a pointer for the aux data */
57static 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
Jan Schmidtcd1b4132012-05-22 14:56:50 +020070struct ulist_node *ulist_next(struct ulist *ulist,
71 struct ulist_iterator *uiter);
72
Wang Shilong4c7a6f72014-01-29 00:25:34 +080073#define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
Arne Jansenda5c8132011-09-13 12:29:12 +020074
75#endif