3 * $Id: atom.c,v 1.2 2001/01/21 19:04:51 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.2 2001/01/21 19:04:51 mdw
34 * Include `crc32.h' for @CRC32@ macro.
36 * Revision 1.1 2001/01/20 11:50:16 mdw
37 * Implementation of atom tables (for example, as found in X11).
41 /*----- Header files ------------------------------------------------------*/
52 /*----- Static variables --------------------------------------------------*/
54 static atom_table atoms;
56 /*----- Handy macros ------------------------------------------------------*/
58 #define ATOM_RESOLVE(t) do { \
59 if (t == ATOM_GLOBAL) \
62 atom_createtable(t); \
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @atom_createtable@ --- *
69 * Arguments: @atom_table *t@ = pointer to an atom table
73 * Use: Initializes an atom table.
76 void atom_createtable(atom_table *t)
83 /* --- @atom_destroytable@ --- *
85 * Arguments: @atom_table *t@ = pointer to an atom table
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.
96 void atom_destroytable(atom_table *t)
101 for (a = (atom *)t->g; a; a = aa) {
102 aa = (atom *)a->b.b.next;
108 /* --- @atom_intern@ --- *
110 * Arguments: @atom_table *t@ = pointer to an atom table
111 * @const char *p@ = pointer to the string to intern
113 * Returns: A pointer to the atom block for the given symbol string.
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.
119 atom *atom_intern(atom_table *t, const char *p)
125 a = sym_find(&t->t, p, -1, sizeof(atom), &f);
131 /* --- @atom_gensym@ --- *
133 * Arguments: @atom_table *t@ = pointer to a symbol table
135 * Returns: A pointer to a new atom block, not previously interned.
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.
142 atom *atom_gensym(atom_table *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);
155 CRC32(a->b.b.hash, 0, buf, sz);
162 /* --- @atom_name@ --- *
164 * Arguments: @atom *a@ = pointer to an atom
166 * Returns: The atom's textual name.
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@.
172 const char *atom_name(const atom *a) { return ATOM_NAME(a); }
174 /* --- @atom_len@ --- *
176 * Arguments: @atom *a@ = pointer to an atom
178 * Returns: The atom string's length.
180 * Use: Given an atom, return the length of its textual
184 size_t atom_len(const atom *a) { return ATOM_LEN(a); }
186 /* --- @atom_hash@ --- *
188 * Arguments: @atom *a@ = pointer to an atom
190 * Returns: The atom's hash.
192 * Use: Given an atom, returns its hash.
195 uint32 atom_hash(const atom *a) { return ATOM_HASH(a); }
197 /* --- @atom_mkiter@ , @atom_next@ --- *
199 * Arguments: @atom_table *t@ = pointer to an atom table
200 * @atom_iter *i@ = pointer to an iterator structure
202 * Returns: Next atom, for @atom_next@; nothing for @atom_mkiter@.
204 * Use: Iterates over atoms (both interned and uninterned).
207 void atom_mkiter(atom_iter *i, atom_table *t)
215 atom *atom_next(atom_iter *i)
222 /*----- That's all, folks -------------------------------------------------*/