@nogc with assoc array
Jonathan Marler via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Feb 16 13:12:01 PST 2015
On Monday, 16 February 2015 at 19:12:45 UTC, FG wrote:
> Range violation is an Error, but never mind that. The real
> question is: given all the work related to @nogc, wouldn't it
> be better for such common Errors to be preallocated and only
> have file and line updated when they are thrown?
>
> @nogc already, because they simply cast
> typeid(OutOfMemoryError).init or
> typeid(InvalidMemoryOperationError).init:
> extern (C) void onOutOfMemoryError(void* pretend_sideffect =
> null) @trusted pure nothrow @nogc
> extern (C) void onInvalidMemoryOperationError(void*
> pretend_sideffect = null) @trusted pure nothrow @nogc
>
> Could be made @nogc with one object of each kind preallocated:
> extern (C) void onAssertError( string file = __FILE__, size_t
> line = __LINE__ ) nothrow
> extern (C) void onRangeError( string file = __FILE__, size_t
> line = __LINE__ ) @safe pure nothrow
> extern (C) void onSwitchError( string file = __FILE__, size_t
> line = __LINE__ ) @safe pure nothrow
This could be a good idea for some types of exceptions. I
believe OutOfMemory is already pre-allocated (it has to be since
you can't allocate it once you are out of memory). The problem
with your suggestion is that if you allow the exception to be
updated with the line number/filename(it isn't immutable), then
you have to store it in TLS memory. That may be an acceptable
tradeoff, but you have to take that into consideration. Also if
you have a chain of exceptions you wouldn't be able to include
the same exception more then once in the chain.
The problem D has with exceptions and GC memory is complex and
will have different optimal solutions in different cases. In
some cases, it would be better for D to support non-GC heap
allocated exceptions. Maybe these types of exceptions could be
derived from another class so the user code will know that the
memory needs to be freed. There are also other ideas but my
point is we should make a plan about what solutions we think
would be good to implement and determine which ones we want to
tackle first.
More information about the Digitalmars-d-learn
mailing list