1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2013 by Delphix. All rights reserved.
  28  */
  29 
  30 #ifndef _SYS_RANGE_TREE_H
  31 #define _SYS_RANGE_TREE_H
  32 
  33 #include <sys/avl.h>
  34 #include <sys/dmu.h>
  35 
  36 #ifdef  __cplusplus
  37 extern "C" {
  38 #endif
  39 
  40 #define RANGE_TREE_HISTOGRAM_SIZE       64
  41 
  42 typedef struct range_tree_ops range_tree_ops_t;
  43 
  44 typedef struct range_tree {
  45         avl_tree_t      rt_root;        /* offset-ordered segment AVL tree */
  46         uint64_t        rt_space;       /* sum of all segments in the map */
  47         range_tree_ops_t *rt_ops;
  48         void            *rt_arg;
  49 
  50         /*
  51          * The rt_histogram maintains a histogram of ranges. Each bucket,
  52          * rt_histogram[i], contains the number of ranges whose size is:
  53          * 2^i <= size of range in bytes < 2^(i+1)
  54          */
  55         uint64_t        rt_histogram[RANGE_TREE_HISTOGRAM_SIZE];
  56         kmutex_t        *rt_lock;       /* pointer to lock that protects map */
  57 } range_tree_t;
  58 
  59 typedef struct range_seg {
  60         avl_node_t      rs_node;        /* AVL node */
  61         avl_node_t      rs_pp_node;     /* AVL picker-private node */
  62         uint64_t        rs_start;       /* starting offset of this segment */
  63         uint64_t        rs_end;         /* ending offset (non-inclusive) */
  64 } range_seg_t;
  65 
  66 struct range_tree_ops {
  67         void    (*rtop_create)(range_tree_t *rt, void *arg);
  68         void    (*rtop_destroy)(range_tree_t *rt, void *arg);
  69         void    (*rtop_add)(range_tree_t *rt, range_seg_t *rs, void *arg);
  70         void    (*rtop_remove)(range_tree_t *rt, range_seg_t *rs, void *arg);
  71         void    (*rtop_vacate)(range_tree_t *rt, void *arg);
  72 };
  73 
  74 typedef void range_tree_func_t(void *arg, uint64_t start, uint64_t size);
  75 
  76 void range_tree_init(void);
  77 void range_tree_fini(void);
  78 range_tree_t *range_tree_create(range_tree_ops_t *ops, void *arg, kmutex_t *lp);
  79 void range_tree_destroy(range_tree_t *rt);
  80 boolean_t range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size);
  81 uint64_t range_tree_space(range_tree_t *rt);
  82 void range_tree_verify(range_tree_t *rt, uint64_t start, uint64_t size);
  83 void range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst);
  84 void range_tree_stat_verify(range_tree_t *rt);
  85 
  86 void range_tree_add(void *arg, uint64_t start, uint64_t size);
  87 void range_tree_remove(void *arg, uint64_t start, uint64_t size);
  88 
  89 void range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg);
  90 void range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg);
  91 
  92 #ifdef  __cplusplus
  93 }
  94 #endif
  95 
  96 #endif  /* _SYS_RANGE_TREE_H */