package ocaml-compiler
Build not yet checkedInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=44381a9c2c79abd3ef9bf2cc643bcbe7b394b73af76a0552ca7326e59c661ac0
doc/stdlib/Stdlib/Weak/index.html
Module Stdlib.WeakSource
Arrays of weak pointers and hash sets of weak pointers.
Low-level functions
The type of arrays of weak pointers (weak arrays). A weak pointer is a value that the garbage collector may erase whenever the value is not used any more (through normal pointers) by the program. Note that finalisation functions are run before the weak pointers are erased, because the finalisation functions can make values alive again (before 4.03 the finalisation functions were run after).
A weak pointer is said to be full if it points to a value, empty if the value was erased by the GC.
Notes:
- Integers are not allocated and cannot be stored in weak arrays.
- Weak arrays cannot be marshaled using
Stdlib.output_valuenor the functions of theMarshalmodule.
Weak.create n returns a new weak array of length n. All the pointers are initially empty.
Weak.set ar n (Some el) sets the nth cell of ar to be a (full) pointer to el; Weak.set ar n None sets the nth cell of ar to empty.
Weak.get ar n returns None if the nth cell of ar is empty, Some x (where x is the value) if it is full.
In OCaml versions up to 5.5, Weak.get_copy ar n would return a shallow copy of the weak key when it is still alive; this had the benefit of not forcing the key itself to remain alive, while being able to inspect its value.
If the element is a custom block or a continuation it is not copied.
Performing a shallow-copy is error-prone and proved to interact badly with other parts of the language (for example mutable values); the performance benefits were often offset by the cost of the copy. This function is now deprecated since OCaml 5.6, and behaves as an alias to get ar n.
Note: you can also use check below if you want to check that a key is still set without accessing it.
Weak.check ar n returns true if the nth cell of ar is full, false if it is empty. Note that even if Weak.check ar n returns true, a subsequent Weak.get ar n can return None.
Weak.fill ar ofs len el sets to el all pointers of ar from ofs to ofs + len - 1.
Weak.blit ar1 off1 ar2 off2 len copies len weak pointers from ar1 (starting at off1) to ar2 (starting at off2). It works correctly even if ar1 and ar2 are the same.
Weak hash sets
A weak hash set is a hashed set of values. Each value may magically disappear from the set when it is not used by the rest of the program any more. This is normally used to share data structures without inducing memory leaks. Weak hash sets are defined on values from a Hashtbl.HashedType module; the equal relation and hash function are taken from that module. We will say that v is an instance of x if equal x v is true.
The equal relation must be able to work on a shallow copy of the values and give the same result as with the values themselves.
Unsynchronized accesses
Unsynchronized accesses to weak hash sets are a programming error. Unsynchronized accesses to a weak hash set may lead to an invalid weak hash set state. Thus, concurrent accesses to weak hash sets must be synchronized (for instance with a Mutex.t).