Blame view

fs/btrfs/ulist.h 1.93 KB
9888c3402   David Sterba   btrfs: replace GP...
1
  /* SPDX-License-Identifier: GPL-2.0 */
da5c81356   Arne Jansen   Btrfs: generic da...
2
3
4
  /*
   * Copyright (C) 2011 STRATO AG
   * written by Arne Jansen <sensille@gmx.net>
da5c81356   Arne Jansen   Btrfs: generic da...
5
   */
9888c3402   David Sterba   btrfs: replace GP...
6
7
  #ifndef BTRFS_ULIST_H
  #define BTRFS_ULIST_H
da5c81356   Arne Jansen   Btrfs: generic da...
8

f7f82b81d   Wang Shilong   Btrfs: add a rb_t...
9
10
  #include <linux/list.h>
  #include <linux/rbtree.h>
da5c81356   Arne Jansen   Btrfs: generic da...
11
12
13
14
15
16
  /*
   * ulist is a generic data structure to hold a collection of unique u64
   * values. The only operations it supports is adding to the list and
   * enumerating it.
   * It is possible to store an auxiliary value along with the key.
   *
da5c81356   Arne Jansen   Btrfs: generic da...
17
   */
cd1b413c5   Jan Schmidt   Btrfs: ulist real...
18
  struct ulist_iterator {
4c7a6f74c   Wang Shilong   Btrfs: rework uli...
19
  	struct list_head *cur_list;  /* hint to start search */
cd1b413c5   Jan Schmidt   Btrfs: ulist real...
20
  };
da5c81356   Arne Jansen   Btrfs: generic da...
21
22
23
24
25
  /*
   * element of the list
   */
  struct ulist_node {
  	u64 val;		/* value to store */
34d73f54e   Alexander Block   Btrfs: make aux f...
26
  	u64 aux;		/* auxiliary value saved along with the val */
4c7a6f74c   Wang Shilong   Btrfs: rework uli...
27

4c7a6f74c   Wang Shilong   Btrfs: rework uli...
28
  	struct list_head list;  /* used to link node */
f7f82b81d   Wang Shilong   Btrfs: add a rb_t...
29
  	struct rb_node rb_node;	/* used to speed up search */
da5c81356   Arne Jansen   Btrfs: generic da...
30
31
32
33
34
35
36
  };
  
  struct ulist {
  	/*
  	 * number of elements stored in list
  	 */
  	unsigned long nnodes;
4c7a6f74c   Wang Shilong   Btrfs: rework uli...
37
  	struct list_head nodes;
f7f82b81d   Wang Shilong   Btrfs: add a rb_t...
38
  	struct rb_root root;
da5c81356   Arne Jansen   Btrfs: generic da...
39
40
41
  };
  
  void ulist_init(struct ulist *ulist);
6655bc3de   David Sterba   btrfs: ulist: ren...
42
  void ulist_release(struct ulist *ulist);
da5c81356   Arne Jansen   Btrfs: generic da...
43
  void ulist_reinit(struct ulist *ulist);
2eec6c810   Daniel J Blueman   Fix minor type is...
44
  struct ulist *ulist_alloc(gfp_t gfp_mask);
da5c81356   Arne Jansen   Btrfs: generic da...
45
  void ulist_free(struct ulist *ulist);
34d73f54e   Alexander Block   Btrfs: make aux f...
46
47
48
  int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
  int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  		    u64 *old_aux, gfp_t gfp_mask);
d4b804045   Qu Wenruo   btrfs: ulist: Add...
49
  int ulist_del(struct ulist *ulist, u64 val, u64 aux);
4eb1f66dc   Takashi Iwai   Btrfs: Fix memory...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  
  /* just like ulist_add_merge() but take a pointer for the aux data */
  static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
  				      void **old_aux, gfp_t gfp_mask)
  {
  #if BITS_PER_LONG == 32
  	u64 old64 = (uintptr_t)*old_aux;
  	int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
  	*old_aux = (void *)((uintptr_t)old64);
  	return ret;
  #else
  	return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
  #endif
  }
cd1b413c5   Jan Schmidt   Btrfs: ulist real...
64
65
  struct ulist_node *ulist_next(struct ulist *ulist,
  			      struct ulist_iterator *uiter);
4c7a6f74c   Wang Shilong   Btrfs: rework uli...
66
  #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
da5c81356   Arne Jansen   Btrfs: generic da...
67
68
  
  #endif