Thread overview
Using onOutOfMemoryError in C wrappers
Mar 24, 2021
Per Nordlöw
Mar 24, 2021
Basile B.
Mar 24, 2021
Per Nordlöw
Mar 24, 2021
Basile B.
Mar 24, 2021
Per Nordlöw
Mar 24, 2021
Jack
March 24, 2021
When wrapping C code that tries to allocate memory resources via functions such as

    X* X_create();

should one call `onOutOfMemoryError();` upon null return?

Making more D wrappers `nothrow @nogc`.
March 24, 2021
On Wednesday, 24 March 2021 at 07:58:22 UTC, Per Nordlöw wrote:
> When wrapping C code that tries to allocate memory resources via functions such as
>
>     X* X_create();
>
> should one call `onOutOfMemoryError();` upon null return?
>
> Making more D wrappers `nothrow @nogc`.


There are several ways to do that. In addition to onOutOfMemoryError, you can use a static instance or `if (somePtr is null) assert(0);`

  void v() @nogc nothrow
  {
    __gshared oom = new OutOfMemoryError();
    auto X* = X_create();
    if (X is null)
        throw oom;
  }
March 24, 2021
On Wednesday, 24 March 2021 at 08:31:19 UTC, Basile B. wrote:
> There are several ways to do that. In addition to onOutOfMemoryError, you can use a static instance
>
>   void v() @nogc nothrow
>   {
>     __gshared oom = new OutOfMemoryError();
>     auto X* = X_create();
>     if (X is null)
>         throw oom;
>   }

How is that better than

    void v() @nogc nothrow
    {
        auto X* = X_create();
        if (X is null)
            onOutOfMemoryError();
    }

considering the fact that accessing `__gshared` state is neither `@safe` nor `pure`? Which, in turn, makes `v()` unconditionally unsafe and unpure regardless of safety and purity of `X_create()`.
March 24, 2021
On Wednesday, 24 March 2021 at 08:51:34 UTC, Per Nordlöw wrote:
> On Wednesday, 24 March 2021 at 08:31:19 UTC, Basile B. wrote:
>> There are several ways to do that. In addition to onOutOfMemoryError, you can use a static instance
>>
>>   void v() @nogc nothrow
>>   {
>>     __gshared oom = new OutOfMemoryError();
>>     auto X* = X_create();
>>     if (X is null)
>>         throw oom;
>>   }
>
> How is that better than
>
>     void v() @nogc nothrow
>     {
>         auto X* = X_create();
>         if (X is null)
>             onOutOfMemoryError();
>     }
>
> considering the fact that accessing `__gshared` state is neither `@safe` nor `pure`? Which, in turn, makes `v()` unconditionally unsafe and unpure regardless of safety and purity of `X_create()`.

In now way. But in your question you mentioned @nogc nothrow and not @safe and pure.
March 24, 2021
On Wednesday, 24 March 2021 at 08:31:19 UTC, Basile B. wrote:
> On Wednesday, 24 March 2021 at 07:58:22 UTC, Per Nordlöw wrote:
>> When wrapping C code that tries to allocate memory resources via functions such as
>>
>>     X* X_create();
>>
>> should one call `onOutOfMemoryError();` upon null return?
>>
>> Making more D wrappers `nothrow @nogc`.
>
>
> There are several ways to do that. In addition to onOutOfMemoryError, you can use a static instance or `if (somePtr is null) assert(0);`
>
>   void v() @nogc nothrow
>   {
>     __gshared oom = new OutOfMemoryError();
>     auto X* = X_create();
>     if (X is null)
>         throw oom;
>   }

why are you creating oom variable before the if(x is null) check?
March 24, 2021
On Wednesday, 24 March 2021 at 10:25:21 UTC, Basile B. wrote:
> In now way. But in your question you mentioned @nogc nothrow and not @safe and pure.

Ok, thanks.

Note that core.exception.onOutOfMemoryError is already qualified as @nogc nothrow pure @trusted.