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-Thread: 103376,1b043921d2cf45d2 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: Samuel Tardieu Newsgroups: comp.lang.ada Subject: Re: Interfacing to C without dynamic memory References: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com> Date: Sat, 15 Nov 2008 12:52:54 +0100 Message-ID: <87bpwhurxl.fsf@willow.rfc1149.net> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:g2GJeTwDoRb45bCGPT5W/64CFKw= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Leafnode-NNTP-Posting-Host: 2001:6f8:37a:2::2 Organization: Guest of ProXad - France NNTP-Posting-Date: 15 Nov 2008 12:55:02 MET NNTP-Posting-Host: 91.121.19.179 X-Trace: 1226750102 news-2.free.fr 29900 91.121.19.179:37260 X-Complaints-To: abuse@proxad.net Path: g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!gegeweb.org!feed.ac-versailles.fr!proxad.net!feeder1-2.proxad.net!212.27.60.64.MISMATCH!cleanfeed3-b.proxad.net!nnrp2-2.free.fr!not-for-mail Xref: g2news1.google.com comp.lang.ada:2684 Date: 2008-11-15T12:55:02+01:00 List-Id: >>>>> "Bob" == Robert A Duff writes: Bob> Makes sense. You could write a C program that #include's the Bob> relevant .h file, and prints out an Ada package spec containing Bob> the above Ada code. You could run this as part of your build Bob> scripts. Doing it properly is quite painful when you cross-compile, you have to play dirty unportable tricks such as parsing the assembly file output by the compiler to extract the value. This is the way I originally did it in GLADE, it was then used to generate GNAT.Sockets.Constants, but as you know AdaCore recently switched to parsing the C compiler output, which was possible because the GCC C compiler (native or cross) is always available when building the runtime. Note that you can also do it at run time, even if it costs a few more cycles to allocate the object, by taking advantage of C ability to resolve "sizeof" into a static value at compile time. This obliviates the cross-compilation issues. Here is an example (t.c, t_interface.ads and test.adb): (note that in real life, one would include the header defining "struct T" instead of exporting "T_size" in the same file) /* t.c */ #include struct T { char a; void *b; short c; char d; }; const size_t T_size = sizeof(struct T); -- t_interface.ads with Interfaces.C; package T_Interface is T_Size : constant Interfaces.C.size_t; pragma Import (C, T_Size, "T_size"); type Opaque_T is new Interfaces.C.char_array (1 .. T_Size); for Opaque_T'Alignment use 8; private pragma Linker_Options ("t.o"); end T_Interface; -- test.adb with Ada.Text_IO; use Ada.Text_IO; with T_Interface; use T_Interface; procedure Test is My_T : Opaque_T; begin Put_Line ("Size of T in bits:" & My_T'Size'Img); end Test;