vm_radix.h revision 226876
1226645Sattilio/* 2226645Sattilio * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com> 3226645Sattilio * All rights reserved. 4226645Sattilio * 5226645Sattilio * Redistribution and use in source and binary forms, with or without 6226645Sattilio * modification, are permitted provided that the following conditions 7226645Sattilio * are met: 8226645Sattilio * 1. Redistributions of source code must retain the above copyright 9226645Sattilio * notice, this list of conditions and the following disclaimer. 10226645Sattilio * 2. Redistributions in binary form must reproduce the above copyright 11226645Sattilio * notice, this list of conditions and the following disclaimer in the 12226645Sattilio * documentation and/or other materials provided with the distribution. 13226645Sattilio * 14226645Sattilio * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15226645Sattilio * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16226645Sattilio * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17226645Sattilio * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18226645Sattilio * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19226645Sattilio * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20226645Sattilio * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21226645Sattilio * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22226645Sattilio * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23226645Sattilio * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24226645Sattilio * SUCH DAMAGE. 25226645Sattilio * 26226645Sattilio */ 27226645Sattilio 28226645Sattilio#ifndef _VM_RADIX_H_ 29226645Sattilio#define _VM_RADIX_H_ 30226645Sattilio 31226645Sattilio#include <sys/queue.h> 32226645Sattilio 33226645Sattilio/* Default values of the tree parameters */ 34226645Sattilio#define VM_RADIX_WIDTH 5 35226645Sattilio#define VM_RADIX_COUNT (1 << VM_RADIX_WIDTH) 36226645Sattilio#define VM_RADIX_MASK (VM_RADIX_COUNT - 1) 37226645Sattilio#define VM_RADIX_LIMIT howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH) 38226876Sjeff#define VM_RADIX_HEIGHT 0xf /* Bits of height in root */ 39226645Sattilio 40226876SjeffCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT); 41226876Sjeff 42226645Sattilio/* Calculates maximum value for a tree of height h. */ 43226645Sattilio#define VM_RADIX_MAX(h) \ 44226645Sattilio ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) : \ 45226645Sattilio (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1)) 46226645Sattilio 47226645Sattiliostruct vm_radix_node { 48226645Sattilio void *rn_child[VM_RADIX_COUNT]; /* child nodes. */ 49226645Sattilio uint16_t rn_count; /* Valid children. */ 50226645Sattilio}; 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 60226873Sattiliovoid vm_radix_init(void); 61226645Sattilioint vm_radix_insert(struct vm_radix *, vm_pindex_t, void *); 62226645Sattiliovoid *vm_radix_remove(struct vm_radix *, vm_pindex_t); 63226645Sattiliovoid *vm_radix_lookup(struct vm_radix *, vm_pindex_t); 64226645Sattilioint vm_radix_lookupn(struct vm_radix *rtree, vm_pindex_t start, 65226645Sattilio vm_pindex_t end, void **out, int cnt, vm_pindex_t *next); 66226645Sattiliovoid *vm_radix_lookup_le(struct vm_radix *, vm_pindex_t); 67226645Sattiliovoid vm_radix_shrink(struct vm_radix *); 68226645Sattilio 69226646Sjeff/* 70226646Sjeff * Look up any entry at a position greater or equal to index. 71226646Sjeff */ 72226646Sjeffstatic inline void * 73226646Sjeffvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index) 74226646Sjeff{ 75226646Sjeff vm_pindex_t unused; 76226646Sjeff void *val; 77226646Sjeff 78226646Sjeff if (vm_radix_lookupn(rtree, index, 0, &val, 1, &unused)) 79226646Sjeff return (val); 80226646Sjeff return (NULL); 81226646Sjeff} 82226646Sjeff 83226645Sattilio#endif /* !_VM_RADIX_H_ */ 84