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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1b043921d2cf45d2,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!postnews.google.com!a17g2000prm.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Interfacing to C without dynamic memory Date: Fri, 14 Nov 2008 05:58:03 -0800 (PST) Organization: http://groups.google.com Message-ID: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com> NNTP-Posting-Host: 128.141.45.219 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1226671083 31656 127.0.0.1 (14 Nov 2008 13:58:03 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 14 Nov 2008 13:58:03 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: a17g2000prm.googlegroups.com; posting-host=128.141.45.219; posting-account=bMuEOQoAAACUUr_ghL3RBIi5neBZ5w_S User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:2675 Date: 2008-11-14T05:58:03-08:00 List-Id: 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