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.
17 #include <sys/types.h>
24 #define NAME_BUCKETS 31
25 #define OBJ_BUCKETS 31
27 static struct var *NameHash[NAME_BUCKETS], *ObjHash[OBJ_BUCKETS];
28 static int tempcntr = 1;
32 lispobj (*update_fn)(struct var *var);
35 boolean map_back, permanent;
37 struct var *nnext; /* Next in name list */
38 struct var *onext; /* Next in object list */
41 static int hash_name(char *name)
43 unsigned long value = 0;
45 while (*name != '\0') {
46 value = (value << 1) ^ *(unsigned char *)(name++);
47 value = (value & (1-(1<<24))) ^ (value >> 24);
50 return value % NAME_BUCKETS;
53 static int hash_obj(lispobj obj)
55 return (unsigned long)obj % OBJ_BUCKETS;
61 struct var *var, *next, *perm = NULL;
63 /* Note: all vars in the object hash table also appear in the name hash
64 * table, so if we free everything in the name hash table, we free
65 * everything in the object hash table. */
67 for (index = 0; index < NAME_BUCKETS; index++)
68 for (var = NameHash[index]; var != NULL; var = next) {
79 bzero(NameHash, sizeof(NameHash));
80 bzero(ObjHash, sizeof(ObjHash));
83 for (var = perm; var != NULL; var = next) {
85 index = hash_name(var->name);
86 var->nnext = NameHash[index];
87 NameHash[index] = var;
89 index = hash_obj(var->obj);
90 var->onext = ObjHash[index];
96 struct var *lookup_by_name(name)
101 for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)
102 if (strcmp(var->name, name) == 0)
107 struct var *lookup_by_obj(obj)
112 for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)
118 static struct var *make_var(char *name, boolean perm)
120 struct var *var = (struct var *)malloc(sizeof(struct var));
125 sprintf(buffer, "%d", tempcntr++);
128 var->name = (char *)malloc(strlen(name)+1);
129 strcpy(var->name, name);
131 var->permanent = perm;
134 index = hash_name(name);
135 var->nnext = NameHash[index];
136 NameHash[index] = var;
141 struct var *define_var(char *name, lispobj obj, boolean perm)
143 struct var *var = make_var(name, perm);
147 var->update_fn = NULL;
149 if (lookup_by_obj(obj) == NULL) {
151 index = hash_obj(obj);
152 var->onext = ObjHash[index];
153 ObjHash[index] = var;
159 struct var *define_dynamic_var(char *name, lispobj updatefn(struct var *),
162 struct var *var = make_var(name, perm);
164 var->update_fn = updatefn;
169 char *var_name(struct var *var)
174 lispobj var_value(struct var *var)
176 if (var->update_fn != NULL)
177 var->obj = (*var->update_fn)(var);
181 long var_clock(struct var *var)
186 void var_setclock(struct var *var, long val)