comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: A suggestion about interfacing with C
Date: Mon, 2 May 2016 13:14:10 -0700 (PDT)
Date: 2016-05-02T13:14:10-07:00	[thread overview]
Message-ID: <2b4f1206-dea3-4cf5-9a6b-b857470dfb34@googlegroups.com> (raw)
In-Reply-To: <45cb9b19-b470-4649-843a-7b9f88411e6e@googlegroups.com>


> * others?
> 
> What would you suggest?

Apart from the suggestions that you have got already, I would like to expand a bit on the "others" category.

Note that in the case of this library (and many others, because the pattern is popular) it is not necessary to pass the pointer to Ada, even if it is opaque, as Ada will not operate on it. The only purpose of passing it to Ada is to pass it back to C, where the actual operations will take place, in which case you might as well not pass it at all and just keep it at the C side. Really, you do not need this value (no matter how mangled) at the Ada level. And this observation opens the idea to pass some token instead (if there can be many objects identified this way), which might be of some more Ada-friendly type, even (or maybe in particular) the integer. That is, you can use 32-bit Integer on the Ada side, as a descriptor for the 64-bit pointer at the C side, and there is no need to pass the pointer around.

You will need some additional, very thin layer of C to map one to another (note that I used the word "map" instead of "convert"), but such a thin layer could prove useful anyway, in order to flatten and "sanitize" anything else that might need to be passed between domains. If there are any structs or #defines used in the C API, then you will find such a thin wrapper very valuable (how about checking whether the Ada descriptor is still valid, before doing anything with the pointer?). The target is a C API that uses *only* those types that are trivial to handle at the Ada level, where you can stack up higher-level APIs if necessary.
I have used this approach to create bindings for both C and C++ libraries.

-- 
Maciej Sobczak * http://www.inspirel.com


  parent reply	other threads:[~2016-05-02 20:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-02 16:45 A suggestion about interfacing with C mockturtle
2016-05-02 18:46 ` Simon Wright
2016-05-02 19:07 ` Jeffrey R. Carter
2016-05-02 19:40 ` Per Sandberg
2016-05-02 20:14 ` Maciej Sobczak [this message]
2016-05-03 21:53 ` mockturtle
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox