weak-table.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. #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 ((GC_PTR) &entry->key,
  104. (GC_PTR) 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 ((GC_PTR) &entry->value,
  109. (GC_PTR) 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 ((GC_PTR) &entry->key);
  117. if (kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
  118. GC_unregister_disappearing_link ((GC_PTR) &entry->value);
  119. }
  120. static void
  121. move_disappearing_links (scm_t_weak_entry *from, scm_t_weak_entry *to,
  122. SCM key, SCM value, scm_t_weak_table_kind kind)
  123. {
  124. if ((kind == SCM_WEAK_TABLE_KIND_KEY || kind == SCM_WEAK_TABLE_KIND_BOTH)
  125. && SCM_HEAP_OBJECT_P (key))
  126. {
  127. #ifdef HAVE_GC_MOVE_DISAPPEARING_LINK
  128. GC_move_disappearing_link ((GC_PTR) &from->key, (GC_PTR) &to->key);
  129. #else
  130. GC_unregister_disappearing_link (&from->key);
  131. SCM_I_REGISTER_DISAPPEARING_LINK (&to->key, SCM2PTR (key));
  132. #endif
  133. }
  134. if ((kind == SCM_WEAK_TABLE_KIND_VALUE || kind == SCM_WEAK_TABLE_KIND_BOTH)
  135. && SCM_HEAP_OBJECT_P (value))
  136. {
  137. #ifdef HAVE_GC_MOVE_DISAPPEARING_LINK
  138. GC_move_disappearing_link ((GC_PTR) &from->value, (GC_PTR) &to->value);
  139. #else
  140. GC_unregister_disappearing_link (&from->value);
  141. SCM_I_REGISTER_DISAPPEARING_LINK (&to->value, SCM2PTR (value));
  142. #endif
  143. }
  144. }
  145. static void
  146. move_weak_entry (scm_t_weak_entry *from, scm_t_weak_entry *to,
  147. scm_t_weak_table_kind kind)
  148. {
  149. if (from->hash)
  150. {
  151. scm_t_weak_entry copy;
  152. copy_weak_entry (from, &copy);
  153. to->hash = copy.hash;
  154. to->key = copy.key;
  155. to->value = copy.value;
  156. move_disappearing_links (from, to,
  157. SCM_PACK (copy.key), SCM_PACK (copy.value),
  158. kind);
  159. }
  160. else
  161. {
  162. to->hash = 0;
  163. to->key = 0;
  164. to->value = 0;
  165. }
  166. }
  167. typedef struct {
  168. scm_t_weak_entry *entries; /* the data */
  169. scm_i_pthread_mutex_t lock; /* the lock */
  170. scm_t_weak_table_kind kind; /* what kind of table it is */
  171. unsigned long size; /* total number of slots. */
  172. unsigned long n_items; /* number of items in table */
  173. unsigned long lower; /* when to shrink */
  174. unsigned long upper; /* when to grow */
  175. int size_index; /* index into hashtable_size */
  176. int min_size_index; /* minimum size_index */
  177. } scm_t_weak_table;
  178. #define SCM_WEAK_TABLE_P(x) (SCM_HAS_TYP7 (x, scm_tc7_weak_table))
  179. #define SCM_VALIDATE_WEAK_TABLE(pos, arg) \
  180. SCM_MAKE_VALIDATE_MSG (pos, arg, WEAK_TABLE_P, "weak-table")
  181. #define SCM_WEAK_TABLE(x) ((scm_t_weak_table *) SCM_CELL_WORD_1 (x))
  182. static unsigned long
  183. hash_to_index (unsigned long hash, unsigned long size)
  184. {
  185. return (hash >> 1) % size;
  186. }
  187. static unsigned long
  188. entry_distance (unsigned long hash, unsigned long k, unsigned long size)
  189. {
  190. unsigned long origin = hash_to_index (hash, size);
  191. if (k >= origin)
  192. return k - origin;
  193. else
  194. /* The other key was displaced and wrapped around. */
  195. return size - origin + k;
  196. }
  197. static void
  198. rob_from_rich (scm_t_weak_table *table, unsigned long k)
  199. {
  200. unsigned long empty, size;
  201. size = table->size;
  202. /* If we are to free up slot K in the table, we need room to do so. */
  203. assert (table->n_items < size);
  204. empty = k;
  205. do
  206. empty = (empty + 1) % size;
  207. while (table->entries[empty].hash);
  208. do
  209. {
  210. unsigned long last = empty ? (empty - 1) : (size - 1);
  211. move_weak_entry (&table->entries[last], &table->entries[empty],
  212. table->kind);
  213. empty = last;
  214. }
  215. while (empty != k);
  216. table->entries[empty].hash = 0;
  217. table->entries[empty].key = 0;
  218. table->entries[empty].value = 0;
  219. }
  220. static void
  221. give_to_poor (scm_t_weak_table *table, unsigned long k)
  222. {
  223. /* Slot K was just freed up; possibly shuffle others down. */
  224. unsigned long size = table->size;
  225. while (1)
  226. {
  227. unsigned long next = (k + 1) % size;
  228. unsigned long hash;
  229. scm_t_weak_entry copy;
  230. hash = table->entries[next].hash;
  231. if (!hash || hash_to_index (hash, size) == next)
  232. break;
  233. copy_weak_entry (&table->entries[next], &copy);
  234. if (!copy.key || !copy.value)
  235. /* Lost weak reference. */
  236. {
  237. give_to_poor (table, next);
  238. table->n_items--;
  239. continue;
  240. }
  241. move_weak_entry (&table->entries[next], &table->entries[k],
  242. table->kind);
  243. k = next;
  244. }
  245. /* We have shuffled down any entries that should be shuffled down; now
  246. free the end. */
  247. table->entries[k].hash = 0;
  248. table->entries[k].key = 0;
  249. table->entries[k].value = 0;
  250. }
  251. /* The GC "kinds" for singly-weak tables. */
  252. static int weak_key_gc_kind;
  253. static int weak_value_gc_kind;
  254. static struct GC_ms_entry *
  255. mark_weak_key_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  256. struct GC_ms_entry *mark_stack_limit, GC_word env)
  257. {
  258. scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
  259. unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
  260. for (k = 0; k < size; k++)
  261. if (entries[k].hash && entries[k].key)
  262. {
  263. SCM value = SCM_PACK (entries[k].value);
  264. mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (value),
  265. mark_stack_ptr, mark_stack_limit,
  266. NULL);
  267. }
  268. return mark_stack_ptr;
  269. }
  270. static struct GC_ms_entry *
  271. mark_weak_value_table (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  272. struct GC_ms_entry *mark_stack_limit, GC_word env)
  273. {
  274. scm_t_weak_entry *entries = (scm_t_weak_entry*) addr;
  275. unsigned long k, size = GC_size (addr) / sizeof (scm_t_weak_entry);
  276. for (k = 0; k < size; k++)
  277. if (entries[k].hash && entries[k].value)
  278. {
  279. SCM key = SCM_PACK (entries[k].key);
  280. mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word*) SCM2PTR (key),
  281. mark_stack_ptr, mark_stack_limit,
  282. NULL);
  283. }
  284. return mark_stack_ptr;
  285. }
  286. static scm_t_weak_entry *
  287. allocate_entries (unsigned long size, scm_t_weak_table_kind kind)
  288. {
  289. scm_t_weak_entry *ret;
  290. size_t bytes = size * sizeof (*ret);
  291. switch (kind)
  292. {
  293. case SCM_WEAK_TABLE_KIND_KEY:
  294. ret = GC_generic_malloc (bytes, weak_key_gc_kind);
  295. break;
  296. case SCM_WEAK_TABLE_KIND_VALUE:
  297. ret = GC_generic_malloc (bytes, weak_value_gc_kind);
  298. break;
  299. case SCM_WEAK_TABLE_KIND_BOTH:
  300. ret = scm_gc_malloc_pointerless (bytes, "weak-table");
  301. break;
  302. default:
  303. abort ();
  304. }
  305. memset (ret, 0, bytes);
  306. return ret;
  307. }
  308. /* Growing or shrinking is triggered when the load factor
  309. *
  310. * L = N / S (N: number of items in table, S: bucket vector length)
  311. *
  312. * passes an upper limit of 0.9 or a lower limit of 0.2.
  313. *
  314. * The implementation stores the upper and lower number of items which
  315. * trigger a resize in the hashtable object.
  316. *
  317. * Possible hash table sizes (primes) are stored in the array
  318. * hashtable_size.
  319. */
  320. static unsigned long hashtable_size[] = {
  321. 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
  322. 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041, 28762081,
  323. 57524111, 115048217, 230096423
  324. };
  325. #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
  326. static int
  327. compute_size_index (scm_t_weak_table *table)
  328. {
  329. int i = table->size_index;
  330. if (table->n_items < table->lower)
  331. {
  332. /* rehashing is not triggered when i <= min_size */
  333. do
  334. --i;
  335. while (i > table->min_size_index
  336. && table->n_items < hashtable_size[i] / 5);
  337. }
  338. else if (table->n_items > table->upper)
  339. {
  340. ++i;
  341. if (i >= HASHTABLE_SIZE_N)
  342. /* The biggest size currently is 230096423, which for a 32-bit
  343. machine will occupy 2.3GB of memory at a load of 80%. There
  344. is probably something better to do here, but if you have a
  345. weak map of that size, you are hosed in any case. */
  346. abort ();
  347. }
  348. return i;
  349. }
  350. static int
  351. is_acceptable_size_index (scm_t_weak_table *table, int size_index)
  352. {
  353. int computed = compute_size_index (table);
  354. if (size_index == computed)
  355. /* We were going to grow or shrink, and allocating the new vector
  356. didn't change the target size. */
  357. return 1;
  358. if (size_index == computed + 1)
  359. {
  360. /* We were going to enlarge the table, but allocating the new
  361. vector finalized some objects, making an enlargement
  362. unnecessary. It might still be a good idea to use the larger
  363. table, though. (This branch also gets hit if, while allocating
  364. the vector, some other thread was actively removing items from
  365. the table. That is less likely, though.) */
  366. unsigned long new_lower = hashtable_size[size_index] / 5;
  367. return table->size > new_lower;
  368. }
  369. if (size_index == computed - 1)
  370. {
  371. /* We were going to shrink the table, but when we dropped the lock
  372. to allocate the new vector, some other thread added elements to
  373. the table. */
  374. return 0;
  375. }
  376. /* The computed size differs from our newly allocated size by more
  377. than one size index -- recalculate. */
  378. return 0;
  379. }
  380. static void
  381. resize_table (scm_t_weak_table *table)
  382. {
  383. scm_t_weak_entry *old_entries, *new_entries;
  384. int new_size_index;
  385. unsigned long old_size, new_size, old_k;
  386. do
  387. {
  388. new_size_index = compute_size_index (table);
  389. if (new_size_index == table->size_index)
  390. return;
  391. new_size = hashtable_size[new_size_index];
  392. scm_i_pthread_mutex_unlock (&table->lock);
  393. /* Allocating memory might cause finalizers to run, which could
  394. run anything, so drop our lock to avoid deadlocks. */
  395. new_entries = allocate_entries (new_size, table->kind);
  396. scm_i_pthread_mutex_unlock (&table->lock);
  397. }
  398. while (!is_acceptable_size_index (table, new_size_index));
  399. old_entries = table->entries;
  400. old_size = table->size;
  401. table->size_index = new_size_index;
  402. table->size = new_size;
  403. if (new_size_index <= table->min_size_index)
  404. table->lower = 0;
  405. else
  406. table->lower = new_size / 5;
  407. table->upper = 9 * new_size / 10;
  408. table->n_items = 0;
  409. table->entries = new_entries;
  410. for (old_k = 0; old_k < old_size; old_k++)
  411. {
  412. scm_t_weak_entry copy;
  413. unsigned long new_k, distance;
  414. if (!old_entries[old_k].hash)
  415. continue;
  416. copy_weak_entry (&old_entries[old_k], &copy);
  417. if (!copy.key || !copy.value)
  418. continue;
  419. new_k = hash_to_index (copy.hash, new_size);
  420. for (distance = 0; ; distance++, new_k = (new_k + 1) % new_size)
  421. {
  422. unsigned long other_hash = new_entries[new_k].hash;
  423. if (!other_hash)
  424. /* Found an empty entry. */
  425. break;
  426. /* Displace the entry if our distance is less, otherwise keep
  427. looking. */
  428. if (entry_distance (other_hash, new_k, new_size) < distance)
  429. {
  430. rob_from_rich (table, new_k);
  431. break;
  432. }
  433. }
  434. table->n_items++;
  435. new_entries[new_k].hash = copy.hash;
  436. new_entries[new_k].key = copy.key;
  437. new_entries[new_k].value = copy.value;
  438. register_disappearing_links (&new_entries[new_k],
  439. SCM_PACK (copy.key), SCM_PACK (copy.value),
  440. table->kind);
  441. }
  442. }
  443. /* Run after GC via do_vacuum_weak_table, this function runs over the
  444. whole table, removing lost weak references, reshuffling the table as it
  445. goes. It might resize the table if it reaps enough entries. */
  446. static void
  447. vacuum_weak_table (scm_t_weak_table *table)
  448. {
  449. scm_t_weak_entry *entries = table->entries;
  450. unsigned long size = table->size;
  451. unsigned long k;
  452. for (k = 0; k < size; k++)
  453. {
  454. unsigned long hash = entries[k].hash;
  455. if (hash)
  456. {
  457. scm_t_weak_entry copy;
  458. copy_weak_entry (&entries[k], &copy);
  459. if (!copy.key || !copy.value)
  460. /* Lost weak reference; reshuffle. */
  461. {
  462. give_to_poor (table, k);
  463. table->n_items--;
  464. }
  465. }
  466. }
  467. if (table->n_items < table->lower)
  468. resize_table (table);
  469. }
  470. static SCM
  471. weak_table_ref (scm_t_weak_table *table, unsigned long hash,
  472. scm_t_table_predicate_fn pred, void *closure,
  473. SCM dflt)
  474. {
  475. unsigned long k, distance, size;
  476. scm_t_weak_entry *entries;
  477. size = table->size;
  478. entries = table->entries;
  479. hash = (hash << 1) | 0x1;
  480. k = hash_to_index (hash, size);
  481. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  482. {
  483. unsigned long other_hash;
  484. retry:
  485. other_hash = entries[k].hash;
  486. if (!other_hash)
  487. /* Not found. */
  488. return dflt;
  489. if (hash == other_hash)
  490. {
  491. scm_t_weak_entry copy;
  492. copy_weak_entry (&entries[k], &copy);
  493. if (!copy.key || !copy.value)
  494. /* Lost weak reference; reshuffle. */
  495. {
  496. give_to_poor (table, k);
  497. table->n_items--;
  498. goto retry;
  499. }
  500. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  501. /* Found. */
  502. return SCM_PACK (copy.value);
  503. }
  504. /* If the entry's distance is less, our key is not in the table. */
  505. if (entry_distance (other_hash, k, size) < distance)
  506. return dflt;
  507. }
  508. /* If we got here, then we were unfortunate enough to loop through the
  509. whole table. Shouldn't happen, but hey. */
  510. return dflt;
  511. }
  512. static void
  513. weak_table_put_x (scm_t_weak_table *table, unsigned long hash,
  514. scm_t_table_predicate_fn pred, void *closure,
  515. SCM key, SCM value)
  516. {
  517. unsigned long k, distance, size;
  518. scm_t_weak_entry *entries;
  519. size = table->size;
  520. entries = table->entries;
  521. hash = (hash << 1) | 0x1;
  522. k = hash_to_index (hash, size);
  523. for (distance = 0; ; distance++, k = (k + 1) % size)
  524. {
  525. unsigned long other_hash;
  526. retry:
  527. other_hash = entries[k].hash;
  528. if (!other_hash)
  529. /* Found an empty entry. */
  530. break;
  531. if (other_hash == hash)
  532. {
  533. scm_t_weak_entry copy;
  534. copy_weak_entry (&entries[k], &copy);
  535. if (!copy.key || !copy.value)
  536. /* Lost weak reference; reshuffle. */
  537. {
  538. give_to_poor (table, k);
  539. table->n_items--;
  540. goto retry;
  541. }
  542. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  543. /* Found an entry with this key. */
  544. break;
  545. }
  546. if (table->n_items > table->upper)
  547. /* Full table, time to resize. */
  548. {
  549. resize_table (table);
  550. return weak_table_put_x (table, hash >> 1, pred, closure, key, value);
  551. }
  552. /* Displace the entry if our distance is less, otherwise keep
  553. looking. */
  554. if (entry_distance (other_hash, k, size) < distance)
  555. {
  556. rob_from_rich (table, k);
  557. break;
  558. }
  559. }
  560. if (entries[k].hash)
  561. unregister_disappearing_links (&entries[k], table->kind);
  562. else
  563. table->n_items++;
  564. entries[k].hash = hash;
  565. entries[k].key = SCM_UNPACK (key);
  566. entries[k].value = SCM_UNPACK (value);
  567. register_disappearing_links (&entries[k], key, value, table->kind);
  568. }
  569. static void
  570. weak_table_remove_x (scm_t_weak_table *table, unsigned long hash,
  571. scm_t_table_predicate_fn pred, void *closure)
  572. {
  573. unsigned long k, distance, size;
  574. scm_t_weak_entry *entries;
  575. size = table->size;
  576. entries = table->entries;
  577. hash = (hash << 1) | 0x1;
  578. k = hash_to_index (hash, size);
  579. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  580. {
  581. unsigned long other_hash;
  582. retry:
  583. other_hash = entries[k].hash;
  584. if (!other_hash)
  585. /* Not found. */
  586. return;
  587. if (other_hash == hash)
  588. {
  589. scm_t_weak_entry copy;
  590. copy_weak_entry (&entries[k], &copy);
  591. if (!copy.key || !copy.value)
  592. /* Lost weak reference; reshuffle. */
  593. {
  594. give_to_poor (table, k);
  595. table->n_items--;
  596. goto retry;
  597. }
  598. if (pred (SCM_PACK (copy.key), SCM_PACK (copy.value), closure))
  599. /* Found an entry with this key. */
  600. {
  601. entries[k].hash = 0;
  602. entries[k].key = 0;
  603. entries[k].value = 0;
  604. unregister_disappearing_links (&entries[k], table->kind);
  605. if (--table->n_items < table->lower)
  606. resize_table (table);
  607. else
  608. give_to_poor (table, k);
  609. return;
  610. }
  611. }
  612. /* If the entry's distance is less, our key is not in the table. */
  613. if (entry_distance (other_hash, k, size) < distance)
  614. return;
  615. }
  616. }
  617. static SCM
  618. make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  619. {
  620. scm_t_weak_table *table;
  621. int i = 0, n = k ? k : 31;
  622. while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
  623. ++i;
  624. n = hashtable_size[i];
  625. table = scm_gc_malloc (sizeof (*table), "weak-table");
  626. table->entries = allocate_entries (n, kind);
  627. table->kind = kind;
  628. table->n_items = 0;
  629. table->size = n;
  630. table->lower = 0;
  631. table->upper = 9 * n / 10;
  632. table->size_index = i;
  633. table->min_size_index = i;
  634. scm_i_pthread_mutex_init (&table->lock, NULL);
  635. return scm_cell (scm_tc7_weak_table, (scm_t_bits)table);
  636. }
  637. void
  638. scm_i_weak_table_print (SCM exp, SCM port, scm_print_state *pstate)
  639. {
  640. scm_puts_unlocked ("#<", port);
  641. scm_puts_unlocked ("weak-table ", port);
  642. scm_uintprint (SCM_WEAK_TABLE (exp)->n_items, 10, port);
  643. scm_putc_unlocked ('/', port);
  644. scm_uintprint (SCM_WEAK_TABLE (exp)->size, 10, port);
  645. scm_puts_unlocked (">", port);
  646. }
  647. static void
  648. do_vacuum_weak_table (SCM table)
  649. {
  650. scm_t_weak_table *t;
  651. t = SCM_WEAK_TABLE (table);
  652. if (scm_i_pthread_mutex_trylock (&t->lock) == 0)
  653. {
  654. vacuum_weak_table (t);
  655. scm_i_pthread_mutex_unlock (&t->lock);
  656. }
  657. return;
  658. }
  659. /* The before-gc C hook only runs if GC_table_start_callback is available,
  660. so if not, fall back on a finalizer-based implementation. */
  661. static int
  662. weak_gc_callback (void **weak)
  663. {
  664. void *val = weak[0];
  665. void (*callback) (SCM) = weak[1];
  666. if (!val)
  667. return 0;
  668. callback (SCM_PACK_POINTER (val));
  669. return 1;
  670. }
  671. #ifdef HAVE_GC_TABLE_START_CALLBACK
  672. static void*
  673. weak_gc_hook (void *hook_data, void *fn_data, void *data)
  674. {
  675. if (!weak_gc_callback (fn_data))
  676. scm_c_hook_remove (&scm_before_gc_c_hook, weak_gc_hook, fn_data);
  677. return NULL;
  678. }
  679. #else
  680. static void
  681. weak_gc_finalizer (void *ptr, void *data)
  682. {
  683. if (weak_gc_callback (ptr))
  684. scm_i_set_finalizer (ptr, weak_gc_finalizer, data);
  685. }
  686. #endif
  687. static void
  688. scm_c_register_weak_gc_callback (SCM obj, void (*callback) (SCM))
  689. {
  690. void **weak = GC_MALLOC_ATOMIC (sizeof (void*) * 2);
  691. weak[0] = SCM_UNPACK_POINTER (obj);
  692. weak[1] = (void*)callback;
  693. GC_GENERAL_REGISTER_DISAPPEARING_LINK (weak, SCM2PTR (obj));
  694. #ifdef HAVE_GC_TABLE_START_CALLBACK
  695. scm_c_hook_add (&scm_after_gc_c_hook, weak_gc_hook, weak, 0);
  696. #else
  697. scm_i_set_finalizer (weak, weak_gc_finalizer, NULL);
  698. #endif
  699. }
  700. SCM
  701. scm_c_make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
  702. {
  703. SCM ret;
  704. ret = make_weak_table (k, kind);
  705. scm_c_register_weak_gc_callback (ret, do_vacuum_weak_table);
  706. return ret;
  707. }
  708. SCM
  709. scm_weak_table_p (SCM obj)
  710. {
  711. return scm_from_bool (SCM_WEAK_TABLE_P (obj));
  712. }
  713. SCM
  714. scm_c_weak_table_ref (SCM table, unsigned long raw_hash,
  715. scm_t_table_predicate_fn pred,
  716. void *closure, SCM dflt)
  717. #define FUNC_NAME "weak-table-ref"
  718. {
  719. SCM ret;
  720. scm_t_weak_table *t;
  721. SCM_VALIDATE_WEAK_TABLE (1, table);
  722. t = SCM_WEAK_TABLE (table);
  723. scm_i_pthread_mutex_lock (&t->lock);
  724. ret = weak_table_ref (t, raw_hash, pred, closure, dflt);
  725. scm_i_pthread_mutex_unlock (&t->lock);
  726. return ret;
  727. }
  728. #undef FUNC_NAME
  729. void
  730. scm_c_weak_table_put_x (SCM table, unsigned long raw_hash,
  731. scm_t_table_predicate_fn pred,
  732. void *closure, SCM key, SCM value)
  733. #define FUNC_NAME "weak-table-put!"
  734. {
  735. scm_t_weak_table *t;
  736. SCM_VALIDATE_WEAK_TABLE (1, table);
  737. t = SCM_WEAK_TABLE (table);
  738. scm_i_pthread_mutex_lock (&t->lock);
  739. weak_table_put_x (t, raw_hash, pred, closure, key, value);
  740. scm_i_pthread_mutex_unlock (&t->lock);
  741. }
  742. #undef FUNC_NAME
  743. void
  744. scm_c_weak_table_remove_x (SCM table, unsigned long raw_hash,
  745. scm_t_table_predicate_fn pred,
  746. void *closure)
  747. #define FUNC_NAME "weak-table-remove!"
  748. {
  749. scm_t_weak_table *t;
  750. SCM_VALIDATE_WEAK_TABLE (1, table);
  751. t = SCM_WEAK_TABLE (table);
  752. scm_i_pthread_mutex_lock (&t->lock);
  753. weak_table_remove_x (t, raw_hash, pred, closure);
  754. scm_i_pthread_mutex_unlock (&t->lock);
  755. }
  756. #undef FUNC_NAME
  757. static int
  758. assq_predicate (SCM x, SCM y, void *closure)
  759. {
  760. return scm_is_eq (x, SCM_PACK_POINTER (closure));
  761. }
  762. SCM
  763. scm_weak_table_refq (SCM table, SCM key, SCM dflt)
  764. {
  765. if (SCM_UNBNDP (dflt))
  766. dflt = SCM_BOOL_F;
  767. return scm_c_weak_table_ref (table, scm_ihashq (key, -1),
  768. assq_predicate, SCM_UNPACK_POINTER (key),
  769. dflt);
  770. }
  771. void
  772. scm_weak_table_putq_x (SCM table, SCM key, SCM value)
  773. {
  774. scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
  775. assq_predicate, SCM_UNPACK_POINTER (key),
  776. key, value);
  777. }
  778. void
  779. scm_weak_table_remq_x (SCM table, SCM key)
  780. {
  781. scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
  782. assq_predicate, SCM_UNPACK_POINTER (key));
  783. }
  784. void
  785. scm_weak_table_clear_x (SCM table)
  786. #define FUNC_NAME "weak-table-clear!"
  787. {
  788. scm_t_weak_table *t;
  789. SCM_VALIDATE_WEAK_TABLE (1, table);
  790. t = SCM_WEAK_TABLE (table);
  791. scm_i_pthread_mutex_lock (&t->lock);
  792. memset (t->entries, 0, sizeof (scm_t_weak_entry) * t->size);
  793. t->n_items = 0;
  794. scm_i_pthread_mutex_unlock (&t->lock);
  795. }
  796. #undef FUNC_NAME
  797. SCM
  798. scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
  799. SCM init, SCM table)
  800. {
  801. scm_t_weak_table *t;
  802. scm_t_weak_entry *entries;
  803. unsigned long k, size;
  804. t = SCM_WEAK_TABLE (table);
  805. scm_i_pthread_mutex_lock (&t->lock);
  806. size = t->size;
  807. entries = t->entries;
  808. for (k = 0; k < size; k++)
  809. {
  810. if (entries[k].hash)
  811. {
  812. scm_t_weak_entry copy;
  813. copy_weak_entry (&entries[k], &copy);
  814. if (copy.key && copy.value)
  815. {
  816. /* Release table lock while we call the function. */
  817. scm_i_pthread_mutex_unlock (&t->lock);
  818. init = proc (closure,
  819. SCM_PACK (copy.key), SCM_PACK (copy.value),
  820. init);
  821. scm_i_pthread_mutex_lock (&t->lock);
  822. }
  823. }
  824. }
  825. scm_i_pthread_mutex_unlock (&t->lock);
  826. return init;
  827. }
  828. static SCM
  829. fold_trampoline (void *closure, SCM k, SCM v, SCM init)
  830. {
  831. return scm_call_3 (SCM_PACK_POINTER (closure), k, v, init);
  832. }
  833. SCM
  834. scm_weak_table_fold (SCM proc, SCM init, SCM table)
  835. #define FUNC_NAME "weak-table-fold"
  836. {
  837. SCM_VALIDATE_WEAK_TABLE (3, table);
  838. SCM_VALIDATE_PROC (1, proc);
  839. return scm_c_weak_table_fold (fold_trampoline, SCM_UNPACK_POINTER (proc), init, table);
  840. }
  841. #undef FUNC_NAME
  842. static SCM
  843. for_each_trampoline (void *closure, SCM k, SCM v, SCM seed)
  844. {
  845. scm_call_2 (SCM_PACK_POINTER (closure), k, v);
  846. return seed;
  847. }
  848. void
  849. scm_weak_table_for_each (SCM proc, SCM table)
  850. #define FUNC_NAME "weak-table-for-each"
  851. {
  852. SCM_VALIDATE_WEAK_TABLE (2, table);
  853. SCM_VALIDATE_PROC (1, proc);
  854. scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
  855. }
  856. #undef FUNC_NAME
  857. static SCM
  858. map_trampoline (void *closure, SCM k, SCM v, SCM seed)
  859. {
  860. return scm_cons (scm_call_2 (SCM_PACK_POINTER (closure), k, v), seed);
  861. }
  862. SCM
  863. scm_weak_table_map_to_list (SCM proc, SCM table)
  864. #define FUNC_NAME "weak-table-map->list"
  865. {
  866. SCM_VALIDATE_WEAK_TABLE (2, table);
  867. SCM_VALIDATE_PROC (1, proc);
  868. return scm_c_weak_table_fold (map_trampoline, SCM_UNPACK_POINTER (proc), SCM_EOL, table);
  869. }
  870. #undef FUNC_NAME
  871. /* Legacy interface. */
  872. SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0,
  873. (SCM n),
  874. "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
  875. "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
  876. "Return a weak hash table with @var{size} buckets.\n"
  877. "\n"
  878. "You can modify weak hash tables in exactly the same way you\n"
  879. "would modify regular hash tables. (@pxref{Hash Tables})")
  880. #define FUNC_NAME s_scm_make_weak_key_hash_table
  881. {
  882. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  883. SCM_WEAK_TABLE_KIND_KEY);
  884. }
  885. #undef FUNC_NAME
  886. SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 0, 1, 0,
  887. (SCM n),
  888. "Return a hash table with weak values with @var{size} buckets.\n"
  889. "(@pxref{Hash Tables})")
  890. #define FUNC_NAME s_scm_make_weak_value_hash_table
  891. {
  892. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  893. SCM_WEAK_TABLE_KIND_VALUE);
  894. }
  895. #undef FUNC_NAME
  896. SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0,
  897. (SCM n),
  898. "Return a hash table with weak keys and values with @var{size}\n"
  899. "buckets. (@pxref{Hash Tables})")
  900. #define FUNC_NAME s_scm_make_doubly_weak_hash_table
  901. {
  902. return scm_c_make_weak_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n),
  903. SCM_WEAK_TABLE_KIND_BOTH);
  904. }
  905. #undef FUNC_NAME
  906. SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
  907. (SCM obj),
  908. "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n"
  909. "@deffnx {Scheme Procedure} doubly-weak-hash-table? obj\n"
  910. "Return @code{#t} if @var{obj} is the specified weak hash\n"
  911. "table. Note that a doubly weak hash table is neither a weak key\n"
  912. "nor a weak value hash table.")
  913. #define FUNC_NAME s_scm_weak_key_hash_table_p
  914. {
  915. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  916. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_KEY);
  917. }
  918. #undef FUNC_NAME
  919. SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
  920. (SCM obj),
  921. "Return @code{#t} if @var{obj} is a weak value hash table.")
  922. #define FUNC_NAME s_scm_weak_value_hash_table_p
  923. {
  924. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  925. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_VALUE);
  926. }
  927. #undef FUNC_NAME
  928. SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
  929. (SCM obj),
  930. "Return @code{#t} if @var{obj} is a doubly weak hash table.")
  931. #define FUNC_NAME s_scm_doubly_weak_hash_table_p
  932. {
  933. return scm_from_bool (SCM_WEAK_TABLE_P (obj) &&
  934. SCM_WEAK_TABLE (obj)->kind == SCM_WEAK_TABLE_KIND_BOTH);
  935. }
  936. #undef FUNC_NAME
  937. void
  938. scm_weak_table_prehistory (void)
  939. {
  940. weak_key_gc_kind =
  941. GC_new_kind (GC_new_free_list (),
  942. GC_MAKE_PROC (GC_new_proc (mark_weak_key_table), 0),
  943. 0, 0);
  944. weak_value_gc_kind =
  945. GC_new_kind (GC_new_free_list (),
  946. GC_MAKE_PROC (GC_new_proc (mark_weak_value_table), 0),
  947. 0, 0);
  948. }
  949. void
  950. scm_init_weak_table ()
  951. {
  952. #include "libguile/weak-table.x"
  953. }
  954. /*
  955. Local Variables:
  956. c-file-style: "gnu"
  957. End:
  958. */