better hysteresis in weak-set, weak-table
[bpt/guile.git] / libguile / weak-table.c
1 /* Copyright (C) 2011, 2012 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <assert.h>
26
27 #include "libguile/bdw-gc.h"
28 #include <gc/gc_mark.h>
29
30 #include "libguile/_scm.h"
31 #include "libguile/hash.h"
32 #include "libguile/eval.h"
33 #include "libguile/ports.h"
34
35 #include "libguile/validate.h"
36 #include "libguile/weak-table.h"
37
38
39 /* Weak Tables
40
41 This file implements weak hash tables. Weak hash tables are
42 generally used when you want to augment some object with additional
43 data, but when you don't have space to store the data in the object.
44 For example, procedure properties are implemented with weak tables.
45
46 Weak tables are implemented using an open-addressed hash table.
47 Basically this means that there is an array of entries, and the item
48 is expected to be found the slot corresponding to its hash code,
49 modulo the length of the array.
50
51 Collisions are handled using linear probing with the Robin Hood
52 technique. See Pedro Celis' paper, "Robin Hood Hashing":
53
54 http://www.cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf
55
56 The vector of entries is allocated in such a way that the GC doesn't
57 trace the weak values. For doubly-weak tables, this means that the
58 entries are allocated as an "atomic" piece of memory. Key-weak and
59 value-weak tables use a special GC kind with a custom mark procedure.
60 When items are added weakly into table, a disappearing link is
61 registered to their locations. If the referent is collected, then
62 that link will be zeroed out.
63
64 An entry in the table consists of the key and the value, together
65 with the hash code of the key. We munge hash codes so that they are
66 never 0. In this way we can detect removed entries (key of zero but
67 nonzero hash code), and can then reshuffle elements as needed to
68 maintain the robin hood ordering.
69
70 Compared to buckets-and-chains hash tables, open addressing has the
71 advantage that it is very cache-friendly. It also uses less memory.
72
73 Implementation-wise, there are two things to note.
74
75 1. We assume that hash codes are evenly distributed across the
76 range of unsigned longs. The actual hash code stored in the
77 entry is left-shifted by 1 bit (losing 1 bit of hash precision),
78 and then or'd with 1. In this way we ensure that the hash field
79 of an occupied entry is nonzero. To map to an index, we
80 right-shift the hash by one, divide by the size, and take the
81 remainder.
82
83 2. Since the weak references are stored in an atomic region with
84 disappearing links, they need to be accessed with the GC alloc
85 lock. `copy_weak_entry' will do that for you. The hash code
86 itself can be read outside the lock, though.
87 */
88
89
90 typedef struct {
91 unsigned long hash;
92 scm_t_bits key;
93 scm_t_bits value;
94 } scm_t_weak_entry;
95
96
97 struct weak_entry_data {
98 scm_t_weak_entry *in;
99 scm_t_weak_entry *out;
100 };
101
102 static void*
103 do_copy_weak_entry (void *data)
104 {
105 struct weak_entry_data *e = data;
106
107 e->out->hash = e->in->hash;
108 e->out->key = e->in->key;
109 e->out->value = e->in->value;
110
111 return NULL;
112 }
113
114 static void
115 copy_weak_entry (scm_t_weak_entry *src, scm_t_weak_entry *dst)
116 {
117 struct weak_entry_data data;
118
119 data.in = src;
120 data.out = dst;
121
122 GC_call_with_alloc_lock (do_copy_weak_entry, &data);
123 }
124
125 static void
126 register_disappearing_links (scm_t_weak_entry *entry,
127 SCM k, SCM v,
128 scm_t_weak_table_kind kind)
129 {
130 if (SCM_UNPACK (k) && SCM_HEAP_OBJECT_P (k)
131 && (kind == SCM_WEAK_TABLE_KIND_KEY
132 || kind == SCM_WEAK_TABLE_KIND_BOTH))
133 SCM_I_REGISTER_DISAPPEARING_LINK ((GC_PTR) &entry->key,
134 (GC_PTR) SCM2PTR (k));
135
136 if (SCM_UNPACK (v) && SCM_HEAP_OBJECT_P (v)
137 && (kind == SCM_WEAK_TABLE_KIND_VALUE
138 || kind == SCM_WEAK_TABLE_KIND_BOTH))
139 SCM_I_REGISTER_DISAPPEARING_LINK ((GC_PTR) &entry->value,
140 (GC_PTR) SCM2PTR (v));
141 }
142
143 static void
144 unregister_disappearing_links (scm_t_weak_entry *entry,
145 scm_t_weak_table_kind kind)
146 {
147 if (kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
148 GC_unregister_disappearing_link ((GC_PTR) &entry->key);
149
150 if (kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
151 GC_unregister_disappearing_link ((GC_PTR) &entry->value);
152 }
153
154 static void
155 move_disappearing_links (scm_t_weak_entry *from, scm_t_weak_entry *to,
156 SCM key, SCM value, scm_t_weak_table_kind kind)
157 {
158 if ((kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
159 && SCM_HEAP_OBJECT_P (key))
160 {
161 #ifdef HAVE_GC_MOVE_DISAPPEARING_LINK
162 GC_move_disappearing_link ((GC_PTR) &from->key, (GC_PTR) &to->key);
163 #else
164 GC_unregister_disappearing_link (&from->key);
165 SCM_I_REGISTER_DISAPPEARING_LINK (&to->key, SCM2PTR (key));
166 #endif
167 }
168
169 if ((kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
170 && SCM_HEAP_OBJECT_P (value))
171 {
172 #ifdef HAVE_GC_MOVE_DISAPPEARING_LINK
173 GC_move_disappearing_link ((GC_PTR) &from->value, (GC_PTR) &to->value);
174 #else
175 GC_unregister_disappearing_link (&from->value);
176 SCM_I_REGISTER_DISAPPEARING_LINK (&to->value, SCM2PTR (value));
177 #endif
178 }
179 }
180
181 static void
182 move_weak_entry (scm_t_weak_entry *from, scm_t_weak_entry *to,
183 scm_t_weak_table_kind kind)
184 {
185 if (from->hash)
186 {
187 scm_t_weak_entry copy;
188
189 copy_weak_entry (from, &copy);
190 to->hash = copy.hash;
191 to->key = copy.key;
192 to->value = copy.value;
193
194 move_disappearing_links (from, to,
195 SCM_PACK (copy.key), SCM_PACK (copy.value),
196 kind);
197 }
198 else
199 {
200 to->hash = 0;
201 to->key = 0;
202 to->value = 0;
203 }
204 }
205
206
207 typedef struct {
208 scm_t_weak_entry *entries; /* the data */
209 scm_i_pthread_mutex_t lock; /* the lock */
210 scm_t_weak_table_kind kind; /* what kind of table it is */
211 unsigned long size; /* total number of slots. */
212 unsigned long n_items; /* number of items in table */
213 unsigned long lower; /* when to shrink */
214 unsigned long upper; /* when to grow */
215 int size_index; /* index into hashtable_size */
216 int min_size_index; /* minimum size_index */
217 } scm_t_weak_table;
218
219
220 #define SCM_WEAK_TABLE_P(x) (SCM_HAS_TYP7 (x, scm_tc7_weak_table))
221 #define SCM_VALIDATE_WEAK_TABLE(pos, arg) \
222 SCM_MAKE_VALIDATE_MSG (pos, arg, WEAK_TABLE_P, "weak-table")
223 #define SCM_WEAK_TABLE(x) ((scm_t_weak_table *) SCM_CELL_WORD_1 (x))
224
225
226 static unsigned long
227 hash_to_index (unsigned long hash, unsigned long size)
228 {
229 return (hash >> 1) % size;
230 }
231
232 static unsigned long
233 entry_distance (unsigned long hash, unsigned long k, unsigned long size)
234 {
235 unsigned long origin = hash_to_index (hash, size);
236
237 if (k >= origin)
238 return k - origin;
239 else
240 /* The other key was displaced and wrapped around. */
241 return size - origin + k;
242 }
243
244 static void
245 rob_from_rich (scm_t_weak_table *table, unsigned long k)
246 {
247 unsigned long empty, size;
248
249 size = table->size;
250
251 /* If we are to free up slot K in the table, we need room to do so. */
252 assert (table->n_items < size);
253
254 empty = k;
255 do
256 empty = (empty + 1) % size;
257 while (table->entries[empty].hash);
258
259 do
260 {
261 unsigned long last = empty ? (empty - 1) : (size - 1);
262 move_weak_entry (&table->entries[last], &table->entries[empty],
263 table->kind);
264 empty = last;
265 }
266 while (empty != k);
267
268 table->entries[empty].hash = 0;
269 table->entries[empty].key = 0;
270 table->entries[empty].value = 0;
271 }
272
273 static void
274 give_to_poor (scm_t_weak_table *table, unsigned long k)
275 {
276 /* Slot K was just freed up; possibly shuffle others down. */
277 unsigned long size = table->size;
278
279 while (1)
280 {
281 unsigned long next = (k + 1) % size;
282 unsigned long hash;
283 scm_t_weak_entry copy;
284
285 hash = table->entries[next].hash;
286
287 if (!hash || hash_to_index (hash, size) == next)
288 break;
289
290 copy_weak_entry (&table->entries[next], &copy);
291
292 if (!copy.key || !copy.value)
293 /* Lost weak reference. */
294 {
295 give_to_poor (table, next);
296 table->n_items--;
297 continue;
298 }
299
300 move_weak_entry (&table->entries[next], &table->entries[k],
301 table->kind);
302
303 k = next;
304 }
305
306 /* We have shuffled down any entries that should be shuffled down; now
307 free the end. */
308 table->entries[k].hash = 0;
309 table->entries[k].key = 0;
310 table->entries[k].value = 0;
311 }
312
313
314 \f
315
316 /* The GC "kinds" for singly-weak tables. */
317 static int weak_key_gc_kind;
318 static int weak_value_gc_kind;
319
320 static struct GC_ms_entry *
321 mark_weak_key_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
322 struct GC_ms_entry *mark_stack_limit, GC_word env)
323 {
324 scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
325 unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
326
327 for (k = 0; k < size; k++)
328 if (entries[k].hash && entries[k].key)
329 {
330 SCM value = SCM_PACK (entries[k].value);
331 mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (value),
332 mark_stack_ptr, mark_stack_limit,
333 NULL);
334 }
335
336 return mark_stack_ptr;
337 }
338
339 static struct GC_ms_entry *
340 mark_weak_value_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
341 struct GC_ms_entry *mark_stack_limit, GC_word env)
342 {
343 scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
344 unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
345
346 for (k = 0; k < size; k++)
347 if (entries[k].hash && entries[k].value)
348 {
349 SCM key = SCM_PACK (entries[k].key);
350 mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (key),
351 mark_stack_ptr, mark_stack_limit,
352 NULL);
353 }
354
355 return mark_stack_ptr;
356 }
357
358 static scm_t_weak_entry *
359 allocate_entries (unsigned long size, scm_t_weak_table_kind kind)
360 {
361 scm_t_weak_entry *ret;
362 size_t bytes = size * sizeof (*ret);
363
364 switch (kind)
365 {
366 case SCM_WEAK_TABLE_KIND_KEY:
367 ret = GC_generic_malloc (bytes, weak_key_gc_kind);
368 break;
369 case SCM_WEAK_TABLE_KIND_VALUE:
370 ret = GC_generic_malloc (bytes, weak_value_gc_kind);
371 break;
372 case SCM_WEAK_TABLE_KIND_BOTH:
373 ret = scm_gc_malloc_pointerless (bytes, "weak-table");
374 break;
375 default:
376 abort ();
377 }
378
379 memset (ret, 0, bytes);
380
381 return ret;
382 }
383
384 \f
385
386 /* Growing or shrinking is triggered when the load factor
387 *
388 * L = N / S (N: number of items in table, S: bucket vector length)
389 *
390 * passes an upper limit of 0.9 or a lower limit of 0.2.
391 *
392 * The implementation stores the upper and lower number of items which
393 * trigger a resize in the hashtable object.
394 *
395 * Possible hash table sizes (primes) are stored in the array
396 * hashtable_size.
397 */
398
399 static unsigned long hashtable_size[] = {
400 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
401 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041, 28762081,
402 57524111, 115048217, 230096423
403 };
404
405 #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
406
407 static int
408 compute_size_index (scm_t_weak_table *table)
409 {
410 int i = table->size_index;
411
412 if (table->n_items < table->lower)
413 {
414 /* rehashing is not triggered when i <= min_size */
415 do
416 --i;
417 while (i > table->min_size_index
418 && table->n_items < hashtable_size[i] / 5);
419 }
420 else if (table->n_items > table->upper)
421 {
422 ++i;
423 if (i >= HASHTABLE_SIZE_N)
424 /* The biggest size currently is 230096423, which for a 32-bit
425 machine will occupy 2.3GB of memory at a load of 80%. There
426 is probably something better to do here, but if you have a
427 weak map of that size, you are hosed in any case. */
428 abort ();
429 }
430
431 return i;
432 }
433
434 static int
435 is_acceptable_size_index (scm_t_weak_table *table, int size_index)
436 {
437 int computed = compute_size_index (table);
438
439 if (size_index == computed)
440 /* We were going to grow or shrink, and allocating the new vector
441 didn't change the target size. */
442 return 1;
443
444 if (size_index == computed + 1)
445 {
446 /* We were going to enlarge the table, but allocating the new
447 vector finalized some objects, making an enlargement
448 unnecessary. It might still be a good idea to use the larger
449 table, though. (This branch also gets hit if, while allocating
450 the vector, some other thread was actively removing items from
451 the table. That is less likely, though.) */
452 unsigned long new_lower = hashtable_size[size_index] / 5;
453
454 return table->size > new_lower;
455 }
456
457 if (size_index == computed - 1)
458 {
459 /* We were going to shrink the table, but when we dropped the lock
460 to allocate the new vector, some other thread added elements to
461 the table. */
462 return 0;
463 }
464
465 /* The computed size differs from our newly allocated size by more
466 than one size index -- recalculate. */
467 return 0;
468 }
469
470 static void
471 resize_table (scm_t_weak_table *table)
472 {
473 scm_t_weak_entry *old_entries, *new_entries;
474 int new_size_index;
475 unsigned long old_size, new_size, old_k;
476
477 do
478 {
479 new_size_index = compute_size_index (table);
480 if (new_size_index == table->size_index)
481 return;
482 new_size = hashtable_size[new_size_index];
483 scm_i_pthread_mutex_unlock (&table->lock);
484 /* Allocating memory might cause finalizers to run, which could
485 run anything, so drop our lock to avoid deadlocks. */
486 new_entries = allocate_entries (new_size, table->kind);
487 scm_i_pthread_mutex_unlock (&table->lock);
488 }
489 while (!is_acceptable_size_index (table, new_size_index));
490
491 old_entries = table->entries;
492 old_size = table->size;
493
494 table->size_index = new_size_index;
495 table->size = new_size;
496 if (new_size_index <= table->min_size_index)
497 table->lower = 0;
498 else
499 table->lower = new_size / 5;
500 table->upper = 9 * new_size / 10;
501 table->n_items = 0;
502 table->entries = new_entries;
503
504 for (old_k = 0; old_k < old_size; old_k++)
505 {
506 scm_t_weak_entry copy;
507 unsigned long new_k, distance;
508
509 if (!old_entries[old_k].hash)
510 continue;
511
512 copy_weak_entry (&old_entries[old_k], &copy);
513
514 if (!copy.key || !copy.value)
515 continue;
516
517 new_k = hash_to_index (copy.hash, new_size);
518
519 for (distance = 0; ; distance++, new_k = (new_k + 1) % new_size)
520 {
521 unsigned long other_hash = new_entries[new_k].hash;
522
523 if (!other_hash)
524 /* Found an empty entry. */
525 break;
526
527 /* Displace the entry if our distance is less, otherwise keep
528 looking. */
529 if (entry_distance (other_hash, new_k, new_size) < distance)
530 {
531 rob_from_rich (table, new_k);
532 break;
533 }
534 }
535
536 table->n_items++;
537 new_entries[new_k].hash = copy.hash;
538 new_entries[new_k].key = copy.key;
539 new_entries[new_k].value = copy.value;
540
541 register_disappearing_links (&new_entries[new_k],
542 SCM_PACK (copy.key), SCM_PACK (copy.value),
543 table->kind);
544 }
545 }
546
547 /* Run after GC via do_vacuum_weak_table, this function runs over the
548 whole table, removing lost weak references, reshuffling the table as it
549 goes. It might resize the table if it reaps enough entries. */
550 static void
551 vacuum_weak_table (scm_t_weak_table *table)
552 {
553 scm_t_weak_entry *entries = table->entries;
554 unsigned long size = table->size;
555 unsigned long k;
556
557 for (k = 0; k < size; k++)
558 {
559 unsigned long hash = entries[k].hash;
560
561 if (hash)
562 {
563 scm_t_weak_entry copy;
564
565 copy_weak_entry (&entries[k], &copy);
566
567 if (!copy.key || !copy.value)
568 /* Lost weak reference; reshuffle. */
569 {
570 give_to_poor (table, k);
571 table->n_items--;
572 }
573 }
574 }
575
576 if (table->n_items < table->lower)
577 resize_table (table);
578 }
579
580
581 \f
582
583 static SCM
584 weak_table_ref (scm_t_weak_table *table, unsigned long hash,
585 scm_t_table_predicate_fn pred, void *closure,
586 SCM dflt)
587 {
588 unsigned long k, distance, size;
589 scm_t_weak_entry *entries;
590
591 size = table->size;
592 entries = table->entries;
593
594 hash = (hash << 1) | 0x1;
595 k = hash_to_index (hash, size);
596
597 for (distance = 0; distance < size; distance++, k = (k + 1) % size)
598 {
599 unsigned long other_hash;
600
601 retry:
602 other_hash = entries[k].hash;
603
604 if (!other_hash)
605 /* Not found. */
606 return dflt;
607
608 if (hash == other_hash)
609 {
610 scm_t_weak_entry copy;
611
612 copy_weak_entry (&entries[k], &copy);
613
614 if (!copy.key || !copy.value)
615 /* Lost weak reference; reshuffle. */
616 {
617 give_to_poor (table, k);
618 table->n_items--;
619 goto retry;
620 }
621
622 if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
623 /* Found. */
624 return SCM_PACK (copy.value);
625 }
626
627 /* If the entry's distance is less, our key is not in the table. */
628 if (entry_distance (other_hash, k, size) < distance)
629 return dflt;
630 }
631
632 /* If we got here, then we were unfortunate enough to loop through the
633 whole table. Shouldn't happen, but hey. */
634 return dflt;
635 }
636
637
638 static void
639 weak_table_put_x (scm_t_weak_table *table, unsigned long hash,
640 scm_t_table_predicate_fn pred, void *closure,
641 SCM key, SCM value)
642 {
643 unsigned long k, distance, size;
644 scm_t_weak_entry *entries;
645
646 size = table->size;
647 entries = table->entries;
648
649 hash = (hash << 1) | 0x1;
650 k = hash_to_index (hash, size);
651
652 for (distance = 0; ; distance++, k = (k + 1) % size)
653 {
654 unsigned long other_hash;
655
656 retry:
657 other_hash = entries[k].hash;
658
659 if (!other_hash)
660 /* Found an empty entry. */
661 break;
662
663 if (other_hash == hash)
664 {
665 scm_t_weak_entry copy;
666
667 copy_weak_entry (&entries[k], &copy);
668
669 if (!copy.key || !copy.value)
670 /* Lost weak reference; reshuffle. */
671 {
672 give_to_poor (table, k);
673 table->n_items--;
674 goto retry;
675 }
676
677 if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
678 /* Found an entry with this key. */
679 break;
680 }
681
682 if (table->n_items > table->upper)
683 /* Full table, time to resize. */
684 {
685 resize_table (table);
686 return weak_table_put_x (table, hash >> 1, pred, closure, key, value);
687 }
688
689 /* Displace the entry if our distance is less, otherwise keep
690 looking. */
691 if (entry_distance (other_hash, k, size) < distance)
692 {
693 rob_from_rich (table, k);
694 break;
695 }
696 }
697
698 if (entries[k].hash)
699 unregister_disappearing_links (&entries[k], table->kind);
700 else
701 table->n_items++;
702
703 entries[k].hash = hash;
704 entries[k].key = SCM_UNPACK (key);
705 entries[k].value = SCM_UNPACK (value);
706
707 register_disappearing_links (&entries[k], key, value, table->kind);
708 }
709
710
711 static void
712 weak_table_remove_x (scm_t_weak_table *table, unsigned long hash,
713 scm_t_table_predicate_fn pred, void *closure)
714 {
715 unsigned long k, distance, size;
716 scm_t_weak_entry *entries;
717
718 size = table->size;
719 entries = table->entries;
720
721 hash = (hash << 1) | 0x1;
722 k = hash_to_index (hash, size);
723
724 for (distance = 0; distance < size; distance++, k = (k + 1) % size)
725 {
726 unsigned long other_hash;
727
728 retry:
729 other_hash = entries[k].hash;
730
731 if (!other_hash)
732 /* Not found. */
733 return;
734
735 if (other_hash == hash)
736 {
737 scm_t_weak_entry copy;
738
739 copy_weak_entry (&entries[k], &copy);
740
741 if (!copy.key || !copy.value)
742 /* Lost weak reference; reshuffle. */
743 {
744 give_to_poor (table, k);
745 table->n_items--;
746 goto retry;
747 }
748
749 if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
750 /* Found an entry with this key. */
751 {
752 entries[k].hash = 0;
753 entries[k].key = 0;
754 entries[k].value = 0;
755
756 unregister_disappearing_links (&entries[k], table->kind);
757
758 if (--table->n_items < table->lower)
759 resize_table (table);
760 else
761 give_to_poor (table, k);
762
763 return;
764 }
765 }
766
767 /* If the entry's distance is less, our key is not in the table. */
768 if (entry_distance (other_hash, k, size) < distance)
769 return;
770 }
771 }
772
773
774 \f
775
776 static void
777 lock_weak_table (scm_t_weak_table *table)
778 {
779 scm_i_pthread_mutex_lock (&table->lock);
780 }
781
782 static void
783 unlock_weak_table (scm_t_weak_table *table)
784 {
785 scm_i_pthread_mutex_unlock (&table->lock);
786 }
787
788 /* A weak table of weak tables, for use in the pthread_atfork handler. */
789 static SCM all_weak_tables = SCM_BOOL_F;
790
791 #if SCM_USE_PTHREAD_THREADS
792
793 static void
794 lock_all_weak_tables (void)
795 {
796 scm_t_weak_table *s;
797 scm_t_weak_entry *entries;
798 unsigned long k, size;
799 scm_t_weak_entry copy;
800
801 s = SCM_WEAK_TABLE (all_weak_tables);
802 lock_weak_table (s);
803 size = s->size;
804 entries = s->entries;
805
806 for (k = 0; k < size; k++)
807 if (entries[k].hash)
808 {
809 copy_weak_entry (&entries[k], &copy);
810 if (copy.key)
811 lock_weak_table (SCM_WEAK_TABLE (SCM_PACK (copy.key)));
812 }
813 }
814
815 static void
816 unlock_all_weak_tables (void)
817 {
818 scm_t_weak_table *s;
819 scm_t_weak_entry *entries;
820 unsigned long k, size;
821 scm_t_weak_entry copy;
822
823 s = SCM_WEAK_TABLE (all_weak_tables);
824 size = s->size;
825 entries = s->entries;
826
827 for (k = 0; k < size; k++)
828 if (entries[k].hash)
829 {
830 copy_weak_entry (&entries[k], &copy);
831 if (copy.key)
832 unlock_weak_table (SCM_WEAK_TABLE (SCM_PACK (copy.key)));
833 }
834
835 unlock_weak_table (s);
836 }
837
838 #endif /* SCM_USE_PTHREAD_THREADS */
839
840
841 \f
842
843 static SCM
844 make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
845 {
846 scm_t_weak_table *table;
847 SCM ret;
848
849 int i = 0, n = k ? k : 31;
850 while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
851 ++i;
852 n = hashtable_size[i];
853
854 table = scm_gc_malloc (sizeof (*table), "weak-table");
855 table->entries = allocate_entries (n, kind);
856 table->kind = kind;
857 table->n_items = 0;
858 table->size = n;
859 table->lower = 0;
860 table->upper = 9 * n / 10;
861 table->size_index = i;
862 table->min_size_index = i;
863 scm_i_pthread_mutex_init (&table->lock, NULL);
864
865 ret = scm_cell (scm_tc7_weak_table, (scm_t_bits)table);
866
867 if (scm_is_true (all_weak_tables))
868 scm_weak_table_putq_x (all_weak_tables, ret, SCM_BOOL_T);
869
870 return ret;
871 }
872
873 void
874 scm_i_weak_table_print (SCM exp, SCM port, scm_print_state *pstate)
875 {
876 scm_puts_unlocked ("#<", port);
877 scm_puts_unlocked ("weak-table ", port);
878 scm_uintprint (SCM_WEAK_TABLE (exp)->n_items, 10, port);
879 scm_putc_unlocked ('/', port);
880 scm_uintprint (SCM_WEAK_TABLE (exp)->size, 10, port);
881 scm_puts_unlocked (">", port);
882 }
883
884 static void
885 do_vacuum_weak_table (SCM table)
886 {
887 scm_t_weak_table *t;
888
889 t = SCM_WEAK_TABLE (table);
890
891 if (scm_i_pthread_mutex_trylock (&t->lock) == 0)
892 {
893 vacuum_weak_table (t);
894 unlock_weak_table (t);
895 }
896
897 return;
898 }
899
900 /* The before-gc C hook only runs if GC_table_start_callback is available,
901 so if not, fall back on a finalizer-based implementation. */
902 static int
903 weak_gc_callback (void **weak)
904 {
905 void *val = weak[0];
906 void (*callback) (SCM) = weak[1];
907
908 if (!val)
909 return 0;
910
911 callback (SCM_PACK_POINTER (val));
912
913 return 1;
914 }
915
916 #ifdef HAVE_GC_TABLE_START_CALLBACK
917 static void*
918 weak_gc_hook (void *hook_data, void *fn_data, void *data)
919 {
920 if (!weak_gc_callback (fn_data))
921 scm_c_hook_remove (&scm_before_gc_c_hook, weak_gc_hook, fn_data);
922
923 return NULL;
924 }
925 #else
926 static void
927 weak_gc_finalizer (void *ptr, void *data)
928 {
929 if (weak_gc_callback (ptr))
930 GC_REGISTER_FINALIZER_NO_ORDER (ptr, weak_gc_finalizer, data, NULL, NULL);
931 }
932 #endif
933
934 static void
935 scm_c_register_weak_gc_callback (SCM obj, void (*callback) (SCM))
936 {
937 void **weak = GC_MALLOC_ATOMIC (sizeof (void*) * 2);
938
939 weak[0] = SCM_UNPACK_POINTER (obj);
940 weak[1] = (void*)callback;
941 GC_GENERAL_REGISTER_DISAPPEARING_LINK (weak, SCM2PTR (obj));
942
943 #ifdef HAVE_GC_TABLE_START_CALLBACK
944 scm_c_hook_add (&scm_after_gc_c_hook, weak_gc_hook, weak, 0);
945 #else
946 GC_REGISTER_FINALIZER_NO_ORDER (weak, weak_gc_finalizer, NULL, NULL, NULL);
947 #endif
948 }
949
950 SCM
951 scm_c_make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
952 {
953 SCM ret;
954
955 ret = make_weak_table (k, kind);
956
957 scm_c_register_weak_gc_callback (ret, do_vacuum_weak_table);
958
959 return ret;
960 }
961
962 SCM
963 scm_weak_table_p (SCM obj)
964 {
965 return scm_from_bool (SCM_WEAK_TABLE_P (obj));
966 }
967
968 SCM
969 scm_c_weak_table_ref (SCM table, unsigned long raw_hash,
970 scm_t_table_predicate_fn pred,
971 void *closure, SCM dflt)
972 #define FUNC_NAME "weak-table-ref"
973 {
974 SCM ret;
975 scm_t_weak_table *t;
976
977 SCM_VALIDATE_WEAK_TABLE (1, table);
978
979 t = SCM_WEAK_TABLE (table);
980
981 lock_weak_table (t);
982
983 ret = weak_table_ref (t, raw_hash, pred, closure, dflt);
984
985 unlock_weak_table (t);
986
987 return ret;
988 }
989 #undef FUNC_NAME
990
991 void
992 scm_c_weak_table_put_x (SCM table, unsigned long raw_hash,
993 scm_t_table_predicate_fn pred,
994 void *closure, SCM key, SCM value)
995 #define FUNC_NAME "weak-table-put!"
996 {
997 scm_t_weak_table *t;
998
999 SCM_VALIDATE_WEAK_TABLE (1, table);
1000
1001 t = SCM_WEAK_TABLE (table);
1002
1003 lock_weak_table (t);
1004
1005 weak_table_put_x (t, raw_hash, pred, closure, key, value);
1006
1007 unlock_weak_table (t);
1008 }
1009 #undef FUNC_NAME
1010
1011 void
1012 scm_c_weak_table_remove_x (SCM table, unsigned long raw_hash,
1013 scm_t_table_predicate_fn pred,
1014 void *closure)
1015 #define FUNC_NAME "weak-table-remove!"
1016 {
1017 scm_t_weak_table *t;
1018
1019 SCM_VALIDATE_WEAK_TABLE (1, table);
1020
1021 t = SCM_WEAK_TABLE (table);
1022
1023 lock_weak_table (t);
1024
1025 weak_table_remove_x (t, raw_hash, pred, closure);
1026
1027 unlock_weak_table (t);
1028 }
1029 #undef FUNC_NAME
1030
1031 static int
1032 assq_predicate (SCM x, SCM y, void *closure)
1033 {
1034 return scm_is_eq (x, SCM_PACK_POINTER (closure));
1035 }
1036
1037 SCM
1038 scm_weak_table_refq (SCM table, SCM key, SCM dflt)
1039 {
1040 if (SCM_UNBNDP (dflt))
1041 dflt = SCM_BOOL_F;
1042
1043 return scm_c_weak_table_ref (table, scm_ihashq (key, -1),
1044 assq_predicate, SCM_UNPACK_POINTER (key),
1045 dflt);
1046 }
1047
1048 SCM
1049 scm_weak_table_putq_x (SCM table, SCM key, SCM value)
1050 {
1051 scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
1052 assq_predicate, SCM_UNPACK_POINTER (key),
1053 key, value);
1054 return SCM_UNSPECIFIED;
1055 }
1056
1057 SCM
1058 scm_weak_table_remq_x (SCM table, SCM key)
1059 {
1060 scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
1061 assq_predicate, SCM_UNPACK_POINTER (key));
1062 return SCM_UNSPECIFIED;
1063 }
1064
1065 SCM
1066 scm_weak_table_clear_x (SCM table)
1067 #define FUNC_NAME "weak-table-clear!"
1068 {
1069 scm_t_weak_table *t;
1070
1071 SCM_VALIDATE_WEAK_TABLE (1, table);
1072
1073 t = SCM_WEAK_TABLE (table);
1074
1075 lock_weak_table (t);
1076
1077 memset (t->entries, 0, sizeof (scm_t_weak_entry) * t->size);
1078 t->n_items = 0;
1079
1080 unlock_weak_table (t);
1081
1082 return SCM_UNSPECIFIED;
1083 }
1084 #undef FUNC_NAME
1085
1086 SCM
1087 scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
1088 SCM init, SCM table)
1089 {
1090 scm_t_weak_table *t;
1091 scm_t_weak_entry *entries;
1092 unsigned long k, size;
1093
1094 t = SCM_WEAK_TABLE (table);
1095
1096 lock_weak_table (t);
1097
1098 size = t->size;
1099 entries = t->entries;
1100
1101 for (k = 0; k < size; k++)
1102 {
1103 if (entries[k].hash)
1104 {
1105 scm_t_weak_entry copy;
1106
1107 copy_weak_entry (&entries[k], &copy);
1108
1109 if (copy.key && copy.value)
1110 {
1111 /* Release table lock while we call the function. */
1112 unlock_weak_table (t);
1113 init = proc (closure,
1114 SCM_PACK (copy.key), SCM_PACK (copy.value),
1115 init);
1116 lock_weak_table (t);
1117 }
1118 }
1119 }
1120
1121 unlock_weak_table (t);
1122
1123 return init;
1124 }
1125
1126 static SCM
1127 fold_trampoline (void *closure, SCM k, SCM v, SCM init)
1128 {
1129 return scm_call_3 (SCM_PACK_POINTER (closure), k, v, init);
1130 }
1131
1132 SCM
1133 scm_weak_table_fold (SCM proc, SCM init, SCM table)
1134 #define FUNC_NAME "weak-table-fold"
1135 {
1136 SCM_VALIDATE_WEAK_TABLE (3, table);
1137 SCM_VALIDATE_PROC (1, proc);
1138
1139 return scm_c_weak_table_fold (fold_trampoline, SCM_UNPACK_POINTER (proc), init, table);
1140 }
1141 #undef FUNC_NAME
1142
1143 static SCM
1144 for_each_trampoline (void *closure, SCM k, SCM v, SCM seed)
1145 {
1146 scm_call_2 (SCM_PACK_POINTER (closure), k, v);
1147 return seed;
1148 }
1149
1150 SCM
1151 scm_weak_table_for_each (SCM proc, SCM table)
1152 #define FUNC_NAME "weak-table-for-each"
1153 {
1154 SCM_VALIDATE_WEAK_TABLE (2, table);
1155 SCM_VALIDATE_PROC (1, proc);
1156
1157 scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
1158
1159 return SCM_UNSPECIFIED;
1160 }
1161 #undef FUNC_NAME
1162
1163 static SCM
1164 map_trampoline (void *closure, SCM k, SCM v, SCM seed)
1165 {
1166 return scm_cons (scm_call_2 (SCM_PACK_POINTER (closure), k, v), seed);
1167 }
1168
1169 SCM
1170 scm_weak_table_map_to_list (SCM proc, SCM table)
1171 #define FUNC_NAME "weak-table-map->list"
1172 {
1173 SCM_VALIDATE_WEAK_TABLE (2, table);
1174 SCM_VALIDATE_PROC (1, proc);
1175
1176 return scm_c_weak_table_fold (map_trampoline, SCM_UNPACK_POINTER (proc), SCM_EOL, table);
1177 }
1178 #undef FUNC_NAME
1179
1180
1181 \f
1182
1183 /* Legacy interface. */
1184
1185 SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0,
1186 (SCM n),
1187 "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
1188 "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
1189 "Return a weak hash table with @var{size} buckets.\n"
1190 "\n"
1191 "You can modify weak hash tables in exactly the same way you\n"
1192 "would modify regular hash tables. (@pxref{Hash Tables})")
1193 #define FUNC_NAME s_scm_make_weak_key_hash_table
1194 {
1195 return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
1196 SCM_WEAK_TABLE_KIND_KEY);
1197 }
1198 #undef FUNC_NAME
1199
1200
1201 SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 0, 1, 0,
1202 (SCM n),
1203 "Return a hash table with weak values with @var{size} buckets.\n"
1204 "(@pxref{Hash Tables})")
1205 #define FUNC_NAME s_scm_make_weak_value_hash_table
1206 {
1207 return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
1208 SCM_WEAK_TABLE_KIND_VALUE);
1209 }
1210 #undef FUNC_NAME
1211
1212
1213 SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0,
1214 (SCM n),
1215 "Return a hash table with weak keys and values with @var{size}\n"
1216 "buckets. (@pxref{Hash Tables})")
1217 #define FUNC_NAME s_scm_make_doubly_weak_hash_table
1218 {
1219 return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
1220 SCM_WEAK_TABLE_KIND_BOTH);
1221 }
1222 #undef FUNC_NAME
1223
1224
1225 SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
1226 (SCM obj),
1227 "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n"
1228 "@deffnx {Scheme Procedure} doubly-weak-hash-table? obj\n"
1229 "Return @code{#t} if @var{obj} is the specified weak hash\n"
1230 "table. Note that a doubly weak hash table is neither a weak key\n"
1231 "nor a weak value hash table.")
1232 #define FUNC_NAME s_scm_weak_key_hash_table_p
1233 {
1234 return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
1235 SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_KEY);
1236 }
1237 #undef FUNC_NAME
1238
1239
1240 SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
1241 (SCM obj),
1242 "Return @code{#t} if @var{obj} is a weak value hash table.")
1243 #define FUNC_NAME s_scm_weak_value_hash_table_p
1244 {
1245 return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
1246 SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_VALUE);
1247 }
1248 #undef FUNC_NAME
1249
1250
1251 SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
1252 (SCM obj),
1253 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
1254 #define FUNC_NAME s_scm_doubly_weak_hash_table_p
1255 {
1256 return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
1257 SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_BOTH);
1258 }
1259 #undef FUNC_NAME
1260
1261
1262
1263 \f
1264
1265 void
1266 scm_weak_table_prehistory (void)
1267 {
1268 weak_key_gc_kind =
1269 GC_new_kind (GC_new_free_list (),
1270 GC_MAKE_PROC (GC_new_proc (mark_weak_key_table), 0),
1271 0, 0);
1272 weak_value_gc_kind =
1273 GC_new_kind (GC_new_free_list (),
1274 GC_MAKE_PROC (GC_new_proc (mark_weak_value_table), 0),
1275 0, 0);
1276
1277 #if SCM_USE_PTHREAD_THREADS
1278 all_weak_tables = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
1279 pthread_atfork (lock_all_weak_tables, unlock_all_weak_tables,
1280 unlock_all_weak_tables);
1281 #endif
1282 }
1283
1284 void
1285 scm_init_weak_table ()
1286 {
1287 #include "libguile/weak-table.x"
1288 }
1289
1290 /*
1291 Local Variables:
1292 c-file-style: "gnu"
1293 End:
1294 */