/[cmucl]/src/lisp/vars.c
ViewVC logotype

Contents of /src/lisp/vars.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue Jul 28 20:15:38 1992 UTC (21 years, 9 months ago) by wlott
Branch: MAIN
Branch point for: solaris_patch
File MIME type: text/plain
Initial revision
1 /* $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/vars.c,v 1.1 1992/07/28 20:15:38 wlott Exp $ */
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <stdlib.h>
5
6 #include "lisp.h"
7 #include "vars.h"
8
9 #define NAME_BUCKETS 31
10 #define OBJ_BUCKETS 31
11
12 static struct var *NameHash[NAME_BUCKETS], *ObjHash[OBJ_BUCKETS];
13 static int tempcntr = 1;
14
15 struct var {
16 lispobj obj;
17 lispobj (*update_fn)(struct var *var);
18 char *name;
19 long clock;
20 boolean map_back, permanent;
21
22 struct var *nnext; /* Next in name list */
23 struct var *onext; /* Next in object list */
24 };
25
26 static int hash_name(char *name)
27 {
28 unsigned long value = 0;
29
30 while (*name != '\0') {
31 value = (value << 1) ^ *(unsigned char *)(name++);
32 value = (value & (1-(1<<24))) ^ (value >> 24);
33 }
34
35 return value % NAME_BUCKETS;
36 }
37
38 static int hash_obj(lispobj obj)
39 {
40 return (unsigned long)obj % OBJ_BUCKETS;
41 }
42
43
44 void flush_vars()
45 {
46 int index;
47 struct var *var, *next, *perm = NULL;
48
49 /* Note: all vars in the object hash table also appear in the name hash table, so if we free everything in the name hash table, we free everything in the object hash table. */
50
51 for (index = 0; index < NAME_BUCKETS; index++)
52 for (var = NameHash[index]; var != NULL; var = next) {
53 next = var->nnext;
54 if (var->permanent) {
55 var->nnext = perm;
56 perm = var;
57 }
58 else {
59 free(var->name);
60 free(var);
61 }
62 }
63 bzero(NameHash, sizeof(NameHash));
64 bzero(ObjHash, sizeof(ObjHash));
65 tempcntr = 1;
66
67 for (var = perm; var != NULL; var = next) {
68 next = var->nnext;
69 index = hash_name(var->name);
70 var->nnext = NameHash[index];
71 NameHash[index] = var;
72 if (var->map_back) {
73 index = hash_obj(var->obj);
74 var->onext = ObjHash[index];
75 ObjHash[index] = var;
76 }
77 }
78 }
79
80 struct var *lookup_by_name(name)
81 char *name;
82 {
83 struct var *var;
84
85 for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)
86 if (strcmp(var->name, name) == 0)
87 return var;
88 return NULL;
89 }
90
91 struct var *lookup_by_obj(obj)
92 lispobj obj;
93 {
94 struct var *var;
95
96 for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)
97 if (var->obj == obj)
98 return var;
99 return NULL;
100 }
101
102 static struct var *make_var(char *name, boolean perm)
103 {
104 struct var *var = (struct var *)malloc(sizeof(struct var));
105 char buffer[256];
106 int index;
107
108 if (name == NULL) {
109 sprintf(buffer, "%d", tempcntr++);
110 name = buffer;
111 }
112 var->name = (char *)malloc(strlen(name)+1);
113 strcpy(var->name, name);
114 var->clock = 0;
115 var->permanent = perm;
116 var->map_back = FALSE;
117
118 index = hash_name(name);
119 var->nnext = NameHash[index];
120 NameHash[index] = var;
121
122 return var;
123 }
124
125 struct var *define_var(char *name, lispobj obj, boolean perm)
126 {
127 struct var *var = make_var(name, perm);
128 int index;
129
130 var->obj = obj;
131 var->update_fn = NULL;
132
133 if (lookup_by_obj(obj) == NULL) {
134 var->map_back = TRUE;
135 index = hash_obj(obj);
136 var->onext = ObjHash[index];
137 ObjHash[index] = var;
138 }
139
140 return var;
141 }
142
143 struct var *define_dynamic_var(char *name, lispobj updatefn(struct var *),
144 boolean perm)
145 {
146 struct var *var = make_var(name, perm);
147
148 var->update_fn = updatefn;
149
150 return var;
151 }
152
153 char *var_name(struct var *var)
154 {
155 return var->name;
156 }
157
158 lispobj var_value(struct var *var)
159 {
160 if (var->update_fn != NULL)
161 var->obj = (*var->update_fn)(var);
162 return var->obj;
163 }
164
165 long var_clock(struct var *var)
166 {
167 return var->clock;
168 }
169
170 void var_setclock(struct var *var, long val)
171 {
172 var->clock = val;
173 }

  ViewVC Help
Powered by ViewVC 1.1.5