comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Storage Pools and alloca
Date: Tue, 15 Oct 2002 19:14:47 GMT
Date: 2002-10-15T19:14:47+00:00	[thread overview]
Message-ID: <wcczntfikew.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: PHWq9.20400$nb.8357@nwrddc02.gnilink.net

"Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net> writes:

> C / C++ sometimes cut down on the hassles of memory management by using the
> "alloca" function. The "alloca" function, which is frequently implicit,
> allocates storage from the stack.

What do you mean by "frequently implicit"?

>... Since memory allocated by "alloca" is on
> the stack, it is reclaimed automatically as soon as the function that
> allocated this memory returns.
> 
> AFAIK, the "alloca" functionality could be added to Ada using the storage
> pool facility. One could derive a type from
> System.Storage_Pools.Root_Storage_Pool and write the required subprograms
> for this type as follows:
> 
>     - The Allocate procedure would allocate memory from the stack, sort of
> like the C "alloca" function;
>     - The Deallocate procedure should do nothing; and
>     - The Storage_Size should return the amount of space left on the stack.

Storage_Size is supposed to return the amount of "reserved" space,
which in virtual memory systems might be zero.  ('Storage_Size is kind of
useless, actually.  I've never written a program that queried
'Storage_Size.  Has anybody?)

> Of course, this is highly platform dependant, and would often require
> machine code insertions.
> 
> Questions:
> 
>     1) Does any Ada compiler provide this type of storage pool?

I believe the GNAT compiler uses a storage pool to implement
dynamically-sized stack objects.  I think most other compilers
don't use a storage pool per se, but use essentially the same
run-time mechanisms as alloca.  (I know of one Ada 83 compiler
that used heap allocation to implement the Ada features -- but I
think an alloca-like mechanism is better.)

I don't see any reason to use such a storage pool directly.
Ada's dynamically-sized stack objects seem to do everything
alloca can do, but more safely.  In fact, the Ada mechanism is
more powerful, since you can return unknown-sized objects from
functions.  (I mean, unknown size at the call site -- the size is
fixed at the point of "return" in the called function.  Like when
the spec of the function says "return String".)

>     2) If this type of storage pool was available, would you find it useful?

No.

>     3) Would an "alloca" storage pool be a worthwhile addition to Ada 0x?

No, I don't think so.  There's alloca-like stuff going on behind the
scenes in Ada, but I don't see why a programmer would want direct access
to that mechanism.

Can you think of any case (in C or C++) that uses alloca, but can't be
easily translated into the existing Ada mechanisms?  I can't.

- Bob



  parent reply	other threads:[~2002-10-15 19:14 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-15 15:42 Storage Pools and alloca Frank J. Lhota
2002-10-15 16:32 ` Matthew Heaney
2002-10-15 17:26   ` Frank J. Lhota
2002-10-15 18:02     ` David C. Hoos
2002-10-15 19:14 ` Robert A Duff [this message]
2002-10-15 20:23   ` Frank J. Lhota
2002-10-15 20:54     ` Robert A Duff
2002-10-16 13:42       ` Frank J. Lhota
2002-10-16  7:32     ` Eric G. Miller
2002-10-16 13:42       ` Frank J. Lhota
2002-10-19 20:30         ` Eric G. Miller
2002-10-16  5:20   ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox