kobj.h revision 120343
1/*- 2 * Copyright (c) 2000 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/sys/kobj.h 120343 2003-09-22 21:32:49Z peter $ 27 */ 28 29#ifndef _SYS_KOBJ_H_ 30#define _SYS_KOBJ_H_ 31 32/* 33 * Forward declarations 34 */ 35typedef struct kobj *kobj_t; 36typedef struct kobj_class *kobj_class_t; 37typedef struct kobj_method kobj_method_t; 38typedef int (*kobjop_t)(void); 39typedef struct kobj_ops *kobj_ops_t; 40typedef struct kobjop_desc *kobjop_desc_t; 41struct malloc_type; 42 43struct kobj_method { 44 kobjop_desc_t desc; 45 kobjop_t func; 46}; 47 48/* 49 * A class is simply a method table and a sizeof value. When the first 50 * instance of the class is created, the method table will be compiled 51 * into a form more suited to efficient method dispatch. This compiled 52 * method table is always the first field of the object. 53 */ 54#define KOBJ_CLASS_FIELDS \ 55 const char *name; /* class name */ \ 56 kobj_method_t *methods; /* method table */ \ 57 size_t size; /* object size */ \ 58 u_int refs; /* reference count */ \ 59 kobj_ops_t ops /* compiled method table */ 60 61struct kobj_class { 62 KOBJ_CLASS_FIELDS; 63}; 64 65/* 66 * Implementation of kobj. 67 */ 68#define KOBJ_FIELDS \ 69 kobj_ops_t ops 70 71struct kobj { 72 KOBJ_FIELDS; 73}; 74 75/* 76 * The ops table is used as a cache of results from kobj_lookup_method(). 77 */ 78 79#define KOBJ_CACHE_SIZE 256 80 81struct kobj_ops { 82 kobj_method_t cache[KOBJ_CACHE_SIZE]; 83 kobj_class_t cls; 84}; 85 86struct kobjop_desc { 87 unsigned int id; /* unique ID */ 88 kobjop_t deflt; /* default implementation */ 89}; 90 91/* 92 * Shorthand for constructing method tables. 93 */ 94#define KOBJMETHOD(NAME, FUNC) { &NAME##_desc, (kobjop_t) FUNC } 95 96#define DEFINE_CLASS(name, methods, size) \ 97 \ 98struct kobj_class name ## _class = { \ 99 #name, methods, size \ 100} 101 102/* 103 * Compile the method table in a class. 104 */ 105void kobj_class_compile(kobj_class_t cls); 106 107/* 108 * Compile the method table, with the caller providing the space for 109 * the ops table.(for use before malloc is initialised). 110 */ 111void kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops); 112 113/* 114 * Free the compiled method table in a class. 115 */ 116void kobj_class_free(kobj_class_t cls); 117 118/* 119 * Allocate memory for and initialise a new object. 120 */ 121kobj_t kobj_create(kobj_class_t cls, 122 struct malloc_type *mtype, 123 int mflags); 124 125/* 126 * Initialise a pre-allocated object. 127 */ 128void kobj_init(kobj_t obj, kobj_class_t cls); 129 130/* 131 * Delete an object. If mtype is non-zero, free the memory. 132 */ 133void kobj_delete(kobj_t obj, struct malloc_type *mtype); 134 135/* 136 * Maintain stats on hits/misses in lookup caches. 137 */ 138#ifdef KOBJ_STATS 139extern u_int kobj_lookup_hits; 140extern u_int kobj_lookup_misses; 141#endif 142 143/* 144 * Lookup the method in the cache and if it isn't there look it up the 145 * slow way. 146 */ 147#ifdef KOBJ_STATS 148#define KOBJOPLOOKUP(OPS,OP) do { \ 149 kobjop_desc_t _desc = &OP##_##desc; \ 150 kobj_method_t *_ce = \ 151 &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)]; \ 152 if (_ce->desc != _desc) { \ 153 kobj_lookup_misses++; \ 154 kobj_lookup_method(OPS->cls->methods, _ce, _desc); \ 155 } else { \ 156 kobj_lookup_hits++; \ 157 } \ 158 _m = _ce->func; \ 159} while(0) 160#else /* !KOBJ_STATS */ 161#define KOBJOPLOOKUP(OPS,OP) do { \ 162 kobjop_desc_t _desc = &OP##_##desc; \ 163 kobj_method_t *_ce = \ 164 &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)]; \ 165 if (_ce->desc != _desc) \ 166 kobj_lookup_method(OPS->cls->methods, _ce, _desc); \ 167 _m = _ce->func; \ 168} while(0) 169#endif /* !KOBJ_STATS */ 170 171void kobj_lookup_method(kobj_method_t *methods, 172 kobj_method_t *ce, 173 kobjop_desc_t desc); 174 175#endif /* !_SYS_KOBJ_H_ */ 176