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