Allocating a wstring on the stack (no GC)?

Denis Shelomovskij via Digitalmars-d digitalmars-d at puremagic.com
Sun May 18 00:49:44 PDT 2014


07.05.2014 22:26, Maxime Chevalier-Boisvert пишет:
> I have a very specific use case (JIT compiler) in which I have a
> pre-allocated array of wchar string data stored somewhere in memory. I'd
> like to be able to create a temporary D wstring object to pass this as a
> "regular" string to other functions. For performance reasons, it would
> be preferable not to dynamically allocate or copy any data. Dynamically
> allocating the strings tends to trigger the D GC which severely impacts
> the performance.
>
> So, my question is, would it be possible for me to allocate a wstring
> object on the stack, and manually set its string data pointer and
> length? If so, how? Your wizardly help is much appreciated.

If you have a preallocated data, just using slicing will be enough. A 
result will be `wstring` for immutable data or `const(wchar)[]` for 
non-immutable, if you respect a typesystem.

If a new data is generated but you want it to be put on stack if it's 
small enough (you can't put big data on stack anyway) you need some 
allocation facility. E.g. `unstd.memory.allocation.tempAlloc` [1] will 
do the work.

[1] 
http://denis-sh.bitbucket.org/unstandard/unstd.memory.allocation.html#tempAlloc

-- 
Денис В. Шеломовский
Denis V. Shelomovskij


More information about the Digitalmars-d mailing list