0
|
1 /* Lisp interface to hash tables.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
223
|
4 Copyright (C) 1997 Free Software Foundation, Inc.
|
0
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27 #include "hash.h"
|
|
28 #include "elhash.h"
|
|
29 #include "bytecode.h"
|
|
30
|
223
|
31 Lisp_Object Qhashtablep, Qhashtable;
|
|
32 Lisp_Object Qweak, Qkey_weak, Qvalue_weak, Qnon_weak;
|
0
|
33
|
|
34 #define LISP_OBJECTS_PER_HENTRY (sizeof (hentry) / sizeof (Lisp_Object))/* 2 */
|
|
35
|
185
|
36 struct hashtable
|
0
|
37 {
|
|
38 struct lcrecord_header header;
|
|
39 unsigned int fullness;
|
|
40 unsigned long (*hash_function) (CONST void *);
|
|
41 int (*test_function) (CONST void *, CONST void *);
|
|
42 Lisp_Object zero_entry;
|
|
43 Lisp_Object harray;
|
|
44 enum hashtable_type type; /* whether and how this hashtable is weak */
|
|
45 Lisp_Object next_weak; /* Used to chain together all of the weak
|
|
46 hashtables. Don't mark through this. */
|
|
47 };
|
|
48
|
|
49 static Lisp_Object Vall_weak_hashtables;
|
|
50
|
|
51 static Lisp_Object mark_hashtable (Lisp_Object, void (*) (Lisp_Object));
|
|
52 static void print_hashtable (Lisp_Object, Lisp_Object, int);
|
231
|
53 static int hashtable_equal (Lisp_Object t1, Lisp_Object t2, int depth);
|
0
|
54 DEFINE_LRECORD_IMPLEMENTATION ("hashtable", hashtable,
|
231
|
55 mark_hashtable, print_hashtable, 0,
|
|
56 hashtable_equal, 0, struct hashtable);
|
0
|
57
|
|
58 static Lisp_Object
|
|
59 mark_hashtable (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
60 {
|
185
|
61 struct hashtable *table = XHASHTABLE (obj);
|
0
|
62
|
|
63 if (table->type != HASHTABLE_NONWEAK)
|
|
64 {
|
|
65 /* If the table is weak, we don't want to mark the keys and values
|
|
66 (we scan over them after everything else has been marked,
|
|
67 and mark or remove them as necessary). Note that we will mark
|
|
68 the table->harray itself at the same time; it's hard to mark
|
|
69 that here without also marking its contents. */
|
|
70 return Qnil;
|
|
71 }
|
|
72 ((markobj) (table->zero_entry));
|
173
|
73 return table->harray;
|
0
|
74 }
|
223
|
75
|
231
|
76 /* Equality of hashtables. Two hashtables are equal when they are of
|
|
77 the same type and test function, they have the same number of
|
|
78 elements, and for each key in hashtable, the values are `equal'.
|
|
79
|
|
80 This is similar to Common Lisp `equalp' of hashtables, with the
|
|
81 difference that CL requires the keys to be compared using the
|
|
82 `:test' function, which we don't do. Doing that would require
|
|
83 consing, and consing is bad idea in `equal'. Anyway, our method
|
|
84 should provide the same result -- if the keys are not equal
|
|
85 according to `:test', then Fgethash() in hashtable_equal_mapper()
|
|
86 will fail. */
|
|
87 struct hashtable_equal_closure
|
|
88 {
|
|
89 int depth;
|
|
90 int equal_so_far;
|
|
91 Lisp_Object other_table;
|
|
92 };
|
|
93
|
|
94 static void
|
|
95 hashtable_equal_mapper (void *key, void *contents, void *arg)
|
|
96 {
|
|
97 struct hashtable_equal_closure *closure =
|
|
98 (struct hashtable_equal_closure *)arg;
|
|
99 Lisp_Object keytem, valuetem;
|
|
100
|
|
101 /* It would be beautiful if maphash() allowed us to bail out when C
|
|
102 function returns non-zero, a la map_extents() et al. #### Make
|
|
103 it so! */
|
|
104 if (closure->equal_so_far)
|
|
105 {
|
|
106 Lisp_Object value_in_other;
|
|
107 CVOID_TO_LISP (keytem, key);
|
|
108 CVOID_TO_LISP (valuetem, contents);
|
|
109 /* Look up the key in the other hashtable, and compare the
|
|
110 values. */
|
|
111 value_in_other = Fgethash (keytem, closure->other_table, Qunbound);
|
|
112 if (UNBOUNDP (value_in_other)
|
|
113 || !internal_equal (valuetem, value_in_other, closure->depth))
|
|
114 closure->equal_so_far = 0;
|
|
115 /* return 1; */
|
|
116 }
|
|
117 /* return 0; */
|
|
118 }
|
|
119
|
|
120 static int
|
|
121 hashtable_equal (Lisp_Object t1, Lisp_Object t2, int depth)
|
|
122 {
|
|
123 struct hashtable_equal_closure closure;
|
|
124 struct hashtable *table1 = XHASHTABLE (t1);
|
|
125 struct hashtable *table2 = XHASHTABLE (t2);
|
|
126
|
|
127 /* The objects are `equal' if they are of the same type, so return 0
|
|
128 if types or test functions are not the same. Obviously, the
|
|
129 number of elements must be equal, too. */
|
|
130 if ((table1->test_function != table2->test_function)
|
|
131 || (table1->type != table2->type)
|
|
132 || (table1->fullness != table2->fullness))
|
|
133 return 0;
|
|
134
|
|
135 closure.depth = depth + 1;
|
|
136 closure.equal_so_far = 1;
|
|
137 closure.other_table = t2;
|
|
138 elisp_maphash (hashtable_equal_mapper, t1, &closure);
|
|
139 return closure.equal_so_far;
|
|
140 }
|
|
141
|
223
|
142 /* Printing hashtables.
|
|
143
|
|
144 This is non-trivial, because we use a readable structure-style
|
|
145 syntax for hashtables. This means that a typical hashtable will be
|
|
146 readably printed in the form of:
|
|
147
|
|
148 #s(hashtable size 2 data (key1 value1 key2 value2))
|
|
149
|
|
150 The supported keywords are `type' (non-weak (or nil), weak,
|
|
151 key-weak and value-weak), `test' (eql (or nil), eq or equal),
|
|
152 `size' (a natnum or nil) and `data' (a list).
|
|
153
|
|
154 If `print-readably' is non-nil, then a simpler syntax is used; for
|
|
155 instance:
|
|
156
|
|
157 #<hashtable size 2/13 data (key1 value1 key2 value2) 0x874d>
|
|
158
|
|
159 The data is truncated to four pairs, and the rest is shown with
|
|
160 `...'. The actual printer is non-consing. */
|
|
161
|
231
|
162 struct print_hashtable_data_closure
|
|
163 {
|
|
164 EMACS_INT count; /* Used to implement truncation for
|
|
165 non-readable printing, as well as
|
|
166 to avoid the unnecessary space at
|
|
167 the beginning. */
|
223
|
168 Lisp_Object printcharfun;
|
|
169 };
|
|
170
|
|
171 static void
|
|
172 print_hashtable_data_mapper (void *key, void *contents, void *arg)
|
|
173 {
|
|
174 Lisp_Object keytem, valuetem;
|
231
|
175 struct print_hashtable_data_closure *closure =
|
|
176 (struct print_hashtable_data_closure *)arg;
|
223
|
177
|
|
178 if (closure->count < 4 || print_readably)
|
|
179 {
|
|
180 CVOID_TO_LISP (keytem, key);
|
|
181 CVOID_TO_LISP (valuetem, contents);
|
|
182
|
|
183 if (closure->count)
|
|
184 write_c_string (" ", closure->printcharfun);
|
|
185
|
|
186 print_internal (keytem, closure->printcharfun, 1);
|
|
187 write_c_string (" ", closure->printcharfun);
|
|
188 print_internal (valuetem, closure->printcharfun, 1);
|
|
189 }
|
|
190 ++closure->count;
|
|
191 }
|
|
192
|
|
193 /* Print the data of the hashtable. This maps through a Lisp
|
|
194 hashtable and prints key/value pairs using PRINTCHARFUN. */
|
|
195 static void
|
|
196 print_hashtable_data (Lisp_Object hashtable, Lisp_Object printcharfun)
|
|
197 {
|
231
|
198 struct print_hashtable_data_closure closure;
|
223
|
199 closure.count = 0;
|
|
200 closure.printcharfun = printcharfun;
|
|
201
|
|
202 write_c_string (" data (", printcharfun);
|
|
203 elisp_maphash (print_hashtable_data_mapper, hashtable, &closure);
|
|
204 write_c_string ((!print_readably && closure.count > 4) ? " ...)" : ")",
|
|
205 printcharfun);
|
|
206 }
|
|
207
|
|
208 /* Needed for tests. */
|
|
209 static int lisp_object_eql_equal (CONST void *x1, CONST void *x2);
|
|
210 static int lisp_object_equal_equal (CONST void *x1, CONST void *x2);
|
185
|
211
|
0
|
212 static void
|
|
213 print_hashtable (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
214 {
|
185
|
215 struct hashtable *table = XHASHTABLE (obj);
|
223
|
216 char buf[128];
|
|
217
|
|
218 write_c_string (print_readably ? "#s(hashtable" : "#<hashtable",
|
|
219 printcharfun);
|
|
220 if (table->type != HASHTABLE_NONWEAK)
|
|
221 {
|
|
222 sprintf (buf, " type %s",
|
|
223 (table->type == HASHTABLE_WEAK ? "weak" :
|
|
224 table->type == HASHTABLE_KEY_WEAK ? "key-weak" :
|
|
225 table->type == HASHTABLE_VALUE_WEAK ? "value-weak" :
|
|
226 "you-d-better-not-see-this"));
|
|
227 write_c_string (buf, printcharfun);
|
|
228 }
|
|
229 /* These checks are way kludgy... */
|
|
230 if (table->test_function == NULL)
|
|
231 write_c_string (" test eq", printcharfun);
|
|
232 else if (table->test_function == lisp_object_equal_equal)
|
|
233 write_c_string (" test equal", printcharfun);
|
|
234 else if (table->test_function == lisp_object_eql_equal)
|
231
|
235 DO_NOTHING;
|
223
|
236 else
|
|
237 abort ();
|
|
238 if (table->fullness || !print_readably)
|
|
239 {
|
|
240 if (print_readably)
|
231
|
241 sprintf (buf, " size %u", table->fullness);
|
223
|
242 else
|
|
243 sprintf (buf, " size %u/%ld", table->fullness,
|
|
244 XVECTOR_LENGTH (table->harray) / LISP_OBJECTS_PER_HENTRY);
|
|
245 write_c_string (buf, printcharfun);
|
|
246 }
|
|
247 if (table->fullness)
|
|
248 print_hashtable_data (obj, printcharfun);
|
0
|
249 if (print_readably)
|
223
|
250 write_c_string (")", printcharfun);
|
|
251 else
|
|
252 {
|
|
253 sprintf (buf, " 0x%x>", table->header.uid);
|
|
254 write_c_string (buf, printcharfun);
|
|
255 }
|
|
256 }
|
|
257
|
|
258
|
|
259 /* Pretty reading of hashtables.
|
|
260
|
|
261 Here we use the existing structures mechanism (which is,
|
|
262 unfortunately, pretty cumbersome) for validating and instantiating
|
|
263 the hashtables. The idea is that the side-effect of reading a
|
|
264 #s(hashtable PLIST) object is creation of a hashtable with desired
|
|
265 properties, and that the hashtable is returned. */
|
|
266
|
|
267 /* Validation functions: each keyword provides its own validation
|
|
268 function. The errors should maybe be continuable, but it is
|
|
269 unclear how this would cope with ERRB. */
|
|
270 static int
|
|
271 hashtable_type_validate (Lisp_Object keyword, Lisp_Object value,
|
|
272 Error_behavior errb)
|
|
273 {
|
|
274 if (!(NILP (value)
|
|
275 || EQ (value, Qnon_weak)
|
|
276 || EQ (value, Qweak)
|
|
277 || EQ (value, Qkey_weak)
|
|
278 || EQ (value, Qvalue_weak)))
|
|
279 {
|
|
280 maybe_signal_simple_error ("Invalid hashtable type", value,
|
|
281 Qhashtable, errb);
|
|
282 return 0;
|
|
283 }
|
|
284 return 1;
|
|
285 }
|
|
286
|
|
287 static int
|
|
288 hashtable_test_validate (Lisp_Object keyword, Lisp_Object value,
|
|
289 Error_behavior errb)
|
|
290 {
|
|
291 if (!(NILP (value)
|
|
292 || EQ (value, Qeq)
|
|
293 || EQ (value, Qeql)
|
|
294 || EQ (value, Qequal)))
|
|
295 {
|
|
296 maybe_signal_simple_error ("Invalid hashtable test", value,
|
|
297 Qhashtable, errb);
|
|
298 return 0;
|
|
299 }
|
|
300 return 1;
|
|
301 }
|
|
302
|
|
303 static int
|
|
304 hashtable_size_validate (Lisp_Object keyword, Lisp_Object value,
|
|
305 Error_behavior errb)
|
|
306 {
|
|
307 if (!NATNUMP (value))
|
|
308 {
|
|
309 maybe_signal_error (Qwrong_type_argument, list2 (Qnatnump, value),
|
|
310 Qhashtable, errb);
|
|
311 return 0;
|
|
312 }
|
|
313 return 1;
|
0
|
314 }
|
|
315
|
223
|
316 static int
|
|
317 hashtable_data_validate (Lisp_Object keyword, Lisp_Object value,
|
|
318 Error_behavior errb)
|
|
319 {
|
|
320 int num = 0;
|
|
321 Lisp_Object tail;
|
|
322
|
|
323 /* #### Doesn't respect ERRB! */
|
|
324 EXTERNAL_LIST_LOOP (tail, value)
|
|
325 {
|
|
326 ++num;
|
|
327 QUIT;
|
|
328 }
|
|
329 if (num & 1)
|
|
330 {
|
|
331 maybe_signal_simple_error
|
|
332 ("Hashtable data must have alternating keyword/value pairs", value,
|
|
333 Qhashtable, errb);
|
|
334 return 0;
|
|
335 }
|
|
336 return 1;
|
|
337 }
|
|
338
|
|
339 /* The actual instantiation of hashtable. This does practically no
|
|
340 error checking, because it relies on the fact that the paranoid
|
|
341 functions above have error-checked everything to the last details.
|
|
342 If this assumption is wrong, we will get a crash immediately (with
|
|
343 error-checking compiled in), and we'll know if there is a bug in
|
|
344 the structure mechanism. So there. */
|
|
345 static Lisp_Object
|
|
346 hashtable_instantiate (Lisp_Object plist)
|
|
347 {
|
|
348 /* I'm not sure whether this can GC, but better safe than sorry. */
|
|
349 Lisp_Object hashtab = Qnil;
|
|
350 Lisp_Object type = Qnil, test = Qnil, size = Qnil, data = Qnil;
|
|
351 Lisp_Object key, value;
|
|
352 struct gcpro gcpro1;
|
|
353 GCPRO1 (hashtab);
|
|
354
|
|
355 while (!NILP (plist))
|
|
356 {
|
|
357 key = XCAR (plist);
|
|
358 plist = XCDR (plist);
|
|
359 value = XCAR (plist);
|
|
360 plist = XCDR (plist);
|
|
361 if (EQ (key, Qtype))
|
|
362 type = value;
|
|
363 else if (EQ (key, Qtest))
|
|
364 test = value;
|
|
365 else if (EQ (key, Qsize))
|
|
366 size = value;
|
|
367 else if (EQ (key, Qdata))
|
|
368 data = value;
|
|
369 else
|
|
370 abort ();
|
|
371 }
|
|
372 if (NILP (type))
|
|
373 type = Qnon_weak;
|
|
374 if (NILP (size))
|
|
375 {
|
|
376 /* Divide by two, because data is a plist. */
|
|
377 XSETINT (size, XINT (Flength (data)) / 2);
|
|
378 }
|
|
379
|
|
380 /* Create the hashtable. */
|
|
381 if (EQ (type, Qnon_weak))
|
|
382 hashtab = Fmake_hashtable (size, test);
|
|
383 else if (EQ (type, Qweak))
|
|
384 hashtab = Fmake_weak_hashtable (size, test);
|
|
385 else if (EQ (type, Qkey_weak))
|
|
386 hashtab = Fmake_key_weak_hashtable (size, test);
|
|
387 else if (EQ (type, Qvalue_weak))
|
|
388 hashtab = Fmake_value_weak_hashtable (size, test);
|
|
389 else
|
|
390 abort ();
|
|
391
|
|
392 /* And fill it with data. */
|
|
393 while (!NILP (data))
|
|
394 {
|
|
395 key = XCAR (data);
|
|
396 data = XCDR (data);
|
|
397 value = XCAR (data);
|
|
398 data = XCDR (data);
|
|
399 Fputhash (key, value, hashtab);
|
|
400 }
|
|
401
|
|
402 UNGCPRO;
|
|
403 return hashtab;
|
|
404 }
|
|
405
|
|
406 /* Initialize the hashtable as a structure type. This is called from
|
|
407 emacs.c. */
|
|
408 void
|
|
409 structure_type_create_hashtable (void)
|
|
410 {
|
|
411 struct structure_type *st;
|
|
412
|
|
413 st = define_structure_type (Qhashtable, 0, hashtable_instantiate);
|
|
414 define_structure_type_keyword (st, Qtype, hashtable_type_validate);
|
|
415 define_structure_type_keyword (st, Qtest, hashtable_test_validate);
|
|
416 define_structure_type_keyword (st, Qsize, hashtable_size_validate);
|
|
417 define_structure_type_keyword (st, Qdata, hashtable_data_validate);
|
|
418 }
|
|
419
|
|
420 /* Basic conversion and allocation functions. */
|
|
421
|
|
422 /* Create a C hashtable from the data in the Lisp hashtable. The
|
|
423 actual vector is not copied, nor are the keys or values copied. */
|
0
|
424 static void
|
185
|
425 ht_copy_to_c (struct hashtable *ht, c_hashtable c_table)
|
0
|
426 {
|
173
|
427 int len = XVECTOR_LENGTH (ht->harray);
|
0
|
428
|
185
|
429 c_table->harray = (hentry *) XVECTOR_DATA (ht->harray);
|
0
|
430 c_table->zero_set = (!GC_UNBOUNDP (ht->zero_entry));
|
|
431 c_table->zero_entry = LISP_TO_VOID (ht->zero_entry);
|
207
|
432 #ifndef LRECORD_VECTOR
|
0
|
433 if (len < 0)
|
|
434 {
|
|
435 /* #### if alloc.c mark_object() changes, this must change too. */
|
|
436 /* barf gag retch. When a vector is marked, its len is
|
|
437 made less than 0. In the prune_weak_hashtables() stage,
|
|
438 we are called on vectors that are like this, and we must
|
|
439 be able to deal. */
|
|
440 assert (gc_in_progress);
|
|
441 len = -1 - len;
|
|
442 }
|
207
|
443 #endif
|
173
|
444 c_table->size = len/LISP_OBJECTS_PER_HENTRY;
|
|
445 c_table->fullness = ht->fullness;
|
0
|
446 c_table->hash_function = ht->hash_function;
|
|
447 c_table->test_function = ht->test_function;
|
|
448 XSETHASHTABLE (c_table->elisp_table, ht);
|
|
449 }
|
|
450
|
|
451 static void
|
185
|
452 ht_copy_from_c (c_hashtable c_table, struct hashtable *ht)
|
0
|
453 {
|
|
454 struct Lisp_Vector dummy;
|
|
455 /* C is truly hateful */
|
|
456 void *vec_addr
|
185
|
457 = ((char *) c_table->harray
|
173
|
458 - ((char *) &(dummy.contents[0]) - (char *) &dummy));
|
0
|
459
|
|
460 XSETVECTOR (ht->harray, vec_addr);
|
|
461 if (c_table->zero_set)
|
|
462 VOID_TO_LISP (ht->zero_entry, c_table->zero_entry);
|
|
463 else
|
|
464 ht->zero_entry = Qunbound;
|
|
465 ht->fullness = c_table->fullness;
|
|
466 }
|
|
467
|
|
468
|
185
|
469 static struct hashtable *
|
0
|
470 allocate_hashtable (void)
|
|
471 {
|
185
|
472 struct hashtable *table =
|
|
473 alloc_lcrecord_type (struct hashtable, lrecord_hashtable);
|
173
|
474 table->harray = Qnil;
|
|
475 table->zero_entry = Qunbound;
|
|
476 table->fullness = 0;
|
0
|
477 table->hash_function = 0;
|
|
478 table->test_function = 0;
|
173
|
479 return table;
|
0
|
480 }
|
|
481
|
173
|
482 void *
|
0
|
483 elisp_hvector_malloc (unsigned int bytes, Lisp_Object table)
|
|
484 {
|
|
485 Lisp_Object new_vector;
|
185
|
486 struct hashtable *ht = XHASHTABLE (table);
|
0
|
487
|
173
|
488 assert (bytes > XVECTOR_LENGTH (ht->harray) * sizeof (Lisp_Object));
|
207
|
489 new_vector = make_vector ((bytes / sizeof (Lisp_Object)), Qnull_pointer);
|
173
|
490 return (void *) XVECTOR_DATA (new_vector);
|
0
|
491 }
|
|
492
|
|
493 void
|
|
494 elisp_hvector_free (void *ptr, Lisp_Object table)
|
|
495 {
|
185
|
496 struct hashtable *ht = XHASHTABLE (table);
|
0
|
497 #if defined (USE_ASSERTIONS) || defined (DEBUG_XEMACS)
|
|
498 Lisp_Object current_vector = ht->harray;
|
|
499 #endif
|
|
500
|
173
|
501 assert (((void *) XVECTOR_DATA (current_vector)) == ptr);
|
0
|
502 ht->harray = Qnil; /* Let GC do its job */
|
|
503 }
|
|
504
|
|
505
|
20
|
506 DEFUN ("hashtablep", Fhashtablep, 1, 1, 0, /*
|
0
|
507 Return t if OBJ is a hashtable, else nil.
|
20
|
508 */
|
|
509 (obj))
|
0
|
510 {
|
173
|
511 return HASHTABLEP (obj) ? Qt : Qnil;
|
0
|
512 }
|
|
513
|
|
514
|
|
515
|
|
516
|
|
517 #if 0 /* I don't think these are needed any more.
|
|
518 If using the general lisp_object_equal_*() functions
|
|
519 causes efficiency problems, these can be resurrected. --ben */
|
|
520 /* equality and hash functions for Lisp strings */
|
|
521 int
|
|
522 lisp_string_equal (CONST void *x1, CONST void *x2)
|
|
523 {
|
|
524 Lisp_Object str1, str2;
|
|
525 CVOID_TO_LISP (str1, x1);
|
|
526 CVOID_TO_LISP (str2, x2);
|
14
|
527 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
|
0
|
528 }
|
|
529
|
|
530 unsigned long
|
|
531 lisp_string_hash (CONST void *x)
|
|
532 {
|
|
533 Lisp_Object str;
|
|
534 CVOID_TO_LISP (str, x);
|
14
|
535 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
|
0
|
536 }
|
|
537
|
|
538 #endif /* 0 */
|
|
539
|
|
540 static int
|
|
541 lisp_object_eql_equal (CONST void *x1, CONST void *x2)
|
|
542 {
|
|
543 Lisp_Object obj1, obj2;
|
|
544 CVOID_TO_LISP (obj1, x1);
|
|
545 CVOID_TO_LISP (obj2, x2);
|
195
|
546 return FLOATP (obj1) ? internal_equal (obj1, obj2, 0) : EQ (obj1, obj2);
|
0
|
547 }
|
|
548
|
|
549 static unsigned long
|
|
550 lisp_object_eql_hash (CONST void *x)
|
|
551 {
|
|
552 Lisp_Object obj;
|
|
553 CVOID_TO_LISP (obj, x);
|
|
554 if (FLOATP (obj))
|
|
555 return internal_hash (obj, 0);
|
|
556 else
|
|
557 return LISP_HASH (obj);
|
|
558 }
|
|
559
|
|
560 static int
|
|
561 lisp_object_equal_equal (CONST void *x1, CONST void *x2)
|
|
562 {
|
|
563 Lisp_Object obj1, obj2;
|
|
564 CVOID_TO_LISP (obj1, x1);
|
|
565 CVOID_TO_LISP (obj2, x2);
|
195
|
566 return internal_equal (obj1, obj2, 0);
|
0
|
567 }
|
|
568
|
|
569 static unsigned long
|
|
570 lisp_object_equal_hash (CONST void *x)
|
|
571 {
|
|
572 Lisp_Object obj;
|
|
573 CVOID_TO_LISP (obj, x);
|
|
574 return internal_hash (obj, 0);
|
|
575 }
|
|
576
|
|
577 Lisp_Object
|
|
578 make_lisp_hashtable (int size,
|
|
579 enum hashtable_type type,
|
|
580 enum hashtable_test_fun test)
|
|
581 {
|
|
582 Lisp_Object result;
|
185
|
583 struct hashtable *table = allocate_hashtable ();
|
0
|
584
|
|
585 table->harray = make_vector ((compute_harray_size (size)
|
|
586 * LISP_OBJECTS_PER_HENTRY),
|
207
|
587 Qnull_pointer);
|
0
|
588 switch (test)
|
|
589 {
|
|
590 case HASHTABLE_EQ:
|
187
|
591 table->test_function = NULL;
|
|
592 table->hash_function = NULL;
|
0
|
593 break;
|
|
594
|
|
595 case HASHTABLE_EQL:
|
|
596 table->test_function = lisp_object_eql_equal;
|
|
597 table->hash_function = lisp_object_eql_hash;
|
|
598 break;
|
|
599
|
|
600 case HASHTABLE_EQUAL:
|
|
601 table->test_function = lisp_object_equal_equal;
|
|
602 table->hash_function = lisp_object_equal_hash;
|
|
603 break;
|
|
604
|
|
605 default:
|
|
606 abort ();
|
|
607 }
|
|
608
|
|
609 table->type = type;
|
|
610 XSETHASHTABLE (result, table);
|
|
611
|
|
612 if (table->type != HASHTABLE_NONWEAK)
|
|
613 {
|
|
614 table->next_weak = Vall_weak_hashtables;
|
|
615 Vall_weak_hashtables = result;
|
|
616 }
|
|
617 else
|
|
618 table->next_weak = Qunbound;
|
|
619
|
173
|
620 return result;
|
0
|
621 }
|
|
622
|
|
623 static enum hashtable_test_fun
|
|
624 decode_hashtable_test_fun (Lisp_Object sym)
|
|
625 {
|
187
|
626 if (NILP (sym)) return HASHTABLE_EQL;
|
2
|
627 if (EQ (sym, Qeq)) return HASHTABLE_EQ;
|
|
628 if (EQ (sym, Qequal)) return HASHTABLE_EQUAL;
|
|
629 if (EQ (sym, Qeql)) return HASHTABLE_EQL;
|
185
|
630
|
231
|
631 signal_simple_error ("Invalid hashtable test function", sym);
|
2
|
632 return HASHTABLE_EQ; /* not reached */
|
0
|
633 }
|
|
634
|
20
|
635 DEFUN ("make-hashtable", Fmake_hashtable, 1, 2, 0, /*
|
0
|
636 Make a hashtable of initial size SIZE.
|
|
637 Comparison between keys is done with TEST-FUN, which must be one of
|
|
638 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must
|
|
639 be the same object (or have the same floating-point value, for floats)
|
|
640 to be considered equivalent.
|
|
641
|
|
642 See also `make-weak-hashtable', `make-key-weak-hashtable', and
|
|
643 `make-value-weak-hashtable'.
|
20
|
644 */
|
|
645 (size, test_fun))
|
0
|
646 {
|
|
647 CHECK_NATNUM (size);
|
|
648 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK,
|
|
649 decode_hashtable_test_fun (test_fun));
|
|
650 }
|
|
651
|
20
|
652 DEFUN ("copy-hashtable", Fcopy_hashtable, 1, 1, 0, /*
|
0
|
653 Make a new hashtable which contains the same keys and values
|
|
654 as the given table. The keys and values will not themselves be copied.
|
20
|
655 */
|
|
656 (old_table))
|
0
|
657 {
|
|
658 struct _C_hashtable old_htbl;
|
|
659 struct _C_hashtable new_htbl;
|
185
|
660 struct hashtable *old_ht;
|
|
661 struct hashtable *new_ht;
|
0
|
662 Lisp_Object result;
|
|
663
|
|
664 CHECK_HASHTABLE (old_table);
|
|
665 old_ht = XHASHTABLE (old_table);
|
|
666 ht_copy_to_c (old_ht, &old_htbl);
|
|
667
|
|
668 /* we can't just call Fmake_hashtable() here because that will make a
|
|
669 table that is slightly larger than the one we're trying to copy,
|
|
670 which will make copy_hash() blow up. */
|
|
671 new_ht = allocate_hashtable ();
|
|
672 new_ht->fullness = 0;
|
|
673 new_ht->zero_entry = Qunbound;
|
|
674 new_ht->hash_function = old_ht->hash_function;
|
|
675 new_ht->test_function = old_ht->test_function;
|
207
|
676 new_ht->harray = Fmake_vector (Flength (old_ht->harray), Qnull_pointer);
|
0
|
677 ht_copy_to_c (new_ht, &new_htbl);
|
|
678 copy_hash (&new_htbl, &old_htbl);
|
|
679 ht_copy_from_c (&new_htbl, new_ht);
|
|
680 new_ht->type = old_ht->type;
|
|
681 XSETHASHTABLE (result, new_ht);
|
|
682
|
|
683 if (UNBOUNDP (old_ht->next_weak))
|
|
684 new_ht->next_weak = Qunbound;
|
|
685 else
|
|
686 {
|
|
687 new_ht->next_weak = Vall_weak_hashtables;
|
|
688 Vall_weak_hashtables = result;
|
|
689 }
|
|
690
|
173
|
691 return result;
|
0
|
692 }
|
|
693
|
|
694
|
20
|
695 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
|
187
|
696 Find hash value for KEY in HASHTABLE.
|
0
|
697 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
698 */
|
187
|
699 (key, hashtable, default_))
|
0
|
700 {
|
|
701 CONST void *vval;
|
|
702 struct _C_hashtable htbl;
|
|
703 if (!gc_in_progress)
|
187
|
704 CHECK_HASHTABLE (hashtable);
|
|
705 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
706 if (gethash (LISP_TO_VOID (key), &htbl, &vval))
|
|
707 {
|
|
708 Lisp_Object val;
|
|
709 CVOID_TO_LISP (val, vval);
|
|
710 return val;
|
|
711 }
|
185
|
712 else
|
173
|
713 return default_;
|
0
|
714 }
|
|
715
|
|
716
|
20
|
717 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
|
187
|
718 Remove hash value for KEY in HASHTABLE.
|
20
|
719 */
|
187
|
720 (key, hashtable))
|
0
|
721 {
|
|
722 struct _C_hashtable htbl;
|
187
|
723 CHECK_HASHTABLE (hashtable);
|
0
|
724
|
187
|
725 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
726 remhash (LISP_TO_VOID (key), &htbl);
|
187
|
727 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
728 return Qnil;
|
|
729 }
|
|
730
|
|
731
|
20
|
732 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
|
187
|
733 Hash KEY to VAL in HASHTABLE.
|
20
|
734 */
|
187
|
735 (key, val, hashtable))
|
0
|
736 {
|
185
|
737 struct hashtable *ht;
|
0
|
738 void *vkey = LISP_TO_VOID (key);
|
|
739
|
187
|
740 CHECK_HASHTABLE (hashtable);
|
|
741 ht = XHASHTABLE (hashtable);
|
0
|
742 if (!vkey)
|
|
743 ht->zero_entry = val;
|
|
744 else
|
|
745 {
|
|
746 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
747 struct _C_hashtable htbl;
|
|
748
|
187
|
749 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
|
750 GCPRO3 (key, val, hashtable);
|
0
|
751 puthash (vkey, LISP_TO_VOID (val), &htbl);
|
187
|
752 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
753 UNGCPRO;
|
|
754 }
|
173
|
755 return val;
|
0
|
756 }
|
|
757
|
20
|
758 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
|
187
|
759 Remove all entries from HASHTABLE.
|
20
|
760 */
|
187
|
761 (hashtable))
|
0
|
762 {
|
|
763 struct _C_hashtable htbl;
|
187
|
764 CHECK_HASHTABLE (hashtable);
|
|
765 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
766 clrhash (&htbl);
|
187
|
767 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
768 return Qnil;
|
|
769 }
|
|
770
|
20
|
771 DEFUN ("hashtable-fullness", Fhashtable_fullness, 1, 1, 0, /*
|
187
|
772 Return number of entries in HASHTABLE.
|
20
|
773 */
|
187
|
774 (hashtable))
|
0
|
775 {
|
|
776 struct _C_hashtable htbl;
|
187
|
777 CHECK_HASHTABLE (hashtable);
|
|
778 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
173
|
779 return make_int (htbl.fullness);
|
0
|
780 }
|
|
781
|
|
782
|
|
783 static void
|
|
784 verify_function (Lisp_Object function, CONST char *description)
|
|
785 {
|
223
|
786 /* #### Unused DESCRIPTION? */
|
0
|
787 if (SYMBOLP (function))
|
|
788 {
|
|
789 if (NILP (function))
|
|
790 return;
|
|
791 else
|
|
792 function = indirect_function (function, 1);
|
|
793 }
|
|
794 if (SUBRP (function) || COMPILED_FUNCTIONP (function))
|
|
795 return;
|
|
796 else if (CONSP (function))
|
|
797 {
|
223
|
798 Lisp_Object funcar = XCAR (function);
|
187
|
799 if ((SYMBOLP (funcar)) && (EQ (funcar, Qlambda) ||
|
|
800 EQ (funcar, Qautoload)))
|
0
|
801 return;
|
|
802 }
|
|
803 signal_error (Qinvalid_function, list1 (function));
|
|
804 }
|
|
805
|
|
806 static void
|
|
807 lisp_maphash_function (CONST void *void_key,
|
|
808 void *void_val,
|
|
809 void *void_fn)
|
|
810 {
|
|
811 /* This function can GC */
|
|
812 Lisp_Object key, val, fn;
|
|
813 CVOID_TO_LISP (key, void_key);
|
|
814 VOID_TO_LISP (val, void_val);
|
|
815 VOID_TO_LISP (fn, void_fn);
|
|
816 call2 (fn, key, val);
|
|
817 }
|
|
818
|
|
819
|
20
|
820 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
|
187
|
821 Map FUNCTION over entries in HASHTABLE, calling it with two args,
|
0
|
822 each key and value in the table.
|
20
|
823 */
|
187
|
824 (function, hashtable))
|
0
|
825 {
|
|
826 struct _C_hashtable htbl;
|
|
827 struct gcpro gcpro1, gcpro2;
|
|
828
|
|
829 verify_function (function, GETTEXT ("hashtable mapping function"));
|
187
|
830 CHECK_HASHTABLE (hashtable);
|
|
831 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
|
832 GCPRO2 (hashtable, function);
|
0
|
833 maphash (lisp_maphash_function, &htbl, LISP_TO_VOID (function));
|
|
834 UNGCPRO;
|
|
835 return Qnil;
|
|
836 }
|
|
837
|
|
838
|
|
839 /* This function is for mapping a *C* function over the elements of a
|
|
840 lisp hashtable.
|
|
841 */
|
|
842 void
|
187
|
843 elisp_maphash (maphash_function function, Lisp_Object hashtable, void *closure)
|
0
|
844 {
|
|
845 struct _C_hashtable htbl;
|
|
846
|
187
|
847 if (!gc_in_progress) CHECK_HASHTABLE (hashtable);
|
|
848 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
849 maphash (function, &htbl, closure);
|
|
850 }
|
|
851
|
|
852 void
|
187
|
853 elisp_map_remhash (remhash_predicate function, Lisp_Object hashtable,
|
|
854 void *closure)
|
0
|
855 {
|
|
856 struct _C_hashtable htbl;
|
|
857
|
187
|
858 if (!gc_in_progress) CHECK_HASHTABLE (hashtable);
|
|
859 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
860 map_remhash (function, &htbl, closure);
|
187
|
861 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
862 }
|
|
863
|
|
864 #if 0
|
|
865 void
|
|
866 elisp_table_op (Lisp_Object table, generic_hashtable_op op, void *arg1,
|
|
867 void *arg2, void *arg3)
|
|
868 {
|
|
869 struct _C_hashtable htbl;
|
|
870 CHECK_HASHTABLE (table);
|
|
871 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
872 (*op) (&htbl, arg1, arg2, arg3);
|
|
873 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
874 }
|
|
875 #endif /* 0 */
|
|
876
|
|
877
|
|
878
|
20
|
879 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, 1, 2, 0, /*
|
0
|
880 Make a fully weak hashtable of initial size SIZE.
|
|
881 A weak hashtable is one whose pointers do not count as GC referents:
|
|
882 for any key-value pair in the hashtable, if the only remaining pointer
|
|
883 to either the key or the value is in a weak hash table, then the pair
|
|
884 will be removed from the table, and the key and value collected. A
|
|
885 non-weak hash table (or any other pointer) would prevent the object
|
|
886 from being collected.
|
|
887
|
|
888 You can also create semi-weak hashtables; see `make-key-weak-hashtable'
|
|
889 and `make-value-weak-hashtable'.
|
20
|
890 */
|
|
891 (size, test_fun))
|
0
|
892 {
|
|
893 CHECK_NATNUM (size);
|
|
894 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK,
|
|
895 decode_hashtable_test_fun (test_fun));
|
|
896 }
|
|
897
|
20
|
898 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 1, 2, 0, /*
|
0
|
899 Make a key-weak hashtable of initial size SIZE.
|
|
900 A key-weak hashtable is similar to a fully-weak hashtable (see
|
|
901 `make-weak-hashtable') except that a key-value pair will be removed
|
|
902 only if the key remains unmarked outside of weak hashtables. The pair
|
|
903 will remain in the hashtable if the key is pointed to by something other
|
|
904 than a weak hashtable, even if the value is not.
|
20
|
905 */
|
|
906 (size, test_fun))
|
0
|
907 {
|
|
908 CHECK_NATNUM (size);
|
|
909 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK,
|
|
910 decode_hashtable_test_fun (test_fun));
|
|
911 }
|
|
912
|
20
|
913 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 1, 2, 0, /*
|
0
|
914 Make a value-weak hashtable of initial size SIZE.
|
|
915 A value-weak hashtable is similar to a fully-weak hashtable (see
|
|
916 `make-weak-hashtable') except that a key-value pair will be removed only
|
|
917 if the value remains unmarked outside of weak hashtables. The pair will
|
|
918 remain in the hashtable if the value is pointed to by something other
|
|
919 than a weak hashtable, even if the key is not.
|
20
|
920 */
|
|
921 (size, test_fun))
|
0
|
922 {
|
|
923 CHECK_NATNUM (size);
|
|
924 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK,
|
|
925 decode_hashtable_test_fun (test_fun));
|
|
926 }
|
|
927
|
|
928 struct marking_closure
|
|
929 {
|
|
930 int (*obj_marked_p) (Lisp_Object);
|
|
931 void (*markobj) (Lisp_Object);
|
|
932 enum hashtable_type type;
|
|
933 int did_mark;
|
|
934 };
|
|
935
|
|
936 static void
|
|
937 marking_mapper (CONST void *key, void *contents, void *closure)
|
|
938 {
|
|
939 Lisp_Object keytem, valuetem;
|
|
940 struct marking_closure *fmh =
|
|
941 (struct marking_closure *) closure;
|
|
942
|
|
943 /* This function is called over each pair in the hashtable.
|
|
944 We complete the marking for semi-weak hashtables. */
|
|
945 CVOID_TO_LISP (keytem, key);
|
|
946 CVOID_TO_LISP (valuetem, contents);
|
185
|
947
|
0
|
948 switch (fmh->type)
|
|
949 {
|
|
950 case HASHTABLE_KEY_WEAK:
|
|
951 if ((fmh->obj_marked_p) (keytem) &&
|
|
952 !(fmh->obj_marked_p) (valuetem))
|
|
953 {
|
|
954 (fmh->markobj) (valuetem);
|
|
955 fmh->did_mark = 1;
|
|
956 }
|
|
957 break;
|
|
958
|
|
959 case HASHTABLE_VALUE_WEAK:
|
|
960 if ((fmh->obj_marked_p) (valuetem) &&
|
|
961 !(fmh->obj_marked_p) (keytem))
|
|
962 {
|
|
963 (fmh->markobj) (keytem);
|
|
964 fmh->did_mark = 1;
|
|
965 }
|
|
966 break;
|
|
967
|
|
968 case HASHTABLE_KEY_CAR_WEAK:
|
|
969 if (!CONSP (keytem) || (fmh->obj_marked_p) (XCAR (keytem)))
|
|
970 {
|
|
971 if (!(fmh->obj_marked_p) (keytem))
|
|
972 {
|
|
973 (fmh->markobj) (keytem);
|
|
974 fmh->did_mark = 1;
|
|
975 }
|
|
976 if (!(fmh->obj_marked_p) (valuetem))
|
|
977 {
|
|
978 (fmh->markobj) (valuetem);
|
|
979 fmh->did_mark = 1;
|
|
980 }
|
|
981 }
|
|
982 break;
|
|
983
|
|
984 case HASHTABLE_VALUE_CAR_WEAK:
|
|
985 if (!CONSP (valuetem) || (fmh->obj_marked_p) (XCAR (valuetem)))
|
|
986 {
|
|
987 if (!(fmh->obj_marked_p) (keytem))
|
|
988 {
|
|
989 (fmh->markobj) (keytem);
|
|
990 fmh->did_mark = 1;
|
|
991 }
|
|
992 if (!(fmh->obj_marked_p) (valuetem))
|
|
993 {
|
|
994 (fmh->markobj) (valuetem);
|
|
995 fmh->did_mark = 1;
|
|
996 }
|
|
997 }
|
|
998 break;
|
|
999
|
|
1000 default:
|
|
1001 abort (); /* Huh? */
|
|
1002 }
|
185
|
1003
|
0
|
1004 return;
|
|
1005 }
|
|
1006
|
|
1007 int
|
|
1008 finish_marking_weak_hashtables (int (*obj_marked_p) (Lisp_Object),
|
|
1009 void (*markobj) (Lisp_Object))
|
|
1010 {
|
|
1011 Lisp_Object rest;
|
|
1012 int did_mark = 0;
|
|
1013
|
|
1014 for (rest = Vall_weak_hashtables;
|
|
1015 !GC_NILP (rest);
|
|
1016 rest = XHASHTABLE (rest)->next_weak)
|
|
1017 {
|
|
1018 enum hashtable_type type;
|
|
1019
|
|
1020 if (! ((*obj_marked_p) (rest)))
|
|
1021 /* The hashtable is probably garbage. Ignore it. */
|
|
1022 continue;
|
|
1023 type = XHASHTABLE (rest)->type;
|
187
|
1024 if (type == HASHTABLE_KEY_WEAK ||
|
|
1025 type == HASHTABLE_VALUE_WEAK ||
|
|
1026 type == HASHTABLE_KEY_CAR_WEAK ||
|
|
1027 type == HASHTABLE_VALUE_CAR_WEAK)
|
0
|
1028 {
|
|
1029 struct marking_closure fmh;
|
|
1030
|
|
1031 fmh.obj_marked_p = obj_marked_p;
|
|
1032 fmh.markobj = markobj;
|
|
1033 fmh.type = type;
|
|
1034 fmh.did_mark = 0;
|
|
1035 /* Now, scan over all the pairs. For all pairs that are
|
|
1036 half-marked, we may need to mark the other half if we're
|
|
1037 keeping this pair. */
|
|
1038 elisp_maphash (marking_mapper, rest, &fmh);
|
|
1039 if (fmh.did_mark)
|
|
1040 did_mark = 1;
|
|
1041 }
|
|
1042
|
|
1043 /* #### If alloc.c mark_object changes, this must change also... */
|
|
1044 {
|
|
1045 /* Now mark the vector itself. (We don't need to call markobj
|
|
1046 here because we know that everything *in* it is already marked,
|
|
1047 we just need to prevent the vector itself from disappearing.)
|
|
1048 (The remhash above has taken care of zero_entry.)
|
|
1049 */
|
|
1050 struct Lisp_Vector *ptr = XVECTOR (XHASHTABLE (rest)->harray);
|
207
|
1051 #ifdef LRECORD_VECTOR
|
|
1052 if (! MARKED_RECORD_P(XHASHTABLE(rest)->harray))
|
|
1053 {
|
|
1054 MARK_RECORD_HEADER(&(ptr->header.lheader));
|
|
1055 did_mark = 1;
|
|
1056 }
|
|
1057 #else
|
223
|
1058 int len = vector_length (ptr);
|
0
|
1059 if (len >= 0)
|
|
1060 {
|
|
1061 ptr->size = -1 - len;
|
|
1062 did_mark = 1;
|
|
1063 }
|
207
|
1064 #endif
|
0
|
1065 /* else it's already marked (remember, this function is iterated
|
|
1066 until marking stops) */
|
|
1067 }
|
|
1068 }
|
|
1069
|
|
1070 return did_mark;
|
|
1071 }
|
|
1072
|
|
1073 struct pruning_closure
|
|
1074 {
|
|
1075 int (*obj_marked_p) (Lisp_Object);
|
|
1076 };
|
|
1077
|
|
1078 static int
|
|
1079 pruning_mapper (CONST void *key, CONST void *contents, void *closure)
|
|
1080 {
|
|
1081 Lisp_Object keytem, valuetem;
|
185
|
1082 struct pruning_closure *fmh = (struct pruning_closure *) closure;
|
0
|
1083
|
|
1084 /* This function is called over each pair in the hashtable.
|
|
1085 We remove the pairs that aren't completely marked (everything
|
|
1086 that is going to stay ought to have been marked already
|
|
1087 by the finish_marking stage). */
|
|
1088 CVOID_TO_LISP (keytem, key);
|
|
1089 CVOID_TO_LISP (valuetem, contents);
|
|
1090
|
173
|
1091 return ! ((*fmh->obj_marked_p) (keytem) &&
|
|
1092 (*fmh->obj_marked_p) (valuetem));
|
0
|
1093 }
|
|
1094
|
|
1095 void
|
|
1096 prune_weak_hashtables (int (*obj_marked_p) (Lisp_Object))
|
|
1097 {
|
|
1098 Lisp_Object rest, prev = Qnil;
|
|
1099 for (rest = Vall_weak_hashtables;
|
|
1100 !GC_NILP (rest);
|
|
1101 rest = XHASHTABLE (rest)->next_weak)
|
|
1102 {
|
|
1103 if (! ((*obj_marked_p) (rest)))
|
|
1104 {
|
|
1105 /* This table itself is garbage. Remove it from the list. */
|
|
1106 if (GC_NILP (prev))
|
|
1107 Vall_weak_hashtables = XHASHTABLE (rest)->next_weak;
|
|
1108 else
|
|
1109 XHASHTABLE (prev)->next_weak = XHASHTABLE (rest)->next_weak;
|
|
1110 }
|
|
1111 else
|
|
1112 {
|
|
1113 struct pruning_closure fmh;
|
|
1114 fmh.obj_marked_p = obj_marked_p;
|
|
1115 /* Now, scan over all the pairs. Remove all of the pairs
|
|
1116 in which the key or value, or both, is unmarked
|
|
1117 (depending on the type of weak hashtable). */
|
|
1118 elisp_map_remhash (pruning_mapper, rest, &fmh);
|
|
1119 prev = rest;
|
|
1120 }
|
|
1121 }
|
|
1122 }
|
|
1123
|
|
1124 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
|
|
1125
|
|
1126 unsigned long
|
|
1127 internal_array_hash (Lisp_Object *arr, int size, int depth)
|
|
1128 {
|
|
1129 int i;
|
|
1130 unsigned long hash = 0;
|
|
1131
|
|
1132 if (size <= 5)
|
|
1133 {
|
|
1134 for (i = 0; i < size; i++)
|
|
1135 hash = HASH2 (hash, internal_hash (arr[i], depth + 1));
|
|
1136 return hash;
|
|
1137 }
|
185
|
1138
|
0
|
1139 /* just pick five elements scattered throughout the array.
|
|
1140 A slightly better approach would be to offset by some
|
|
1141 noise factor from the points chosen below. */
|
|
1142 for (i = 0; i < 5; i++)
|
|
1143 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth + 1));
|
185
|
1144
|
0
|
1145 return hash;
|
|
1146 }
|
|
1147
|
|
1148 /* Return a hash value for a Lisp_Object. This is for use when hashing
|
|
1149 objects with the comparison being `equal' (for `eq', you can just
|
|
1150 use the Lisp_Object itself as the hash value). You need to make a
|
|
1151 tradeoff between the speed of the hash function and how good the
|
|
1152 hashing is. In particular, the hash function needs to be FAST,
|
|
1153 so you can't just traipse down the whole tree hashing everything
|
|
1154 together. Most of the time, objects will differ in the first
|
|
1155 few elements you hash. Thus, we only go to a short depth (5)
|
|
1156 and only hash at most 5 elements out of a vector. Theoretically
|
|
1157 we could still take 5^5 time (a big big number) to compute a
|
|
1158 hash, but practically this won't ever happen. */
|
|
1159
|
|
1160 unsigned long
|
|
1161 internal_hash (Lisp_Object obj, int depth)
|
|
1162 {
|
|
1163 if (depth > 5)
|
|
1164 return 0;
|
|
1165 if (CONSP (obj))
|
|
1166 {
|
|
1167 /* no point in worrying about tail recursion, since we're not
|
|
1168 going very deep */
|
|
1169 return HASH2 (internal_hash (XCAR (obj), depth + 1),
|
|
1170 internal_hash (XCDR (obj), depth + 1));
|
|
1171 }
|
|
1172 else if (STRINGP (obj))
|
14
|
1173 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
|
0
|
1174 else if (VECTORP (obj))
|
|
1175 {
|
|
1176 struct Lisp_Vector *v = XVECTOR (obj);
|
|
1177 return HASH2 (vector_length (v),
|
|
1178 internal_array_hash (v->contents, vector_length (v),
|
|
1179 depth + 1));
|
|
1180 }
|
|
1181 else if (LRECORDP (obj))
|
|
1182 {
|
|
1183 CONST struct lrecord_implementation
|
211
|
1184 *imp = XRECORD_LHEADER_IMPLEMENTATION (obj);
|
0
|
1185 if (imp->hash)
|
173
|
1186 return (imp->hash) (obj, depth);
|
0
|
1187 }
|
|
1188
|
|
1189 return LISP_HASH (obj);
|
|
1190 }
|
|
1191
|
|
1192
|
|
1193 /************************************************************************/
|
|
1194 /* initialization */
|
|
1195 /************************************************************************/
|
|
1196
|
|
1197 void
|
|
1198 syms_of_elhash (void)
|
|
1199 {
|
20
|
1200 DEFSUBR (Fmake_hashtable);
|
|
1201 DEFSUBR (Fcopy_hashtable);
|
|
1202 DEFSUBR (Fhashtablep);
|
|
1203 DEFSUBR (Fgethash);
|
|
1204 DEFSUBR (Fputhash);
|
|
1205 DEFSUBR (Fremhash);
|
|
1206 DEFSUBR (Fclrhash);
|
|
1207 DEFSUBR (Fmaphash);
|
|
1208 DEFSUBR (Fhashtable_fullness);
|
|
1209 DEFSUBR (Fmake_weak_hashtable);
|
|
1210 DEFSUBR (Fmake_key_weak_hashtable);
|
|
1211 DEFSUBR (Fmake_value_weak_hashtable);
|
0
|
1212 defsymbol (&Qhashtablep, "hashtablep");
|
223
|
1213 defsymbol (&Qhashtable, "hashtable");
|
|
1214 defsymbol (&Qweak, "weak");
|
|
1215 defsymbol (&Qkey_weak, "key-weak");
|
|
1216 defsymbol (&Qvalue_weak, "value-weak");
|
|
1217 defsymbol (&Qnon_weak, "non-weak");
|
0
|
1218 }
|
|
1219
|
|
1220 void
|
|
1221 vars_of_elhash (void)
|
|
1222 {
|
2
|
1223 /* This must NOT be staticpro'd */
|
0
|
1224 Vall_weak_hashtables = Qnil;
|
|
1225 }
|