5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
39 /*----- Static variables --------------------------------------------------*/
41 static atom_table atoms
;
43 /*----- Handy macros ------------------------------------------------------*/
45 #define ATOM_RESOLVE(t) do { \
46 if (t == ATOM_GLOBAL) \
49 atom_createtable(t); \
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @atom_createtable@ --- *
56 * Arguments: @atom_table *t@ = pointer to an atom table
60 * Use: Initializes an atom table.
63 void atom_createtable(atom_table
*t
)
70 /* --- @atom_destroytable@ --- *
72 * Arguments: @atom_table *t@ = pointer to an atom table
76 * Use: Destroys all of the atoms in an atom table. All of the atoms
77 * (including uninterned atoms) are freed. Any references to
78 * atoms from the table become invalid, and any association
79 * tables dependent on the atom table are unusable, except that
80 * they may be destroyed safely.
83 void atom_destroytable(atom_table
*t
)
88 for (a
= (atom
*)t
->g
; a
; a
= aa
) {
89 aa
= (atom
*)a
->b
.b
.next
;
95 /* --- @atom_intern@, @atom_nintern@ --- *
97 * Arguments: @atom_table *t@ = pointer to an atom table
98 * @const char *p@ = pointer to the string to intern
99 * @size_t n@ = size of the string (for @atom_nintern)
101 * Returns: A pointer to the atom block for the given symbol string.
103 * Use: Interns an atom, returning the atom block. The string can be
104 * extracted from the atom by means of the @ATOM_NAME@ macro.
107 atom
*atom_intern(atom_table
*t
, const char *p
)
113 a
= sym_find(&t
->t
, p
, -1, sizeof(atom
), &f
);
119 atom
*atom_nintern(atom_table
*t
, const char *p
, size_t n
)
125 a
= sym_find(&t
->t
, p
, n
, 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 a
->b
.b
.hash
= UNIHASH(&unihash_global
, 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 -------------------------------------------------*/