From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8055158949c56618 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-15 12:15:15 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!uunet!sea.uu.net!ash.uu.net!world!news From: Robert A Duff Subject: Re: Storage Pools and alloca User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Sender: news@world.std.com (Mr Usenet Himself) Message-ID: Date: Tue, 15 Oct 2002 19:14:47 GMT Content-Type: text/plain; charset=us-ascii References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Organization: The World Public Access UNIX, Brookline, MA Xref: archiver1.google.com comp.lang.ada:29816 Date: 2002-10-15T19:14:47+00:00 List-Id: "Frank J. Lhota" 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