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.
13 #include <sys/types.h>
20 #define NAME_BUCKETS 31
21 #define OBJ_BUCKETS 31
23 static struct var *NameHash[NAME_BUCKETS], *ObjHash[OBJ_BUCKETS];
24 static int tempcntr = 1;
28 lispobj (*update_fn)(struct var *var);
31 boolean map_back, permanent;
33 struct var *nnext; /* Next in name list */
34 struct var *onext; /* Next in object list */
37 static int hash_name(char *name)
39 unsigned long value = 0;
41 while (*name != '\0') {
42 value = (value << 1) ^ *(unsigned char *)(name++);
43 value = (value & (1-(1<<24))) ^ (value >> 24);
46 return value % NAME_BUCKETS;
49 static int hash_obj(lispobj obj)
51 return (unsigned long)obj % OBJ_BUCKETS;
57 struct var *var, *next, *perm = NULL;
59 /* Note: all vars in the object hash table also appear in the name hash
60 * table, so if we free everything in the name hash table, we free
61 * everything in the object hash table. */
63 for (index = 0; index < NAME_BUCKETS; index++)
64 for (var = NameHash[index]; var != NULL; var = next) {
75 bzero(NameHash, sizeof(NameHash));
76 bzero(ObjHash, sizeof(ObjHash));
79 for (var = perm; var != NULL; var = next) {
81 index = hash_name(var->name);
82 var->nnext = NameHash[index];
83 NameHash[index] = var;
85 index = hash_obj(var->obj);
86 var->onext = ObjHash[index];
92 struct var *lookup_by_name(name)
97 for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)
98 if (strcmp(var->name, name) == 0)
103 struct var *lookup_by_obj(obj)
108 for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)
114 static struct var *make_var(char *name, boolean perm)
116 struct var *var = (struct var *)malloc(sizeof(struct var));
121 sprintf(buffer, "%d", tempcntr++);
124 var->name = (char *)malloc(strlen(name)+1);
125 strcpy(var->name, name);
127 var->permanent = perm;
130 index = hash_name(name);
131 var->nnext = NameHash[index];
132 NameHash[index] = var;
137 struct var *define_var(char *name, lispobj obj, boolean perm)
139 struct var *var = make_var(name, perm);
143 var->update_fn = NULL;
145 if (lookup_by_obj(obj) == NULL) {
147 index = hash_obj(obj);
148 var->onext = ObjHash[index];
149 ObjHash[index] = var;
155 struct var *define_dynamic_var(char *name, lispobj updatefn(struct var *),
158 struct var *var = make_var(name, perm);
160 var->update_fn = updatefn;
165 char *var_name(struct var *var)
170 lispobj var_value(struct var *var)
172 if (var->update_fn != NULL)
173 var->obj = (*var->update_fn)(var);
177 long var_clock(struct var *var)
182 void var_setclock(struct var *var, long val)