1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
* Copyright 1989 Massachusetts Institute of Technology
*/
/*********************\
* *
* WOOL_OBJECT Atom *
* BODY *
* *
\*********************/
#include "EXTERN.h"
#include <stdio.h>
#include "wool.h"
#include "wl_coll.h"
#include "wl_number.h"
#include "wl_list.h"
#include "wl_string.h"
#include "wl_pointer.h"
#include "wl_active.h"
#include "wl_name.h"
#include "INTERN.h"
#include "wl_atom.h"
/*
* Constructor:
* Constructed via the hash table management routines.
*/
/*
* Warning: You should NEVER call WLAtom_make, call wool_atom instead,
* or your atom won't be in the hash table!
*/
WOOL_Atom
WLAtom_make(p_name, c_val) /* makes an atom */
char *p_name; /* is COPIED to atom's p_name */
WOOL_OBJECT c_val; /* is just pointed to */
{
WOOL_Quark object = (WOOL_Quark)
Malloc(sizeof(struct _WOOL_Quark) + strlen(p_name));
object -> type = WLAtom;
zrt_put(&(object -> type));
strcpy(object -> p_name, p_name);
object -> c_val = c_val;
if (c_val)
increase_reference(c_val);
return (WOOL_Atom) &(object -> type);
}
WOOL_OBJECT
WLAtom_equal(a1, a2)
WOOL_Atom a1, a2;
{
if (a2 != a1)
return NIL;
else if (a1 == (WOOL_Atom) NIL)
return TRU;
else
return (WOOL_OBJECT) a1;
}
/************************\
* *
* hash table routines *
* *
\************************/
/*
* Hash function definition:
* HASH_FUNCTION: hash function, hash = hashcode, hp = pointer on char,
* hash2 = temporary for hashcode.
* INITIAL_TABLE_SIZE in slots
* HASH_TABLE_GROWS how hash table grows.
*/
/* Mock lisp function */
/*
#define HASH_FUNCTION hash = (hash << 5) - hash + *hp++;
#define INITIAL_HASH_SIZE 2017
#define HASH_TABLE_GROWS HashTableSize = HashTableSize * 2;
*/
/* aho-sethi-ullman's HPJ (sizes should be primes)*/
#define HASH_FUNCTION hash <<= 4; hash += *hp++; \
if(hash2 = hash & 0xf0000000) hash ^= (hash2 >> 24) ^ hash2;
#define INITIAL_HASH_SIZE 4095 /* should be 2^n - 1 */
#define HASH_TABLE_GROWS HashTableSize = HashTableSize << 1 + 1;
/* GNU emacs function */
/*
#define HASH_FUNCTION hash = (hash << 3) + (hash >> 28) + *hp++;
#define INITIAL_HASH_SIZE 2017
#define HASH_TABLE_GROWS HashTableSize = HashTableSize * 2;
*/
/* end of hash functions */
/*
* The hash table is used to store atoms via their P_NAME:
*
* P_NAME --hash--> ATOM |--p_name--> "foo"
* |--c_val--> value of the atom (result of eval)
*
* if c_val is UNDEFINED, symbol was undefined. If c_val is NULL,
* symbol value is NIL. Parsing replaces p_names with ATOMS.
*/
int HashTableSize;
static HashTableLimit;
static HashTableUsed;
static WOOL_Atom *HashTable; /* table of WLAtom objects */
/*
* HashSlot gives the slot (pointer to WOOL_Atom) of a name
* (slot points to NULL if it is not defined)
*/
WOOL_Atom *
HashSlot(s)
char *s;
{
unsigned int hash, hash2;
WOOL_Atom *p;
char *hp = s;
char *ns;
hash = 0;
while (*hp) { /* computes hash function */
HASH_FUNCTION
}
p = HashTable + hash % HashTableSize;
while (*p) {
ns = (*p) -> p_name;
if (ns[0] == s[0] && strcmp(ns, s) == 0)
break;
p--;
if (p < HashTable)
p = HashTable + HashTableSize - 1;
}
return p;
}
HashTableGrows()
{
WOOL_Atom *t, *p;
int i;
int OldHashTableSize = HashTableSize;
t = HashTable;
HASH_TABLE_GROWS
HashTableLimit = HashTableSize / 3;
HashTable = (WOOL_Atom *) Malloc(HashTableSize * sizeof(*HashTable));
for (p = HashTable + HashTableSize; p > HashTable;)
*--p = NULL;
for (i = 0; i < OldHashTableSize; i++)
if (t[i]) {
WOOL_Atom *ps = HashSlot(t[i] -> p_name);
*ps = t[i];
}
Free(t);
}
/*
* wool_atom(name)
* return an WOOL_Atom, which is the one at the slot, if present,
* or is created if name didn't exist, with c_val UNDEFINED. (NULL)
* This function is called by the parser for each NAME encountered.
* so that the parsed expression points directly to atoms.
* The reference count of the atom is set to 1.
*/
WOOL_Atom
wool_atom(tag)
char *tag;
{
WOOL_Atom *slot;
if (HashTableUsed >= HashTableLimit)
HashTableGrows();
if (!*(slot = HashSlot(tag))) { /* undefined, make a new one */
HashTableUsed++;
increase_reference((*slot = WLAtom_make(tag, UNDEFINED)));
}
return *slot;
}
/* WLAtom_unbind
* Removes an atom from the hash table
*/
WOOL_OBJECT
WLAtom_unbind(obj)
WOOL_Atom obj;
{
decrease_reference(obj -> c_val);
obj -> c_val = UNDEFINED;
if (obj -> reference_count == 1)
decrease_reference(obj);
return NIL;
}
/* must be called before allocating any atom
*/
HashTable_init()
{
WOOL_Atom *p;
HashTableSize = INITIAL_HASH_SIZE;
HashTableLimit = HashTableSize / 3;
HashTable = (WOOL_Atom *) Malloc(HashTableSize * sizeof(*HashTable));
for (p = HashTable + HashTableSize; p > HashTable;)
*--p = NULL;
}
#ifdef STATS
/*
* hashstats:
* statistics about the hash table
*/
WOOL_OBJECT
hashstats()
{
int out_of_place;
wool_puts("Statistics about hash table:\n");
wool_printf(" %d slots used ", HashTableUsed);
wool_printf("out of %d allocated\n", HashTableSize);
out_of_place = outplacedslots();
wool_printf(" and %d slots out of place ", out_of_place);
wool_printf("(%d %%)\n", (out_of_place * 100) / HashTableUsed);
return NIL;
}
int
outplacedslots()
{
WOOL_Atom *slot;
int n = 0;
for (slot = HashTable; slot < HashTable + HashTableSize; slot++) {
if (*slot) {
unsigned int hash, hash2;
char *hp = (*slot) -> p_name;
char *ns;
hash = 0;
while (*hp) { /* computes hash function */
HASH_FUNCTION
}
ns = (*(HashTable + hash % HashTableSize)) -> p_name;
if (!(ns[0] == (*slot) -> p_name[0] &&
strcmp(ns, (*slot) -> p_name) == 0)) {
n++;
}
}
}
return n;
}
/*
* prints the whole hash table
*/
WOOL_OBJECT
oblist()
{
WOOL_Atom *slot;
int num = 0;
for (slot = HashTable; slot < HashTable + HashTableSize; slot++) {
if (*slot) {
wool_printf("%s ", (*slot) -> p_name);
if ((*slot) -> type == WLAtom) {
if (((*slot) -> p_name[0] > ' ') && ((*slot) -> c_val)) {
wool_printf("(%s): ", (((*slot) -> c_val) -> type)[0]);
wool_print((*slot) -> c_val);
}
} else {
wool_print(*slot);
}
num++;
wool_newline();
}
}
return (WOOL_OBJECT) WLNumber_make(num);
}
#endif /* STATS */
/*
* prints the names of the atoms pointing to this object (or nothing)
*/
print_atom_pointing_to(object)
WOOL_OBJECT object;
{
WOOL_Atom *slot;
for (slot = HashTable; slot < HashTable + HashTableSize; slot++)
if (*slot)
if ((*slot) -> type == WLAtom)
if ((*slot) -> c_val)
if ((*slot) -> c_val == object)
wool_printf("%s ", (*slot) -> p_name);
}
#ifdef MLEAK
/*
* gives the atoms with prefix prefix successivly (or NULL on end);
* re-initialise with a '\0' prefix
*/
WOOL_Atom
find_next_prefixed_atom(prefix)
char prefix;
{
WOOL_Atom *slot;
static WOOL_Atom *slot0;
if (prefix == '\0') {
slot0 = HashTable;
} else {
for (slot = slot0; slot < HashTable + HashTableSize; slot++)
if ((*slot)
&& ((*slot) -> p_name[0] == prefix)
&& ((*slot) -> c_val)) {
slot0 = slot + 1;
return (*slot);
}
}
return NULL;
}
#endif /* MLEAK */
/*
* XLAtom_eval:
* evaluating an atom is giving a pointer to its c_val field.
* an atom returns its value, or calls wool_error if undefined
* (increase ref. of value)
*/
WOOL_OBJECT
WLAtom_eval(obj)
WOOL_Atom obj;
{
if (obj -> c_val != UNDEFINED) {
return obj -> c_val;
} else
return wool_error(UNDEFINED_VARIABLE, obj -> p_name);
}
WOOL_OBJECT
WLAtom_silent_eval(obj)
WOOL_Atom obj;
{
return obj -> c_val;
}
/*
* WLAtom_print:
* printing an atom is printing the string in the p_name field.
*/
WOOL_OBJECT
WLAtom_print(obj)
WOOL_Atom obj;
{
wool_puts(obj -> p_name); /* perhaps () for NIL? */
return (WOOL_OBJECT) obj;
}
/*
* WLAtom_free;
* Frees the Quark of this Atom
*/
WOOL_OBJECT
WLAtom_free(obj)
WOOL_Atom obj;
{
WOOL_Atom *slot = HashSlot(obj -> p_name);
WOOL_Atom *next_slot = slot - 1;
WOOL_Atom atom;
*slot = NULL;
while (atom = *(next_slot = (next_slot < HashTable ?
HashTable + HashTableSize - 1 : next_slot))) {
*next_slot = NULL;
*(HashSlot(atom -> p_name)) = atom;
next_slot--;
}
Free((((char *) obj)
- (sizeof(struct _WOOL_Quark) - sizeof(struct _WOOL_Atom))));
return NULL;
}
/*
* WLAtom_execute:
* executes the object in the C_val
*/
WOOL_OBJECT
WLAtom_execute(obj, list)
WOOL_Atom obj;
WOOL_List list;
{
if (obj -> c_val && (obj -> c_val -> type != WLAtom)) {
return WOOL_send(WOOL_execute, obj -> c_val, (obj -> c_val, list));
} else if (obj -> c_val && (obj -> c_val == NIL)) {
return NIL;
} else {
return (wool_error(UNDEFINED_FUNCTION, obj));
}
}
#ifdef USER_DEBUG
wool_put_spaces(n)
int n;
{
int i;
wool_printf("%d ", n);
for (i = 0; i < n; i++)
wool_puts(" ");
}
#endif /* USER_DEBUG */
/*
* WLAtom_set
* the normal setq routine
*/
WOOL_OBJECT
WLAtom_set(atom, value)
WOOL_Atom atom;
WOOL_OBJECT value;
{
WOOL_OBJECT new = WOOL_send(WOOL_eval, value, (value));
decrease_reference(atom -> c_val);
increase_reference(atom -> c_val = new);
return new;
}
WOOL_OBJECT
WLAtom_setq(atom, value)
WOOL_Atom atom;
WOOL_OBJECT value;
{
decrease_reference(atom -> c_val);
increase_reference(atom -> c_val = value);
return value;
}
/*
* C_value of an atom:
* NIL => 0
* t => 1
* oth => adress of atom itself
*/
int
WLAtom_get_C_value(obj)
WOOL_Atom obj;
{
if (obj == (WOOL_Atom) NIL)
return 0;
else if (obj == (WOOL_Atom) TRU)
return 1;
else
return (int) obj;
}
void
must_be_atom(atom, n)
WOOL_Atom atom;
int n;
{
if ((atom -> type != WLAtom) /* verify type of arg1 */
&&(atom -> type != WLPointer)
&& (atom -> type != WLActive)
&& (atom -> type != WLName))
bad_argument(atom, n, "symbol");
}
int
is_an_atom(atom)
WOOL_Atom atom;
{
if ((atom -> type != WLAtom) /* verify type of arg1 */
&&(atom -> type != WLPointer)
&& (atom -> type != WLActive)
&& (atom -> type != WLName))
return 0;
else
return 1;
}
|