vm_radix.h revision 226980
1148456Spjd/* 2148456Spjd * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com> 3148456Spjd * All rights reserved. 4148456Spjd * 5148456Spjd * Redistribution and use in source and binary forms, with or without 6148456Spjd * modification, are permitted provided that the following conditions 7148456Spjd * are met: 8148456Spjd * 1. Redistributions of source code must retain the above copyright 9148456Spjd * notice, this list of conditions and the following disclaimer. 10148456Spjd * 2. Redistributions in binary form must reproduce the above copyright 11148456Spjd * notice, this list of conditions and the following disclaimer in the 12148456Spjd * documentation and/or other materials provided with the distribution. 13155174Spjd * 14148456Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15148456Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16148456Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17148456Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18148456Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19148456Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20148456Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21148456Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22148456Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23148456Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24148456Spjd * SUCH DAMAGE. 25148456Spjd * 26148456Spjd */ 27148456Spjd 28148456Spjd#ifndef _VM_RADIX_H_ 29148456Spjd#define _VM_RADIX_H_ 30148456Spjd 31148456Spjd#include <sys/queue.h> 32148456Spjd 33148456Spjd/* Default values of the tree parameters */ 34148456Spjd#define VM_RADIX_WIDTH 5 35148456Spjd#define VM_RADIX_COUNT (1 << VM_RADIX_WIDTH) 36148456Spjd#define VM_RADIX_MASK (VM_RADIX_COUNT - 1) 37148456Spjd#define VM_RADIX_LIMIT howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH) 38148456Spjd#define VM_RADIX_FLAGS 0x3 /* Flag bits stored in node pointers. */ 39148456Spjd#define VM_RADIX_BLACK 0x1 /* Black node. (leaf only) */ 40148456Spjd#define VM_RADIX_RED 0x2 /* Red node. (leaf only) */ 41148456Spjd#define VM_RADIX_ANY (VM_RADIX_RED | VM_RADIX_BLACK) 42148456Spjd#define VM_RADIX_EMPTY 0x1 /* Empty hint. (internal only) */ 43148456Spjd#define VM_RADIX_HEIGHT 0xf /* Bits of height in root */ 44148456Spjd#define VM_RADIX_STACK 8 /* Nodes to store on stack. */ 45148456Spjd 46148456SpjdCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT); 47148456Spjd 48148456Spjd/* Calculates maximum value for a tree of height h. */ 49148456Spjd#define VM_RADIX_MAX(h) \ 50148456Spjd ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) : \ 51148456Spjd (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1)) 52148456Spjd 53148456Spjdstruct vm_radix_node { 54148456Spjd void *rn_child[VM_RADIX_COUNT]; /* child nodes. */ 55148456Spjd uint16_t rn_count; /* Valid children. */ 56148456Spjd}; 57148456Spjd 58148456Spjd/* 59148456Spjd * Radix tree root. The height and pointer are set together to permit 60148456Spjd * coherent lookups while the root is modified. 61148456Spjd */ 62148456Spjdstruct vm_radix { 63148456Spjd uintptr_t rt_root; /* root + height */ 64148456Spjd}; 65148456Spjd 66148456Spjdvoid vm_radix_init(void); 67148456Spjd 68148456Spjd/* 69148456Spjd * Functions which only work with black nodes. (object lock) 70148456Spjd */ 71148456Spjdint vm_radix_insert(struct vm_radix *, vm_pindex_t, void *); 72148456Spjdvoid vm_radix_shrink(struct vm_radix *); 73148456Spjd 74148456Spjd/* 75148456Spjd * Functions which work on specified colors. (object, vm_page_queue_free locks) 76167755Ssam */ 77148456Spjdvoid *vm_radix_color(struct vm_radix *, vm_pindex_t, int); 78148456Spjdvoid *vm_radix_lookup(struct vm_radix *, vm_pindex_t, int); 79148456Spjdint vm_radix_lookupn(struct vm_radix *, vm_pindex_t, vm_pindex_t, int, 80148456Spjd void **, int, vm_pindex_t *); 81148456Spjdvoid *vm_radix_lookup_le(struct vm_radix *, vm_pindex_t, int); 82148456Spjdvoid vm_radix_reclaim_allnodes(struct vm_radix *); 83148456Spjdvoid *vm_radix_remove(struct vm_radix *, vm_pindex_t, int); 84148456Spjdvoid vm_radix_foreach(struct vm_radix *, vm_pindex_t, vm_pindex_t, int, 85148456Spjd void (*)(void *)); 86148456Spjd 87148456Spjd/* 88148456Spjd * Look up any entry at a position greater or equal to index. 89148456Spjd */ 90148456Spjdstatic inline void * 91148456Spjdvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index, int color) 92148456Spjd{ 93148456Spjd void *val; 94148456Spjd 95148456Spjd if (vm_radix_lookupn(rtree, index, 0, color, &val, 1, &index)) 96148456Spjd return (val); 97148456Spjd return (NULL); 98148456Spjd} 99148456Spjd 100157900Spjd#endif /* !_VM_RADIX_H_ */ 101148456Spjd