.TH "Ephemeron" 3o source: 2019-01-25 OCamldoc "OCaml library" .SH NAME Ephemeron \- Ephemerons and weak hash table .SH Module Module Ephemeron .SH Documentation .sp Module .BI "Ephemeron" : .B sig end .sp Ephemerons and weak hash table .sp .sp .sp .PP .B === Ephemerons and weak hash table .B .B Ephemerons and weak hash table are useful when one wants to cache .B or memorize the computation of a function, as long as the .B arguments and the function are used, without creating memory leaks .B by continuously keeping old computation results that are not .B useful anymore because one argument or the function is freed\&. An .B implementation using \&.t is not suitable because all .B associations would keep in memory the arguments and the result\&. .B .B Ephemerons can also be used for "adding" a field to an arbitrary .B boxed ocaml value: you can attach an information to a value .B created by an external library without memory leaks\&. .B .B Ephemerons hold some keys and one or no data\&. They are all boxed .B ocaml values\&. The keys of an ephemeron have the same behavior .B than weak pointers according to the garbage collector\&. In fact .B ocaml weak pointers are implemented as ephemerons without data\&. .B .B The keys and data of an ephemeron are said to be full if they .B point to a value, empty if the value have never been set, have .B been unset, or was erased by the GC\&. In the function that accesses .B the keys or data these two states are represented by the option .B type\&. .B .B The data is considered by the garbage collector alive if all the .B full keys are alive and if the ephemeron is alive\&. When one of the .B keys is not considered alive anymore by the GC, the data is .B emptied from the ephemeron\&. The data could be alive for another .B reason and in that case the GC will not free it, but the ephemeron .B will not hold the data anymore\&. .B .B The ephemerons complicate the notion of liveness of values, because .B it is not anymore an equivalence with the reachability from root .B value by usual pointers (not weak and not ephemerons)\&. With ephemerons .B the notion of liveness is constructed by the least fixpoint of: .B A value is alive if: .B \- it is a root value .B \- it is reachable from alive value by usual pointers .B \- it is the data of an alive ephemeron with all its full keys alive .B .B Notes: .B \- All the types defined in this module cannot be marshaled .B using Pervasives\&.output_value or the functions of the .B Marshal module\&. .B .B Ephemerons are defined in a language agnostic way in this paper: .B B\&. Hayes, Ephemerons: a New Finalization Mechanism, OOPSLA\&'9 === .PP .I module type S = .B sig end .sp The output signature of the functor .B Ephemeron\&.K1\&.Make and .B Ephemeron\&.K2\&.Make \&. These hash tables are weak in the keys\&. If all the keys of a binding are alive the binding is kept, but if one of the keys of the binding is dead then the binding is removed\&. .sp .I module type SeededS = .B sig end .sp The output signature of the functor .B Ephemeron\&.K1\&.MakeSeeded and .B Ephemeron\&.K2\&.MakeSeeded \&. .sp .I module K1 : .B sig end .sp .sp .I module K2 : .B sig end .sp .sp .I module Kn : .B sig end .sp .sp .I module GenHashTable : .B sig end .sp .sp