digitalmars.D - run-time stack-based allocation
- Gor Gyolchanyan (14/14) May 07 2012 I'm working on dynamic memory layout manager. Simply put, it will
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (4/15) May 07 2012 alloca?
- Gor Gyolchanyan (8/26) May 07 2012 I can't use alloca, because the stack-based allocation will be done in
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (7/31) May 07 2012 If that's the case, I don't know how you actually want this stack
- Gor Gyolchanyan (11/50) May 07 2012 Basically I want what alloca does, but instead of considering the
- Mehrdad (3/6) May 07 2012 I think you'd need to modify the compiler for this, since alloca
- Gor Gyolchanyan (7/14) May 07 2012 I'd decrease ESP to allocate my space, but the problem arises when I
- Gor Gyolchanyan (7/14) May 07 2012 Wasn't there an allocator mechanism under development for phobos? I
- Mehrdad (2/20) May 07 2012
- dsimcha (4/7) May 07 2012 I wrote one. It's at https://github.com/dsimcha/TempAlloc . It hasn't
- Gor Gyolchanyan (6/14) May 08 2012 t hasn't been
- Tove (25/27) May 09 2012 I just invented an absolutely wicked way of using alloca() in the
- Gor Gyolchanyan (7/35) May 09 2012 I thought function default parameters need to be statically known...
- David Nadlinger (4/7) May 09 2012 Nope – AFAIK you can also do something like (Foo param = new
- Andrei Alexandrescu (5/10) May 09 2012 Yah, me too.
- Tove (6/19) May 10 2012 Muharrr, way cool :D We seriously need a highly visible "blog:ish
- David Piepgrass (5/19) Jul 09 2012 That is so awesome that it can't possibly be legal by the spec!
- Arne (25/31) May 07 2012 wouldn't mixin's be a solution, one can inject an alloca to the
- Gor Gyolchanyan (8/43) May 07 2012 That won't do. This is way too ugly, considering, that it needs to be
- Mehrdad (3/7) May 07 2012 Yeah, but mixins are so hacky.
- Arne (9/17) May 07 2012 /rant
- Chris Cain (3/5) May 07 2012 I'd have to say that C macros have many, _many_ more pitfalls
- deadalnix (4/15) May 07 2012 About that, I already had the need to return a variable that will be
- Gor Gyolchanyan (8/25) May 07 2012 Yes! I really want it! There are tons of instances when a heap
I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size. -- Bye, Gor Gyolchanyan.
May 07 2012
On 07-05-2012 13:58, Gor Gyolchanyan wrote:I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.alloca? -- - Alex
May 07 2012
I can't use alloca, because the stack-based allocation will be done in the constructor and alloca will free the memory as soon as the constructor exists. On Mon, May 7, 2012 at 4:01 PM, Alex R=C3=B8nne Petersen <xtzgzorex gmail.c= om> wrote:On 07-05-2012 13:58, Gor Gyolchanyan wrote:--=20 Bye, Gor Gyolchanyan.I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.alloca? -- - Alex
May 07 2012
On 07-05-2012 14:37, Gor Gyolchanyan wrote:I can't use alloca, because the stack-based allocation will be done in the constructor and alloca will free the memory as soon as the constructor exists. On Mon, May 7, 2012 at 4:01 PM, Alex Rønne Petersen<xtzgzorex gmail.com> wrote:If that's the case, I don't know how you actually want this stack allocation to work. The only way I see that you could do it would be with dirty hacks making assumptions about the compiler, platform, calling convention, ... -- - AlexOn 07-05-2012 13:58, Gor Gyolchanyan wrote:I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.alloca? -- - Alex
May 07 2012
Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope. On Mon, May 7, 2012 at 5:29 PM, Alex R=C3=B8nne Petersen <xtzgzorex gmail.c= om> wrote:On 07-05-2012 14:37, Gor Gyolchanyan wrote:.com>I can't use alloca, because the stack-based allocation will be done in the constructor and alloca will free the memory as soon as the constructor exists. On Mon, May 7, 2012 at 4:01 PM, Alex R=C3=B8nne Petersen<xtzgzorex gmail=ion=C2=A0wrote:If that's the case, I don't know how you actually want this stack allocat=On 07-05-2012 13:58, Gor Gyolchanyan wrote:I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.alloca? -- - Alexto work. The only way I see that you could do it would be with dirty hack=smaking assumptions about the compiler, platform, calling convention, ... -- - Alex--=20 Bye, Gor Gyolchanyan.
May 07 2012
On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
I'd decrease ESP to allocate my space, but the problem arises when I try to determine when should I increase it back where it was. Any suggestions on how to do this using asm? On Mon, May 7, 2012 at 8:03 PM, Mehrdad <wfunction hotmail.com> wrote:On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:-- Bye, Gor Gyolchanyan.Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
Wasn't there an allocator mechanism under development for phobos? I remember there was a StackAllocator, that can span for arbitrary scopes. What's up with that? On Mon, May 7, 2012 at 8:03 PM, Mehrdad <wfunction hotmail.com> wrote:On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:-- Bye, Gor Gyolchanyan.Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
No idea, sorry. :\ On Monday, 7 May 2012 at 16:08:42 UTC, Gor Gyolchanyan wrote:Wasn't there an allocator mechanism under development for phobos? I remember there was a StackAllocator, that can span for arbitrary scopes. What's up with that? On Mon, May 7, 2012 at 8:03 PM, Mehrdad <wfunction hotmail.com> wrote:On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
On 5/7/2012 12:08 PM, Gor Gyolchanyan wrote:Wasn't there an allocator mechanism under development for phobos? I remember there was a StackAllocator, that can span for arbitrary scopes. What's up with that?I wrote one. It's at https://github.com/dsimcha/TempAlloc . It hasn't been accepted to Phobos, though, because of issues w.r.t. figuring out what a more general allocator interface should look like.
May 07 2012
Cool! Thanks! I'l definitely check it out! I hope it's DDOCed :-D On Tue, May 8, 2012 at 5:29 AM, dsimcha <dsimcha yahoo.com> wrote:On 5/7/2012 12:08 PM, Gor Gyolchanyan wrote:t hasn't beenWasn't there an allocator mechanism under development for phobos? I remember there was a StackAllocator, that can span for arbitrary scopes. What's up with that?I wrote one. =C2=A0It's at https://github.com/dsimcha/TempAlloc . =C2=A0I=accepted to Phobos, though, because of issues w.r.t. figuring out what a more general allocator interface should look like.--=20 Bye, Gor Gyolchanyan.
May 08 2012
On Tuesday, 8 May 2012 at 07:03:35 UTC, Gor Gyolchanyan wrote:Cool! Thanks! I'l definitely check it out! I hope it's DDOCed :-DI just invented an absolutely wicked way of using alloca() in the parent context... unfortunately frame_size is static but with some work, it's still an usable method since it's mutable! struct Wicked { static int frame_size = 0; auto Create(void* buf=alloca(frame_size)) { for(byte i=0;i<frame_size;++i) (cast(byte*)buf)[i]=i; struct Frame { size_t size; } Frame* xxx = cast(Frame*)buf; xxx.size=frame_size; return xxx; } disable this(); this(size_t size) { frame_size = size; } }
May 09 2012
I thought function default parameters need to be statically known... isn't it the case? On Thu, May 10, 2012 at 12:17 AM, Tove <tove fransson.se> wrote:On Tuesday, 8 May 2012 at 07:03:35 UTC, Gor Gyolchanyan wrote:illCool! Thanks! I'l definitely check it out! I hope it's DDOCed :-DI just invented an absolutely wicked way of using alloca() in the parent context... unfortunately frame_size is static but with some work, it's st=an usable method since it's mutable! struct Wicked { =C2=A0static int frame_size =3D 0; =C2=A0auto Create(void* buf=3Dalloca(frame_size)) =C2=A0{ =C2=A0 =C2=A0for(byte i=3D0;i<frame_size;++i) =C2=A0 =C2=A0 =C2=A0(cast(byte*)buf)[i]=3Di; =C2=A0 =C2=A0struct Frame =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0size_t size; =C2=A0 =C2=A0} =C2=A0 =C2=A0Frame* xxx =3D cast(Frame*)buf; =C2=A0 =C2=A0xxx.size=3Dframe_size; =C2=A0 =C2=A0return xxx; =C2=A0} =C2=A0 disable this(); =C2=A0this(size_t size) =C2=A0{ =C2=A0 =C2=A0frame_size =3D size; =C2=A0} }--=20 Bye, Gor Gyolchanyan.
May 09 2012
On Wednesday, 9 May 2012 at 20:52:33 UTC, Gor Gyolchanyan wrote:I thought function default parameters need to be statically known... isn't it the case?Nope – AFAIK you can also do something like (Foo param = new Foo). David
May 09 2012
On 5/9/12 3:17 PM, Tove wrote:On Tuesday, 8 May 2012 at 07:03:35 UTC, Gor Gyolchanyan wrote:Yah, me too. http://forum.dlang.org/thread/i1gnlo$18g0$1 digitalmars.com#post-i1gql2:241k6o:2 1:40digitalmars.com I found it by googling for my name and "dark" and "devious" :o). AndreiCool! Thanks! I'l definitely check it out! I hope it's DDOCed :-DI just invented an absolutely wicked way of using alloca() in the parent context...
May 09 2012
On Thursday, 10 May 2012 at 03:03:22 UTC, Andrei Alexandrescu wrote:On 5/9/12 3:17 PM, Tove wrote:Muharrr, way cool :D We seriously need a highly visible "blog:ish page" (which was suggested in another lost thread), listing some useful D gems... I have a feeling these forums are a treasure trove with forgotten snippets...On Tuesday, 8 May 2012 at 07:03:35 UTC, Gor Gyolchanyan wrote:Yah, me too. http://forum.dlang.org/thread/i1gnlo$18g0$1 digitalmars.com#post-i1gql2:241k6o:2 1:40digitalmars.com I found it by googling for my name and "dark" and "devious" :o). AndreiCool! Thanks! I'l definitely check it out! I hope it's DDOCed :-DI just invented an absolutely wicked way of using alloca() in the parent context...
May 10 2012
On Thursday, 10 May 2012 at 03:03:22 UTC, Andrei Alexandrescu wrote:On 5/9/12 3:17 PM, Tove wrote:That is so awesome that it can't possibly be legal by the spec! This "runtime struct" sounds really cool too. Pinch me, I must be dreaming :DOn Tuesday, 8 May 2012 at 07:03:35 UTC, Gor Gyolchanyan wrote:Yah, me too. http://forum.dlang.org/thread/i1gnlo$18g0$1 digitalmars.com#post-i1gql2:241k6o:2 1:40digitalmars.com I found it by googling for my name and "dark" and "devious" :o).Cool! Thanks! I'l definitely check it out! I hope it's DDOCed :-DI just invented an absolutely wicked way of using alloca() in the parent context... auto Create(void* buf=alloca(frame_size))
Jul 09 2012
On Monday, 7 May 2012 at 16:03:15 UTC, Mehrdad wrote:On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:wouldn't mixin's be a solution, one can inject an alloca to the current scope, and then call the constructor... import std.stdio; import std.c.stdlib; import std.random; mixin template Init(alias var, alias size) { void* buf = alloca(size); bool foo_init() { var = (cast(typeof(var))buf); var[0]=0; var[1]=1; var[2]=2; return true; } auto foo_dummy_statement = foo_init(); } void main() { int my_size = uniform(12, 24); int* my_var = void; mixin Init!(my_var, my_size); writeln(my_var[0..3]); }Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclosing scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
That won't do. This is way too ugly, considering, that it needs to be heavily used in user code. I'm thinking an inline ASM solution, but can't figure out when to deallocate. On Mon, May 7, 2012 at 8:52 PM, Arne <arne linux.nu> wrote:On Monday, 7 May 2012 at 16:03:15 UTC, Mehrdad wrote:ingOn Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:Basically I want what alloca does, but instead of considering the constructor's scope, I want it to hand to the constructor call's enclos=--=20 Bye, Gor Gyolchanyan.wouldn't mixin's be a solution, one can inject an alloca to the current scope, and then call the constructor... import std.stdio; import std.c.stdlib; import std.random; mixin template Init(alias var, alias size) { =C2=A0void* buf =3D alloca(size); =C2=A0bool foo_init() =C2=A0{ =C2=A0 =C2=A0var =3D (cast(typeof(var))buf); =C2=A0 =C2=A0var[0]=3D0; =C2=A0 =C2=A0var[1]=3D1; =C2=A0 =C2=A0var[2]=3D2; =C2=A0 =C2=A0return true; =C2=A0} =C2=A0auto foo_dummy_statement =3D foo_init(); } void main() { =C2=A0int my_size =3D uniform(12, 24); =C2=A0int* my_var =3D void; mixin Init!(my_var, my_size); =C2=A0writeln(my_var[0..3]); }scope.I think you'd need to modify the compiler for this, since alloca is 'magical'.
May 07 2012
On Monday, 7 May 2012 at 16:52:18 UTC, Arne wrote:Yeah, but mixins are so hacky. They're like C macros, basically.I think you'd need to modify the compiler for this, since alloca is 'magical'.wouldn't mixin's be a solution, one can inject an alloca to the current scope, and then call the constructor...
May 07 2012
On Monday, 7 May 2012 at 20:20:34 UTC, Mehrdad wrote:On Monday, 7 May 2012 at 16:52:18 UTC, Arne wrote:/rant Well, I think if the world had never seen all the bad side-effects from 'C macros', most people would wholeheartedly embrace mixin's today... since it cleanly avoids 'x' number of design pitfalls from #define. For maintenance, performance, portability considerations I definitely prefer mixin's over reimplementing alloca for every current and future targets/calling conventions.Yeah, but mixins are so hacky. They're like C macros, basically.I think you'd need to modify the compiler for this, since alloca is 'magical'.wouldn't mixin's be a solution, one can inject an alloca to the current scope, and then call the constructor...
May 07 2012
On Monday, 7 May 2012 at 20:20:34 UTC, Mehrdad wrote:Yeah, but mixins are so hacky. They're like C macros, basically.I'd have to say that C macros have many, _many_ more pitfalls than mixins.
May 07 2012
Le 07/05/2012 13:58, Gor Gyolchanyan a écrit :I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.About that, I already had the need to return a variable that will be qualified as scope after the return. It would also be usefull to safely implement stack allocator.
May 07 2012
Yes! I really want it! There are tons of instances when a heap allocation is done instead of stack allocation because of dynamic size alone. If its lifetime is limited by a scope (any scope) - it doesn't belong on the heap! On Tue, May 8, 2012 at 2:07 AM, deadalnix <deadalnix gmail.com> wrote:Le 07/05/2012 13:58, Gor Gyolchanyan a =C3=A9crit :--=20 Bye, Gor Gyolchanyan.I'm working on dynamic memory layout manager. Simply put, it will allow one to create and use struct types at run-time. Normally, you create a struct at compile-time type by specifying an ordered list of fields, each with its own type (basically a size) and name. You then access those fields by calling a compile-time evaluated dot operator, which computes the address of the specified field given the address of the struct. What I'm trying to make is precisely that, except at run-time. My question is: what is the best way of allocating such a structure on the stack? It will, of course, have a dynamically known size.About that, I already had the need to return a variable that will be qualified as scope after the return. It would also be usefull to safely implement stack allocator.
May 07 2012