Is it possible to assumeSafeAppend malloced memory?
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 19 06:44:44 PDT 2014
On Monday, 19 May 2014 at 06:08:18 UTC, Ali Çehreli wrote:
> We know that most of the time memory is allocated more than the
> requested amount. Is there a way to take advantage of that
> extra trailing space? (And potentially the pages that come
> after that.)
>
> import core.memory;
>
> void main()
> {
> const count = 1;
>
> // I think there is extra capacity beyond the 'count'
> elements
> int* ptr = cast(int*)GC.malloc(count * int.sizeof);
> int[] arr = ptr[0 .. count];
>
> assert(arr.capacity == 0);
> arr.assumeSafeAppend;
> assert(arr.capacity == 0); // still 0. :(
> }
>
> This issue puts std.array.array to a disadvantage compared to
> proper slices because array() involves the following call
> chain, the last of which does call GC.malloc:
>
> trustedAllocateArray
> uninitializedArray
> arrayAllocImpl
>
> As a result, iota(10).array.assumeSafeAppend ends up having 0
> capacity. :(
>
> Ali
Recently, a new function in druntime was added: "_d_newarrayU".
This void allocates a new array *with* appendable information. We
can hope it will be given a more formal and public interface, and
it would then be useable by array and/or Appender, to produce
slices that have appendable data.
More information about the Digitalmars-d-learn
mailing list