vm_radix.h revision 238245
1226645Sattilio/*
2226983Sjeff * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org>
3226645Sattilio * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
4226645Sattilio * All rights reserved.
5226645Sattilio *
6226645Sattilio * Redistribution and use in source and binary forms, with or without
7226645Sattilio * modification, are permitted provided that the following conditions
8226645Sattilio * are met:
9226645Sattilio * 1. Redistributions of source code must retain the above copyright
10226645Sattilio *    notice, this list of conditions and the following disclaimer.
11226645Sattilio * 2. Redistributions in binary form must reproduce the above copyright
12226645Sattilio *    notice, this list of conditions and the following disclaimer in the
13226645Sattilio *    documentation and/or other materials provided with the distribution.
14226645Sattilio *
15226645Sattilio * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16226645Sattilio * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17226645Sattilio * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18226645Sattilio * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19226645Sattilio * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20226645Sattilio * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21226645Sattilio * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22226645Sattilio * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23226645Sattilio * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24226645Sattilio * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25226645Sattilio * SUCH DAMAGE.
26226645Sattilio *
27226645Sattilio */
28226645Sattilio
29226645Sattilio#ifndef _VM_RADIX_H_
30226645Sattilio#define _VM_RADIX_H_
31226645Sattilio
32226930Sjeff#define	VM_RADIX_STACK	8	/* Nodes to store on stack. */
33226645Sattilio
34226876Sjeff/*
35226876Sjeff * Radix tree root.  The height and pointer are set together to permit
36226876Sjeff * coherent lookups while the root is modified.
37226876Sjeff */
38226645Sattiliostruct vm_radix {
39226876Sjeff	uintptr_t	rt_root;		/* root + height */
40226645Sattilio};
41226645Sattilio
42227544Sattilio#ifdef _KERNEL
43227544Sattilio
44226873Sattiliovoid	vm_radix_init(void);
45226645Sattilioint 	vm_radix_insert(struct vm_radix *, vm_pindex_t, void *);
46238245Sattiliovoid	*vm_radix_lookup(struct vm_radix *, vm_pindex_t);
47238245Sattilioint	vm_radix_lookupn(struct vm_radix *, vm_pindex_t, vm_pindex_t, void **,
48238245Sattilio	    int, vm_pindex_t *, u_int *);
49238245Sattiliovoid	*vm_radix_lookup_le(struct vm_radix *, vm_pindex_t);
50226980Sattiliovoid	vm_radix_reclaim_allnodes(struct vm_radix *);
51238245Sattiliovoid	vm_radix_remove(struct vm_radix *, vm_pindex_t);
52226930Sjeff
53226930Sjeff/*
54226646Sjeff * Look up any entry at a position greater or equal to index.
55226646Sjeff */
56226646Sjeffstatic inline void *
57238245Sattiliovm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index)
58226646Sjeff{
59226646Sjeff        void *val;
60238245Sattilio	u_int dummy;
61226646Sjeff
62238245Sattilio        if (vm_radix_lookupn(rtree, index, 0, &val, 1, &index, &dummy))
63226646Sjeff                return (val);
64226646Sjeff        return (NULL);
65226646Sjeff}
66226646Sjeff
67226983Sjeffstatic inline void *
68238245Sattiliovm_radix_last(struct vm_radix *rtree)
69226983Sjeff{
70226983Sjeff
71238245Sattilio	return vm_radix_lookup_le(rtree, 0);
72226983Sjeff}
73226983Sjeff
74226983Sjeffstatic inline void *
75238245Sattiliovm_radix_first(struct vm_radix *rtree)
76226983Sjeff{
77226983Sjeff
78238245Sattilio	return vm_radix_lookup_ge(rtree, 0);
79226983Sjeff}
80226983Sjeff
81226983Sjeffstatic inline void *
82238245Sattiliovm_radix_next(struct vm_radix *rtree, vm_pindex_t index)
83226983Sjeff{
84226983Sjeff
85226983Sjeff	if (index == -1)
86226983Sjeff		return (NULL);
87238245Sattilio	return vm_radix_lookup_ge(rtree, index + 1);
88226983Sjeff}
89226983Sjeff
90226983Sjeffstatic inline void *
91238245Sattiliovm_radix_prev(struct vm_radix *rtree, vm_pindex_t index)
92226983Sjeff{
93226983Sjeff
94226983Sjeff	if (index == 0)
95226983Sjeff		return (NULL);
96238245Sattilio	return vm_radix_lookup_le(rtree, index - 1);
97226983Sjeff}
98226983Sjeff
99226981Sattilio#endif /* _KERNEL */
100226645Sattilio#endif /* !_VM_RADIX_H_ */
101