The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/kobj.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2000,2003 Doug Rabson
    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  *      $FreeBSD$
   29  */
   30 
   31 #ifndef _SYS_KOBJ_H_
   32 #define _SYS_KOBJ_H_
   33 
   34 /*
   35  * Forward declarations
   36  */
   37 typedef struct kobj             *kobj_t;
   38 typedef struct kobj_class       *kobj_class_t;
   39 typedef const struct kobj_method kobj_method_t;
   40 typedef void                    (*kobjop_t)(void);
   41 typedef struct kobj_ops         *kobj_ops_t;
   42 typedef struct kobjop_desc      *kobjop_desc_t;
   43 struct malloc_type;
   44 
   45 struct kobj_method {
   46         kobjop_desc_t   desc;
   47         kobjop_t        func;
   48 };
   49 
   50 /*
   51  * A class is simply a method table and a sizeof value. When the first
   52  * instance of the class is created, the method table will be compiled 
   53  * into a form more suited to efficient method dispatch. This compiled 
   54  * method table is always the first field of the object.
   55  */
   56 #define KOBJ_CLASS_FIELDS                                               \
   57         const char      *name;          /* class name */                \
   58         kobj_method_t   *methods;       /* method table */              \
   59         size_t          size;           /* object size */               \
   60         kobj_class_t    *baseclasses;   /* base classes */              \
   61         u_int           refs;           /* reference count */           \
   62         kobj_ops_t      ops             /* compiled method table */
   63 
   64 struct kobj_class {
   65         KOBJ_CLASS_FIELDS;
   66 };
   67 
   68 /*
   69  * Implementation of kobj.
   70  */
   71 #define KOBJ_FIELDS                             \
   72         kobj_ops_t      ops
   73 
   74 struct kobj {
   75         KOBJ_FIELDS;
   76 };
   77 
   78 /*
   79  * The ops table is used as a cache of results from kobj_lookup_method().
   80  */
   81 
   82 #define KOBJ_CACHE_SIZE 256
   83 
   84 struct kobj_ops {
   85         kobj_method_t   *cache[KOBJ_CACHE_SIZE];
   86         kobj_class_t    cls;
   87 };
   88 
   89 struct kobjop_desc {
   90         unsigned int    id;             /* unique ID */
   91         kobj_method_t   deflt;          /* default implementation */
   92 };
   93 
   94 /*
   95  * Shorthand for constructing method tables.
   96  * The ternary operator is (ab)used to provoke a warning when FUNC
   97  * has a signature that is not compatible with kobj method signature.
   98  */
   99 #define KOBJMETHOD(NAME, FUNC) \
  100         { &NAME##_desc, (kobjop_t) (1 ? FUNC : (NAME##_t *)NULL) }
  101 
  102 /*
  103  *
  104  */
  105 #define KOBJMETHOD_END  { NULL, NULL }
  106 
  107 /*
  108  * Declare a class (which should be defined in another file.
  109  */
  110 #define DECLARE_CLASS(name) extern struct kobj_class name
  111 
  112 /*
  113  * Define a class with no base classes (api backward-compatible. with
  114  * FreeBSD-5.1 and earlier).
  115  */
  116 #define DEFINE_CLASS(name, methods, size)               \
  117 DEFINE_CLASS_0(name, name ## _class, methods, size)
  118 
  119 /*
  120  * Define a class with no base classes. Use like this:
  121  *
  122  * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc));
  123  */
  124 #define DEFINE_CLASS_0(name, classvar, methods, size)   \
  125                                                         \
  126 struct kobj_class classvar = {                          \
  127         #name, methods, size, NULL                      \
  128 }
  129 
  130 /*
  131  * Define a class inheriting a single base class. Use like this:
  132  *
  133  * DEFINE_CLASS_1(foo, foo_class, foo_methods, sizeof(foo_softc),
  134  *                        bar);
  135  */
  136 #define DEFINE_CLASS_1(name, classvar, methods, size,   \
  137                        base1)                           \
  138                                                         \
  139 static kobj_class_t name ## _baseclasses[] =            \
  140         { &base1, NULL };                               \
  141 struct kobj_class classvar = {                          \
  142         #name, methods, size, name ## _baseclasses      \
  143 }
  144 
  145 /*
  146  * Define a class inheriting two base classes. Use like this:
  147  *
  148  * DEFINE_CLASS_2(foo, foo_class, foo_methods, sizeof(foo_softc),
  149  *                        bar, baz);
  150  */
  151 #define DEFINE_CLASS_2(name, classvar, methods, size,   \
  152                        base1, base2)                    \
  153                                                         \
  154 static kobj_class_t name ## _baseclasses[] =            \
  155         { &base1,                                       \
  156           &base2, NULL };                               \
  157 struct kobj_class classvar = {                          \
  158         #name, methods, size, name ## _baseclasses      \
  159 }
  160 
  161 /*
  162  * Define a class inheriting three base classes. Use like this:
  163  *
  164  * DEFINE_CLASS_3(foo, foo_class, foo_methods, sizeof(foo_softc),
  165  *                        bar, baz, foobar);
  166  */
  167 #define DEFINE_CLASS_3(name, classvar, methods, size,   \
  168                        base1, base2, base3)             \
  169                                                         \
  170 static kobj_class_t name ## _baseclasses[] =            \
  171         { &base1,                                       \
  172           &base2,                                       \
  173           &base3, NULL };                               \
  174 struct kobj_class classvar = {                          \
  175         #name, methods, size, name ## _baseclasses      \
  176 }
  177 
  178 /*
  179  * Compile the method table in a class.
  180  */
  181 void            kobj_class_compile(kobj_class_t cls);
  182 
  183 /*
  184  * Compile the method table, with the caller providing the space for
  185  * the ops table.(for use before malloc is initialised).
  186  */
  187 void            kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops);
  188 
  189 /*
  190  * Free the compiled method table in a class.
  191  */
  192 void            kobj_class_free(kobj_class_t cls);
  193 
  194 /*
  195  * Allocate memory for and initialise a new object.
  196  */
  197 kobj_t          kobj_create(kobj_class_t cls,
  198                             struct malloc_type *mtype,
  199                             int mflags);
  200 
  201 /*
  202  * Initialise a pre-allocated object.
  203  */
  204 void            kobj_init(kobj_t obj, kobj_class_t cls);
  205 void            kobj_init_static(kobj_t obj, kobj_class_t cls);
  206 
  207 /*
  208  * Delete an object. If mtype is non-zero, free the memory.
  209  */
  210 void            kobj_delete(kobj_t obj, struct malloc_type *mtype);
  211 
  212 /*
  213  * Maintain stats on hits/misses in lookup caches.
  214  */
  215 #ifdef KOBJ_STATS
  216 extern u_int kobj_lookup_hits;
  217 extern u_int kobj_lookup_misses;
  218 #endif
  219 
  220 /*
  221  * Lookup the method in the cache and if it isn't there look it up the
  222  * slow way.
  223  */
  224 #ifdef KOBJ_STATS
  225 #define KOBJOPLOOKUP(OPS,OP) do {                               \
  226         kobjop_desc_t _desc = &OP##_##desc;                     \
  227         kobj_method_t **_cep =                                  \
  228             &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];       \
  229         kobj_method_t *_ce = *_cep;                             \
  230         if (_ce->desc != _desc) {                               \
  231                 _ce = kobj_lookup_method(OPS->cls,              \
  232                                          _cep, _desc);          \
  233                 kobj_lookup_misses++;                           \
  234         } else                                                  \
  235                 kobj_lookup_hits++;                             \
  236         _m = _ce->func;                                         \
  237 } while (0)
  238 #else
  239 #define KOBJOPLOOKUP(OPS,OP) do {                               \
  240         kobjop_desc_t _desc = &OP##_##desc;                     \
  241         kobj_method_t **_cep =                                  \
  242             &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];       \
  243         kobj_method_t *_ce = *_cep;                             \
  244         if (_ce->desc != _desc)                                 \
  245                 _ce = kobj_lookup_method(OPS->cls,              \
  246                                          _cep, _desc);          \
  247         _m = _ce->func;                                         \
  248 } while (0)
  249 #endif
  250 
  251 kobj_method_t* kobj_lookup_method(kobj_class_t cls,
  252                                   kobj_method_t **cep,
  253                                   kobjop_desc_t desc);
  254 
  255 /*
  256  * Default method implementation. Returns ENXIO.
  257  */
  258 int kobj_error_method(void);
  259 
  260 #endif /* !_SYS_KOBJ_H_ */

Cache object: f68440decb0e2aeefe33ad515a29871e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.