| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: sym.h,v 1.9 1999/08/02 16:53:48 mdw Exp $ |
| 4 | * |
| 5 | * Symbol table management |
| 6 | * |
| 7 | * (c) 1998 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 14 | * mLib is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * mLib is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with mLib; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: sym.h,v $ |
| 33 | * Revision 1.9 1999/08/02 16:53:48 mdw |
| 34 | * Improve type safety for sym_iter objects. |
| 35 | * |
| 36 | * Revision 1.8 1999/08/02 14:45:48 mdw |
| 37 | * Break low-level hashtable code out from sym. |
| 38 | * |
| 39 | * Revision 1.7 1999/06/01 09:49:33 mdw |
| 40 | * Allow things to be looked up by just their caller-supplied hashes. This |
| 41 | * actually needs to be thought through better. |
| 42 | * |
| 43 | * Revision 1.6 1999/05/26 21:08:31 mdw |
| 44 | * Rename symbols in line with newer conventions. |
| 45 | * |
| 46 | * Revision 1.5 1999/05/13 22:48:37 mdw |
| 47 | * Change `-ise' to `-ize' throughout. |
| 48 | * |
| 49 | * Revision 1.4 1999/05/06 19:51:35 mdw |
| 50 | * Reformatted the LGPL notice a little bit. |
| 51 | * |
| 52 | * Revision 1.3 1999/05/05 18:50:31 mdw |
| 53 | * Change licensing conditions to LGPL. |
| 54 | * |
| 55 | * Revision 1.2 1998/11/26 19:27:34 mdw |
| 56 | * Move SYM_NAME into the header file. Fix bugs. |
| 57 | * |
| 58 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
| 59 | * Initial version of mLib |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | #ifndef SYM_H |
| 64 | #define SYM_H |
| 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
| 70 | /*----- Required headers --------------------------------------------------*/ |
| 71 | |
| 72 | #include <stddef.h> |
| 73 | |
| 74 | #ifndef BITS_H |
| 75 | # include "bits.h" |
| 76 | #endif |
| 77 | |
| 78 | #ifndef HASH_H |
| 79 | # include "hash.h" |
| 80 | #endif |
| 81 | |
| 82 | /*----- Type definitions --------------------------------------------------*/ |
| 83 | |
| 84 | /* --- Symbol table --- * |
| 85 | * |
| 86 | * A @sym_table@ contains the information needed to manage a symbol table. |
| 87 | * Users shouldn't fiddle with this information directly, but it needs to be |
| 88 | * here so that objects of the correct type can be created. |
| 89 | */ |
| 90 | |
| 91 | typedef struct sym_table { |
| 92 | hash_table t; |
| 93 | size_t load; |
| 94 | } sym_table; |
| 95 | |
| 96 | /* --- A symbol table entry --- * |
| 97 | * |
| 98 | * I don't care what actually gets stored in symbol entries because I don't |
| 99 | * create them: that's the responsibility of my client. All I care about |
| 100 | * here is that whatever gets passed to me is a structure whose first member |
| 101 | * is a @sym_base@. The ANSI guarantees about structure layout are |
| 102 | * sufficient to allow me to manipulate such objects. |
| 103 | */ |
| 104 | |
| 105 | #define SYM_BUFSZ 16 /* Size of local string buffer */ |
| 106 | |
| 107 | typedef struct sym_base { |
| 108 | hash_base b; |
| 109 | union { |
| 110 | char *p; /* Pointer to name string */ |
| 111 | char b[SYM_BUFSZ]; /* Buffer containing a short name */ |
| 112 | } name; /* Name of this symbol */ |
| 113 | size_t len; /* Length of the symbol's name */ |
| 114 | } sym_base; |
| 115 | |
| 116 | /* --- A macro to pick a symbol's name out from the mess --- */ |
| 117 | |
| 118 | #define SYM_NAME(sy) \ |
| 119 | (((sym_base *)(sy))->len > SYM_BUFSZ ? \ |
| 120 | ((sym_base *)(sy))->name.p : \ |
| 121 | ((sym_base *)(sy))->name.b) |
| 122 | |
| 123 | /* --- An iterator block --- */ |
| 124 | |
| 125 | typedef struct { hash_iter i; } sym_iter; |
| 126 | |
| 127 | /*----- External functions ------------------------------------------------*/ |
| 128 | |
| 129 | /* --- @sym_create@ --- * |
| 130 | * |
| 131 | * Arguments: @sym_table *t@ = symbol table to initialize |
| 132 | * |
| 133 | * Returns: --- |
| 134 | * |
| 135 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
| 136 | * there isn't enough memory. |
| 137 | */ |
| 138 | |
| 139 | extern void sym_create(sym_table */*t*/); |
| 140 | |
| 141 | /* --- @sym_destroy@ --- * |
| 142 | * |
| 143 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
| 144 | * |
| 145 | * Returns: --- |
| 146 | * |
| 147 | * Use: Destroys a symbol table, freeing all the memory it used to |
| 148 | * occupy. |
| 149 | */ |
| 150 | |
| 151 | extern void sym_destroy(sym_table */*t*/); |
| 152 | |
| 153 | /* --- @sym_find@ --- * |
| 154 | * |
| 155 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
| 156 | * @const char *n@ = pointer to symbol table to look up |
| 157 | * @long l@ = length of the name string or negative to measure |
| 158 | * @size_t sz@ = size of desired symbol object, or zero |
| 159 | * @unsigned *f@ = pointer to a flag, or null. |
| 160 | * |
| 161 | * Returns: The address of a @sym_base@ structure, or null if not found |
| 162 | * and @sz@ is zero. |
| 163 | * |
| 164 | * Use: Looks up a symbol in a given symbol table. The name is |
| 165 | * passed by the address of its first character. The length |
| 166 | * may be given, in which case the name may contain arbitrary |
| 167 | * binary data, or it may be given as a negative number, in |
| 168 | * which case the length of the name is calculated as |
| 169 | * @strlen(n) + 1@. |
| 170 | * |
| 171 | * The return value is the address of a pointer to a @sym_base@ |
| 172 | * block (which may have other things on the end, as above). If |
| 173 | * the symbol could be found, the return value points to the |
| 174 | * symbol block. If the symbol wasn't there, then if @sz@ is |
| 175 | * nonzero, a new symbol is created and its address is returned; |
| 176 | * otherwise a null pointer is returned. The exception |
| 177 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
| 178 | * |
| 179 | * The value of @*f@ indicates whether a new symbol entry was |
| 180 | * created: a nonzero value indicates that an old value was |
| 181 | * found. |
| 182 | */ |
| 183 | |
| 184 | extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, |
| 185 | size_t /*sz*/, unsigned */*f*/); |
| 186 | |
| 187 | /* --- @sym_remove@ --- * |
| 188 | * |
| 189 | * Arguments: @sym_table *i@ = pointer to a symbol table object |
| 190 | * @void *b@ = pointer to symbol table entry |
| 191 | * |
| 192 | * Returns: --- |
| 193 | * |
| 194 | * Use: Removes the object from the symbol table. The space occupied |
| 195 | * by the object and its name is freed; anything else attached |
| 196 | * to the entry should already be gone by this point. |
| 197 | */ |
| 198 | |
| 199 | extern void sym_remove(sym_table */*t*/, void */*b*/); |
| 200 | |
| 201 | /* --- @sym_mkiter@ --- * |
| 202 | * |
| 203 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
| 204 | * @sym_table *t@ = pointer to a symbol table object |
| 205 | * |
| 206 | * Returns: --- |
| 207 | * |
| 208 | * Use: Creates a new symbol table iterator which may be used to |
| 209 | * iterate through a symbol table. |
| 210 | */ |
| 211 | |
| 212 | #define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t) |
| 213 | |
| 214 | extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); |
| 215 | |
| 216 | /* --- @sym_next@ --- * |
| 217 | * |
| 218 | * Arguments: @sym_iter *i@ = pointer to iterator object |
| 219 | * |
| 220 | * Returns: Pointer to the next symbol found, or null when finished. |
| 221 | * |
| 222 | * Use: Returns the next symbol from the table. Symbols are not |
| 223 | * returned in any particular order. |
| 224 | */ |
| 225 | |
| 226 | #define SYM_NEXT(i_, p) do { \ |
| 227 | hash_base *_q; \ |
| 228 | HASH_NEXT(&(i_)->i, _q); \ |
| 229 | (p) = (void *)_q; \ |
| 230 | } while (0) |
| 231 | |
| 232 | extern void *sym_next(sym_iter */*i*/); |
| 233 | |
| 234 | /*----- That's all, folks -------------------------------------------------*/ |
| 235 | |
| 236 | #ifdef __cplusplus |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | #endif |