vm_radix.h revision 226983
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
32226645Sattilio#include <sys/queue.h>
33226645Sattilio
34226645Sattilio/* Default values of the tree parameters */
35226645Sattilio#define	VM_RADIX_WIDTH	5
36226645Sattilio#define	VM_RADIX_COUNT	(1 << VM_RADIX_WIDTH)
37226645Sattilio#define	VM_RADIX_MASK	(VM_RADIX_COUNT - 1)
38226645Sattilio#define	VM_RADIX_LIMIT	howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH)
39226930Sjeff#define	VM_RADIX_FLAGS	0x3	/* Flag bits stored in node pointers. */
40226930Sjeff#define	VM_RADIX_BLACK	0x1	/* Black node. (leaf only) */
41226930Sjeff#define	VM_RADIX_RED	0x2	/* Red node. (leaf only) */
42226930Sjeff#define	VM_RADIX_ANY	(VM_RADIX_RED | VM_RADIX_BLACK)
43226930Sjeff#define	VM_RADIX_EMPTY	0x1	/* Empty hint. (internal only) */
44226930Sjeff#define	VM_RADIX_HEIGHT	0xf	/* Bits of height in root */
45226930Sjeff#define	VM_RADIX_STACK	8	/* Nodes to store on stack. */
46226645Sattilio
47226876SjeffCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT);
48226876Sjeff
49226645Sattilio/* Calculates maximum value for a tree of height h. */
50226645Sattilio#define	VM_RADIX_MAX(h)							\
51226645Sattilio	    ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) :		\
52226645Sattilio	    (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1))
53226645Sattilio
54226981Sattilio#ifdef _KERNEL
55226981Sattilio
56226645Sattiliostruct vm_radix_node {
57226930Sjeff	void		*rn_child[VM_RADIX_COUNT];	/* child nodes. */
58226645Sattilio    	uint16_t	rn_count;			/* Valid children. */
59226645Sattilio};
60226645Sattilio
61226876Sjeff/*
62226876Sjeff * Radix tree root.  The height and pointer are set together to permit
63226876Sjeff * coherent lookups while the root is modified.
64226876Sjeff */
65226645Sattiliostruct vm_radix {
66226876Sjeff	uintptr_t	rt_root;		/* root + height */
67226645Sattilio};
68226645Sattilio
69226873Sattiliovoid	vm_radix_init(void);
70226930Sjeff
71226930Sjeff/*
72226930Sjeff * Functions which only work with black nodes. (object lock)
73226930Sjeff */
74226645Sattilioint 	vm_radix_insert(struct vm_radix *, vm_pindex_t, void *);
75226645Sattiliovoid 	vm_radix_shrink(struct vm_radix *);
76226645Sattilio
77226646Sjeff/*
78226930Sjeff * Functions which work on specified colors. (object, vm_page_queue_free locks)
79226930Sjeff */
80226952Sjeffvoid	*vm_radix_color(struct vm_radix *, vm_pindex_t, int);
81226952Sjeffvoid	*vm_radix_lookup(struct vm_radix *, vm_pindex_t, int);
82226952Sjeffint	vm_radix_lookupn(struct vm_radix *, vm_pindex_t, vm_pindex_t, int,
83226952Sjeff	    void **, int, vm_pindex_t *);
84226952Sjeffvoid	*vm_radix_lookup_le(struct vm_radix *, vm_pindex_t, int);
85226980Sattiliovoid	vm_radix_reclaim_allnodes(struct vm_radix *);
86226952Sjeffvoid	*vm_radix_remove(struct vm_radix *, vm_pindex_t, int);
87226952Sjeffvoid	vm_radix_foreach(struct vm_radix *, vm_pindex_t, vm_pindex_t, int,
88226952Sjeff	    void (*)(void *));
89226930Sjeff
90226930Sjeff/*
91226646Sjeff * Look up any entry at a position greater or equal to index.
92226646Sjeff */
93226646Sjeffstatic inline void *
94226930Sjeffvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index, int color)
95226646Sjeff{
96226646Sjeff        void *val;
97226646Sjeff
98226930Sjeff        if (vm_radix_lookupn(rtree, index, 0, color, &val, 1, &index))
99226646Sjeff                return (val);
100226646Sjeff        return (NULL);
101226646Sjeff}
102226646Sjeff
103226983Sjeffstatic inline void *
104226983Sjeffvm_radix_last(struct vm_radix *rtree, int color)
105226983Sjeff{
106226983Sjeff
107226983Sjeff	return vm_radix_lookup_le(rtree, 0, color);
108226983Sjeff}
109226983Sjeff
110226983Sjeffstatic inline void *
111226983Sjeffvm_radix_first(struct vm_radix *rtree, int color)
112226983Sjeff{
113226983Sjeff
114226983Sjeff	return vm_radix_lookup_ge(rtree, 0, color);
115226983Sjeff}
116226983Sjeff
117226983Sjeffstatic inline void *
118226983Sjeffvm_radix_next(struct vm_radix *rtree, vm_pindex_t index, int color)
119226983Sjeff{
120226983Sjeff
121226983Sjeff	if (index == -1)
122226983Sjeff		return (NULL);
123226983Sjeff	return vm_radix_lookup_ge(rtree, index + 1, color);
124226983Sjeff}
125226983Sjeff
126226983Sjeffstatic inline void *
127226983Sjeffvm_radix_prev(struct vm_radix *rtree, vm_pindex_t index, int color)
128226983Sjeff{
129226983Sjeff
130226983Sjeff	if (index == 0)
131226983Sjeff		return (NULL);
132226983Sjeff	return vm_radix_lookup_le(rtree, index - 1, color);
133226983Sjeff}
134226983Sjeff
135226981Sattilio#endif /* _KERNEL */
136226645Sattilio#endif /* !_VM_RADIX_H_ */
137