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 /*----- Header files ------------------------------------------------------*/
41 /*----- Static variables --------------------------------------------------*/
43 static atom_table atoms;
45 /*----- Handy macros ------------------------------------------------------*/
47 #define ATOM_RESOLVE(t) do { \
48 if (t == ATOM_GLOBAL) \
51 atom_createtable(t); \
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @atom_createtable@ --- *
58 * Arguments: @atom_table *t@ = pointer to an atom table
62 * Use: Initializes an atom table.
65 void atom_createtable(atom_table *t)
72 /* --- @atom_destroytable@ --- *
74 * Arguments: @atom_table *t@ = pointer to an atom table
78 * Use: Destroys all of the atoms in an atom table. All of the atoms
79 * (including uninterned atoms) are freed. Any references to
80 * atoms from the table become invalid, and any association
81 * tables dependent on the atom table are unusable, except that
82 * they may be destroyed safely.
85 void atom_destroytable(atom_table *t)
90 for (a = (atom *)t->g; a; a = aa) {
91 aa = (atom *)a->b.b.next;
97 /* --- @atom_intern@, @atom_nintern@ --- *
99 * Arguments: @atom_table *t@ = pointer to an atom table
100 * @const char *p@ = pointer to the string to intern
101 * @size_t n@ = size of the string (for @atom_nintern)
103 * Returns: A pointer to the atom block for the given symbol string.
105 * Use: Interns an atom, returning the atom block. The string can be
106 * extracted from the atom by means of the @ATOM_NAME@ macro.
109 atom *atom_intern(atom_table *t, const char *p)
115 a = sym_find(&t->t, p, -1, sizeof(atom), &f);
121 atom *atom_nintern(atom_table *t, const char *p, size_t n)
127 a = sym_find(&t->t, p, n, sizeof(atom), &f);
133 /* --- @atom_gensym@ --- *
135 * Arguments: @atom_table *t@ = pointer to a symbol table
137 * Returns: A pointer to a new atom block, not previously interned.
139 * Use: Creates a new, uninterned atom. This atom will never be
140 * returned by either @atom_intern@ or any other call to
141 * @atom_gensym@, while the symbol table exists.
144 atom *atom_gensym(atom_table *t)
151 sprintf(buf, "*gen-%lu*", t->gseq++);
152 sz = strlen(buf) + 1;
153 a = x_alloc(t->t.t.a, sizeof(atom) + sz);
154 a->b.name = (char *)(a + 1);
155 memcpy(a->b.name, buf, sz);
157 a->b.b.hash = UNIHASH(&unihash_global, buf, sz);
164 /* --- @atom_name@ --- *
166 * Arguments: @atom *a@ = pointer to an atom
168 * Returns: The atom's textual name.
170 * Use: Given an atom, returns the name with which it was interned
171 * (or a made-up name if it was created using @gensym@.
174 const char *atom_name(const atom *a) { return ATOM_NAME(a); }
176 /* --- @atom_len@ --- *
178 * Arguments: @atom *a@ = pointer to an atom
180 * Returns: The atom string's length.
182 * Use: Given an atom, return the length of its textual
186 size_t atom_len(const atom *a) { return ATOM_LEN(a); }
188 /* --- @atom_hash@ --- *
190 * Arguments: @atom *a@ = pointer to an atom
192 * Returns: The atom's hash.
194 * Use: Given an atom, returns its hash.
197 uint32 atom_hash(const atom *a) { return ATOM_HASH(a); }
199 /* --- @atom_mkiter@ , @atom_next@ --- *
201 * Arguments: @atom_table *t@ = pointer to an atom table
202 * @atom_iter *i@ = pointer to an iterator structure
204 * Returns: Next atom, for @atom_next@; nothing for @atom_mkiter@.
206 * Use: Iterates over atoms (both interned and uninterned).
209 void atom_mkiter(atom_iter *i, atom_table *t)
217 atom *atom_next(atom_iter *i)
224 /*----- That's all, folks -------------------------------------------------*/