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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1b043921d2cf45d2 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!cleanfeed2-a.proxad.net!nnrp18-1.free.fr!not-for-mail Date: Fri, 14 Nov 2008 21:35:33 +0100 From: Damien Carbonne User-Agent: Thunderbird 2.0.0.17 (X11/20080914) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Interfacing to C without dynamic memory References: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com> In-Reply-To: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <491de115$0$29376$426a74cc@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 14 Nov 2008 21:35:33 MET NNTP-Posting-Host: 82.247.219.63 X-Trace: 1226694933 news-2.free.fr 29376 82.247.219.63:43643 X-Complaints-To: abuse@proxad.net Xref: g2news2.google.com comp.lang.ada:8406 Date: 2008-11-14T21:35:33+01:00 List-Id: Hi, I have never tried what you propose, but I think it should work. Of course, all fields of T should be initialized in C code (in createT, I guess). In Ada, you could also wrap the array in a record. That way, you could initialize everything to 0: type T is record Bytes : System.Storage_Array (...); end record; pragma Convention (C, T); + other pragma if necessary ... Make T private, or even limited private: Ada users should not have direct access to T fields. Also, you shouldn't need to use 'Address. By default, when you use C convention/import, structures and arrays are passed by address. Of course, as you don't have direct access to T fields, you need to provide all necessary functions to do that. So you would have: package XXX is type T is [limited] private; procedure Create (O : in out T); procedure Destroy (O : in out T); ... private type T is ... -- as your proposal ... pragma Import (C, Create, "CreateT"); pragma Import (C, Destroy, "DestroyT"); ... end XXX; Maciej Sobczak a �crit : > Hi, > > Consider a C (or C++) library that defines some type T with some > functions operating on it. > The type T can be a complex type, encapsulated in a struct or class. > Assuming extern "C" interface, the library functions usually accept a > pointer to T: > > struct T > { > // lots of interesting stuff > }; > > void createT(T * object); > void destroyT(T * object); > void foo(T * object, int something); > void bar(T * object, int something_else); > > In C and C++ it is possible to use objects of type T without > allocating them dynamically, which would be otherwise mandated by an > alternative interface: > > T * newT(); > void deleteT(T * object); > > How would you approach wrapping such a library for Ada while retaining > the requirement that dynamic memory is not obligatory (ie. with the > original interface above)? > > My first ideas are: > 1. Extract sizeof(T) at C level. > 2. In Ada, create appropriately aligned: > > type T is System.Storage_Array (1 .. Size_Of_T); > for T'Alignment use Appropriate_Alignment_Value; > > 3. Wrap imported C functions by passing to them the 'Address of T's > instances. > > This way, users will be able to use T as any other value type, without > resorting to dynamic memory. > > Does it make sense? Is there any better way? > > -- > Maciej Sobczak * www.msobczak.com * www.inspirel.com > > Database Access Library for Ada: www.inspirel.com/soci-ada