Memory management and local GC?
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Sat Oct 31 19:31:23 UTC 2020
On Saturday, 31 October 2020 at 19:05:43 UTC, Kagamin wrote:
> Say, allocate a dictionary, network connection or XML document
> and put it in shared context. How do you deal with them?
Ok, so we assume @safe. When you allocate you have access to it
as an "isolated shared" which taint all references obtained
through it, so you cannot store references to the internals. BUT,
you can configure it.
After you are done with the isolated configuration/usage you do a
move operation to transfer it to a shared pointer (effectively
encapsulating it). Then you need to obtain access to it through
the implementation of the shared protocol for the object (not
implemented as @safe).
So for a dictionary, where you want to store references to local
objects you would need a dictionary written especially for shared
access as you would need to have a borrow-pointer to keep it
pinned in the "foreign" GC-pool. So it is up to the dictionary
implementation to prevent multiple threads to access the same
object at the same time. So that makes it possible to set up a
shared cache and use it in @safe code, but the object accesses
have to be limited somewhat.
We need to think of shared globals as facades in this model.
A network connection would probably best be implemented as a
struct that you embed as a private field in a shared class object.
Or maybe you think about something else? I expect there to be
pitfalls, so more feedback is good. :-)
More information about the Digitalmars-d
mailing list