blob: 02fda0a2d4cebf1542e6275005ad35b2c97a68cc [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Arne Jansenda5c8132011-09-13 12:29:12 +02002/*
3 * Copyright (C) 2011 STRATO AG
4 * written by Arne Jansen <sensille@gmx.net>
Arne Jansenda5c8132011-09-13 12:29:12 +02005 */
6
David Sterba9888c342018-04-03 19:16:55 +02007#ifndef BTRFS_ULIST_H
8#define BTRFS_ULIST_H
Arne Jansenda5c8132011-09-13 12:29:12 +02009
Wang Shilongf7f82b82013-04-12 12:12:17 +000010#include <linux/list.h>
11#include <linux/rbtree.h>
12
Arne Jansenda5c8132011-09-13 12:29:12 +020013/*
14 * ulist is a generic data structure to hold a collection of unique u64
15 * values. The only operations it supports is adding to the list and
16 * enumerating it.
17 * It is possible to store an auxiliary value along with the key.
18 *
Arne Jansenda5c8132011-09-13 12:29:12 +020019 */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020020struct ulist_iterator {
Wang Shilong4c7a6f72014-01-29 00:25:34 +080021 struct list_head *cur_list; /* hint to start search */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020022};
23
Arne Jansenda5c8132011-09-13 12:29:12 +020024/*
25 * element of the list
26 */
27struct ulist_node {
28 u64 val; /* value to store */
Alexander Block34d73f52012-07-28 16:18:58 +020029 u64 aux; /* auxiliary value saved along with the val */
Wang Shilong4c7a6f72014-01-29 00:25:34 +080030
Wang Shilong4c7a6f72014-01-29 00:25:34 +080031 struct list_head list; /* used to link node */
Wang Shilongf7f82b82013-04-12 12:12:17 +000032 struct rb_node rb_node; /* used to speed up search */
Arne Jansenda5c8132011-09-13 12:29:12 +020033};
34
35struct ulist {
36 /*
37 * number of elements stored in list
38 */
39 unsigned long nnodes;
40
Wang Shilong4c7a6f72014-01-29 00:25:34 +080041 struct list_head nodes;
Wang Shilongf7f82b82013-04-12 12:12:17 +000042 struct rb_root root;
Arne Jansenda5c8132011-09-13 12:29:12 +020043};
44
45void ulist_init(struct ulist *ulist);
David Sterba6655bc32017-02-15 16:47:36 +010046void ulist_release(struct ulist *ulist);
Arne Jansenda5c8132011-09-13 12:29:12 +020047void ulist_reinit(struct ulist *ulist);
Daniel J Blueman2eec6c82012-04-26 00:37:14 +080048struct ulist *ulist_alloc(gfp_t gfp_mask);
Arne Jansenda5c8132011-09-13 12:29:12 +020049void ulist_free(struct ulist *ulist);
Alexander Block34d73f52012-07-28 16:18:58 +020050int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
51int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
52 u64 *old_aux, gfp_t gfp_mask);
Qu Wenruod4b80402015-04-20 09:26:02 +080053int ulist_del(struct ulist *ulist, u64 val, u64 aux);
Takashi Iwai4eb1f662014-07-28 10:57:04 +020054
55/* just like ulist_add_merge() but take a pointer for the aux data */
56static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
57 void **old_aux, gfp_t gfp_mask)
58{
59#if BITS_PER_LONG == 32
60 u64 old64 = (uintptr_t)*old_aux;
61 int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
62 *old_aux = (void *)((uintptr_t)old64);
63 return ret;
64#else
65 return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
66#endif
67}
68
Jan Schmidtcd1b4132012-05-22 14:56:50 +020069struct ulist_node *ulist_next(struct ulist *ulist,
70 struct ulist_iterator *uiter);
71
Wang Shilong4c7a6f72014-01-29 00:25:34 +080072#define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
Arne Jansenda5c8132011-09-13 12:29:12 +020073
74#endif