1Ephemeron(3) OCamldoc Ephemeron(3)
2
3
4
6 Ephemeron - Ephemerons and weak hash table
7
9 Module Ephemeron
10
12 Module Ephemeron
13 : sig end
14
15
16 Ephemerons and weak hash table
17
18
19
20
21
22
23
24 === Ephemerons and weak hash table Ephemerons and weak hash table are
25 useful when one wants to cache or memorize the computation of a func‐
26 tion, as long as the arguments and the function are used, without cre‐
27 ating memory leaks by continuously keeping old computation results that
28 are not useful anymore because one argument or the function is freed.
29 An implementation using .t is not suitable because all associations
30 would keep in memory the arguments and the result. Ephemerons can also
31 be used for adding a field to an arbitrary boxed ocaml value: you can
32 attach an information to a value created by an external library without
33 memory leaks. Ephemerons hold some keys and one or no data. They are
34 all boxed ocaml values. The keys of an ephemeron have the same behavior
35 than weak pointers according to the garbage collector. In fact ocaml
36 weak pointers are implemented as ephemerons without data. The keys and
37 data of an ephemeron are said to be full if they point to a value,
38 empty if the value have never been set, have been unset, or was erased
39 by the GC. In the function that accesses the keys or data these two
40 states are represented by the option type. The data is considered by
41 the garbage collector alive if all the full keys are alive and if the
42 ephemeron is alive. When one of the keys is not considered alive any‐
43 more by the GC, the data is emptied from the ephemeron. The data could
44 be alive for another reason and in that case the GC will not free it,
45 but the ephemeron will not hold the data anymore. The ephemerons com‐
46 plicate the notion of liveness of values, because it is not anymore an
47 equivalence with the reachability from root value by usual pointers
48 (not weak and not ephemerons). With ephemerons the notion of liveness
49 is constructed by the least fixpoint of: A value is alive if: - it is a
50 root value - it is reachable from alive value by usual pointers - it is
51 the data of an alive ephemeron with all its full keys alive Notes: -
52 All the types defined in this module cannot be marshaled using Perva‐
53 sives.output_value or the functions of the Marshal module. Ephemerons
54 are defined in a language agnostic way in this paper: B. Hayes,
55 Ephemerons: a New Finalization Mechanism, OOPSLA'9 ===
56
57
58 module type S = sig end
59
60
61 The output signature of the functor Ephemeron.K1.Make and
62 Ephemeron.K2.Make . These hash tables are weak in the keys. If all the
63 keys of a binding are alive the binding is kept, but if one of the keys
64 of the binding is dead then the binding is removed.
65
66
67 module type SeededS = sig end
68
69
70 The output signature of the functor Ephemeron.K1.MakeSeeded and
71 Ephemeron.K2.MakeSeeded .
72
73
74 module K1 : sig end
75
76
77
78
79 module K2 : sig end
80
81
82
83
84 module Kn : sig end
85
86
87
88
89 module GenHashTable : sig end
90
91
92
93
94
95
96
972018-04-14 source: Ephemeron(3)