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