comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Dispatching callback handed over to C
Date: Wed, 4 Apr 2012 14:28:05 -0500
Date: 2012-04-04T14:28:05-05:00	[thread overview]
Message-ID: <jli7ca$6cr$1@munin.nbi.dk> (raw)
In-Reply-To: slrnjnnvk6.1lme.lithiumcat@sigil.instinctive.eu

"Natasha Kerensikova" <lithiumcat@gmail.com> wrote in message 
news:slrnjnnvk6.1lme.lithiumcat@sigil.instinctive.eu...
...
>>                                                and prevents the compiler
>> optimizer for working at all (it has to assume the worst about any object 
>> in
>> your program, unless it is willing to completely trash your program).
>
> That was another question I had in mind with that mechanism: I'm giving
> to C a reference to Ada data in one call (namely Event_New) but it gets
> used later, in another call (to Event_Base_Dispatch) which seeming does
> not involve the data at all.
>
> Should go to some extra steps (but which?) to warn the compiler that my
> Event object can change during Event_Base_Dispatch call despite that
> call not involving any Event object?
> Or maybe just marking the Event object as aliased is enough?

Aliased should be enough (Ada 95 suggests that it be used for this purpose). 
Some compilers don't insist on this (mostly because of Ada 83 code 
compatibility concerns), but I presume that they figure this out via some 
other mechanism (probably the same one that C compilers use, which typically 
is fiendishly complicated and rather prone to bugs).

>> The whole idea of the void pointer is so anti-Ada that it is best to get 
>> rid
>> of it as early as you can. (There is no such thing as "untyped data" in 
>> Ada,
>> and, as Dmitry likes to point out, not in real life, either. There is 
>> only
>> data for which the type is not known at compile-time, and that sort of 
>> data
>> is a problem - best to avoid it.)
>
> I feel you are being a bit unfairly harsh towards void pointers here: it
> looks to me that you are looking at them in a purely Ada context, which
> is about as incorrect as coding in Ada with another language in mind and
> complaining about being forced to fight the compiler.

Well, we're talking about Ada here, and what we have to do to interface to 
some foreign language API. I'm not interested in writing C - in any syntax!

> In C, void pointer is not really about untyped data, but data whose type
> is actively treated with agnosticism.
> Transposed to Ada world, it's like saying it does not make sense to
> have a record whose components are unknown, and in real life there is no
> such thing as a record with unknown components. "private" is only about
> preventing client from depending on component information rather than
> "void"ing components.

The void pointer is C is about data where the C type system isn't strong 
enough to describe it. So it simply throws away all of the type information, 
leaving you with none. The data itself surely has a type, but you can't use 
any knowledge about it. OTOH, the richer Ada type system often *can* 
describe this data, and in such a case, you will want to do that. (Yes, 
there are cases where Ada can't describe it, either - but I generally think 
those require a redesign, not a hack.)

An obvious example is the access-to-class-wide that Mariej was talking 
about. Clearly Ada can describe this, so you want to avoid using "void" in 
your (Ada) interface.

>>                                   In Claw, we tried to avoid void by 
>> simply
>> defining the interfaces for the types we cared about. For instance, if we
>> needed to read an array of stream elements, we'd define the API using 
>> that
>> type (rather than void). In other cases, we defined a void type (as 
>> access
>> to something) and used Unchecked_Conversions as needed to make it
>> crystal-clear this is wildly unsafe. Obviously, you have to do something 
>> to
>> interface to mindless APIs, but you have to hide them as quickly as
>> possible.
>
> That's exactly what I'm trying to do. Void pointers are indeed anti-Ada,
> because Ada rather uses generic formal types or tagged types or
> interfaces. The whole art of designing bindings is, in my opinion, to
> expose an interface as Ada-ish as possible despite the underlying
> anti-Ada stuff.

Absolutely. The really low-level stuff is likely to be grungy and not 100% 
portable. But that's OK, so long as it is localized as much as possible. 
(Pragmatism has to trump goodness -- if the binding doesn't work, it doesn't 
matter how "good" it is!)

                                        Randy.





  reply	other threads:[~2012-04-04 19:28 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-03  7:19 Dispatching callback handed over to C Natasha Kerensikova
2012-04-03  9:04 ` Brian Drummond
2012-04-03  9:21   ` Thomas Locke
2012-04-03  9:37 ` Maciej Sobczak
2012-04-03 12:02   ` Natasha Kerensikova
2012-04-03 14:42     ` Maciej Sobczak
2012-04-03 20:20       ` Randy Brukardt
2012-04-04  7:26         ` Georg Bauhaus
2012-04-04  7:56         ` Natasha Kerensikova
2012-04-04 19:28           ` Randy Brukardt [this message]
2012-04-05  8:59             ` Natasha Kerensikova
2012-04-05 21:04               ` Randy Brukardt
2012-04-04 19:55           ` Simon Wright
2012-04-04 11:34         ` Maciej Sobczak
2012-04-04 19:16           ` Randy Brukardt
2012-04-05  7:28             ` Maciej Sobczak
2012-04-05 21:32               ` Randy Brukardt
2012-04-07  8:55                 ` Natasha Kerensikova
2012-04-07 11:32                   ` Simon Wright
2012-04-07 13:28                     ` Robert A Duff
2012-04-05  9:13             ` Natasha Kerensikova
2012-04-05 21:06               ` Randy Brukardt
2012-04-06  7:30               ` Maciej Sobczak
2012-04-06 11:41                 ` Simon Wright
2012-04-10  7:15                   ` Maciej Sobczak
replies disabled

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