1 /* $NetBSD: vmem_impl.h,v 1.3 2013/11/22 21:04:11 christos Exp $ */ 2 3 /*- 4 * Copyright (c)2006 YAMAMOTO Takashi, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * Data structures private to vmem. 31 */ 32 33 #ifndef _SYS_VMEM_IMPL_H_ 34 #define _SYS_VMEM_IMPL_H_ 35 36 #include <sys/types.h> 37 38 #if defined(_KERNEL) 39 #define QCACHE 40 #include <sys/vmem.h> 41 42 #define LOCK_DECL(name) \ 43 kmutex_t name; char lockpad[COHERENCY_UNIT - sizeof(kmutex_t)] 44 45 #define CONDVAR_DECL(name) \ 46 kcondvar_t name 47 48 #else /* defined(_KERNEL) */ 49 #include <stdio.h> 50 #include <errno.h> 51 #include <assert.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "vmem.h" 56 57 #define LOCK_DECL(name) /* nothing */ 58 #define CONDVAR_DECL(name) /* nothing */ 59 #endif /* defined(_KERNEL) */ 60 61 #define VMEM_MAXORDER (sizeof(vmem_size_t) * CHAR_BIT) 62 63 typedef struct vmem_btag bt_t; 64 65 TAILQ_HEAD(vmem_seglist, vmem_btag); 66 LIST_HEAD(vmem_freelist, vmem_btag); 67 LIST_HEAD(vmem_hashlist, vmem_btag); 68 69 #if defined(QCACHE) 70 #define VMEM_QCACHE_IDX_MAX 16 71 72 #define QC_NAME_MAX 16 73 74 struct qcache { 75 pool_cache_t qc_cache; 76 vmem_t *qc_vmem; 77 char qc_name[QC_NAME_MAX]; 78 }; 79 typedef struct qcache qcache_t; 80 #define QC_POOL_TO_QCACHE(pool) ((qcache_t *)(pool->pr_qcache)) 81 #endif /* defined(QCACHE) */ 82 83 #define VMEM_NAME_MAX 16 84 85 /* vmem arena */ 86 struct vmem { 87 CONDVAR_DECL(vm_cv); 88 LOCK_DECL(vm_lock); 89 vm_flag_t vm_flags; 90 vmem_import_t *vm_importfn; 91 vmem_release_t *vm_releasefn; 92 size_t vm_nfreetags; 93 LIST_HEAD(, vmem_btag) vm_freetags; 94 void *vm_arg; 95 struct vmem_seglist vm_seglist; 96 struct vmem_freelist vm_freelist[VMEM_MAXORDER]; 97 size_t vm_hashsize; 98 size_t vm_nbusytag; 99 struct vmem_hashlist *vm_hashlist; 100 struct vmem_hashlist vm_hash0; 101 size_t vm_quantum_mask; 102 int vm_quantum_shift; 103 size_t vm_size; 104 size_t vm_inuse; 105 char vm_name[VMEM_NAME_MAX+1]; 106 LIST_ENTRY(vmem) vm_alllist; 107 108 #if defined(QCACHE) 109 /* quantum cache */ 110 size_t vm_qcache_max; 111 struct pool_allocator vm_qcache_allocator; 112 qcache_t vm_qcache_store[VMEM_QCACHE_IDX_MAX]; 113 qcache_t *vm_qcache[VMEM_QCACHE_IDX_MAX]; 114 #endif /* defined(QCACHE) */ 115 }; 116 117 /* boundary tag */ 118 struct vmem_btag { 119 TAILQ_ENTRY(vmem_btag) bt_seglist; 120 union { 121 LIST_ENTRY(vmem_btag) u_freelist; /* BT_TYPE_FREE */ 122 LIST_ENTRY(vmem_btag) u_hashlist; /* BT_TYPE_BUSY */ 123 } bt_u; 124 #define bt_hashlist bt_u.u_hashlist 125 #define bt_freelist bt_u.u_freelist 126 vmem_addr_t bt_start; 127 vmem_size_t bt_size; 128 int bt_type; 129 }; 130 131 #define BT_TYPE_SPAN 1 132 #define BT_TYPE_SPAN_STATIC 2 133 #define BT_TYPE_FREE 3 134 #define BT_TYPE_BUSY 4 135 #define BT_ISSPAN_P(bt) ((bt)->bt_type <= BT_TYPE_SPAN_STATIC) 136 137 #define BT_END(bt) ((bt)->bt_start + (bt)->bt_size - 1) 138 139 #endif /* !_SYS_VMEM_IMPL_H_ */ 140