How to compile Phobos with other D code to create a shared library?

Steven Schveighoffer schveiguy at gmail.com
Tue Jun 1 14:32:33 UTC 2021


On 5/31/21 5:46 PM, data pulverizer wrote:
> On Monday, 31 May 2021 at 21:26:15 UTC, Steven Schveighoffer wrote:
>>
>> You need to call it wherever you think it might not have been called yet.
>>
>> It's reentrant, so if you call it more than once, it will only 
>> initialize once, and count how many times you have to call 
>> `Runtime.terminate`.
>>
>> Best to use a `scope(exit)` to call `Runtime.terminate` if you are 
>> calling it periodically.
> 
> Many thanks!
> 
> Something interesting is using arrays. I can see that if you instantiate 
> an array within the D function using `new`, for instance
> 
> ```
> auto result = new double[n];
> ```
> 
> you can't return that array to Julia (or whatever) if your function does
> 
> ```
> scope(exit) Runtime.terminate();
> ```
> 
> and it segfaults. But while you can do:
> 
> ```
> auto result = cast(double*)malloc(double.sizeof * n);
> ```
> 
> you're left with the issue of not being able to free the memory since 
> you've terminated the runtime.
> 
> So I guess you have to appropriately pair up `initialize` and 
> `terminate` as appropriate to kind of *startup* and and *shutdown* the 
> connection with druntime with your allocations if they stay within D?
> 

You should use whatever Julia is expecting for memory management. I 
don't know what that is, but typically you want to use your host 
language's memory management system to create blocks instead of the D GC.

-Steve


More information about the Digitalmars-d-learn mailing list