Re: Allocating a wstring on the stack (no GC)?

2014-05-18 Thread Denis Shelomovskij via Digitalmars-d
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.

Re: Allocating a wstring on the stack (no GC)?

2014-05-18 Thread Walter Bright via Digitalmars-d
On 5/7/2014 11:26 AM, Maxime Chevalier-Boisvert wrote: 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

Re: Allocating a wstring on the stack (no GC)?

2014-05-18 Thread Walter Bright via Digitalmars-d
On 5/7/2014 12:03 PM, Maxime Chevalier-Boisvert wrote: auto ptr = cast(wchar*)alloca(wchar.sizeof * len); if (ptr == null) throw new Error(...); auto mySlice = ptr[0 .. len]; Is the slice going to be allocated on the stack? (I imagine the answer is yes) Yes (slices do not copy).

Re: Allocating a wstring on the stack (no GC)?

2014-05-09 Thread Regan Heath via Digitalmars-d
On Wed, 07 May 2014 19:41:16 +0100, Maxime Chevalier-Boisvert maximechevali...@gmail.com wrote: Unless I'm misunderstanding it should be as simple as: wchar[100] stackws; // alloca() if you need it to be dynamically sized. A slice of this static array behaves just like a slice of a dynamic

Re: Allocating a wstring on the stack (no GC)?

2014-05-08 Thread John Colvin via Digitalmars-d
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime Chevalier-Boisvert wrote: 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

Re: Allocating a wstring on the stack (no GC)?

2014-05-08 Thread John Colvin via Digitalmars-d
On Wednesday, 7 May 2014 at 18:34:10 UTC, Meta wrote: On Wednesday, 7 May 2014 at 18:29:23 UTC, Brad Anderson wrote: Unless I'm misunderstanding it should be as simple as: wchar[100] stackws; // alloca() if you need it to be dynamically sized. A slice of this static array behaves just like

Re: Allocating a wstring on the stack (no GC)?

2014-05-08 Thread bearophile via Digitalmars-d
John Colvin: Sure, you have to keep track of the memory to make sure it doesn't survive past the current scope (or at least isn't used past that point, depending on how security critical your application is), but that's often trivial to do and saving the extra allocation can make a big

Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Maxime Chevalier-Boisvert via Digitalmars-d
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

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Brad Anderson via Digitalmars-d
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime Chevalier-Boisvert wrote: 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

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Meta via Digitalmars-d
On Wednesday, 7 May 2014 at 18:29:23 UTC, Brad Anderson wrote: Unless I'm misunderstanding it should be as simple as: wchar[100] stackws; // alloca() if you need it to be dynamically sized. A slice of this static array behaves just like a slice of a dynamic array. But you should avoid

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread bearophile via Digitalmars-d
Meta: But you should avoid slicing the static array unless it's via arr.dup. Slicing fixed size arrays is often necessary, because many Phobos functions don't accept fixed size arrays, they force you to lose the compile-time knowledge of the length. Bye, bearophile

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Maxime Chevalier-Boisvert via Digitalmars-d
Unless I'm misunderstanding it should be as simple as: wchar[100] stackws; // alloca() if you need it to be dynamically sized. A slice of this static array behaves just like a slice of a dynamic array. I do need it to be dynamically sized. I also want to avoid copying my string data if

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread bearophile via Digitalmars-d
Maxime Chevalier-Boisvert: I do need it to be dynamically sized. But often you can determine statically a maximum length of the string, so you can use a fixed size stack buffer and slice it with a dynamic length. If this is not acceptable, then use alloca. Basically, I just want to

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Maxime Chevalier-Boisvert via Digitalmars-d
This is named slicing. You can also slice a global/static/__gshared buffer. alloca returns a void*, then you can cast it to the pointer type you want, and then you slice the pointer: auto ptr = cast(wchar*)alloca(wchar.sizeof * len); if (ptr == null) throw new Error(...); auto mySlice =

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread bearophile via Digitalmars-d
Maxime Chevalier-Boisvert: Is the slice going to be allocated on the stack? (I imagine the answer is yes) Slicing doesn't change where the data is allocated. Slicing means just creating a new struct that contains a length and pointer to the data (and the struct itself is allocated in-place.

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Dicebot via Digitalmars-d
On Wednesday, 7 May 2014 at 18:41:17 UTC, Maxime Chevalier-Boisvert wrote: Basically, I just want to create a wstring view on an existing raw buffer that exists in memory somewhere, based on a pointer to this buffer and its length. Looks like you actually don't need to allocate anything at

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Maxime Chevalier-Boisvert via Digitalmars-d
Is the slice going to be allocated on the stack? (I imagine the answer is yes) Slicing doesn't change where the data is allocated. Slicing means just creating a new struct that contains a length and pointer to the data (and the struct itself is allocated in-place. So it's allocated on the

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Benjamin Thaut via Digitalmars-d
Am 07.05.2014 20:41, schrieb Maxime Chevalier-Boisvert: I do need it to be dynamically sized. I also want to avoid copying my string data if possible. Basically, I just want to create a wstring view on an existing raw buffer that exists in memory somewhere, based on a pointer to this buffer and

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread monarch_dodra via Digitalmars-d
On Wednesday, 7 May 2014 at 19:18:09 UTC, Maxime Chevalier-Boisvert wrote: Is the slice going to be allocated on the stack? (I imagine the answer is yes) Slicing doesn't change where the data is allocated. Slicing means just creating a new struct that contains a length and pointer to the

Re: Allocating a wstring on the stack (no GC)?

2014-05-07 Thread Jesse Phillips via Digitalmars-d
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime Chevalier-Boisvert wrote: 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