Is it possible to assumeSafeAppend malloced memory?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 18 23:08:17 PDT 2014


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


More information about the Digitalmars-d-learn mailing list