3 * $Id: atom.c,v 1.1 2001/01/20 11:50:16 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.1 2001/01/20 11:50:16 mdw
34 * Implementation of atom tables (for example, as found in X11).
38 /*----- Header files ------------------------------------------------------*/
48 /*----- Static variables --------------------------------------------------*/
50 static atom_table atoms
;
52 /*----- Handy macros ------------------------------------------------------*/
54 #define ATOM_RESOLVE(t) do { \
55 if (t == ATOM_GLOBAL) \
58 atom_createtable(t); \
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @atom_createtable@ --- *
65 * Arguments: @atom_table *t@ = pointer to an atom table
69 * Use: Initializes an atom table.
72 void atom_createtable(atom_table
*t
)
79 /* --- @atom_destroytable@ --- *
81 * Arguments: @atom_table *t@ = pointer to an atom table
85 * Use: Destroys all of the atoms in an atom table. All of the atoms
86 * (including uninterned atoms) are freed. Any references to
87 * atoms from the table become invalid, and any association
88 * tables dependent on the atom table are unusable, except that
89 * they may be destroyed safely.
92 void atom_destroytable(atom_table
*t
)
97 for (a
= (atom
*)t
->g
; a
; a
= aa
) {
98 aa
= (atom
*)a
->b
.b
.next
;
104 /* --- @atom_intern@ --- *
106 * Arguments: @atom_table *t@ = pointer to an atom table
107 * @const char *p@ = pointer to the string to intern
109 * Returns: A pointer to the atom block for the given symbol string.
111 * Use: Interns an atom, returning the atom block. The string can be
112 * extracted from the atom by means of the @ATOM_NAME@ macro.
115 atom
*atom_intern(atom_table
*t
, const char *p
)
121 a
= sym_find(&t
->t
, p
, -1, sizeof(atom
), &f
);
127 /* --- @atom_gensym@ --- *
129 * Arguments: @atom_table *t@ = pointer to a symbol table
131 * Returns: A pointer to a new atom block, not previously interned.
133 * Use: Creates a new, uninterned atom. This atom will never be
134 * returned by either @atom_intern@ or any other call to
135 * @atom_gensym@, while the symbol table exists.
138 atom
*atom_gensym(atom_table
*t
)
145 sprintf(buf
, "*gen-%lu*", t
->gseq
++);
146 sz
= strlen(buf
) + 1;
147 a
= x_alloc(t
->t
.t
.a
, sizeof(atom
) + sz
);
148 a
->b
.name
= (char *)(a
+ 1);
149 memcpy(a
->b
.name
, buf
, sz
);
151 CRC32(a
->b
.b
.hash
, 0, buf
, sz
);
158 /* --- @atom_name@ --- *
160 * Arguments: @atom *a@ = pointer to an atom
162 * Returns: The atom's textual name.
164 * Use: Given an atom, returns the name with which it was interned
165 * (or a made-up name if it was created using @gensym@.
168 const char *atom_name(const atom
*a
) { return ATOM_NAME(a
); }
170 /* --- @atom_len@ --- *
172 * Arguments: @atom *a@ = pointer to an atom
174 * Returns: The atom string's length.
176 * Use: Given an atom, return the length of its textual
180 size_t atom_len(const atom
*a
) { return ATOM_LEN(a
); }
182 /* --- @atom_hash@ --- *
184 * Arguments: @atom *a@ = pointer to an atom
186 * Returns: The atom's hash.
188 * Use: Given an atom, returns its hash.
191 uint32
atom_hash(const atom
*a
) { return ATOM_HASH(a
); }
193 /* --- @atom_mkiter@ , @atom_next@ --- *
195 * Arguments: @atom_table *t@ = pointer to an atom table
196 * @atom_iter *i@ = pointer to an iterator structure
198 * Returns: Next atom, for @atom_next@; nothing for @atom_mkiter@.
200 * Use: Iterates over atoms (both interned and uninterned).
203 void atom_mkiter(atom_iter
*i
, atom_table
*t
)
211 atom
*atom_next(atom_iter
*i
)
218 /*----- That's all, folks -------------------------------------------------*/