vm_radix.h revision 226930
12786Ssos/*
22786Ssos * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
32786Ssos * All rights reserved.
42786Ssos *
52786Ssos * Redistribution and use in source and binary forms, with or without
632822Syokota * modification, are permitted provided that the following conditions
72786Ssos * are met:
82786Ssos * 1. Redistributions of source code must retain the above copyright
92786Ssos *    notice, this list of conditions and the following disclaimer.
102786Ssos * 2. Redistributions in binary form must reproduce the above copyright
112786Ssos *    notice, this list of conditions and the following disclaimer in the
122786Ssos *    documentation and/or other materials provided with the distribution.
132786Ssos *
142786Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152786Ssos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162786Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738140Syokota * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182786Ssos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197420Ssos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202786Ssos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212786Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222786Ssos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232786Ssos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242786Ssos * SUCH DAMAGE.
252786Ssos *
262786Ssos */
272786Ssos
282786Ssos#ifndef _VM_RADIX_H_
292786Ssos#define _VM_RADIX_H_
302786Ssos
312786Ssos#include <sys/queue.h>
322786Ssos
332786Ssos/* Default values of the tree parameters */
342786Ssos#define	VM_RADIX_WIDTH	5
352786Ssos#define	VM_RADIX_COUNT	(1 << VM_RADIX_WIDTH)
362786Ssos#define	VM_RADIX_MASK	(VM_RADIX_COUNT - 1)
372786Ssos#define	VM_RADIX_LIMIT	howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH)
382786Ssos#define	VM_RADIX_FLAGS	0x3	/* Flag bits stored in node pointers. */
392786Ssos#define	VM_RADIX_BLACK	0x1	/* Black node. (leaf only) */
402786Ssos#define	VM_RADIX_RED	0x2	/* Red node. (leaf only) */
412786Ssos#define	VM_RADIX_ANY	(VM_RADIX_RED | VM_RADIX_BLACK)
422786Ssos#define	VM_RADIX_EMPTY	0x1	/* Empty hint. (internal only) */
432786Ssos#define	VM_RADIX_HEIGHT	0xf	/* Bits of height in root */
442786Ssos#define	VM_RADIX_STACK	8	/* Nodes to store on stack. */
452786Ssos
4644160SyokotaCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT);
472786Ssos
482786Ssos/* Calculates maximum value for a tree of height h. */
492786Ssos#define	VM_RADIX_MAX(h)							\
502786Ssos	    ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) :		\
512786Ssos	    (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1))
522786Ssos
532786Ssosstruct vm_radix_node {
542786Ssos	void		*rn_child[VM_RADIX_COUNT];	/* child nodes. */
552786Ssos    	uint16_t	rn_count;			/* Valid children. */
562786Ssos};
572786Ssos
582786Ssos/*
592786Ssos * Radix tree root.  The height and pointer are set together to permit
6043334Syokota * coherent lookups while the root is modified.
612786Ssos */
6232822Syokotastruct vm_radix {
632786Ssos	uintptr_t	rt_root;		/* root + height */
642786Ssos};
652786Ssos
662786Ssosvoid	vm_radix_init(void);
672786Ssos
682786Ssos/*
692786Ssos * Functions which only work with black nodes. (object lock)
702786Ssos */
712786Ssosint 	vm_radix_insert(struct vm_radix *, vm_pindex_t, void *);
722786Ssosvoid 	vm_radix_shrink(struct vm_radix *);
732786Ssos
7443334Syokota/*
7543334Syokota * Functions which work on specified colors. (object, vm_page_queue_free locks)
762786Ssos */
772786Ssosvoid	*vm_radix_color(struct vm_radix *, vm_pindex_t, int color);
782786Ssosvoid	*vm_radix_lookup(struct vm_radix *, vm_pindex_t, int color);
7943334Syokotaint	vm_radix_lookupn(struct vm_radix *rtree, vm_pindex_t start,
802786Ssos	    vm_pindex_t end, int color, void **out, int cnt, vm_pindex_t *next);
815994Ssosvoid	*vm_radix_lookup_le(struct vm_radix *, vm_pindex_t, int color);
8243334Syokotavoid	*vm_radix_remove(struct vm_radix *, vm_pindex_t, int color);
832786Ssos
842786Ssos/*
852786Ssos * Look up any entry at a position greater or equal to index.
862786Ssos */
872786Ssosstatic inline void *
886045Ssosvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index, int color)
8943334Syokota{
902786Ssos        void *val;
912786Ssos
922786Ssos        if (vm_radix_lookupn(rtree, index, 0, color, &val, 1, &index))
932786Ssos                return (val);
9418194Ssos        return (NULL);
952786Ssos}
962786Ssos
9743334Syokota#endif /* !_VM_RADIX_H_ */
982786Ssos