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,e4468348bac7e58c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-07 06:33:23 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newspeer.radix.net!uunet!ash.uu.net!world!news From: Robert A Duff Subject: Re: Creating tagged records with C "malloc" rather than Ada "new" User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Sender: news@world.std.com (Mr Usenet Himself) Message-ID: Date: Sat, 7 Dec 2002 14:32:56 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:31529 Date: 2002-12-07T14:32:56+00:00 List-Id: news01+Steven.Murdoch@cl.cam.ac.uk (Steven Murdoch) writes: > However I think I can simplify this by allocating space for the > tagged record in Lua, and hence it will deallocate this space at > the right time. I would like to know if this is possible to do this > reliably, and if it is relativly safe, You should look at RM-13.11, "Storage Management". You can declare your own storage pool type. The Allocate operation could call Lua. The Deallocate operation could do nothing (which might be appropriate if you're doing garbage collection). But there are lots of other issues related to GC. I don't know anything about Lua, but I don't see how Lua can know where all the pointers are. So does it do so-called "conservative" GC? > The code I am proposing would look something like as follows: > > C: > /* Actually part of Lua, but this is equivalent */ > void *allocate(int size) { > return malloc(size); > } > > Ada: > type Parameter is tagged private; > type Parameter_Access is access Parameter'Class; > > function Allocate(Size: Storage_Unit) return Parameter_Access; > pragma Import(C, Allocate); > > function Add_To_Lua(Param: in Parameter) return Parameter_Access > is > Param_Ptr: Parameter_Access; > begin > Param_Ptr:=Allocate(Parameter'Max_Size_In_Storage_Units); > Param_Ptr.all:=Param; --[1] > return Param_Ptr; > end; > > Will this do what I want and is is comapritively safe. In particular > will statement [1] sort of the tag of Param properly? Are there any > other potential problems. You might be able to make the above scheme work for *some* types, but it has some problems. Parameter'Max_Size_In_Storage_Units returns the maximum size for all objects of type Parameter (not Parameter'Class). What you want is the size of Param itself. Parameter'Max_Size_In_Storage_Units could be (way) too big if the type has discriminant dependent arrays. And Parameter'Max_Size_In_Storage_Units will be too small if Param'Tag /= Parameter'Tag (i.e. the caller passed in an object from deeper in the type hierarchy). What you want is Param'Size (but that's in bits). It won't work for limited types (because you have an assignment). There's no way to create an uninitialized heap object. The storage pool scheme solves all of these problems. You just do a normal Ada "new", with or without initialization, as you wish, perhaps with discriminant constraint and whatever other bells and whistles Ada allows. The compiler will translate the "new" into two steps: a call on your Allocate (which calls Lua), followed by all the Ada-specific default or explicit initializations. - Bob