weak-table.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /* Copyright (C) 2011, 2012, 2013, 2014 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include "libguile/bdw-gc.h"
  23. #include <gc/gc_mark.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/hash.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/validate.h"
  29. #include "libguile/weak-table.h"
  30. /* Weak Tables
  31. This file implements weak hash tables. Weak hash tables are
  32. generally used when you want to augment some object with additional
  33. data, but when you don't have space to store the data in the object.
  34. For example, procedure properties are implemented with weak tables.
  35. Weak tables are implemented using an open-addressed hash table.
  36. Basically this means that there is an array of entries, and the item
  37. is expected to be found the slot corresponding to its hash code,
  38. modulo the length of the array.
  39. Collisions are handled using linear probing with the Robin Hood
  40. technique. See Pedro Celis' paper, "Robin Hood Hashing":
  41. http://www.cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf
  42. The vector of entries is allocated in such a way that the GC doesn't
  43. trace the weak values. For doubly-weak tables, this means that the
  44. entries are allocated as an "atomic" piece of memory. Key-weak and
  45. value-weak tables use a special GC kind with a custom mark procedure.
  46. When items are added weakly into table, a disappearing link is
  47. registered to their locations. If the referent is collected, then
  48. that link will be zeroed out.
  49. An entry in the table consists of the key and the value, together
  50. with the hash code of the key. We munge hash codes so that they are
  51. never 0. In this way we can detect removed entries (key of zero but
  52. nonzero hash code), and can then reshuffle elements as needed to
  53. maintain the robin hood ordering.
  54. Compared to buckets-and-chains hash tables, open addressing has the
  55. advantage that it is very cache-friendly. It also uses less memory.
  56. Implementation-wise, there are two things to note.
  57. 1. We assume that hash codes are evenly distributed across the
  58. range of unsigned longs. The actual hash code stored in the
  59. entry is left-shifted by 1 bit (losing 1 bit of hash precision),
  60. and then or'd with 1. In this way we ensure that the hash field
  61. of an occupied entry is nonzero. To map to an index, we
  62. right-shift the hash by one, divide by the size, and take the
  63. remainder.
  64. 2. Since the weak references are stored in an atomic region with
  65. disappearing links, they need to be accessed with the GC alloc
  66. lock. `copy_weak_entry' will do that for you. The hash code
  67. itself can be read outside the lock, though.
  68. */
  69. typedef struct {
  70. unsigned long hash;
  71. scm_t_bits key;
  72. scm_t_bits value;
  73. } scm_t_weak_entry;
  74. struct weak_entry_data {
  75. scm_t_weak_entry *in;
  76. scm_t_weak_entry *out;
  77. };
  78. static void*
  79. do_copy_weak_entry (void *data)
  80. {
  81. struct weak_entry_data *e = data;
  82. e->out->hash = e->in->hash;
  83. e->out->key = e->in->key;
  84. e->out->value = e->in->value;
  85. return NULL;
  86. }
  87. static void
  88. copy_weak_entry (scm_t_weak_entry *src, scm_t_weak_entry *dst)
  89. {
  90. struct weak_entry_data data;
  91. data.in = src;
  92. data.out = dst;
  93. GC_call_with_alloc_lock (do_copy_weak_entry, &data);
  94. }
  95. static void
  96. register_disappearing_links (scm_t_weak_entry *entry,
  97. SCM k, SCM v,
  98. scm_t_weak_table_kind kind)
  99. {
  100. if (SCM_UNPACK (k) && SCM_HEAP_OBJECT_P (k)
  101. && (kind == SCM_WEAK_TABLE_KIND_KEY
  102. || kind == SCM_WEAK_TABLE_KIND_BOTH))
  103. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &entry->key,
  104. SCM2PTR (k));
  105. if (SCM_UNPACK (v) && SCM_HEAP_OBJECT_P (v)
  106. && (kind == SCM_WEAK_TABLE_KIND_VALUE
  107. || kind == SCM_WEAK_TABLE_KIND_BOTH))
  108. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &entry->value,
  109. SCM2PTR (v));
  110. }
  111. static void
  112. unregister_disappearing_links (scm_t_weak_entry *entry,
  113. scm_t_weak_table_kind kind)
  114. {
  115. if (kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
  116. GC_unregister_disappearing_link ((void **) &entry->key);
  117. if (kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
  118. GC_unregister_disappearing_link ((void **) &entry->value);
  119. }
  120. #ifndef HAVE_GC_MOVE_DISAPPEARING_LINK
  121. static void
  122. GC_move_disappearing_link (void **from, void **to)
  123. {
  124. GC_unregister_disappearing_link (from);
  125. SCM_I_REGISTER_DISAPPEARING_LINK (to, *to);
  126. }
  127. #endif
  128. static void
  129. move_disappearing_links (scm_t_weak_entry *from, scm_t_weak_entry *to,
  130. SCM key, SCM value, scm_t_weak_table_kind kind)
  131. {
  132. if ((kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
  133. && SCM_HEAP_OBJECT_P (key))
  134. GC_move_disappearing_link ((void **) &from->key, (void **) &to->key);
  135. if ((kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
  136. && SCM_HEAP_OBJECT_P (value))
  137. GC_move_disappearing_link ((void **) &from->value, (void **) &to->value);
  138. }
  139. static void
  140. move_weak_entry (scm_t_weak_entry *from, scm_t_weak_entry *to,
  141. scm_t_weak_table_kind kind)
  142. {
  143. if (from->hash)
  144. {
  145. scm_t_weak_entry copy;
  146. copy_weak_entry (from, &copy);
  147. to->hash = copy.hash;
  148. to->key = copy.key;
  149. to->value = copy.value;
  150. move_disappearing_links (from, to,
  151. SCM_PACK (copy.key), SCM_PACK (copy.value),
  152. kind);
  153. }
  154. else
  155. {
  156. to->hash = 0;
  157. to->key = 0;
  158. to->value = 0;
  159. }
  160. }
  161. typedef struct {
  162. scm_t_weak_entry *entries; /* the data */
  163. scm_i_pthread_mutex_t lock; /* the lock */
  164. scm_t_weak_table_kind kind; /* what kind of table it is */
  165. unsigned long size; /* total number of slots. */
  166. unsigned long n_items; /* number of items in table */
  167. unsigned long lower; /* when to shrink */
  168. unsigned long upper; /* when to grow */
  169. int size_index; /* index into hashtable_size */
  170. int min_size_index; /* minimum size_index */
  171. } scm_t_weak_table;
  172. #define SCM_WEAK_TABLE_P(x) (SCM_HAS_TYP7 (x, scm_tc7_weak_table))
  173. #define SCM_VALIDATE_WEAK_TABLE(pos, arg) \
  174. SCM_MAKE_VALIDATE_MSG (pos, arg, WEAK_TABLE_P, "weak-table")
  175. #define SCM_WEAK_TABLE(x) ((scm_t_weak_table *) SCM_CELL_WORD_1 (x))
  176. static unsigned long
  177. hash_to_index (unsigned long hash, unsigned long size)
  178. {
  179. return (hash >> 1) % size;
  180. }
  181. static unsigned long
  182. entry_distance (unsigned long hash, unsigned long k, unsigned long size)
  183. {
  184. unsigned long origin = hash_to_index (hash, size);
  185. if (k >= origin)
  186. return k - origin;
  187. else
  188. /* The other key was displaced and wrapped around. */
  189. return size - origin + k;
  190. }
  191. static void
  192. rob_from_rich (scm_t_weak_table *table, unsigned long k)
  193. {
  194. unsigned long empty, size;
  195. size = table->size;
  196. /* If we are to free up slot K in the table, we need room to do so. */
  197. assert (table->n_items < size);
  198. empty = k;
  199. do
  200. empty = (empty + 1) % size;
  201. while (table->entries[empty].hash);
  202. do
  203. {
  204. unsigned long last = empty ? (empty - 1) : (size - 1);
  205. move_weak_entry (&table->entries[last], &table->entries[empty],
  206. table->kind);
  207. empty = last;
  208. }
  209. while (empty != k);
  210. table->entries[empty].hash = 0;
  211. table->entries[empty].key = 0;
  212. table->entries[empty].value = 0;
  213. }
  214. static void
  215. give_to_poor (scm_t_weak_table *table, unsigned long k)
  216. {
  217. /* Slot K was just freed up; possibly shuffle others down. */
  218. unsigned long size = table->size;
  219. while (1)
  220. {
  221. unsigned long next = (k + 1) % size;
  222. unsigned long hash;
  223. scm_t_weak_entry copy;
  224. hash = table->entries[next].hash;
  225. if (!hash || hash_to_index (hash, size) == next)
  226. break;
  227. copy_weak_entry (&table->entries[next], &copy);
  228. if (!copy.key || !copy.value)
  229. /* Lost weak reference. */
  230. {
  231. give_to_poor (table, next);
  232. table->n_items--;
  233. continue;
  234. }
  235. move_weak_entry (&table->entries[next], &table->entries[k],
  236. table->kind);
  237. k = next;
  238. }
  239. /* We have shuffled down any entries that should be shuffled down; now
  240. free the end. */
  241. table->entries[k].hash = 0;
  242. table->entries[k].key = 0;
  243. table->entries[k].value = 0;
  244. }
  245. /* The GC "kinds" for singly-weak tables. */
  246. static int weak_key_gc_kind;
  247. static int weak_value_gc_kind;
  248. static struct GC_ms_entry *
  249. mark_weak_key_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  250. struct GC_ms_entry *mark_stack_limit, GC_word env)
  251. {
  252. scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
  253. unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
  254. for (k = 0; k < size; k++)
  255. if (entries[k].hash && entries[k].key)
  256. {
  257. SCM value = SCM_PACK (entries[k].value);
  258. mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (value),
  259. mark_stack_ptr, mark_stack_limit,
  260. NULL);
  261. }
  262. return mark_stack_ptr;
  263. }
  264. static struct GC_ms_entry *
  265. mark_weak_value_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  266. struct GC_ms_entry *mark_stack_limit, GC_word env)
  267. {
  268. scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
  269. unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
  270. for (k = 0; k < size; k++)
  271. if (entries[k].hash && entries[k].value)
  272. {
  273. SCM key = SCM_PACK (entries[k].key);
  274. mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (key),
  275. mark_stack_ptr, mark_stack_limit,
  276. NULL);
  277. }
  278. return mark_stack_ptr;
  279. }
  280. static scm_t_weak_entry *
  281. allocate_entries (unsigned long size, scm_t_weak_table_kind kind)
  282. {
  283. scm_t_weak_entry *ret;
  284. size_t bytes = size * sizeof (*ret);
  285. switch (kind)
  286. {
  287. case SCM_WEAK_TABLE_KIND_KEY:
  288. ret = GC_generic_malloc (bytes, weak_key_gc_kind);
  289. break;
  290. case SCM_WEAK_TABLE_KIND_VALUE:
  291. ret = GC_generic_malloc (bytes, weak_value_gc_kind);
  292. break;
  293. case SCM_WEAK_TABLE_KIND_BOTH:
  294. ret = scm_gc_malloc_pointerless (bytes, "weak-table");
  295. break;
  296. default:
  297. abort ();
  298. }
  299. memset (ret, 0, bytes);
  300. return ret;
  301. }
  302. /* Growing or shrinking is triggered when the load factor
  303. *
  304. * L = N / S (N: number of items in table, S: bucket vector length)
  305. *
  306. * passes an upper limit of 0.9 or a lower limit of 0.2.
  307. *
  308. * The implementation stores the upper and lower number of items which
  309. * trigger a resize in the hashtable object.
  310. *
  311. * Possible hash table sizes (primes) are stored in the array
  312. * hashtable_size.
  313. */
  314. static unsigned long hashtable_size[] = {
  315. 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
  316. 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041, 28762081,
  317. 57524111, 115048217, 230096423
  318. };
  319. #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
  320. static int
  321. compute_size_index (scm_t_weak_table *table)
  322. {
  323. int i = table->size_index;
  324. if (table->n_items < table->lower)
  325. {
  326. /* rehashing is not triggered when i <= min_size */
  327. do
  328. --i;
  329. while (i > table->min_size_index
  330. && table->n_items < hashtable_size[i] / 5);
  331. }
  332. else if (table->n_items > table->upper)
  333. {
  334. ++i;
  335. if (i >= HASHTABLE_SIZE_N)
  336. /* The biggest size currently is 230096423, which for a 32-bit
  337. machine will occupy 2.3GB of memory at a load of 80%. There
  338. is probably something better to do here, but if you have a
  339. weak map of that size, you are hosed in any case. */
  340. abort ();
  341. }
  342. return i;
  343. }
  344. static int
  345. is_acceptable_size_index (scm_t_weak_table *table, int size_index)
  346. {
  347. int computed = compute_size_index (table);
  348. if (size_index == computed)
  349. /* We were going to grow or shrink, and allocating the new vector
  350. didn't change the target size. */
  351. return 1;
  352. if (size_index == computed + 1)
  353. {
  354. /* We were going to enlarge the table, but allocating the new
  355. vector finalized some objects, making an enlargement
  356. unnecessary. It might still be a good idea to use the larger
  357. table, though. (This branch also gets hit if, while allocating
  358. the vector, some other thread was actively removing items from
  359. the table. That is less likely, though.) */
  360. unsigned long new_lower = hashtable_size[size_index] / 5;
  361. return table->size > new_lower;
  362. }
  363. if (size_index == computed - 1)
  364. {
  365. /* We were going to shrink the table, but when we dropped the lock
  366. to allocate the new vector, some other thread added elements to
  367. the table. */
  368. return 0;
  369. }
  370. /* The computed size differs from our newly allocated size by more
  371. than one size index -- recalculate. */
  372. return 0;
  373. }
  374. static void
  375. resize_table (scm_t_weak_table *table)
  376. {
  377. scm_t_weak_entry *old_entries, *new_entries;
  378. int new_size_index;
  379. unsigned long old_size, new_size, old_k;
  380. do
  381. {
  382. new_size_index = compute_size_index (table);
  383. if (new_size_index == table->size_index)
  384. return;
  385. new_size = hashtable_size[new_size_index];
  386. new_entries = allocate_entries (new_size, table->kind);
  387. }
  388. while (!is_acceptable_size_index (table, new_size_index));
  389. old_entries = table->entries;
  390. old_size = table->size;
  391. table->size_index = new_size_index;
  392. table->size = new_size;
  393. if (new_size_index <= table->min_size_index)
  394. table->lower = 0;
  395. else
  396. table->lower = new_size / 5;
  397. table->upper = 9 * new_size / 10;
  398. table->n_items = 0;
  399. table->entries = new_entries;
  400. for (old_k = 0; old_k < old_size; old_k++)
  401. {
  402. scm_t_weak_entry copy;
  403. unsigned long new_k, distance;
  404. if (!old_entries[old_k].hash)
  405. continue;
  406. copy_weak_entry (&old_entries[old_k], &copy);
  407. if (!copy.key || !copy.value)
  408. continue;
  409. new_k = hash_to_index (copy.hash, new_size);
  410. for (distance = 0; ; distance++, new_k = (new_k + 1) % new_size)
  411. {
  412. unsigned long other_hash = new_entries[new_k].hash;
  413. if (!other_hash)
  414. /* Found an empty entry. */
  415. break;
  416. /* Displace the entry if our distance is less, otherwise keep
  417. looking. */
  418. if (entry_distance (other_hash, new_k, new_size) < distance)
  419. {
  420. rob_from_rich (table, new_k);
  421. break;
  422. }
  423. }
  424. table->n_items++;
  425. new_entries[new_k].hash = copy.hash;
  426. new_entries[new_k].key = copy.key;
  427. new_entries[new_k].value = copy.value;
  428. register_disappearing_links (&new_entries[new_k],
  429. SCM_PACK (copy.key), SCM_PACK (copy.value),
  430. table->kind);
  431. }
  432. }
  433. /* Run after GC via do_vacuum_weak_table, this function runs over the
  434. whole table, removing lost weak references, reshuffling the table as it
  435. goes. It might resize the table if it reaps enough entries. */
  436. static void
  437. vacuum_weak_table (scm_t_weak_table *table)
  438. {
  439. scm_t_weak_entry *entries = table->entries;
  440. unsigned long size = table->size;
  441. unsigned long k;
  442. for (k = 0; k < size; k++)
  443. {
  444. unsigned long hash = entries[k].hash;
  445. if (hash)
  446. {
  447. scm_t_weak_entry copy;
  448. copy_weak_entry (&entries[k], &copy);
  449. if (!copy.key || !copy.value)
  450. /* Lost weak reference; reshuffle. */
  451. {
  452. give_to_poor (table, k);
  453. table->n_items--;
  454. }
  455. }
  456. }
  457. if (table->n_items < table->lower)
  458. resize_table (table);
  459. }
  460. static SCM
  461. weak_table_ref (scm_t_weak_table *table, unsigned long hash,
  462. scm_t_table_predicate_fn pred, void *closure,
  463. SCM dflt)
  464. {
  465. unsigned long k, distance, size;
  466. scm_t_weak_entry *entries;
  467. size = table->size;
  468. entries = table->entries;
  469. hash = (hash << 1) | 0x1;
  470. k = hash_to_index (hash, size);
  471. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  472. {
  473. unsigned long other_hash;
  474. retry:
  475. other_hash = entries[k].hash;
  476. if (!other_hash)
  477. /* Not found. */
  478. return dflt;
  479. if (hash == other_hash)
  480. {
  481. scm_t_weak_entry copy;
  482. copy_weak_entry (&entries[k], &copy);
  483. if (!copy.key || !copy.value)
  484. /* Lost weak reference; reshuffle. */
  485. {
  486. give_to_poor (table, k);
  487. table->n_items--;
  488. goto retry;
  489. }
  490. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  491. /* Found. */
  492. return SCM_PACK (copy.value);
  493. }
  494. /* If the entry's distance is less, our key is not in the table. */
  495. if (entry_distance (other_hash, k, size) < distance)
  496. return dflt;
  497. }
  498. /* If we got here, then we were unfortunate enough to loop through the
  499. whole table. Shouldn't happen, but hey. */
  500. return dflt;
  501. }
  502. static void
  503. weak_table_put_x (scm_t_weak_table *table, unsigned long hash,
  504. scm_t_table_predicate_fn pred, void *closure,
  505. SCM key, SCM value)
  506. {
  507. unsigned long k, distance, size;
  508. scm_t_weak_entry *entries;
  509. size = table->size;
  510. entries = table->entries;
  511. hash = (hash << 1) | 0x1;
  512. k = hash_to_index (hash, size);
  513. for (distance = 0; ; distance++, k = (k + 1) % size)
  514. {
  515. unsigned long other_hash;
  516. retry:
  517. other_hash = entries[k].hash;
  518. if (!other_hash)
  519. /* Found an empty entry. */
  520. break;
  521. if (other_hash == hash)
  522. {
  523. scm_t_weak_entry copy;
  524. copy_weak_entry (&entries[k], &copy);
  525. if (!copy.key || !copy.value)
  526. /* Lost weak reference; reshuffle. */
  527. {
  528. give_to_poor (table, k);
  529. table->n_items--;
  530. goto retry;
  531. }
  532. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  533. /* Found an entry with this key. */
  534. break;
  535. }
  536. if (table->n_items > table->upper)
  537. /* Full table, time to resize. */
  538. {
  539. resize_table (table);
  540. return weak_table_put_x (table, hash >> 1, pred, closure, key, value);
  541. }
  542. /* Displace the entry if our distance is less, otherwise keep
  543. looking. */
  544. if (entry_distance (other_hash, k, size) < distance)
  545. {
  546. rob_from_rich (table, k);
  547. break;
  548. }
  549. }
  550. if (entries[k].hash)
  551. unregister_disappearing_links (&entries[k], table->kind);
  552. else
  553. table->n_items++;
  554. entries[k].hash = hash;
  555. entries[k].key = SCM_UNPACK (key);
  556. entries[k].value = SCM_UNPACK (value);
  557. register_disappearing_links (&entries[k], key, value, table->kind);
  558. }
  559. static void
  560. weak_table_remove_x (scm_t_weak_table *table, unsigned long hash,
  561. scm_t_table_predicate_fn pred, void *closure)
  562. {
  563. unsigned long k, distance, size;
  564. scm_t_weak_entry *entries;
  565. size = table->size;
  566. entries = table->entries;
  567. hash = (hash << 1) | 0x1;
  568. k = hash_to_index (hash, size);
  569. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  570. {
  571. unsigned long other_hash;
  572. retry:
  573. other_hash = entries[k].hash;
  574. if (!other_hash)
  575. /* Not found. */
  576. return;
  577. if (other_hash == hash)
  578. {
  579. scm_t_weak_entry copy;
  580. copy_weak_entry (&entries[k], &copy);
  581. if (!copy.key || !copy.value)
  582. /* Lost weak reference; reshuffle. */
  583. {
  584. give_to_poor (table, k);
  585. table->n_items--;
  586. goto retry;
  587. }
  588. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  589. /* Found an entry with this key. */
  590. {
  591. entries[k].hash = 0;
  592. entries[k].key = 0;
  593. entries[k].value = 0;
  594. unregister_disappearing_links (&entries[k], table->kind);
  595. if (--table->n_items < table->lower)
  596. resize_table (table);
  597. else
  598. give_to_poor (table, k);
  599. return;
  600. }
  601. }
  602. /* If the entry's distance is less, our key is not in the table. */
  603. if (entry_distance (other_hash, k, size) < distance)
  604. return;
  605. }
  606. }
  607. static SCM
  608. make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  609. {
  610. scm_t_weak_table *table;
  611. int i = 0, n = k ? k : 31;
  612. while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
  613. ++i;
  614. n = hashtable_size[i];
  615. table = scm_gc_malloc (sizeof (*table), "weak-table");
  616. table->entries = allocate_entries (n, kind);
  617. table->kind = kind;
  618. table->n_items = 0;
  619. table->size = n;
  620. table->lower = 0;
  621. table->upper = 9 * n / 10;
  622. table->size_index = i;
  623. table->min_size_index = i;
  624. scm_i_pthread_mutex_init (&table->lock, NULL);
  625. return scm_cell (scm_tc7_weak_table, (scm_t_bits)table);
  626. }
  627. void
  628. scm_i_weak_table_print (SCM exp, SCM port, scm_print_state *pstate)
  629. {
  630. scm_puts_unlocked ("#<", port);
  631. scm_puts_unlocked ("weak-table ", port);
  632. scm_uintprint (SCM_WEAK_TABLE (exp)->n_items, 10, port);
  633. scm_putc_unlocked ('/', port);
  634. scm_uintprint (SCM_WEAK_TABLE (exp)->size, 10, port);
  635. scm_puts_unlocked (">", port);
  636. }
  637. static void
  638. do_vacuum_weak_table (SCM table)
  639. {
  640. scm_t_weak_table *t;
  641. t = SCM_WEAK_TABLE (table);
  642. /* Unlike weak sets, the weak table interface allows custom predicates
  643. to call out to arbitrary Scheme. There are two ways that this code
  644. can be re-entrant, then: calling weak hash procedures while in a
  645. custom predicate, or via finalizers run explicitly by (gc) or in an
  646. async (for non-threaded Guile). We add a restriction that
  647. prohibits the first case, by convention. But since we can't
  648. prohibit the second case, here we trylock instead of lock. Not so
  649. nice. */
  650. if (scm_i_pthread_mutex_trylock (&t->lock) == 0)
  651. {
  652. vacuum_weak_table (t);
  653. scm_i_pthread_mutex_unlock (&t->lock);
  654. }
  655. return;
  656. }
  657. SCM
  658. scm_c_make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  659. {
  660. SCM ret;
  661. ret = make_weak_table (k, kind);
  662. scm_i_register_weak_gc_callback (ret, do_vacuum_weak_table);
  663. return ret;
  664. }
  665. SCM
  666. scm_weak_table_p (SCM obj)
  667. {
  668. return scm_from_bool (SCM_WEAK_TABLE_P (obj));
  669. }
  670. SCM
  671. scm_c_weak_table_ref (SCM table, unsigned long raw_hash,
  672. scm_t_table_predicate_fn pred,
  673. void *closure, SCM dflt)
  674. #define FUNC_NAME "weak-table-ref"
  675. {
  676. SCM ret;
  677. scm_t_weak_table *t;
  678. SCM_VALIDATE_WEAK_TABLE (1, table);
  679. t = SCM_WEAK_TABLE (table);
  680. scm_i_pthread_mutex_lock (&t->lock);
  681. ret = weak_table_ref (t, raw_hash, pred, closure, dflt);
  682. scm_i_pthread_mutex_unlock (&t->lock);
  683. return ret;
  684. }
  685. #undef FUNC_NAME
  686. void
  687. scm_c_weak_table_put_x (SCM table, unsigned long raw_hash,
  688. scm_t_table_predicate_fn pred,
  689. void *closure, SCM key, SCM value)
  690. #define FUNC_NAME "weak-table-put!"
  691. {
  692. scm_t_weak_table *t;
  693. SCM_VALIDATE_WEAK_TABLE (1, table);
  694. t = SCM_WEAK_TABLE (table);
  695. scm_i_pthread_mutex_lock (&t->lock);
  696. weak_table_put_x (t, raw_hash, pred, closure, key, value);
  697. scm_i_pthread_mutex_unlock (&t->lock);
  698. }
  699. #undef FUNC_NAME
  700. void
  701. scm_c_weak_table_remove_x (SCM table, unsigned long raw_hash,
  702. scm_t_table_predicate_fn pred,
  703. void *closure)
  704. #define FUNC_NAME "weak-table-remove!"
  705. {
  706. scm_t_weak_table *t;
  707. SCM_VALIDATE_WEAK_TABLE (1, table);
  708. t = SCM_WEAK_TABLE (table);
  709. scm_i_pthread_mutex_lock (&t->lock);
  710. weak_table_remove_x (t, raw_hash, pred, closure);
  711. scm_i_pthread_mutex_unlock (&t->lock);
  712. }
  713. #undef FUNC_NAME
  714. static int
  715. assq_predicate (SCM x, SCM y, void *closure)
  716. {
  717. return scm_is_eq (x, SCM_PACK_POINTER (closure));
  718. }
  719. SCM
  720. scm_weak_table_refq (SCM table, SCM key, SCM dflt)
  721. {
  722. if (SCM_UNBNDP (dflt))
  723. dflt = SCM_BOOL_F;
  724. return scm_c_weak_table_ref (table, scm_ihashq (key, -1),
  725. assq_predicate, SCM_UNPACK_POINTER (key),
  726. dflt);
  727. }
  728. void
  729. scm_weak_table_putq_x (SCM table, SCM key, SCM value)
  730. {
  731. scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
  732. assq_predicate, SCM_UNPACK_POINTER (key),
  733. key, value);
  734. }
  735. void
  736. scm_weak_table_remq_x (SCM table, SCM key)
  737. {
  738. scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
  739. assq_predicate, SCM_UNPACK_POINTER (key));
  740. }
  741. void
  742. scm_weak_table_clear_x (SCM table)
  743. #define FUNC_NAME "weak-table-clear!"
  744. {
  745. scm_t_weak_table *t;
  746. SCM_VALIDATE_WEAK_TABLE (1, table);
  747. t = SCM_WEAK_TABLE (table);
  748. scm_i_pthread_mutex_lock (&t->lock);
  749. memset (t->entries, 0, sizeof (scm_t_weak_entry) * t->size);
  750. t->n_items = 0;
  751. scm_i_pthread_mutex_unlock (&t->lock);
  752. }
  753. #undef FUNC_NAME
  754. SCM
  755. scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
  756. SCM init, SCM table)
  757. {
  758. scm_t_weak_table *t;
  759. scm_t_weak_entry *entries;
  760. unsigned long k, size;
  761. t = SCM_WEAK_TABLE (table);
  762. scm_i_pthread_mutex_lock (&t->lock);
  763. size = t->size;
  764. entries = t->entries;
  765. for (k = 0; k < size; k++)
  766. {
  767. if (entries[k].hash)
  768. {
  769. scm_t_weak_entry copy;
  770. copy_weak_entry (&entries[k], &copy);
  771. if (copy.key && copy.value)
  772. {
  773. /* Release table lock while we call the function. */
  774. scm_i_pthread_mutex_unlock (&t->lock);
  775. init = proc (closure,
  776. SCM_PACK (copy.key), SCM_PACK (copy.value),
  777. init);
  778. scm_i_pthread_mutex_lock (&t->lock);
  779. }
  780. }
  781. }
  782. scm_i_pthread_mutex_unlock (&t->lock);
  783. return init;
  784. }
  785. static SCM
  786. fold_trampoline (void *closure, SCM k, SCM v, SCM init)
  787. {
  788. return scm_call_3 (SCM_PACK_POINTER (closure), k, v, init);
  789. }
  790. SCM
  791. scm_weak_table_fold (SCM proc, SCM init, SCM table)
  792. #define FUNC_NAME "weak-table-fold"
  793. {
  794. SCM_VALIDATE_WEAK_TABLE (3, table);
  795. SCM_VALIDATE_PROC (1, proc);
  796. return scm_c_weak_table_fold (fold_trampoline, SCM_UNPACK_POINTER (proc), init, table);
  797. }
  798. #undef FUNC_NAME
  799. static SCM
  800. for_each_trampoline (void *closure, SCM k, SCM v, SCM seed)
  801. {
  802. scm_call_2 (SCM_PACK_POINTER (closure), k, v);
  803. return seed;
  804. }
  805. void
  806. scm_weak_table_for_each (SCM proc, SCM table)
  807. #define FUNC_NAME "weak-table-for-each"
  808. {
  809. SCM_VALIDATE_WEAK_TABLE (2, table);
  810. SCM_VALIDATE_PROC (1, proc);
  811. scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
  812. }
  813. #undef FUNC_NAME
  814. static SCM
  815. map_trampoline (void *closure, SCM k, SCM v, SCM seed)
  816. {
  817. return scm_cons (scm_call_2 (SCM_PACK_POINTER (closure), k, v), seed);
  818. }
  819. SCM
  820. scm_weak_table_map_to_list (SCM proc, SCM table)
  821. #define FUNC_NAME "weak-table-map->list"
  822. {
  823. SCM_VALIDATE_WEAK_TABLE (2, table);
  824. SCM_VALIDATE_PROC (1, proc);
  825. return scm_c_weak_table_fold (map_trampoline, SCM_UNPACK_POINTER (proc), SCM_EOL, table);
  826. }
  827. #undef FUNC_NAME
  828. /* Legacy interface. */
  829. SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0,
  830. (SCM n),
  831. "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
  832. "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
  833. "Return a weak hash table with @var{size} buckets.\n"
  834. "\n"
  835. "You can modify weak hash tables in exactly the same way you\n"
  836. "would modify regular hash tables. (@pxref{Hash Tables})")
  837. #define FUNC_NAME s_scm_make_weak_key_hash_table
  838. {
  839. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  840. SCM_WEAK_TABLE_KIND_KEY);
  841. }
  842. #undef FUNC_NAME
  843. SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 0, 1, 0,
  844. (SCM n),
  845. "Return a hash table with weak values with @var{size} buckets.\n"
  846. "(@pxref{Hash Tables})")
  847. #define FUNC_NAME s_scm_make_weak_value_hash_table
  848. {
  849. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  850. SCM_WEAK_TABLE_KIND_VALUE);
  851. }
  852. #undef FUNC_NAME
  853. SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 0, 1, 0,
  854. (SCM n),
  855. "Return a hash table with weak keys and values with @var{size}\n"
  856. "buckets. (@pxref{Hash Tables})")
  857. #define FUNC_NAME s_scm_make_doubly_weak_hash_table
  858. {
  859. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  860. SCM_WEAK_TABLE_KIND_BOTH);
  861. }
  862. #undef FUNC_NAME
  863. SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
  864. (SCM obj),
  865. "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n"
  866. "@deffnx {Scheme Procedure} doubly-weak-hash-table? obj\n"
  867. "Return @code{#t} if @var{obj} is the specified weak hash\n"
  868. "table. Note that a doubly weak hash table is neither a weak key\n"
  869. "nor a weak value hash table.")
  870. #define FUNC_NAME s_scm_weak_key_hash_table_p
  871. {
  872. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  873. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_KEY);
  874. }
  875. #undef FUNC_NAME
  876. SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
  877. (SCM obj),
  878. "Return @code{#t} if @var{obj} is a weak value hash table.")
  879. #define FUNC_NAME s_scm_weak_value_hash_table_p
  880. {
  881. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  882. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_VALUE);
  883. }
  884. #undef FUNC_NAME
  885. SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
  886. (SCM obj),
  887. "Return @code{#t} if @var{obj} is a doubly weak hash table.")
  888. #define FUNC_NAME s_scm_doubly_weak_hash_table_p
  889. {
  890. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  891. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_BOTH);
  892. }
  893. #undef FUNC_NAME
  894. void
  895. scm_weak_table_prehistory (void)
  896. {
  897. weak_key_gc_kind =
  898. GC_new_kind (GC_new_free_list (),
  899. GC_MAKE_PROC (GC_new_proc (mark_weak_key_table), 0),
  900. 0, 0);
  901. weak_value_gc_kind =
  902. GC_new_kind (GC_new_free_list (),
  903. GC_MAKE_PROC (GC_new_proc (mark_weak_value_table), 0),
  904. 0, 0);
  905. }
  906. void
  907. scm_init_weak_table ()
  908. {
  909. #include "libguile/weak-table.x"
  910. }
  911. /*
  912. Local Variables:
  913. c-file-style: "gnu"
  914. End:
  915. */