2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
14 #include <sys/types.h>
21 #define NAME_BUCKETS 31
22 #define OBJ_BUCKETS 31
24 static struct var *NameHash[NAME_BUCKETS], *ObjHash[OBJ_BUCKETS];
25 static int tempcntr = 1;
29 lispobj (*update_fn)(struct var *var);
32 boolean map_back, permanent;
34 struct var *nnext; /* Next in name list */
35 struct var *onext; /* Next in object list */
38 static int hash_name(char *name)
40 unsigned long value = 0;
42 while (*name != '\0') {
43 value = (value << 1) ^ *(unsigned char *)(name++);
44 value = (value & (1-(1<<24))) ^ (value >> 24);
47 return value % NAME_BUCKETS;
50 static int hash_obj(lispobj obj)
52 return (unsigned long)obj % OBJ_BUCKETS;
58 struct var *var, *next, *perm = NULL;
60 /* Note: all vars in the object hash table also appear in the name hash
61 * table, so if we free everything in the name hash table, we free
62 * everything in the object hash table. */
64 for (index = 0; index < NAME_BUCKETS; index++)
65 for (var = NameHash[index]; var != NULL; var = next) {
76 bzero(NameHash, sizeof(NameHash));
77 bzero(ObjHash, sizeof(ObjHash));
80 for (var = perm; var != NULL; var = next) {
82 index = hash_name(var->name);
83 var->nnext = NameHash[index];
84 NameHash[index] = var;
86 index = hash_obj(var->obj);
87 var->onext = ObjHash[index];
93 struct var *lookup_by_name(name)
98 for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)
99 if (strcmp(var->name, name) == 0)
104 struct var *lookup_by_obj(obj)
109 for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)
115 static struct var *make_var(char *name, boolean perm)
117 struct var *var = (struct var *)malloc(sizeof(struct var));
122 sprintf(buffer, "%d", tempcntr++);
125 var->name = (char *)malloc(strlen(name)+1);
126 strcpy(var->name, name);
128 var->permanent = perm;
131 index = hash_name(name);
132 var->nnext = NameHash[index];
133 NameHash[index] = var;
138 struct var *define_var(char *name, lispobj obj, boolean perm)
140 struct var *var = make_var(name, perm);
144 var->update_fn = NULL;
146 if (lookup_by_obj(obj) == NULL) {
148 index = hash_obj(obj);
149 var->onext = ObjHash[index];
150 ObjHash[index] = var;
156 struct var *define_dynamic_var(char *name, lispobj updatefn(struct var *),
159 struct var *var = make_var(name, perm);
161 var->update_fn = updatefn;
166 char *var_name(struct var *var)
171 lispobj var_value(struct var *var)
173 if (var->update_fn != NULL)
174 var->obj = (*var->update_fn)(var);
178 long var_clock(struct var *var)
183 void var_setclock(struct var *var, long val)