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

Contents of /src/lisp/vars.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5