vm_radix.h revision 226873
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) 38226645Sattilio 39226645Sattilio/* Calculates maximum value for a tree of height h. */ 40226645Sattilio#define VM_RADIX_MAX(h) \ 41226645Sattilio ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) : \ 42226645Sattilio (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1)) 43226645Sattilio 44226645Sattiliostruct vm_radix_node { 45226645Sattilio SLIST_ENTRY(vm_radix_node) next; 46226645Sattilio void *rn_child[VM_RADIX_COUNT]; /* child nodes. */ 47226645Sattilio uint16_t rn_count; /* Valid children. */ 48226645Sattilio}; 49226645Sattilio 50226645Sattiliostruct vm_radix { 51226645Sattilio struct vm_radix_node *rt_root; /* Root node. */ 52226645Sattilio int rt_height; /* Number of levels + 1. */ 53226645Sattilio}; 54226645Sattilio 55226873Sattiliovoid vm_radix_init(void); 56226645Sattilioint vm_radix_insert(struct vm_radix *, vm_pindex_t, void *); 57226645Sattiliovoid *vm_radix_remove(struct vm_radix *, vm_pindex_t); 58226645Sattiliovoid *vm_radix_lookup(struct vm_radix *, vm_pindex_t); 59226645Sattilioint vm_radix_lookupn(struct vm_radix *rtree, vm_pindex_t start, 60226645Sattilio vm_pindex_t end, void **out, int cnt, vm_pindex_t *next); 61226645Sattiliovoid *vm_radix_lookup_le(struct vm_radix *, vm_pindex_t); 62226645Sattiliovoid vm_radix_shrink(struct vm_radix *); 63226645Sattilio 64226646Sjeff/* 65226646Sjeff * Look up any entry at a position greater or equal to index. 66226646Sjeff */ 67226646Sjeffstatic inline void * 68226646Sjeffvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index) 69226646Sjeff{ 70226646Sjeff vm_pindex_t unused; 71226646Sjeff void *val; 72226646Sjeff 73226646Sjeff if (vm_radix_lookupn(rtree, index, 0, &val, 1, &unused)) 74226646Sjeff return (val); 75226646Sjeff return (NULL); 76226646Sjeff} 77226646Sjeff 78226645Sattilio#endif /* !_VM_RADIX_H_ */ 79