vm_radix.h revision 227544
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 47226645Sattilio/* Calculates maximum value for a tree of height h. */ 48226645Sattilio#define VM_RADIX_MAX(h) \ 49226645Sattilio ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) : \ 50226645Sattilio (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1)) 51226645Sattilio 52226876Sjeff/* 53226876Sjeff * Radix tree root. The height and pointer are set together to permit 54226876Sjeff * coherent lookups while the root is modified. 55226876Sjeff */ 56226645Sattiliostruct vm_radix { 57226876Sjeff uintptr_t rt_root; /* root + height */ 58226645Sattilio}; 59226645Sattilio 60227544Sattilio#ifdef _KERNEL 61227544SattilioCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT); 62227544Sattilio 63227544Sattiliostruct vm_radix_node { 64227544Sattilio void *rn_child[VM_RADIX_COUNT]; /* child nodes. */ 65227544Sattilio uint16_t rn_count; /* Valid children. */ 66227544Sattilio}; 67227544Sattilio 68226873Sattiliovoid vm_radix_init(void); 69226930Sjeff 70226930Sjeff/* 71226930Sjeff * Functions which only work with black nodes. (object lock) 72226930Sjeff */ 73226645Sattilioint vm_radix_insert(struct vm_radix *, vm_pindex_t, void *); 74226645Sattiliovoid vm_radix_shrink(struct vm_radix *); 75226645Sattilio 76226646Sjeff/* 77226930Sjeff * Functions which work on specified colors. (object, vm_page_queue_free locks) 78226930Sjeff */ 79226952Sjeffvoid *vm_radix_color(struct vm_radix *, vm_pindex_t, int); 80226952Sjeffvoid *vm_radix_lookup(struct vm_radix *, vm_pindex_t, int); 81226952Sjeffint vm_radix_lookupn(struct vm_radix *, vm_pindex_t, vm_pindex_t, int, 82226952Sjeff void **, int, vm_pindex_t *); 83226952Sjeffvoid *vm_radix_lookup_le(struct vm_radix *, vm_pindex_t, int); 84226980Sattiliovoid vm_radix_reclaim_allnodes(struct vm_radix *); 85226952Sjeffvoid *vm_radix_remove(struct vm_radix *, vm_pindex_t, int); 86226952Sjeffvoid vm_radix_foreach(struct vm_radix *, vm_pindex_t, vm_pindex_t, int, 87226952Sjeff void (*)(void *)); 88226930Sjeff 89226930Sjeff/* 90226646Sjeff * Look up any entry at a position greater or equal to index. 91226646Sjeff */ 92226646Sjeffstatic inline void * 93226930Sjeffvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index, int color) 94226646Sjeff{ 95226646Sjeff void *val; 96226646Sjeff 97226930Sjeff if (vm_radix_lookupn(rtree, index, 0, color, &val, 1, &index)) 98226646Sjeff return (val); 99226646Sjeff return (NULL); 100226646Sjeff} 101226646Sjeff 102226983Sjeffstatic inline void * 103226983Sjeffvm_radix_last(struct vm_radix *rtree, int color) 104226983Sjeff{ 105226983Sjeff 106226983Sjeff return vm_radix_lookup_le(rtree, 0, color); 107226983Sjeff} 108226983Sjeff 109226983Sjeffstatic inline void * 110226983Sjeffvm_radix_first(struct vm_radix *rtree, int color) 111226983Sjeff{ 112226983Sjeff 113226983Sjeff return vm_radix_lookup_ge(rtree, 0, color); 114226983Sjeff} 115226983Sjeff 116226983Sjeffstatic inline void * 117226983Sjeffvm_radix_next(struct vm_radix *rtree, vm_pindex_t index, int color) 118226983Sjeff{ 119226983Sjeff 120226983Sjeff if (index == -1) 121226983Sjeff return (NULL); 122226983Sjeff return vm_radix_lookup_ge(rtree, index + 1, color); 123226983Sjeff} 124226983Sjeff 125226983Sjeffstatic inline void * 126226983Sjeffvm_radix_prev(struct vm_radix *rtree, vm_pindex_t index, int color) 127226983Sjeff{ 128226983Sjeff 129226983Sjeff if (index == 0) 130226983Sjeff return (NULL); 131226983Sjeff return vm_radix_lookup_le(rtree, index - 1, color); 132226983Sjeff} 133226983Sjeff 134226981Sattilio#endif /* _KERNEL */ 135226645Sattilio#endif /* !_VM_RADIX_H_ */ 136