3 * $Id: atom.c,v 1.3 2001/01/25 21:13:15 mdw Exp $
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2001/01/25 21:13:15 mdw
34 * New function allowing an atom's length to be specified at intern time.
36 * Revision 1.2 2001/01/21 19:04:51 mdw
37 * Include `crc32.h' for @CRC32@ macro.
39 * Revision 1.1 2001/01/20 11:50:16 mdw
40 * Implementation of atom tables (for example, as found in X11).
44 /*----- Header files ------------------------------------------------------*/
55 /*----- Static variables --------------------------------------------------*/
57 static atom_table atoms;
59 /*----- Handy macros ------------------------------------------------------*/
61 #define ATOM_RESOLVE(t) do { \
62 if (t == ATOM_GLOBAL) \
65 atom_createtable(t); \
68 /*----- Main code ---------------------------------------------------------*/
70 /* --- @atom_createtable@ --- *
72 * Arguments: @atom_table *t@ = pointer to an atom table
76 * Use: Initializes an atom table.
79 void atom_createtable(atom_table *t)
86 /* --- @atom_destroytable@ --- *
88 * Arguments: @atom_table *t@ = pointer to an atom table
92 * Use: Destroys all of the atoms in an atom table. All of the atoms
93 * (including uninterned atoms) are freed. Any references to
94 * atoms from the table become invalid, and any association
95 * tables dependent on the atom table are unusable, except that
96 * they may be destroyed safely.
99 void atom_destroytable(atom_table *t)
104 for (a = (atom *)t->g; a; a = aa) {
105 aa = (atom *)a->b.b.next;
111 /* --- @atom_intern@, @atom_nintern@ --- *
113 * Arguments: @atom_table *t@ = pointer to an atom table
114 * @const char *p@ = pointer to the string to intern
115 * @size_t n@ = size of the string (for @atom_nintern)
117 * Returns: A pointer to the atom block for the given symbol string.
119 * Use: Interns an atom, returning the atom block. The string can be
120 * extracted from the atom by means of the @ATOM_NAME@ macro.
123 atom *atom_intern(atom_table *t, const char *p)
129 a = sym_find(&t->t, p, -1, sizeof(atom), &f);
135 atom *atom_nintern(atom_table *t, const char *p, size_t n)
141 a = sym_find(&t->t, p, n, sizeof(atom), &f);
147 /* --- @atom_gensym@ --- *
149 * Arguments: @atom_table *t@ = pointer to a symbol table
151 * Returns: A pointer to a new atom block, not previously interned.
153 * Use: Creates a new, uninterned atom. This atom will never be
154 * returned by either @atom_intern@ or any other call to
155 * @atom_gensym@, while the symbol table exists.
158 atom *atom_gensym(atom_table *t)
165 sprintf(buf, "*gen-%lu*", t->gseq++);
166 sz = strlen(buf) + 1;
167 a = x_alloc(t->t.t.a, sizeof(atom) + sz);
168 a->b.name = (char *)(a + 1);
169 memcpy(a->b.name, buf, sz);
171 CRC32(a->b.b.hash, 0, buf, sz);
178 /* --- @atom_name@ --- *
180 * Arguments: @atom *a@ = pointer to an atom
182 * Returns: The atom's textual name.
184 * Use: Given an atom, returns the name with which it was interned
185 * (or a made-up name if it was created using @gensym@.
188 const char *atom_name(const atom *a) { return ATOM_NAME(a); }
190 /* --- @atom_len@ --- *
192 * Arguments: @atom *a@ = pointer to an atom
194 * Returns: The atom string's length.
196 * Use: Given an atom, return the length of its textual
200 size_t atom_len(const atom *a) { return ATOM_LEN(a); }
202 /* --- @atom_hash@ --- *
204 * Arguments: @atom *a@ = pointer to an atom
206 * Returns: The atom's hash.
208 * Use: Given an atom, returns its hash.
211 uint32 atom_hash(const atom *a) { return ATOM_HASH(a); }
213 /* --- @atom_mkiter@ , @atom_next@ --- *
215 * Arguments: @atom_table *t@ = pointer to an atom table
216 * @atom_iter *i@ = pointer to an iterator structure
218 * Returns: Next atom, for @atom_next@; nothing for @atom_mkiter@.
220 * Use: Iterates over atoms (both interned and uninterned).
223 void atom_mkiter(atom_iter *i, atom_table *t)
231 atom *atom_next(atom_iter *i)
238 /*----- That's all, folks -------------------------------------------------*/