Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2011 STRATO AG
0004  * written by Arne Jansen <sensille@gmx.net>
0005  */
0006 
0007 #ifndef BTRFS_ULIST_H
0008 #define BTRFS_ULIST_H
0009 
0010 #include <linux/list.h>
0011 #include <linux/rbtree.h>
0012 
0013 /*
0014  * ulist is a generic data structure to hold a collection of unique u64
0015  * values. The only operations it supports is adding to the list and
0016  * enumerating it.
0017  * It is possible to store an auxiliary value along with the key.
0018  *
0019  */
0020 struct ulist_iterator {
0021     struct list_head *cur_list;  /* hint to start search */
0022 };
0023 
0024 /*
0025  * element of the list
0026  */
0027 struct ulist_node {
0028     u64 val;        /* value to store */
0029     u64 aux;        /* auxiliary value saved along with the val */
0030 
0031     struct list_head list;  /* used to link node */
0032     struct rb_node rb_node; /* used to speed up search */
0033 };
0034 
0035 struct ulist {
0036     /*
0037      * number of elements stored in list
0038      */
0039     unsigned long nnodes;
0040 
0041     struct list_head nodes;
0042     struct rb_root root;
0043 };
0044 
0045 void ulist_init(struct ulist *ulist);
0046 void ulist_release(struct ulist *ulist);
0047 void ulist_reinit(struct ulist *ulist);
0048 struct ulist *ulist_alloc(gfp_t gfp_mask);
0049 void ulist_free(struct ulist *ulist);
0050 int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
0051 int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
0052             u64 *old_aux, gfp_t gfp_mask);
0053 int ulist_del(struct ulist *ulist, u64 val, u64 aux);
0054 
0055 /* just like ulist_add_merge() but take a pointer for the aux data */
0056 static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux,
0057                       void **old_aux, gfp_t gfp_mask)
0058 {
0059 #if BITS_PER_LONG == 32
0060     u64 old64 = (uintptr_t)*old_aux;
0061     int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask);
0062     *old_aux = (void *)((uintptr_t)old64);
0063     return ret;
0064 #else
0065     return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask);
0066 #endif
0067 }
0068 
0069 struct ulist_node *ulist_next(struct ulist *ulist,
0070                   struct ulist_iterator *uiter);
0071 
0072 #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
0073 
0074 #endif