Jump to page: 1 2 3
Thread overview
run-time stack-based allocation
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Mehrdad
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Mehrdad
May 08, 2012
dsimcha
May 08, 2012
Gor Gyolchanyan
May 09, 2012
Tove
May 09, 2012
Gor Gyolchanyan
May 09, 2012
David Nadlinger
May 10, 2012
Tove
Jul 09, 2012
David Piepgrass
May 07, 2012
Arne
May 07, 2012
Gor Gyolchanyan
May 07, 2012
Mehrdad
May 07, 2012
Arne
May 07, 2012
Chris Cain
May 07, 2012
deadalnix
May 07, 2012
Gor Gyolchanyan
May 07, 2012
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ønne Petersen <xtzgzorex@gmail.com> wrote:
> 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



-- 
Bye,
Gor Gyolchanyan.
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:
>> 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
>
>
>

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, ...

-- 
- 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ønne Petersen <xtzgzorex@gmail.com> wrote:
> 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:
>>>
>>> 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
>>
>>
>>
>>
>
> 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, ...
>
> --
> - Alex



-- 
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:
>>
>> 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'.



-- 
Bye,
Gor Gyolchanyan.
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:
>>
>> 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'.



-- 
Bye,
Gor Gyolchanyan.
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 Monday, 7 May 2012 at 16:03:15 UTC, Mehrdad 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'.

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]);
}
« First   ‹ Prev
1 2 3