chiark / gitweb /
Fix licence text.
[storin] / sym.h
1 /* -*-c-*-
2  *
3  * $Id: sym.h,v 1.2 2000/07/02 15:21:20 mdw Exp $
4  *
5  * Symbol table management
6  *
7  * (c) 1998 Straylight
8  * (c) 2000 Mark Wooding
9  */
10
11 /*----- Licensing notice --------------------------------------------------* 
12  *
13  * Copyright (c) 2000 Mark Wooding
14  * All rights reserved.
15  * 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are
18  * met:
19  * 
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 
23  * 2, Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 
27  * 3. The name of the authors may not be used to endorse or promote
28  *    products derived from this software without specific prior written
29  *    permission.
30  * 
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
34  * NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  * 
43  * Instead of accepting the above terms, you may redistribute and/or modify
44  * this software under the terms of either the GNU General Public License,
45  * or the GNU Library General Public License, published by the Free
46  * Software Foundation; either version 2 of the License, or (at your
47  * option) any later version.
48  */
49
50 /*----- Revision history --------------------------------------------------*
51  *
52  * $Log: sym.h,v $
53  * Revision 1.2  2000/07/02 15:21:20  mdw
54  * Fix licence text.
55  *
56  * Revision 1.1  2000/05/21 11:28:30  mdw
57  * Initial check-in.
58  *
59  * --- Past lives (Become) --- *
60  *
61  * Revision 1.3  1998/01/12 16:46:30  mdw
62  * Fix copyright date.
63  *
64  * Revision 1.2  1997/08/04 10:24:25  mdw
65  * Sources placed under CVS control.
66  *
67  * Revision 1.1  1997/07/21  13:47:43  mdw
68  * Initial revision
69  *
70  */
71
72 #ifndef SYM_H
73 #define SYM_H
74
75 #ifdef __cplusplus
76   extern "C" {
77 #endif
78
79 /*----- Required headers --------------------------------------------------*/
80
81 #include <stddef.h>
82
83 #ifndef BITS_H
84 #  include "bits.h"
85 #endif
86
87 /*----- Type definitions --------------------------------------------------*/
88
89 /* --- Symbol table --- *
90  *
91  * A @sym_table@ contains the information needed to manage a symbol table.
92  * Users shouldn't fiddle with this information directly, but it needs to be
93  * here so that objects of the correct type can be created.
94  */
95
96 typedef struct sym_table {
97   uint32 mask;                          /* Bit mask for hashing purposes */
98   size_t c;                             /* Down counter for growing table */
99   struct sym_base **a;                  /* Array of hash bins */
100 } sym_table;
101
102 /* --- A symbol table entry --- *
103  *
104  * I don't care what actually gets stored in symbol entries because I don't
105  * create them: that's the responsibility of my client.  All I care about
106  * here is that whatever gets passed to me is a structure whose first member
107  * is a @sym_base@.  The ANSI guarantees about structure layout are
108  * sufficient to allow me to manipulate such objects.
109  */
110
111 typedef struct sym_base {
112   struct sym_base *next;                /* Next symbol in hash bin */
113   uint32 hash;                          /* Hash value for symbol's name */
114   char *name;                           /* Name of this symbol */
115   size_t len;                           /* Length of the symbol's name */
116 } sym_base;
117
118 /* --- An iterator block --- */
119
120 typedef struct sym_iter {
121   sym_table *t;                         /* Symbol table being iterated */
122   sym_base *n;                          /* Address of next item to return */
123   size_t i;                             /* Index of next hash bin to use */
124 } sym_iter;
125
126 /*----- External functions ------------------------------------------------*/
127
128 /* --- @sym_create@ --- *
129  *
130  * Arguments:   @sym_table *t@ = symbol table to initialize
131  *
132  * Returns:     ---
133  *
134  * Use:         Initialises the given symbol table.
135  */
136
137 extern void sym_create(sym_table */*t*/);
138
139 /* --- @sym_destroy@ --- *
140  *
141  * Arguments:   @sym_table *t@ = pointer to symbol table in question
142  *
143  * Returns:     ---
144  *
145  * Use:         Destroys a symbol table, freeing all the memory it used to
146  *              occupy.
147  */
148
149 extern void sym_destroy(sym_table */*t*/);
150
151 /* --- @sym_find@ --- *
152  *
153  * Arguments:   @sym_table *t@ = pointer to symbol table in question
154  *              @const char *n@ = pointer to symbol table to look up
155  *              @long l@ = length of the name string or negative to measure
156  *              @size_t sz@ = size of desired symbol object, or zero
157  *              @unsigned *f@ = pointer to a flag, or null.
158  *
159  * Returns:     The address of a @sym_base@ structure, or null if not found
160  *              and @sz@ is zero.
161  *
162  * Use:         Looks up a symbol in a given symbol table.  The name is
163  *              passed by the address of its first character.  The length
164  *              may be given, in which case the name may contain arbitrary
165  *              binary data, or it may be given as a negative number, in
166  *              which case the length of the name is calculated as
167  *              @strlen(n)@.
168  *
169  *              The return value is the address of a pointer to a @sym_base@
170  *              block (which may have other things on the end, as above).  If
171  *              the symbol could be found, the return value points to the
172  *              symbol block.  If the symbol wasn't there, then if @sz@ is
173  *              nonzero, a new symbol is created and its address is returned;
174  *              otherwise a null pointer is returned.
175  *
176  *              The value of @*f@ indicates whether a new symbol entry was
177  *              created: a nonzero value indicates that an old value was
178  *              found.
179  */
180
181 extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
182                       size_t /*sz*/, unsigned */*f*/);
183
184 /* --- @sym_remove@ --- *
185  *
186  * Arguments:   @sym_table *i@ = pointer to a symbol table object
187  *              @void *b@ = pointer to symbol table entry
188  *
189  * Returns:     ---
190  *
191  * Use:         Removes the object from the symbol table.  The space occupied
192  *              by the object and its name is freed; anything else attached
193  *              to the entry should already be gone by this point.
194  */
195
196 extern void sym_remove(sym_table */*t*/, void */*b*/);
197
198 /* --- @sym_mkiter@ --- *
199  *
200  * Arguments:   @sym_iter *i@ = pointer to an iterator object
201  *              @sym_table *t@ = pointer to a symbol table object
202  *
203  * Returns:     ---
204  *
205  * Use:         Creates a new symbol table iterator which may be used to
206  *              iterate through a symbol table.
207  */
208
209 extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
210
211 /* --- @sym_next@ --- *
212  *
213  * Arguments:   @sym_iter *i@ = pointer to iterator object
214  *
215  * Returns:     Pointer to the next symbol found, or null when finished.
216  *
217  * Use:         Returns the next symbol from the table.  Symbols are not
218  *              returned in any particular order.
219  */
220
221 extern void *sym_next(sym_iter */*i*/);
222
223 /*----- That's all, folks -------------------------------------------------*/
224
225 #ifdef __cplusplus
226   }
227 #endif
228
229 #endif