weak-table.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Copyright 2011-2014,2017-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <assert.h>
  19. #include "alist.h"
  20. #include "bdw-gc.h"
  21. #include "eval.h"
  22. #include "finalizers.h"
  23. #include "gsubr.h"
  24. #include "hash.h"
  25. #include "numbers.h"
  26. #include "pairs.h"
  27. #include "ports.h"
  28. #include "procs.h"
  29. #include "threads.h"
  30. #include "weak-list.h"
  31. #include "weak-table.h"
  32. #include <gc/gc_typed.h>
  33. /* Weak Tables
  34. This file implements weak hash tables. Weak hash tables are
  35. generally used when you want to augment some object with additional
  36. data, but when you don't have space to store the data in the object.
  37. For example, procedure properties are implemented with weak tables.
  38. This is a normal bucket-and-chain hash table, except that the chain
  39. entries are allocated in such a way that the GC doesn't trace the
  40. weak values. For doubly-weak tables, this means that the entries are
  41. allocated as an "atomic" piece of memory. Key-weak and value-weak
  42. tables use a special GC kind with a custom mark procedure. When
  43. items are added weakly into table, a disappearing link is registered
  44. to their locations. If the referent is collected, then that link
  45. will be zeroed out.
  46. An entry in the table consists of the key and the value, together
  47. with the hash code of the key.
  48. Note that since the weak references are stored in an atomic region
  49. with disappearing links, they need to be accessed with the GC alloc
  50. lock. `read_weak_entry' will do that for you. The hash code itself
  51. can be read outside the lock, though.
  52. */
  53. typedef struct scm_weak_entry scm_t_weak_entry;
  54. struct scm_weak_entry {
  55. unsigned long hash;
  56. scm_t_weak_entry *next;
  57. scm_t_bits key;
  58. scm_t_bits value;
  59. };
  60. struct weak_entry_data {
  61. scm_t_weak_entry *entry;
  62. scm_t_bits key;
  63. scm_t_bits value;
  64. };
  65. static void*
  66. do_read_weak_entry (void *data)
  67. {
  68. struct weak_entry_data *e = data;
  69. e->key = e->entry->key;
  70. e->value = e->entry->value;
  71. return NULL;
  72. }
  73. static void
  74. read_weak_entry (scm_t_weak_entry *entry, scm_t_bits *key, scm_t_bits *value)
  75. {
  76. struct weak_entry_data data;
  77. data.entry = entry;
  78. GC_call_with_alloc_lock (do_read_weak_entry, &data);
  79. *key = data.key;
  80. *value = data.value;
  81. }
  82. static void
  83. register_disappearing_links (scm_t_weak_entry *entry,
  84. SCM k, SCM v,
  85. scm_t_weak_table_kind kind)
  86. {
  87. if (SCM_UNPACK (k) && SCM_HEAP_OBJECT_P (k)
  88. && (kind == SCM_WEAK_TABLE_KIND_KEY
  89. || kind == SCM_WEAK_TABLE_KIND_BOTH))
  90. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &entry->key,
  91. SCM2PTR (k));
  92. if (SCM_UNPACK (v) && SCM_HEAP_OBJECT_P (v)
  93. && (kind == SCM_WEAK_TABLE_KIND_VALUE
  94. || kind == SCM_WEAK_TABLE_KIND_BOTH))
  95. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &entry->value,
  96. SCM2PTR (v));
  97. }
  98. static void
  99. unregister_disappearing_links (scm_t_weak_entry *entry,
  100. scm_t_weak_table_kind kind)
  101. {
  102. if (kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
  103. GC_unregister_disappearing_link ((void **) &entry->key);
  104. if (kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
  105. GC_unregister_disappearing_link ((void **) &entry->value);
  106. }
  107. typedef struct {
  108. scm_t_weak_entry **buckets; /* the data */
  109. scm_i_pthread_mutex_t lock; /* the lock */
  110. scm_t_weak_table_kind kind; /* what kind of table it is */
  111. unsigned long n_buckets; /* total number of buckets. */
  112. unsigned long n_items; /* number of items in table */
  113. unsigned long lower; /* when to shrink */
  114. unsigned long upper; /* when to grow */
  115. int size_index; /* index into hashtable_size */
  116. int min_size_index; /* minimum size_index */
  117. GC_word last_gc_no;
  118. } scm_t_weak_table;
  119. #define SCM_WEAK_TABLE_P(x) (SCM_HAS_TYP11 (x, scm_tc11_weak_table))
  120. #define SCM_VALIDATE_WEAK_TABLE(pos, arg) \
  121. SCM_MAKE_VALIDATE_MSG (pos, arg, WEAK_TABLE_P, "weak-table")
  122. #define SCM_WEAK_TABLE(x) ((scm_t_weak_table *) SCM_CELL_WORD_1 (x))
  123. /* GC descriptors for the various kinds of scm_t_weak_entry. */
  124. static GC_descr weak_key_descr;
  125. static GC_descr weak_value_descr;
  126. static GC_descr doubly_weak_descr;
  127. static scm_t_weak_entry *
  128. allocate_entry (scm_t_weak_table_kind kind)
  129. {
  130. scm_t_weak_entry *ret;
  131. switch (kind)
  132. {
  133. case SCM_WEAK_TABLE_KIND_KEY:
  134. ret = GC_malloc_explicitly_typed (sizeof (*ret), weak_key_descr);
  135. break;
  136. case SCM_WEAK_TABLE_KIND_VALUE:
  137. ret = GC_malloc_explicitly_typed (sizeof (*ret), weak_value_descr);
  138. break;
  139. case SCM_WEAK_TABLE_KIND_BOTH:
  140. ret = GC_malloc_explicitly_typed (sizeof (*ret), doubly_weak_descr);
  141. break;
  142. default:
  143. abort ();
  144. }
  145. return ret;
  146. }
  147. static void
  148. add_entry (scm_t_weak_table *table, scm_t_weak_entry *entry)
  149. {
  150. unsigned long bucket = entry->hash % table->n_buckets;
  151. entry->next = table->buckets[bucket];
  152. table->buckets[bucket] = entry;
  153. table->n_items++;
  154. }
  155. /* Growing or shrinking is triggered when the load factor
  156. *
  157. * L = N / S (N: number of items in table, S: bucket vector length)
  158. *
  159. * passes an upper limit of 0.9 or a lower limit of 0.25.
  160. *
  161. * The implementation stores the upper and lower number of items which
  162. * trigger a resize in the hashtable object.
  163. *
  164. * Possible hash table sizes (primes) are stored in the array
  165. * hashtable_size.
  166. */
  167. static unsigned long hashtable_size[] = {
  168. 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
  169. 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041, 28762081,
  170. 57524111, 115048217, 230096423
  171. };
  172. #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
  173. static void
  174. resize_table (scm_t_weak_table *table)
  175. {
  176. scm_t_weak_entry **old_buckets, **new_buckets;
  177. int new_size_index;
  178. unsigned long old_n_buckets, new_n_buckets, old_k;
  179. new_size_index = table->size_index;
  180. if (table->n_items < table->lower)
  181. {
  182. /* Rehashing is not triggered when i <= min_size. */
  183. do
  184. new_size_index -= 1;
  185. while (new_size_index > table->min_size_index
  186. && table->n_items < hashtable_size[new_size_index] / 4);
  187. }
  188. else if (table->n_items > table->upper)
  189. {
  190. new_size_index += 1;
  191. if (new_size_index >= HASHTABLE_SIZE_N)
  192. /* Limit max bucket count. */
  193. return;
  194. }
  195. else
  196. /* Nothing to do. */
  197. return;
  198. new_n_buckets = hashtable_size[new_size_index];
  199. new_buckets = scm_gc_malloc (sizeof (*new_buckets) * new_n_buckets,
  200. "weak table buckets");
  201. old_buckets = table->buckets;
  202. old_n_buckets = table->n_buckets;
  203. table->size_index = new_size_index;
  204. table->n_buckets = new_n_buckets;
  205. if (new_size_index <= table->min_size_index)
  206. table->lower = 0;
  207. else
  208. table->lower = new_n_buckets / 4;
  209. table->upper = 9 * new_n_buckets / 10;
  210. table->n_items = 0;
  211. table->buckets = new_buckets;
  212. for (old_k = 0; old_k < old_n_buckets; old_k++)
  213. {
  214. scm_t_weak_entry *entry = old_buckets[old_k];
  215. while (entry)
  216. {
  217. scm_t_weak_entry *next = entry->next;
  218. entry->next = NULL;
  219. add_entry (table, entry);
  220. entry = next;
  221. }
  222. }
  223. }
  224. /* Run after GC via do_vacuum_weak_table, this function runs over the
  225. whole table, removing lost weak references, reshuffling the table as it
  226. goes. It might resize the table if it reaps enough buckets. */
  227. static void
  228. vacuum_weak_table (scm_t_weak_table *table)
  229. {
  230. GC_word gc_no = GC_get_gc_no ();
  231. unsigned long k;
  232. if (gc_no == table->last_gc_no)
  233. return;
  234. table->last_gc_no = gc_no;
  235. for (k = 0; k < table->n_buckets; k++)
  236. {
  237. scm_t_weak_entry **loc = table->buckets + k;
  238. scm_t_weak_entry *entry;
  239. for (entry = *loc; entry; entry = *loc)
  240. {
  241. scm_t_bits key, value;
  242. read_weak_entry (entry, &key, &value);
  243. if (!key || !value)
  244. /* Lost weak reference; prune entry. */
  245. {
  246. *loc = entry->next;
  247. table->n_items--;
  248. entry->next = NULL;
  249. unregister_disappearing_links (entry, table->kind);
  250. }
  251. else
  252. loc = &entry->next;
  253. }
  254. }
  255. if (table->n_items < table->lower)
  256. resize_table (table);
  257. }
  258. static SCM
  259. weak_table_ref (scm_t_weak_table *table, unsigned long hash,
  260. scm_t_table_predicate_fn pred, void *closure,
  261. SCM dflt)
  262. {
  263. unsigned long bucket = hash % table->n_buckets;
  264. scm_t_weak_entry *entry;
  265. for (entry = table->buckets[bucket]; entry; entry = entry->next)
  266. {
  267. if (entry->hash == hash)
  268. {
  269. scm_t_bits key, value;
  270. read_weak_entry (entry, &key, &value);
  271. if (key && value && pred (SCM_PACK (key), SCM_PACK (value), closure))
  272. /* Found. */
  273. return SCM_PACK (value);
  274. }
  275. }
  276. return dflt;
  277. }
  278. static void
  279. weak_table_put_x (scm_t_weak_table *table, unsigned long hash,
  280. scm_t_table_predicate_fn pred, void *closure,
  281. SCM key, SCM value)
  282. {
  283. unsigned long bucket = hash % table->n_buckets;
  284. scm_t_weak_entry *entry;
  285. for (entry = table->buckets[bucket]; entry; entry = entry->next)
  286. {
  287. if (entry->hash == hash)
  288. {
  289. scm_t_bits k, v;
  290. read_weak_entry (entry, &k, &v);
  291. if (k && v && pred (SCM_PACK (k), SCM_PACK (v), closure))
  292. {
  293. unregister_disappearing_links (entry, table->kind);
  294. key = SCM_PACK (k);
  295. entry->value = SCM_UNPACK (value);
  296. register_disappearing_links (entry, key, value, table->kind);
  297. return;
  298. }
  299. }
  300. }
  301. if (table->n_items > table->upper)
  302. /* Full table, time to resize. */
  303. resize_table (table);
  304. entry = allocate_entry (table->kind);
  305. entry->hash = hash;
  306. entry->key = SCM_UNPACK (key);
  307. entry->value = SCM_UNPACK (value);
  308. register_disappearing_links (entry, key, value, table->kind);
  309. add_entry (table, entry);
  310. }
  311. static void
  312. weak_table_remove_x (scm_t_weak_table *table, unsigned long hash,
  313. scm_t_table_predicate_fn pred, void *closure)
  314. {
  315. unsigned long bucket = hash % table->n_buckets;
  316. scm_t_weak_entry **loc = table->buckets + bucket;
  317. scm_t_weak_entry *entry;
  318. for (entry = *loc; entry; entry = *loc)
  319. {
  320. if (entry->hash == hash)
  321. {
  322. scm_t_bits k, v;
  323. read_weak_entry (entry, &k, &v);
  324. if (k && v && pred (SCM_PACK (k), SCM_PACK (v), closure))
  325. {
  326. *loc = entry->next;
  327. table->n_items--;
  328. entry->next = NULL;
  329. unregister_disappearing_links (entry, table->kind);
  330. if (table->n_items < table->lower)
  331. resize_table (table);
  332. return;
  333. }
  334. }
  335. loc = &entry->next;
  336. }
  337. return;
  338. }
  339. static SCM
  340. make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  341. {
  342. scm_t_weak_table *table;
  343. int i = 0, n = k ? k : 31;
  344. while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
  345. ++i;
  346. n = hashtable_size[i];
  347. table = scm_gc_malloc (sizeof (*table), "weak-table");
  348. table->buckets = scm_gc_malloc (sizeof (*table->buckets) * n,
  349. "weak table buckets");
  350. table->kind = kind;
  351. table->n_items = 0;
  352. table->n_buckets = n;
  353. table->lower = 0;
  354. table->upper = 9 * n / 10;
  355. table->size_index = i;
  356. table->min_size_index = i;
  357. table->last_gc_no = GC_get_gc_no ();
  358. scm_i_pthread_mutex_init (&table->lock, NULL);
  359. return scm_cell (scm_tc11_weak_table, (scm_t_bits)table);
  360. }
  361. void
  362. scm_i_weak_table_print (SCM exp, SCM port, scm_print_state *pstate)
  363. {
  364. scm_puts ("#<", port);
  365. scm_puts ("weak-table ", port);
  366. scm_uintprint (SCM_WEAK_TABLE (exp)->n_items, 10, port);
  367. scm_putc ('/', port);
  368. scm_uintprint (SCM_WEAK_TABLE (exp)->n_buckets, 10, port);
  369. scm_puts (">", port);
  370. }
  371. static void
  372. do_vacuum_weak_table (SCM table)
  373. {
  374. scm_t_weak_table *t;
  375. t = SCM_WEAK_TABLE (table);
  376. /* Unlike weak sets, the weak table interface allows custom predicates
  377. to call out to arbitrary Scheme. There are two ways that this code
  378. can be re-entrant, then: calling weak hash procedures while in a
  379. custom predicate, or via finalizers run explicitly by (gc) or in an
  380. async (for non-threaded Guile). We add a restriction that
  381. prohibits the first case, by convention. But since we can't
  382. prohibit the second case, here we trylock instead of lock. In any
  383. case, if the mutex is held by another thread, then the table is in
  384. active use, so the next user of the table will handle the vacuum
  385. for us. */
  386. if (scm_i_pthread_mutex_trylock (&t->lock) == 0)
  387. {
  388. vacuum_weak_table (t);
  389. scm_i_pthread_mutex_unlock (&t->lock);
  390. }
  391. return;
  392. }
  393. static scm_i_pthread_mutex_t all_weak_tables_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  394. static SCM all_weak_tables = SCM_EOL;
  395. static void
  396. vacuum_all_weak_tables (void)
  397. {
  398. scm_i_pthread_mutex_lock (&all_weak_tables_lock);
  399. scm_i_visit_weak_list (&all_weak_tables, do_vacuum_weak_table);
  400. scm_i_pthread_mutex_unlock (&all_weak_tables_lock);
  401. }
  402. SCM
  403. scm_c_make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  404. {
  405. SCM ret;
  406. ret = make_weak_table (k, kind);
  407. scm_i_pthread_mutex_lock (&all_weak_tables_lock);
  408. all_weak_tables = scm_i_weak_cons (ret, all_weak_tables);
  409. scm_i_pthread_mutex_unlock (&all_weak_tables_lock);
  410. return ret;
  411. }
  412. SCM
  413. scm_weak_table_p (SCM obj)
  414. {
  415. return scm_from_bool (SCM_WEAK_TABLE_P (obj));
  416. }
  417. SCM
  418. scm_c_weak_table_ref (SCM table, unsigned long raw_hash,
  419. scm_t_table_predicate_fn pred,
  420. void *closure, SCM dflt)
  421. #define FUNC_NAME "weak-table-ref"
  422. {
  423. SCM ret;
  424. scm_t_weak_table *t;
  425. SCM_VALIDATE_WEAK_TABLE (1, table);
  426. t = SCM_WEAK_TABLE (table);
  427. scm_i_pthread_mutex_lock (&t->lock);
  428. vacuum_weak_table (t);
  429. ret = weak_table_ref (t, raw_hash, pred, closure, dflt);
  430. scm_i_pthread_mutex_unlock (&t->lock);
  431. return ret;
  432. }
  433. #undef FUNC_NAME
  434. void
  435. scm_c_weak_table_put_x (SCM table, unsigned long raw_hash,
  436. scm_t_table_predicate_fn pred,
  437. void *closure, SCM key, SCM value)
  438. #define FUNC_NAME "weak-table-put!"
  439. {
  440. scm_t_weak_table *t;
  441. SCM_VALIDATE_WEAK_TABLE (1, table);
  442. t = SCM_WEAK_TABLE (table);
  443. scm_i_pthread_mutex_lock (&t->lock);
  444. vacuum_weak_table (t);
  445. weak_table_put_x (t, raw_hash, pred, closure, key, value);
  446. scm_i_pthread_mutex_unlock (&t->lock);
  447. }
  448. #undef FUNC_NAME
  449. void
  450. scm_c_weak_table_remove_x (SCM table, unsigned long raw_hash,
  451. scm_t_table_predicate_fn pred,
  452. void *closure)
  453. #define FUNC_NAME "weak-table-remove!"
  454. {
  455. scm_t_weak_table *t;
  456. SCM_VALIDATE_WEAK_TABLE (1, table);
  457. t = SCM_WEAK_TABLE (table);
  458. scm_i_pthread_mutex_lock (&t->lock);
  459. vacuum_weak_table (t);
  460. weak_table_remove_x (t, raw_hash, pred, closure);
  461. scm_i_pthread_mutex_unlock (&t->lock);
  462. }
  463. #undef FUNC_NAME
  464. static int
  465. assq_predicate (SCM x, SCM y, void *closure)
  466. {
  467. return scm_is_eq (x, SCM_PACK_POINTER (closure));
  468. }
  469. SCM
  470. scm_weak_table_refq (SCM table, SCM key, SCM dflt)
  471. {
  472. return scm_c_weak_table_ref (table, scm_ihashq (key, -1),
  473. assq_predicate, SCM_UNPACK_POINTER (key),
  474. dflt);
  475. }
  476. void
  477. scm_weak_table_putq_x (SCM table, SCM key, SCM value)
  478. {
  479. scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
  480. assq_predicate, SCM_UNPACK_POINTER (key),
  481. key, value);
  482. }
  483. void
  484. scm_weak_table_remq_x (SCM table, SCM key)
  485. {
  486. scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
  487. assq_predicate, SCM_UNPACK_POINTER (key));
  488. }
  489. void
  490. scm_weak_table_clear_x (SCM table)
  491. #define FUNC_NAME "weak-table-clear!"
  492. {
  493. scm_t_weak_table *t;
  494. unsigned long k;
  495. scm_t_weak_entry *entry;
  496. SCM_VALIDATE_WEAK_TABLE (1, table);
  497. t = SCM_WEAK_TABLE (table);
  498. scm_i_pthread_mutex_lock (&t->lock);
  499. t->last_gc_no = GC_get_gc_no ();
  500. for (k = 0; k < t->n_buckets; k++)
  501. {
  502. for (entry = t->buckets[k]; entry; entry = entry->next)
  503. unregister_disappearing_links (entry, t->kind);
  504. t->buckets[k] = NULL;
  505. }
  506. t->n_items = 0;
  507. scm_i_pthread_mutex_unlock (&t->lock);
  508. }
  509. #undef FUNC_NAME
  510. SCM
  511. scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
  512. SCM init, SCM table)
  513. {
  514. scm_t_weak_table *t;
  515. unsigned long k;
  516. SCM alist = SCM_EOL;
  517. t = SCM_WEAK_TABLE (table);
  518. scm_i_pthread_mutex_lock (&t->lock);
  519. vacuum_weak_table (t);
  520. for (k = 0; k < t->n_buckets; k++)
  521. {
  522. scm_t_weak_entry *entry;
  523. for (entry = t->buckets[k]; entry; entry = entry->next)
  524. {
  525. scm_t_bits key, value;
  526. read_weak_entry (entry, &key, &value);
  527. if (key && value)
  528. alist = scm_acons (SCM_PACK (key), SCM_PACK (value), alist);
  529. }
  530. }
  531. scm_i_pthread_mutex_unlock (&t->lock);
  532. /* Call the proc outside the lock. */
  533. for (; !scm_is_null (alist); alist = scm_cdr (alist))
  534. init = proc (closure, scm_caar (alist), scm_cdar (alist), init);
  535. return init;
  536. }
  537. static SCM
  538. fold_trampoline (void *closure, SCM k, SCM v, SCM init)
  539. {
  540. return scm_call_3 (SCM_PACK_POINTER (closure), k, v, init);
  541. }
  542. SCM
  543. scm_weak_table_fold (SCM proc, SCM init, SCM table)
  544. #define FUNC_NAME "weak-table-fold"
  545. {
  546. SCM_VALIDATE_WEAK_TABLE (3, table);
  547. SCM_VALIDATE_PROC (1, proc);
  548. return scm_c_weak_table_fold (fold_trampoline, SCM_UNPACK_POINTER (proc), init, table);
  549. }
  550. #undef FUNC_NAME
  551. static SCM
  552. for_each_trampoline (void *closure, SCM k, SCM v, SCM seed)
  553. {
  554. scm_call_2 (SCM_PACK_POINTER (closure), k, v);
  555. return seed;
  556. }
  557. void
  558. scm_weak_table_for_each (SCM proc, SCM table)
  559. #define FUNC_NAME "weak-table-for-each"
  560. {
  561. SCM_VALIDATE_WEAK_TABLE (2, table);
  562. SCM_VALIDATE_PROC (1, proc);
  563. scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
  564. }
  565. #undef FUNC_NAME
  566. static SCM
  567. map_trampoline (void *closure, SCM k, SCM v, SCM seed)
  568. {
  569. return scm_cons (scm_call_2 (SCM_PACK_POINTER (closure), k, v), seed);
  570. }
  571. SCM
  572. scm_weak_table_map_to_list (SCM proc, SCM table)
  573. #define FUNC_NAME "weak-table-map->list"
  574. {
  575. SCM_VALIDATE_WEAK_TABLE (2, table);
  576. SCM_VALIDATE_PROC (1, proc);
  577. return scm_c_weak_table_fold (map_trampoline, SCM_UNPACK_POINTER (proc), SCM_EOL, table);
  578. }
  579. #undef FUNC_NAME
  580. /* Legacy interface. */
  581. SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0,
  582. (SCM n),
  583. "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
  584. "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
  585. "Return a weak hash table with @var{size} buckets.\n"
  586. "\n"
  587. "You can modify weak hash tables in exactly the same way you\n"
  588. "would modify regular hash tables. (@pxref{Hash Tables})")
  589. #define FUNC_NAME s_scm_make_weak_key_hash_table
  590. {
  591. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  592. SCM_WEAK_TABLE_KIND_KEY);
  593. }
  594. #undef FUNC_NAME
  595. SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 0, 1, 0,
  596. (SCM n),
  597. "Return a hash table with weak values with @var{size} buckets.\n"
  598. "(@pxref{Hash Tables})")
  599. #define FUNC_NAME s_scm_make_weak_value_hash_table
  600. {
  601. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  602. SCM_WEAK_TABLE_KIND_VALUE);
  603. }
  604. #undef FUNC_NAME
  605. SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 0, 1, 0,
  606. (SCM n),
  607. "Return a hash table with weak keys and values with @var{size}\n"
  608. "buckets. (@pxref{Hash Tables})")
  609. #define FUNC_NAME s_scm_make_doubly_weak_hash_table
  610. {
  611. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  612. SCM_WEAK_TABLE_KIND_BOTH);
  613. }
  614. #undef FUNC_NAME
  615. SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
  616. (SCM obj),
  617. "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n"
  618. "@deffnx {Scheme Procedure} doubly-weak-hash-table? obj\n"
  619. "Return @code{#t} if @var{obj} is the specified weak hash\n"
  620. "table. Note that a doubly weak hash table is neither a weak key\n"
  621. "nor a weak value hash table.")
  622. #define FUNC_NAME s_scm_weak_key_hash_table_p
  623. {
  624. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  625. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_KEY);
  626. }
  627. #undef FUNC_NAME
  628. SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
  629. (SCM obj),
  630. "Return @code{#t} if @var{obj} is a weak value hash table.")
  631. #define FUNC_NAME s_scm_weak_value_hash_table_p
  632. {
  633. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  634. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_VALUE);
  635. }
  636. #undef FUNC_NAME
  637. SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
  638. (SCM obj),
  639. "Return @code{#t} if @var{obj} is a doubly weak hash table.")
  640. #define FUNC_NAME s_scm_doubly_weak_hash_table_p
  641. {
  642. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  643. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_BOTH);
  644. }
  645. #undef FUNC_NAME
  646. void
  647. scm_weak_table_prehistory (void)
  648. {
  649. GC_word weak_key_bitmap[GC_BITMAP_SIZE (scm_t_weak_entry)] = { 0 };
  650. GC_word weak_value_bitmap[GC_BITMAP_SIZE (scm_t_weak_entry)] = { 0 };
  651. GC_word doubly_weak_bitmap[GC_BITMAP_SIZE (scm_t_weak_entry)] = { 0 };
  652. GC_set_bit (weak_key_bitmap, GC_WORD_OFFSET (scm_t_weak_entry, next));
  653. GC_set_bit (weak_value_bitmap, GC_WORD_OFFSET (scm_t_weak_entry, next));
  654. GC_set_bit (doubly_weak_bitmap, GC_WORD_OFFSET (scm_t_weak_entry, next));
  655. GC_set_bit (weak_key_bitmap, GC_WORD_OFFSET (scm_t_weak_entry, value));
  656. GC_set_bit (weak_value_bitmap, GC_WORD_OFFSET (scm_t_weak_entry, key));
  657. weak_key_descr = GC_make_descriptor (weak_key_bitmap,
  658. GC_WORD_LEN (scm_t_weak_entry));
  659. weak_value_descr = GC_make_descriptor (weak_value_bitmap,
  660. GC_WORD_LEN (scm_t_weak_entry));
  661. doubly_weak_descr = GC_make_descriptor (doubly_weak_bitmap,
  662. GC_WORD_LEN (scm_t_weak_entry));
  663. }
  664. void
  665. scm_init_weak_table ()
  666. {
  667. #include "weak-table.x"
  668. scm_i_register_async_gc_callback (vacuum_all_weak_tables);
  669. }