DIP60: @nogc attribute
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Tue Apr 15 12:12:30 PDT 2014
On Tue, 15 Apr 2014 14:58:25 -0400, Walter Bright
<newshound2 at digitalmars.com> wrote:
> On 4/15/2014 11:41 AM, Steven Schveighoffer wrote:
>> On Tue, 15 Apr 2014 13:01:40 -0400, Walter Bright
>> <newshound2 at digitalmars.com>
>> wrote:
>>
>>> http://wiki.dlang.org/DIP60'
>>
>> Wow, that's quite underspecified.
>
> Ok, but I don't see how.
i.e. the rest of my post.
>> What about syscalls?
>
> Not sure what you mean by that, but obviously Windows API functions
> would be @nogc.
Linux syscalls are not Windows API, they are extern(C) calls. Basically,
we will have to mark most as @nogc, right? or are extern(C) calls
automatically considered @nogc?
>> Nothrow functions allow one to call non-nothrow functions,
>> but catch all exceptions. What if you want to use the GC for allocation
>> temporarily, and then delete all the usage before returning?
>
> @nogc doesn't allow an escape from it. That's the point of it.
My point:
void foo1() {throw new Exception("hi");}
void foo2() nothrow { try {foo1();} catch(Exception e) {} }
This is valid.
int isthisok(int x, int y) @nogc
{
// need scratch space
int[] buf = new int[x * y];
scope(exit) GC.free(buf.ptr);
// perform some algorithm using buf
...
//
return buf[$-1];
}
Valid?
>> Must you use C malloc/free?
>
> If you can GC/GCfree, then you can use malloc/free.
What if you can't use GC/GCfree? i.e. @nogc (the point of this thread)
>> What about such code that is @safe, which cannot call free?
>
> There's no point to @nogc if you can still call the GC in it.
This is a follow-on to the previous question. Let's say you cannot use GC,
but clearly, I can use C malloc/free. Safe code that needs arbitrary
buffers must use C malloc/free to manage them, but safe cannot legally
call free.
I think we would need some sort of scoped allocator to make life bearable.
-Steve
More information about the Digitalmars-d
mailing list