1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef _MDB_NV_H
  28 #define _MDB_NV_H
  29 
  30 #pragma ident   "%Z%%M% %I%     %E% SMI"
  31 
  32 #include <sys/types.h>
  33 
  34 #ifdef  __cplusplus
  35 extern "C" {
  36 #endif
  37 
  38 #ifdef _MDB
  39 
  40 #define MDB_NV_NAMELEN  31      /* Max variable name length including null */
  41 
  42 /*
  43  * These flags are stored inside each variable in v_flags:
  44  */
  45 #define MDB_NV_PERSIST  0x01    /* Variable is persistent (cannot be unset) */
  46 #define MDB_NV_RDONLY   0x02    /* Variable is read-only (cannot insert over) */
  47 #define MDB_NV_EXTNAME  0x04    /* Variable name is stored externally */
  48 #define MDB_NV_TAGGED   0x08    /* Variable is tagged (user-defined) */
  49 #define MDB_NV_OVERLOAD 0x10    /* Variable can be overloaded (multiple defs) */
  50 
  51 /*
  52  * These flags may be passed to mdb_nv_insert() but are not stored
  53  * inside the variable (and thus use bits outside of 0x00 - 0xff):
  54  */
  55 #define MDB_NV_SILENT   0x100   /* Silence warnings about existing defs */
  56 #define MDB_NV_INTERPOS 0x200   /* Interpose definition over previous defs */
  57 
  58 struct mdb_var;                 /* Forward declaration */
  59 struct mdb_walk_state;          /* Forward declaration */
  60 
  61 /*
  62  * Each variable's behavior with respect to the get-value and set-value
  63  * operations can be changed using a discipline: a pointer to an ops
  64  * vector which can re-define these operations:
  65  */
  66 typedef struct mdb_nv_disc {
  67         void (*disc_set)(struct mdb_var *, uintmax_t);
  68         uintmax_t (*disc_get)(const struct mdb_var *);
  69 } mdb_nv_disc_t;
  70 
  71 /*
  72  * Each variable is defined by the following variable-length structure.
  73  * The debugger uses name/value collections to hash almost everything, so
  74  * we make a few simple space optimizations:
  75  *
  76  * A variable's name can be a pointer to external storage (v_ename and
  77  * MDB_NV_EXTNAME set), or it can be stored locally (MDB_NV_NAMELEN - 1
  78  * bytes of storage are allocated immediately after v_lname[0]).
  79  *
  80  * A variable may have multiple definitions (v_ndef chain), but this feature
  81  * is mutually exclusive with MDB_NV_EXTNAME in order to save space.
  82  */
  83 typedef struct mdb_var {
  84         uintmax_t v_uvalue;             /* Value as unsigned integral type */
  85         union {
  86                 const char *v_ename;    /* Variable name if stored externally */
  87                 struct mdb_var *v_ndef; /* Variable's next definition */
  88         } v_du;
  89         const mdb_nv_disc_t *v_disc;    /* Link to variable discipline */
  90         struct mdb_var *v_next;         /* Link to next var in hash chain */
  91         uchar_t v_flags;                /* Variable flags (see above) */
  92         char v_lname[1];                /* Variable name if stored locally */
  93 } mdb_var_t;
  94 
  95 #define MDB_NV_VALUE(v)         ((v)->v_uvalue)
  96 #define MDB_NV_COOKIE(v)        ((void *)(uintptr_t)((v)->v_uvalue))
  97 
  98 #define v_ename         v_du.v_ename
  99 #define v_ndef          v_du.v_ndef
 100 
 101 /*
 102  * The name/value collection itself is a simple array of hash buckets,
 103  * as well as a persistent bucket index and pointer for iteration:
 104  */
 105 typedef struct mdb_nv {
 106         mdb_var_t **nv_hash;            /* Hash bucket array */
 107         size_t nv_hashsz;               /* Size of hash bucket array */
 108         size_t nv_nelems;               /* Total number of hashed elements */
 109         mdb_var_t *nv_iter_elt;         /* Iterator element pointer */
 110         size_t nv_iter_bucket;          /* Iterator bucket index */
 111         uint_t nv_um_flags;             /* Flags for the memory allocator */
 112 } mdb_nv_t;
 113 
 114 extern mdb_nv_t *mdb_nv_create(mdb_nv_t *, uint_t);
 115 extern void mdb_nv_destroy(mdb_nv_t *);
 116 
 117 extern mdb_var_t *mdb_nv_insert(mdb_nv_t *, const char *,
 118     const mdb_nv_disc_t *, uintmax_t, uint_t);
 119 
 120 extern mdb_var_t *mdb_nv_lookup(mdb_nv_t *, const char *);
 121 extern void mdb_nv_remove(mdb_nv_t *, mdb_var_t *);
 122 
 123 extern void mdb_nv_rewind(mdb_nv_t *);
 124 extern mdb_var_t *mdb_nv_advance(mdb_nv_t *);
 125 extern mdb_var_t *mdb_nv_peek(mdb_nv_t *);
 126 extern size_t mdb_nv_size(mdb_nv_t *);
 127 
 128 extern void mdb_nv_sort_iter(mdb_nv_t *,
 129     int (*)(mdb_var_t *, void *), void *, uint_t);
 130 
 131 extern void mdb_nv_defn_iter(mdb_var_t *,
 132     int (*)(mdb_var_t *, void *), void *);
 133 
 134 extern uintmax_t mdb_nv_get_value(const mdb_var_t *);
 135 extern void mdb_nv_set_value(mdb_var_t *, uintmax_t);
 136 
 137 extern void *mdb_nv_get_cookie(const mdb_var_t *);
 138 extern void mdb_nv_set_cookie(mdb_var_t *, void *);
 139 
 140 extern const char *mdb_nv_get_name(const mdb_var_t *);
 141 extern mdb_var_t *mdb_nv_get_ndef(const mdb_var_t *);
 142 
 143 #endif /* _MDB */
 144 
 145 #ifdef  __cplusplus
 146 }
 147 #endif
 148 
 149 #endif  /* _MDB_NV_H */