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

Diff of /src/lisp/vars.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by cshapiro, Mon Sep 5 06:09:13 2005 UTC revision 1.6 by rtoy, Thu Sep 15 18:26:53 2005 UTC
# Line 16  static int tempcntr = 1; Line 16  static int tempcntr = 1;
16    
17  struct var {  struct var {
18      lispobj obj;      lispobj obj;
19      lispobj (*update_fn)(struct var *var);        lispobj(*update_fn) (struct var * var);
20      char *name;      char *name;
21      long clock;      long clock;
22      boolean map_back, permanent;      boolean map_back, permanent;
23    
24      struct var *nnext; /* Next in name list */      struct var *nnext;          /* Next in name list */
25      struct var *onext; /* Next in object list */      struct var *onext;          /* Next in object list */
26  };  };
27    
28  static int hash_name(char *name)  static int
29    hash_name(char *name)
30  {  {
31      unsigned long value = 0;      unsigned long value = 0;
32    
33      while (*name != '\0') {      while (*name != '\0') {
34          value = (value << 1) ^ *(unsigned char *)(name++);          value = (value << 1) ^ *(unsigned char *) (name++);
35          value = (value & (1-(1<<24))) ^ (value >> 24);          value = (value & (1 - (1 << 24))) ^ (value >> 24);
36      }      }
37    
38      return value % NAME_BUCKETS;      return value % NAME_BUCKETS;
39  }  }
40    
41  static int hash_obj(lispobj obj)  static int
42    hash_obj(lispobj obj)
43  {  {
44      return (unsigned long)obj % OBJ_BUCKETS;      return (unsigned long) obj % OBJ_BUCKETS;
45  }  }
46    
47    
48  void flush_vars()  void
49    flush_vars()
50  {  {
51      int index;      int index;
52      struct var *var, *next, *perm = NULL;      struct var *var, *next, *perm = NULL;
# Line 51  void flush_vars() Line 54  void flush_vars()
54      /* 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. */      /* 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. */
55    
56      for (index = 0; index < NAME_BUCKETS; index++)      for (index = 0; index < NAME_BUCKETS; index++)
57          for (var = NameHash[index]; var != NULL; var = next) {          for (var = NameHash[index]; var != NULL; var = next) {
58              next = var->nnext;              next = var->nnext;
59              if (var->permanent) {              if (var->permanent) {
60                  var->nnext = perm;                  var->nnext = perm;
61                  perm = var;                  perm = var;
62              }              } else {
63              else {                  free(var->name);
64                  free(var->name);                  free(var);
65                  free(var);              }
66              }          }
         }  
67      memset(NameHash, 0, sizeof(NameHash));      memset(NameHash, 0, sizeof(NameHash));
68      memset(ObjHash, 0, sizeof(ObjHash));      memset(ObjHash, 0, sizeof(ObjHash));
69      tempcntr = 1;      tempcntr = 1;
70    
71      for (var = perm; var != NULL; var = next) {      for (var = perm; var != NULL; var = next) {
72          next = var->nnext;          next = var->nnext;
73          index = hash_name(var->name);          index = hash_name(var->name);
74          var->nnext = NameHash[index];          var->nnext = NameHash[index];
75          NameHash[index] = var;          NameHash[index] = var;
76          if (var->map_back) {          if (var->map_back) {
77              index = hash_obj(var->obj);              index = hash_obj(var->obj);
78              var->onext = ObjHash[index];              var->onext = ObjHash[index];
79              ObjHash[index] = var;              ObjHash[index] = var;
80          }          }
81      }      }
82  }  }
83    
84  struct var *lookup_by_name(name)  struct var *
85  char *name;  lookup_by_name(name)
86         char *name;
87  {  {
88      struct var *var;      struct var *var;
89    
90      for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)      for (var = NameHash[hash_name(name)]; var != NULL; var = var->nnext)
91          if (strcmp(var->name, name) == 0)          if (strcmp(var->name, name) == 0)
92              return var;              return var;
93      return NULL;      return NULL;
94  }  }
95    
96  struct var *lookup_by_obj(obj)  struct var *
97  lispobj obj;  lookup_by_obj(obj)
98         lispobj obj;
99  {  {
100      struct var *var;      struct var *var;
101    
102      for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)      for (var = ObjHash[hash_obj(obj)]; var != NULL; var = var->onext)
103          if (var->obj == obj)          if (var->obj == obj)
104              return var;              return var;
105      return NULL;      return NULL;
106  }  }
107    
108  static struct var *make_var(char *name, boolean perm)  static struct var *
109    make_var(char *name, boolean perm)
110  {  {
111      struct var *var;      struct var *var;
112      char buffer[256];      char buffer[256];
113      int index;      int index;
114    
115      var = (struct var *)malloc(sizeof(struct var));      var = (struct var *) malloc(sizeof(struct var));
116    
117      if (var == NULL) {      if (var == NULL) {
118        perror("malloc");          perror("malloc");
119        exit(1);          exit(1);
120      }      }
121    
122      if (name == NULL) {      if (name == NULL) {
123          sprintf(buffer, "%d", tempcntr++);          sprintf(buffer, "%d", tempcntr++);
124          name = buffer;          name = buffer;
125      }      }
126      var->name = (char *)malloc(strlen(name)+1);      var->name = (char *) malloc(strlen(name) + 1);
127      strcpy(var->name, name);      strcpy(var->name, name);
128      var->clock = 0;      var->clock = 0;
129      var->permanent = perm;      var->permanent = perm;
130      var->map_back = FALSE;      var->map_back = FALSE;
131    
132      index = hash_name(name);      index = hash_name(name);
133      var->nnext = NameHash[index];      var->nnext = NameHash[index];
134      NameHash[index] = var;      NameHash[index] = var;
135    
136      return var;      return var;
137  }  }
138    
139  struct var *define_var(char *name, lispobj obj, boolean perm)  struct var *
140    define_var(char *name, lispobj obj, boolean perm)
141  {  {
142      struct var *var = make_var(name, perm);      struct var *var = make_var(name, perm);
143      int index;      int index;
# Line 139  struct var *define_var(char *name, lispo Line 146  struct var *define_var(char *name, lispo
146      var->update_fn = NULL;      var->update_fn = NULL;
147    
148      if (lookup_by_obj(obj) == NULL) {      if (lookup_by_obj(obj) == NULL) {
149          var->map_back = TRUE;          var->map_back = TRUE;
150          index = hash_obj(obj);          index = hash_obj(obj);
151          var->onext = ObjHash[index];          var->onext = ObjHash[index];
152          ObjHash[index] = var;          ObjHash[index] = var;
153      }      }
154    
155      return var;      return var;
156  }  }
157    
158  struct var *define_dynamic_var(char *name, lispobj updatefn(struct var *),  struct var *
159                                 boolean perm)  define_dynamic_var(char *name, lispobj updatefn(struct var *), boolean perm)
160  {  {
161      struct var *var = make_var(name, perm);      struct var *var = make_var(name, perm);
162    
# Line 158  struct var *define_dynamic_var(char *nam Line 165  struct var *define_dynamic_var(char *nam
165      return var;      return var;
166  }  }
167    
168  char *var_name(struct var *var)  char *
169    var_name(struct var *var)
170  {  {
171      return var->name;      return var->name;
172  }  }
173    
174  lispobj var_value(struct var *var)  lispobj
175    var_value(struct var * var)
176  {  {
177      if (var->update_fn != NULL)      if (var->update_fn != NULL)
178          var->obj = (*var->update_fn)(var);          var->obj = (*var->update_fn) (var);
179      return var->obj;      return var->obj;
180  }  }
181    
182  long var_clock(struct var *var)  long
183    var_clock(struct var *var)
184  {  {
185      return var->clock;      return var->clock;
186  }  }
187    
188  void var_setclock(struct var *var, long val)  void
189    var_setclock(struct var *var, long val)
190  {  {
191      var->clock = val;      var->clock = val;
192  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

  ViewVC Help
Powered by ViewVC 1.1.5