c1c43500 |
1 | /* -*-c-*- |
2 | * |
0ae5e7b3 |
3 | * $Id: atom.c,v 1.2 2001/01/21 19:04:51 mdw Exp $ |
c1c43500 |
4 | * |
5 | * Atom management |
6 | * |
7 | * (c) 2000 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: atom.c,v $ |
0ae5e7b3 |
33 | * Revision 1.2 2001/01/21 19:04:51 mdw |
34 | * Include `crc32.h' for @CRC32@ macro. |
35 | * |
c1c43500 |
36 | * Revision 1.1 2001/01/20 11:50:16 mdw |
37 | * Implementation of atom tables (for example, as found in X11). |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <stdio.h> |
44 | #include <string.h> |
45 | |
46 | #include "alloc.h" |
47 | #include "atom.h" |
0ae5e7b3 |
48 | #include "crc32.h" |
c1c43500 |
49 | #include "hash.h" |
50 | #include "sym.h" |
51 | |
52 | /*----- Static variables --------------------------------------------------*/ |
53 | |
54 | static atom_table atoms; |
55 | |
56 | /*----- Handy macros ------------------------------------------------------*/ |
57 | |
58 | #define ATOM_RESOLVE(t) do { \ |
59 | if (t == ATOM_GLOBAL) \ |
60 | t = &atoms; \ |
61 | if (!t->t.t.v) \ |
62 | atom_createtable(t); \ |
63 | } while (0) |
64 | |
65 | /*----- Main code ---------------------------------------------------------*/ |
66 | |
67 | /* --- @atom_createtable@ --- * |
68 | * |
69 | * Arguments: @atom_table *t@ = pointer to an atom table |
70 | * |
71 | * Returns: --- |
72 | * |
73 | * Use: Initializes an atom table. |
74 | */ |
75 | |
76 | void atom_createtable(atom_table *t) |
77 | { |
78 | sym_create(&t->t); |
79 | t->g = 0; |
80 | t->gseq = 0; |
81 | } |
82 | |
83 | /* --- @atom_destroytable@ --- * |
84 | * |
85 | * Arguments: @atom_table *t@ = pointer to an atom table |
86 | * |
87 | * Returns: --- |
88 | * |
89 | * Use: Destroys all of the atoms in an atom table. All of the atoms |
90 | * (including uninterned atoms) are freed. Any references to |
91 | * atoms from the table become invalid, and any association |
92 | * tables dependent on the atom table are unusable, except that |
93 | * they may be destroyed safely. |
94 | */ |
95 | |
96 | void atom_destroytable(atom_table *t) |
97 | { |
98 | atom *a, *aa; |
99 | |
100 | ATOM_RESOLVE(t); |
101 | for (a = (atom *)t->g; a; a = aa) { |
102 | aa = (atom *)a->b.b.next; |
103 | x_free(t->t.t.a, a); |
104 | } |
105 | sym_destroy(&t->t); |
106 | } |
107 | |
108 | /* --- @atom_intern@ --- * |
109 | * |
110 | * Arguments: @atom_table *t@ = pointer to an atom table |
111 | * @const char *p@ = pointer to the string to intern |
112 | * |
113 | * Returns: A pointer to the atom block for the given symbol string. |
114 | * |
115 | * Use: Interns an atom, returning the atom block. The string can be |
116 | * extracted from the atom by means of the @ATOM_NAME@ macro. |
117 | */ |
118 | |
119 | atom *atom_intern(atom_table *t, const char *p) |
120 | { |
121 | atom *a; |
122 | unsigned f; |
123 | |
124 | ATOM_RESOLVE(t); |
125 | a = sym_find(&t->t, p, -1, sizeof(atom), &f); |
126 | if (!f) |
127 | a->f = 0; |
128 | return (a); |
129 | } |
130 | |
131 | /* --- @atom_gensym@ --- * |
132 | * |
133 | * Arguments: @atom_table *t@ = pointer to a symbol table |
134 | * |
135 | * Returns: A pointer to a new atom block, not previously interned. |
136 | * |
137 | * Use: Creates a new, uninterned atom. This atom will never be |
138 | * returned by either @atom_intern@ or any other call to |
139 | * @atom_gensym@, while the symbol table exists. |
140 | */ |
141 | |
142 | atom *atom_gensym(atom_table *t) |
143 | { |
144 | atom *a; |
145 | char buf[64]; |
146 | size_t sz; |
147 | |
148 | ATOM_RESOLVE(t); |
149 | sprintf(buf, "*gen-%lu*", t->gseq++); |
150 | sz = strlen(buf) + 1; |
151 | a = x_alloc(t->t.t.a, sizeof(atom) + sz); |
152 | a->b.name = (char *)(a + 1); |
153 | memcpy(a->b.name, buf, sz); |
154 | a->b.len = sz; |
155 | CRC32(a->b.b.hash, 0, buf, sz); |
156 | a->f = ATOMF_GENSYM; |
157 | a->b.b.next = t->g; |
158 | t->g = &a->b.b; |
159 | return (a); |
160 | } |
161 | |
162 | /* --- @atom_name@ --- * |
163 | * |
164 | * Arguments: @atom *a@ = pointer to an atom |
165 | * |
166 | * Returns: The atom's textual name. |
167 | * |
168 | * Use: Given an atom, returns the name with which it was interned |
169 | * (or a made-up name if it was created using @gensym@. |
170 | */ |
171 | |
172 | const char *atom_name(const atom *a) { return ATOM_NAME(a); } |
173 | |
174 | /* --- @atom_len@ --- * |
175 | * |
176 | * Arguments: @atom *a@ = pointer to an atom |
177 | * |
178 | * Returns: The atom string's length. |
179 | * |
180 | * Use: Given an atom, return the length of its textual |
181 | * representation. |
182 | */ |
183 | |
184 | size_t atom_len(const atom *a) { return ATOM_LEN(a); } |
185 | |
186 | /* --- @atom_hash@ --- * |
187 | * |
188 | * Arguments: @atom *a@ = pointer to an atom |
189 | * |
190 | * Returns: The atom's hash. |
191 | * |
192 | * Use: Given an atom, returns its hash. |
193 | */ |
194 | |
195 | uint32 atom_hash(const atom *a) { return ATOM_HASH(a); } |
196 | |
197 | /* --- @atom_mkiter@ , @atom_next@ --- * |
198 | * |
199 | * Arguments: @atom_table *t@ = pointer to an atom table |
200 | * @atom_iter *i@ = pointer to an iterator structure |
201 | * |
202 | * Returns: Next atom, for @atom_next@; nothing for @atom_mkiter@. |
203 | * |
204 | * Use: Iterates over atoms (both interned and uninterned). |
205 | */ |
206 | |
207 | void atom_mkiter(atom_iter *i, atom_table *t) |
208 | { |
209 | ATOM_RESOLVE(t); |
210 | i->i.i.t = &t->t.t; |
211 | i->i.i.p = t->g; |
212 | i->i.i.i = 0; |
213 | } |
214 | |
215 | atom *atom_next(atom_iter *i) |
216 | { |
217 | atom *a; |
218 | SYM_NEXT(&i->i, a); |
219 | return (a); |
220 | } |
221 | |
222 | /*----- That's all, folks -------------------------------------------------*/ |