Antipattern in core.memory.GC.addRange?
Petar
Petar
Sat Sep 23 05:55:49 UTC 2017
On Friday, 22 September 2017 at 21:29:10 UTC, Steven
Schveighoffer wrote:
> GC.addRange has this signature:
>
> static nothrow @nogc void addRange(in void* p, size_t sz, const
> TypeInfo ti = null);
>
> I see a large problem with this. Let's say you malloc an array
> of struct pointers:
>
> struct Foo { ... }
>
> import core.stdc.stdlib;
> auto ptrs = (cast(Foo *)malloc(Foo.sizeof * 10))[0 .. 10];
>
> Now, you want to store GC pointers in that block, you need to
> add the range to the GC:
>
> GC.addRange(ptrs.ptr, ptrs.length);
>
> See the problem? Why would addRange work this way, when D has
> such a better mechanism for this? Can we fix it?
>
> -Steve
How about adding a template wrapper function, along the lines of:
static void addRange(T)(const T[] arr)
{
addRange(arr.ptr, T.sizeof * arr.length, typeid(T));
}
To core.memory.GC?
More information about the Digitalmars-d
mailing list