View mode: basic / threaded / horizontal-split · Log in · Help
May 07, 2012
run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Re: run-time stack-based allocation
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
Top | Discussion index | About this forum | D home