0
|
1 /* Lisp interface to hash tables.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 #include <config.h>
|
|
25 #include "lisp.h"
|
|
26 #include "hash.h"
|
|
27 #include "elhash.h"
|
|
28 #include "bytecode.h"
|
|
29
|
|
30 Lisp_Object Qhashtablep;
|
|
31
|
|
32 #define LISP_OBJECTS_PER_HENTRY (sizeof (hentry) / sizeof (Lisp_Object))/* 2 */
|
|
33
|
|
34 struct hashtable_struct
|
|
35 {
|
|
36 struct lcrecord_header header;
|
|
37 unsigned int fullness;
|
|
38 unsigned long (*hash_function) (CONST void *);
|
|
39 int (*test_function) (CONST void *, CONST void *);
|
|
40 Lisp_Object zero_entry;
|
|
41 Lisp_Object harray;
|
|
42 enum hashtable_type type; /* whether and how this hashtable is weak */
|
|
43 Lisp_Object next_weak; /* Used to chain together all of the weak
|
|
44 hashtables. Don't mark through this. */
|
|
45 };
|
|
46
|
|
47 static Lisp_Object Vall_weak_hashtables;
|
|
48
|
|
49 static Lisp_Object mark_hashtable (Lisp_Object, void (*) (Lisp_Object));
|
|
50 static void print_hashtable (Lisp_Object, Lisp_Object, int);
|
|
51 DEFINE_LRECORD_IMPLEMENTATION ("hashtable", hashtable,
|
|
52 mark_hashtable, print_hashtable, 0, 0, 0,
|
|
53 struct hashtable_struct);
|
|
54
|
|
55 static Lisp_Object
|
|
56 mark_hashtable (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
57 {
|
|
58 struct hashtable_struct *table = XHASHTABLE (obj);
|
|
59
|
|
60 if (table->type != HASHTABLE_NONWEAK)
|
|
61 {
|
|
62 /* If the table is weak, we don't want to mark the keys and values
|
|
63 (we scan over them after everything else has been marked,
|
|
64 and mark or remove them as necessary). Note that we will mark
|
|
65 the table->harray itself at the same time; it's hard to mark
|
|
66 that here without also marking its contents. */
|
|
67 return Qnil;
|
|
68 }
|
|
69 ((markobj) (table->zero_entry));
|
|
70 return (table->harray);
|
|
71 }
|
|
72
|
|
73 static void
|
|
74 print_hashtable (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
75 {
|
|
76 struct hashtable_struct *table = XHASHTABLE (obj);
|
|
77 char buf[200];
|
|
78 if (print_readably)
|
|
79 error ("printing unreadable object #<hashtable 0x%x>",
|
|
80 table->header.uid);
|
|
81 sprintf (buf, GETTEXT ("#<%shashtable %d/%ld 0x%x>"),
|
|
82 (table->type == HASHTABLE_WEAK ? "weak " :
|
|
83 table->type == HASHTABLE_KEY_WEAK ? "key-weak " :
|
|
84 table->type == HASHTABLE_VALUE_WEAK ? "value-weak " :
|
|
85 table->type == HASHTABLE_KEY_CAR_WEAK ? "key-car-weak " :
|
|
86 table->type == HASHTABLE_VALUE_CAR_WEAK ? "value-car-weak " :
|
|
87 ""),
|
|
88 table->fullness,
|
|
89 (vector_length (XVECTOR (table->harray)) / LISP_OBJECTS_PER_HENTRY),
|
|
90 table->header.uid);
|
|
91 write_c_string (buf, printcharfun);
|
|
92 }
|
|
93
|
|
94 static void
|
|
95 ht_copy_to_c (struct hashtable_struct *ht,
|
|
96 c_hashtable c_table)
|
|
97 {
|
|
98 int len;
|
|
99
|
|
100 c_table->harray = (void *) vector_data (XVECTOR (ht->harray));
|
|
101 c_table->zero_set = (!GC_UNBOUNDP (ht->zero_entry));
|
|
102 c_table->zero_entry = LISP_TO_VOID (ht->zero_entry);
|
|
103 len = vector_length (XVECTOR (ht->harray));
|
|
104 if (len < 0)
|
|
105 {
|
|
106 /* #### if alloc.c mark_object() changes, this must change too. */
|
|
107 /* barf gag retch. When a vector is marked, its len is
|
|
108 made less than 0. In the prune_weak_hashtables() stage,
|
|
109 we are called on vectors that are like this, and we must
|
|
110 be able to deal. */
|
|
111 assert (gc_in_progress);
|
|
112 len = -1 - len;
|
|
113 }
|
|
114 c_table->size = len/LISP_OBJECTS_PER_HENTRY;
|
|
115 c_table->fullness = ht->fullness;
|
|
116 c_table->hash_function = ht->hash_function;
|
|
117 c_table->test_function = ht->test_function;
|
|
118 XSETHASHTABLE (c_table->elisp_table, ht);
|
|
119 }
|
|
120
|
|
121 static void
|
|
122 ht_copy_from_c (c_hashtable c_table,
|
|
123 struct hashtable_struct *ht)
|
|
124 {
|
|
125 struct Lisp_Vector dummy;
|
|
126 /* C is truly hateful */
|
|
127 void *vec_addr
|
|
128 = ((char *) c_table->harray
|
|
129 - ((char *) &(dummy.contents) - (char *) &dummy));
|
|
130
|
|
131 XSETVECTOR (ht->harray, vec_addr);
|
|
132 if (c_table->zero_set)
|
|
133 VOID_TO_LISP (ht->zero_entry, c_table->zero_entry);
|
|
134 else
|
|
135 ht->zero_entry = Qunbound;
|
|
136 ht->fullness = c_table->fullness;
|
|
137 }
|
|
138
|
|
139
|
|
140 static struct hashtable_struct *
|
|
141 allocate_hashtable (void)
|
|
142 {
|
|
143 struct hashtable_struct *table
|
|
144 = alloc_lcrecord (sizeof (struct hashtable_struct), lrecord_hashtable);
|
|
145 table->harray = Qnil;
|
|
146 table->zero_entry = Qunbound;
|
|
147 table->fullness = 0;
|
|
148 table->hash_function = 0;
|
|
149 table->test_function = 0;
|
|
150 return (table);
|
|
151 }
|
|
152
|
|
153 char *
|
|
154 elisp_hvector_malloc (unsigned int bytes, Lisp_Object table)
|
|
155 {
|
|
156 Lisp_Object new_vector;
|
|
157 struct hashtable_struct *ht;
|
|
158
|
|
159 ht = XHASHTABLE (table);
|
|
160 assert (bytes > vector_length (XVECTOR (ht->harray)) * sizeof (Lisp_Object));
|
|
161 new_vector = make_vector ((bytes / sizeof (Lisp_Object)), Qzero);
|
|
162 return ((char *) (vector_data (XVECTOR (new_vector))));
|
|
163 }
|
|
164
|
|
165 void
|
|
166 elisp_hvector_free (void *ptr, Lisp_Object table)
|
|
167 {
|
|
168 struct hashtable_struct *ht = XHASHTABLE (table);
|
|
169 #if defined (USE_ASSERTIONS) || defined (DEBUG_XEMACS)
|
|
170 Lisp_Object current_vector = ht->harray;
|
|
171 #endif
|
|
172
|
|
173 assert (((void *) vector_data (XVECTOR (current_vector))) == ptr);
|
|
174 ht->harray = Qnil; /* Let GC do its job */
|
|
175 return;
|
|
176 }
|
|
177
|
|
178
|
20
|
179 DEFUN ("hashtablep", Fhashtablep, 1, 1, 0, /*
|
0
|
180 Return t if OBJ is a hashtable, else nil.
|
20
|
181 */
|
|
182 (obj))
|
0
|
183 {
|
|
184 return ((HASHTABLEP (obj)) ? Qt : Qnil);
|
|
185 }
|
|
186
|
|
187
|
|
188
|
|
189
|
|
190 #if 0 /* I don't think these are needed any more.
|
|
191 If using the general lisp_object_equal_*() functions
|
|
192 causes efficiency problems, these can be resurrected. --ben */
|
|
193 /* equality and hash functions for Lisp strings */
|
|
194 int
|
|
195 lisp_string_equal (CONST void *x1, CONST void *x2)
|
|
196 {
|
|
197 Lisp_Object str1, str2;
|
|
198 CVOID_TO_LISP (str1, x1);
|
|
199 CVOID_TO_LISP (str2, x2);
|
14
|
200 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
|
0
|
201 }
|
|
202
|
|
203 unsigned long
|
|
204 lisp_string_hash (CONST void *x)
|
|
205 {
|
|
206 Lisp_Object str;
|
|
207 CVOID_TO_LISP (str, x);
|
14
|
208 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
|
0
|
209 }
|
|
210
|
|
211 #endif /* 0 */
|
|
212
|
|
213 static int
|
|
214 lisp_object_eql_equal (CONST void *x1, CONST void *x2)
|
|
215 {
|
|
216 Lisp_Object obj1, obj2;
|
|
217 CVOID_TO_LISP (obj1, x1);
|
|
218 CVOID_TO_LISP (obj2, x2);
|
|
219 return
|
|
220 (FLOATP (obj1) ? !NILP (Fequal (obj1, obj2)) : EQ (obj1, obj2));
|
|
221 }
|
|
222
|
|
223 static unsigned long
|
|
224 lisp_object_eql_hash (CONST void *x)
|
|
225 {
|
|
226 Lisp_Object obj;
|
|
227 CVOID_TO_LISP (obj, x);
|
|
228 if (FLOATP (obj))
|
|
229 return internal_hash (obj, 0);
|
|
230 else
|
|
231 return LISP_HASH (obj);
|
|
232 }
|
|
233
|
|
234 static int
|
|
235 lisp_object_equal_equal (CONST void *x1, CONST void *x2)
|
|
236 {
|
|
237 Lisp_Object obj1, obj2;
|
|
238 CVOID_TO_LISP (obj1, x1);
|
|
239 CVOID_TO_LISP (obj2, x2);
|
|
240 return !NILP (Fequal (obj1, obj2));
|
|
241 }
|
|
242
|
|
243 static unsigned long
|
|
244 lisp_object_equal_hash (CONST void *x)
|
|
245 {
|
|
246 Lisp_Object obj;
|
|
247 CVOID_TO_LISP (obj, x);
|
|
248 return internal_hash (obj, 0);
|
|
249 }
|
|
250
|
|
251 Lisp_Object
|
|
252 make_lisp_hashtable (int size,
|
|
253 enum hashtable_type type,
|
|
254 enum hashtable_test_fun test)
|
|
255 {
|
|
256 Lisp_Object result;
|
|
257 struct hashtable_struct *table = allocate_hashtable ();
|
|
258
|
|
259 table->harray = make_vector ((compute_harray_size (size)
|
|
260 * LISP_OBJECTS_PER_HENTRY),
|
|
261 Qzero);
|
|
262 switch (test)
|
|
263 {
|
|
264 case HASHTABLE_EQ:
|
|
265 table->test_function = 0;
|
|
266 table->hash_function = 0;
|
|
267 break;
|
|
268
|
|
269 case HASHTABLE_EQL:
|
|
270 table->test_function = lisp_object_eql_equal;
|
|
271 table->hash_function = lisp_object_eql_hash;
|
|
272 break;
|
|
273
|
|
274 case HASHTABLE_EQUAL:
|
|
275 table->test_function = lisp_object_equal_equal;
|
|
276 table->hash_function = lisp_object_equal_hash;
|
|
277 break;
|
|
278
|
|
279 default:
|
|
280 abort ();
|
|
281 }
|
|
282
|
|
283 table->type = type;
|
|
284 XSETHASHTABLE (result, table);
|
|
285
|
|
286 if (table->type != HASHTABLE_NONWEAK)
|
|
287 {
|
|
288 table->next_weak = Vall_weak_hashtables;
|
|
289 Vall_weak_hashtables = result;
|
|
290 }
|
|
291 else
|
|
292 table->next_weak = Qunbound;
|
|
293
|
|
294 return (result);
|
|
295 }
|
|
296
|
|
297 static enum hashtable_test_fun
|
|
298 decode_hashtable_test_fun (Lisp_Object sym)
|
|
299 {
|
|
300 if (NILP (sym))
|
|
301 return HASHTABLE_EQL;
|
|
302
|
|
303 CHECK_SYMBOL (sym);
|
|
304
|
2
|
305 if (EQ (sym, Qeq)) return HASHTABLE_EQ;
|
|
306 if (EQ (sym, Qequal)) return HASHTABLE_EQUAL;
|
|
307 if (EQ (sym, Qeql)) return HASHTABLE_EQL;
|
|
308
|
0
|
309 signal_simple_error ("Invalid hashtable test fun", sym);
|
2
|
310 return HASHTABLE_EQ; /* not reached */
|
0
|
311 }
|
|
312
|
20
|
313 DEFUN ("make-hashtable", Fmake_hashtable, 1, 2, 0, /*
|
0
|
314 Make a hashtable of initial size SIZE.
|
|
315 Comparison between keys is done with TEST-FUN, which must be one of
|
|
316 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must
|
|
317 be the same object (or have the same floating-point value, for floats)
|
|
318 to be considered equivalent.
|
|
319
|
|
320 See also `make-weak-hashtable', `make-key-weak-hashtable', and
|
|
321 `make-value-weak-hashtable'.
|
20
|
322 */
|
|
323 (size, test_fun))
|
0
|
324 {
|
|
325 CHECK_NATNUM (size);
|
|
326 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK,
|
|
327 decode_hashtable_test_fun (test_fun));
|
|
328 }
|
|
329
|
20
|
330 DEFUN ("copy-hashtable", Fcopy_hashtable, 1, 1, 0, /*
|
0
|
331 Make a new hashtable which contains the same keys and values
|
|
332 as the given table. The keys and values will not themselves be copied.
|
20
|
333 */
|
|
334 (old_table))
|
0
|
335 {
|
|
336 struct _C_hashtable old_htbl;
|
|
337 struct _C_hashtable new_htbl;
|
|
338 struct hashtable_struct *old_ht;
|
|
339 struct hashtable_struct *new_ht;
|
|
340 Lisp_Object result;
|
|
341
|
|
342 CHECK_HASHTABLE (old_table);
|
|
343 old_ht = XHASHTABLE (old_table);
|
|
344 ht_copy_to_c (old_ht, &old_htbl);
|
|
345
|
|
346 /* we can't just call Fmake_hashtable() here because that will make a
|
|
347 table that is slightly larger than the one we're trying to copy,
|
|
348 which will make copy_hash() blow up. */
|
|
349 new_ht = allocate_hashtable ();
|
|
350 new_ht->fullness = 0;
|
|
351 new_ht->zero_entry = Qunbound;
|
|
352 new_ht->hash_function = old_ht->hash_function;
|
|
353 new_ht->test_function = old_ht->test_function;
|
|
354 new_ht->harray = Fmake_vector (Flength (old_ht->harray), Qzero);
|
|
355 ht_copy_to_c (new_ht, &new_htbl);
|
|
356 copy_hash (&new_htbl, &old_htbl);
|
|
357 ht_copy_from_c (&new_htbl, new_ht);
|
|
358 new_ht->type = old_ht->type;
|
|
359 XSETHASHTABLE (result, new_ht);
|
|
360
|
|
361 if (UNBOUNDP (old_ht->next_weak))
|
|
362 new_ht->next_weak = Qunbound;
|
|
363 else
|
|
364 {
|
|
365 new_ht->next_weak = Vall_weak_hashtables;
|
|
366 Vall_weak_hashtables = result;
|
|
367 }
|
|
368
|
|
369 return (result);
|
|
370 }
|
|
371
|
|
372
|
20
|
373 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
|
0
|
374 Find hash value for KEY in TABLE.
|
|
375 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
376 */
|
|
377 (key, table, defalt))
|
0
|
378 {
|
|
379 CONST void *vval;
|
|
380 struct _C_hashtable htbl;
|
|
381 if (!gc_in_progress)
|
|
382 CHECK_HASHTABLE (table);
|
|
383 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
384 if (gethash (LISP_TO_VOID (key), &htbl, &vval))
|
|
385 {
|
|
386 Lisp_Object val;
|
|
387 CVOID_TO_LISP (val, vval);
|
|
388 return val;
|
|
389 }
|
|
390 else
|
|
391 return defalt;
|
|
392 }
|
|
393
|
|
394
|
20
|
395 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
|
0
|
396 Remove hash value for KEY in TABLE.
|
20
|
397 */
|
|
398 (key, table))
|
0
|
399 {
|
|
400 struct _C_hashtable htbl;
|
|
401 CHECK_HASHTABLE (table);
|
|
402
|
|
403 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
404 remhash (LISP_TO_VOID (key), &htbl);
|
|
405 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
406 return Qnil;
|
|
407 }
|
|
408
|
|
409
|
20
|
410 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
|
0
|
411 Hash KEY to VAL in TABLE.
|
20
|
412 */
|
|
413 (key, val, table))
|
0
|
414 {
|
|
415 struct hashtable_struct *ht;
|
|
416 void *vkey = LISP_TO_VOID (key);
|
|
417
|
|
418 CHECK_HASHTABLE (table);
|
|
419 ht = XHASHTABLE (table);
|
|
420 if (!vkey)
|
|
421 ht->zero_entry = val;
|
|
422 else
|
|
423 {
|
|
424 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
425 struct _C_hashtable htbl;
|
|
426
|
|
427 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
428 GCPRO3 (key, val, table);
|
|
429 puthash (vkey, LISP_TO_VOID (val), &htbl);
|
|
430 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
431 UNGCPRO;
|
|
432 }
|
|
433 return (val);
|
|
434 }
|
|
435
|
20
|
436 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
|
0
|
437 Flush TABLE.
|
20
|
438 */
|
|
439 (table))
|
0
|
440 {
|
|
441 struct _C_hashtable htbl;
|
|
442 CHECK_HASHTABLE (table);
|
|
443 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
444 clrhash (&htbl);
|
|
445 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
446 return Qnil;
|
|
447 }
|
|
448
|
20
|
449 DEFUN ("hashtable-fullness", Fhashtable_fullness, 1, 1, 0, /*
|
0
|
450 Return number of entries in TABLE.
|
20
|
451 */
|
|
452 (table))
|
0
|
453 {
|
|
454 struct _C_hashtable htbl;
|
|
455 CHECK_HASHTABLE (table);
|
|
456 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
457 return (make_int (htbl.fullness));
|
|
458 }
|
|
459
|
|
460
|
|
461 static void
|
|
462 verify_function (Lisp_Object function, CONST char *description)
|
|
463 {
|
|
464 if (SYMBOLP (function))
|
|
465 {
|
|
466 if (NILP (function))
|
|
467 return;
|
|
468 else
|
|
469 function = indirect_function (function, 1);
|
|
470 }
|
|
471 if (SUBRP (function) || COMPILED_FUNCTIONP (function))
|
|
472 return;
|
|
473 else if (CONSP (function))
|
|
474 {
|
|
475 Lisp_Object funcar = Fcar (function);
|
|
476 if ((SYMBOLP (funcar))
|
|
477 && (EQ (funcar, Qlambda)
|
|
478 || EQ (funcar, Qautoload)))
|
|
479 return;
|
|
480 }
|
|
481 signal_error (Qinvalid_function, list1 (function));
|
|
482 }
|
|
483
|
|
484 static void
|
|
485 lisp_maphash_function (CONST void *void_key,
|
|
486 void *void_val,
|
|
487 void *void_fn)
|
|
488 {
|
|
489 /* This function can GC */
|
|
490 Lisp_Object key, val, fn;
|
|
491 CVOID_TO_LISP (key, void_key);
|
|
492 VOID_TO_LISP (val, void_val);
|
|
493 VOID_TO_LISP (fn, void_fn);
|
|
494 call2 (fn, key, val);
|
|
495 }
|
|
496
|
|
497
|
20
|
498 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
|
0
|
499 Map FUNCTION over entries in TABLE, calling it with two args,
|
|
500 each key and value in the table.
|
20
|
501 */
|
|
502 (function, table))
|
0
|
503 {
|
|
504 struct _C_hashtable htbl;
|
|
505 struct gcpro gcpro1, gcpro2;
|
|
506
|
|
507 verify_function (function, GETTEXT ("hashtable mapping function"));
|
|
508 CHECK_HASHTABLE (table);
|
|
509 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
510 GCPRO2 (table, function);
|
|
511 maphash (lisp_maphash_function, &htbl, LISP_TO_VOID (function));
|
|
512 UNGCPRO;
|
|
513 return Qnil;
|
|
514 }
|
|
515
|
|
516
|
|
517 /* This function is for mapping a *C* function over the elements of a
|
|
518 lisp hashtable.
|
|
519 */
|
|
520 void
|
|
521 elisp_maphash (maphash_function function, Lisp_Object table, void *closure)
|
|
522 {
|
|
523 struct _C_hashtable htbl;
|
|
524
|
|
525 if (!gc_in_progress) CHECK_HASHTABLE (table);
|
|
526 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
527 maphash (function, &htbl, closure);
|
|
528 }
|
|
529
|
|
530 void
|
|
531 elisp_map_remhash (remhash_predicate function,
|
|
532 Lisp_Object table,
|
|
533 void *closure)
|
|
534 {
|
|
535 struct _C_hashtable htbl;
|
|
536
|
|
537 if (!gc_in_progress) CHECK_HASHTABLE (table);
|
|
538 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
539 map_remhash (function, &htbl, closure);
|
|
540 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
541 }
|
|
542
|
|
543 #if 0
|
|
544 void
|
|
545 elisp_table_op (Lisp_Object table, generic_hashtable_op op, void *arg1,
|
|
546 void *arg2, void *arg3)
|
|
547 {
|
|
548 struct _C_hashtable htbl;
|
|
549 CHECK_HASHTABLE (table);
|
|
550 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
551 (*op) (&htbl, arg1, arg2, arg3);
|
|
552 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
553 }
|
|
554 #endif /* 0 */
|
|
555
|
|
556
|
|
557
|
20
|
558 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, 1, 2, 0, /*
|
0
|
559 Make a fully weak hashtable of initial size SIZE.
|
|
560 A weak hashtable is one whose pointers do not count as GC referents:
|
|
561 for any key-value pair in the hashtable, if the only remaining pointer
|
|
562 to either the key or the value is in a weak hash table, then the pair
|
|
563 will be removed from the table, and the key and value collected. A
|
|
564 non-weak hash table (or any other pointer) would prevent the object
|
|
565 from being collected.
|
|
566
|
|
567 You can also create semi-weak hashtables; see `make-key-weak-hashtable'
|
|
568 and `make-value-weak-hashtable'.
|
20
|
569 */
|
|
570 (size, test_fun))
|
0
|
571 {
|
|
572 CHECK_NATNUM (size);
|
|
573 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK,
|
|
574 decode_hashtable_test_fun (test_fun));
|
|
575 }
|
|
576
|
20
|
577 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 1, 2, 0, /*
|
0
|
578 Make a key-weak hashtable of initial size SIZE.
|
|
579 A key-weak hashtable is similar to a fully-weak hashtable (see
|
|
580 `make-weak-hashtable') except that a key-value pair will be removed
|
|
581 only if the key remains unmarked outside of weak hashtables. The pair
|
|
582 will remain in the hashtable if the key is pointed to by something other
|
|
583 than a weak hashtable, even if the value is not.
|
20
|
584 */
|
|
585 (size, test_fun))
|
0
|
586 {
|
|
587 CHECK_NATNUM (size);
|
|
588 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK,
|
|
589 decode_hashtable_test_fun (test_fun));
|
|
590 }
|
|
591
|
20
|
592 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 1, 2, 0, /*
|
0
|
593 Make a value-weak hashtable of initial size SIZE.
|
|
594 A value-weak hashtable is similar to a fully-weak hashtable (see
|
|
595 `make-weak-hashtable') except that a key-value pair will be removed only
|
|
596 if the value remains unmarked outside of weak hashtables. The pair will
|
|
597 remain in the hashtable if the value is pointed to by something other
|
|
598 than a weak hashtable, even if the key is not.
|
20
|
599 */
|
|
600 (size, test_fun))
|
0
|
601 {
|
|
602 CHECK_NATNUM (size);
|
|
603 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK,
|
|
604 decode_hashtable_test_fun (test_fun));
|
|
605 }
|
|
606
|
|
607 struct marking_closure
|
|
608 {
|
|
609 int (*obj_marked_p) (Lisp_Object);
|
|
610 void (*markobj) (Lisp_Object);
|
|
611 enum hashtable_type type;
|
|
612 int did_mark;
|
|
613 };
|
|
614
|
|
615 static void
|
|
616 marking_mapper (CONST void *key, void *contents, void *closure)
|
|
617 {
|
|
618 Lisp_Object keytem, valuetem;
|
|
619 struct marking_closure *fmh =
|
|
620 (struct marking_closure *) closure;
|
|
621
|
|
622 /* This function is called over each pair in the hashtable.
|
|
623 We complete the marking for semi-weak hashtables. */
|
|
624 CVOID_TO_LISP (keytem, key);
|
|
625 CVOID_TO_LISP (valuetem, contents);
|
|
626
|
|
627 switch (fmh->type)
|
|
628 {
|
|
629 case HASHTABLE_KEY_WEAK:
|
|
630 if ((fmh->obj_marked_p) (keytem) &&
|
|
631 !(fmh->obj_marked_p) (valuetem))
|
|
632 {
|
|
633 (fmh->markobj) (valuetem);
|
|
634 fmh->did_mark = 1;
|
|
635 }
|
|
636 break;
|
|
637
|
|
638 case HASHTABLE_VALUE_WEAK:
|
|
639 if ((fmh->obj_marked_p) (valuetem) &&
|
|
640 !(fmh->obj_marked_p) (keytem))
|
|
641 {
|
|
642 (fmh->markobj) (keytem);
|
|
643 fmh->did_mark = 1;
|
|
644 }
|
|
645 break;
|
|
646
|
|
647 case HASHTABLE_KEY_CAR_WEAK:
|
|
648 if (!CONSP (keytem) || (fmh->obj_marked_p) (XCAR (keytem)))
|
|
649 {
|
|
650 if (!(fmh->obj_marked_p) (keytem))
|
|
651 {
|
|
652 (fmh->markobj) (keytem);
|
|
653 fmh->did_mark = 1;
|
|
654 }
|
|
655 if (!(fmh->obj_marked_p) (valuetem))
|
|
656 {
|
|
657 (fmh->markobj) (valuetem);
|
|
658 fmh->did_mark = 1;
|
|
659 }
|
|
660 }
|
|
661 break;
|
|
662
|
|
663 case HASHTABLE_VALUE_CAR_WEAK:
|
|
664 if (!CONSP (valuetem) || (fmh->obj_marked_p) (XCAR (valuetem)))
|
|
665 {
|
|
666 if (!(fmh->obj_marked_p) (keytem))
|
|
667 {
|
|
668 (fmh->markobj) (keytem);
|
|
669 fmh->did_mark = 1;
|
|
670 }
|
|
671 if (!(fmh->obj_marked_p) (valuetem))
|
|
672 {
|
|
673 (fmh->markobj) (valuetem);
|
|
674 fmh->did_mark = 1;
|
|
675 }
|
|
676 }
|
|
677 break;
|
|
678
|
|
679 default:
|
|
680 abort (); /* Huh? */
|
|
681 }
|
|
682
|
|
683 return;
|
|
684 }
|
|
685
|
|
686 int
|
|
687 finish_marking_weak_hashtables (int (*obj_marked_p) (Lisp_Object),
|
|
688 void (*markobj) (Lisp_Object))
|
|
689 {
|
|
690 Lisp_Object rest;
|
|
691 int did_mark = 0;
|
|
692
|
|
693 for (rest = Vall_weak_hashtables;
|
|
694 !GC_NILP (rest);
|
|
695 rest = XHASHTABLE (rest)->next_weak)
|
|
696 {
|
|
697 enum hashtable_type type;
|
|
698
|
|
699 if (! ((*obj_marked_p) (rest)))
|
|
700 /* The hashtable is probably garbage. Ignore it. */
|
|
701 continue;
|
|
702 type = XHASHTABLE (rest)->type;
|
|
703 if (type == HASHTABLE_KEY_WEAK || type == HASHTABLE_VALUE_WEAK
|
|
704 || type == HASHTABLE_KEY_CAR_WEAK
|
|
705 || type == HASHTABLE_VALUE_CAR_WEAK)
|
|
706 {
|
|
707 struct marking_closure fmh;
|
|
708
|
|
709 fmh.obj_marked_p = obj_marked_p;
|
|
710 fmh.markobj = markobj;
|
|
711 fmh.type = type;
|
|
712 fmh.did_mark = 0;
|
|
713 /* Now, scan over all the pairs. For all pairs that are
|
|
714 half-marked, we may need to mark the other half if we're
|
|
715 keeping this pair. */
|
|
716 elisp_maphash (marking_mapper, rest, &fmh);
|
|
717 if (fmh.did_mark)
|
|
718 did_mark = 1;
|
|
719 }
|
|
720
|
|
721 /* #### If alloc.c mark_object changes, this must change also... */
|
|
722 {
|
|
723 /* Now mark the vector itself. (We don't need to call markobj
|
|
724 here because we know that everything *in* it is already marked,
|
|
725 we just need to prevent the vector itself from disappearing.)
|
|
726 (The remhash above has taken care of zero_entry.)
|
|
727 */
|
|
728 struct Lisp_Vector *ptr = XVECTOR (XHASHTABLE (rest)->harray);
|
|
729 int len = vector_length (ptr);
|
|
730 if (len >= 0)
|
|
731 {
|
|
732 ptr->size = -1 - len;
|
|
733 did_mark = 1;
|
|
734 }
|
|
735 /* else it's already marked (remember, this function is iterated
|
|
736 until marking stops) */
|
|
737 }
|
|
738 }
|
|
739
|
|
740 return did_mark;
|
|
741 }
|
|
742
|
|
743 struct pruning_closure
|
|
744 {
|
|
745 int (*obj_marked_p) (Lisp_Object);
|
|
746 };
|
|
747
|
|
748 static int
|
|
749 pruning_mapper (CONST void *key, CONST void *contents, void *closure)
|
|
750 {
|
|
751 Lisp_Object keytem, valuetem;
|
|
752 struct pruning_closure *fmh =
|
|
753 (struct pruning_closure *) closure;
|
|
754
|
|
755 /* This function is called over each pair in the hashtable.
|
|
756 We remove the pairs that aren't completely marked (everything
|
|
757 that is going to stay ought to have been marked already
|
|
758 by the finish_marking stage). */
|
|
759 CVOID_TO_LISP (keytem, key);
|
|
760 CVOID_TO_LISP (valuetem, contents);
|
|
761
|
|
762 return (! ((*fmh->obj_marked_p) (keytem) &&
|
|
763 (*fmh->obj_marked_p) (valuetem)));
|
|
764 }
|
|
765
|
|
766 void
|
|
767 prune_weak_hashtables (int (*obj_marked_p) (Lisp_Object))
|
|
768 {
|
|
769 Lisp_Object rest, prev = Qnil;
|
|
770 for (rest = Vall_weak_hashtables;
|
|
771 !GC_NILP (rest);
|
|
772 rest = XHASHTABLE (rest)->next_weak)
|
|
773 {
|
|
774 if (! ((*obj_marked_p) (rest)))
|
|
775 {
|
|
776 /* This table itself is garbage. Remove it from the list. */
|
|
777 if (GC_NILP (prev))
|
|
778 Vall_weak_hashtables = XHASHTABLE (rest)->next_weak;
|
|
779 else
|
|
780 XHASHTABLE (prev)->next_weak = XHASHTABLE (rest)->next_weak;
|
|
781 }
|
|
782 else
|
|
783 {
|
|
784 struct pruning_closure fmh;
|
|
785 fmh.obj_marked_p = obj_marked_p;
|
|
786 /* Now, scan over all the pairs. Remove all of the pairs
|
|
787 in which the key or value, or both, is unmarked
|
|
788 (depending on the type of weak hashtable). */
|
|
789 elisp_map_remhash (pruning_mapper, rest, &fmh);
|
|
790 prev = rest;
|
|
791 }
|
|
792 }
|
|
793 }
|
|
794
|
|
795 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
|
|
796
|
|
797 unsigned long
|
|
798 internal_array_hash (Lisp_Object *arr, int size, int depth)
|
|
799 {
|
|
800 int i;
|
|
801 unsigned long hash = 0;
|
|
802
|
|
803 if (size <= 5)
|
|
804 {
|
|
805 for (i = 0; i < size; i++)
|
|
806 hash = HASH2 (hash, internal_hash (arr[i], depth + 1));
|
|
807 return hash;
|
|
808 }
|
|
809
|
|
810 /* just pick five elements scattered throughout the array.
|
|
811 A slightly better approach would be to offset by some
|
|
812 noise factor from the points chosen below. */
|
|
813 for (i = 0; i < 5; i++)
|
|
814 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth + 1));
|
|
815
|
|
816 return hash;
|
|
817 }
|
|
818
|
|
819 /* Return a hash value for a Lisp_Object. This is for use when hashing
|
|
820 objects with the comparison being `equal' (for `eq', you can just
|
|
821 use the Lisp_Object itself as the hash value). You need to make a
|
|
822 tradeoff between the speed of the hash function and how good the
|
|
823 hashing is. In particular, the hash function needs to be FAST,
|
|
824 so you can't just traipse down the whole tree hashing everything
|
|
825 together. Most of the time, objects will differ in the first
|
|
826 few elements you hash. Thus, we only go to a short depth (5)
|
|
827 and only hash at most 5 elements out of a vector. Theoretically
|
|
828 we could still take 5^5 time (a big big number) to compute a
|
|
829 hash, but practically this won't ever happen. */
|
|
830
|
|
831 unsigned long
|
|
832 internal_hash (Lisp_Object obj, int depth)
|
|
833 {
|
|
834 if (depth > 5)
|
|
835 return 0;
|
|
836 if (CONSP (obj))
|
|
837 {
|
|
838 /* no point in worrying about tail recursion, since we're not
|
|
839 going very deep */
|
|
840 return HASH2 (internal_hash (XCAR (obj), depth + 1),
|
|
841 internal_hash (XCDR (obj), depth + 1));
|
|
842 }
|
|
843 else if (STRINGP (obj))
|
14
|
844 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
|
0
|
845 #ifndef LRECORD_VECTOR
|
|
846 else if (VECTORP (obj))
|
|
847 {
|
|
848 struct Lisp_Vector *v = XVECTOR (obj);
|
|
849 return HASH2 (vector_length (v),
|
|
850 internal_array_hash (v->contents, vector_length (v),
|
|
851 depth + 1));
|
|
852 }
|
|
853 #endif /* !LRECORD_VECTOR */
|
|
854 else if (LRECORDP (obj))
|
|
855 {
|
|
856 CONST struct lrecord_implementation
|
|
857 *imp = XRECORD_LHEADER (obj)->implementation;
|
|
858 if (imp->hash)
|
|
859 return ((imp->hash) (obj, depth));
|
|
860 }
|
|
861
|
|
862 return LISP_HASH (obj);
|
|
863 }
|
|
864
|
|
865
|
|
866 /************************************************************************/
|
|
867 /* initialization */
|
|
868 /************************************************************************/
|
|
869
|
|
870 void
|
|
871 syms_of_elhash (void)
|
|
872 {
|
20
|
873 DEFSUBR (Fmake_hashtable);
|
|
874 DEFSUBR (Fcopy_hashtable);
|
|
875 DEFSUBR (Fhashtablep);
|
|
876 DEFSUBR (Fgethash);
|
|
877 DEFSUBR (Fputhash);
|
|
878 DEFSUBR (Fremhash);
|
|
879 DEFSUBR (Fclrhash);
|
|
880 DEFSUBR (Fmaphash);
|
|
881 DEFSUBR (Fhashtable_fullness);
|
|
882 DEFSUBR (Fmake_weak_hashtable);
|
|
883 DEFSUBR (Fmake_key_weak_hashtable);
|
|
884 DEFSUBR (Fmake_value_weak_hashtable);
|
0
|
885 defsymbol (&Qhashtablep, "hashtablep");
|
|
886 }
|
|
887
|
|
888 void
|
|
889 vars_of_elhash (void)
|
|
890 {
|
2
|
891 /* This must NOT be staticpro'd */
|
0
|
892 Vall_weak_hashtables = Qnil;
|
|
893 }
|