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,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-06 16:58:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!server3.netnews.ja.net!server2.netnews.ja.net!pegasus.csx.cam.ac.uk!solway.cl.cam.ac.uk!sjm217 From: news01+Steven.Murdoch@cl.cam.ac.uk (Steven Murdoch) Newsgroups: comp.lang.ada Subject: Creating tagged records with C "malloc" rather than Ada "new" Date: 7 Dec 2002 00:58:01 GMT Organization: University of Cambridge, England Sender: sjm217@solway.cl.cam.ac.uk (Steven Murdoch) Message-ID: NNTP-Posting-Host: solway.cl.cam.ac.uk X-Newsreader: xrn 9.02 Xref: archiver1.google.com comp.lang.ada:31517 Date: 2002-12-07T00:58:01+00:00 List-Id: This post is again about my binding from Ada95 to Lua. In the last thread I proposed and asked for help for a way of giving Lua access to tagged records by asking Lua to assign enough space for a pointer to a tagged record, then storing the pointer in the memory location provided by Lua. This works (thanks to all those who helped) but I have another possibility and would greatly appreciate any advice on it. Lua provide a garbage collection facility, which I would like to use (along with some other neat features), however I can only do this if I get Lua to allocate the memory for the object. Currently I get Lua to allocate space for the access type, but I allocate space for the tagged record using new. In order to reclaim the space allocated by new I would need to add a hook to the Lua garbage collector to deallocate this space when the pointer is freed. 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, 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. Thank you in advance, Steven Murdoch.