* Interfacing to void *
@ 1999-07-18 0:00 William Starner
1999-07-18 0:00 ` Keith Thompson
1999-07-19 0:00 ` jerry
0 siblings, 2 replies; 5+ messages in thread
From: William Starner @ 1999-07-18 0:00 UTC (permalink / raw)
If I'm trying to interface to a C library that uses this struct
struct BigNum {
void *ptr;
unsigned int size;
unsigned int allocated;
};
how should I interface to void*?
I have
private
type voidpointer is access character;
type BigNum is record
ptr: voidpointer;
size: Interfaces.C.unsigned;
allocated: Interfaces.C.unsigned;
end record;
type RealBigNum is access BigNum;
type UnboundedInt is new Linear with record
Value: RealBigNum;
end record;
end nla23.linear.bnlib;
but char* = void* is a C rule, not an Ada rule. It probably works, but it's not
right. (I don't need to access anything through that pointer.)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Interfacing to void *
1999-07-18 0:00 Interfacing to void * William Starner
@ 1999-07-18 0:00 ` Keith Thompson
1999-07-19 0:00 ` jerry
1 sibling, 0 replies; 5+ messages in thread
From: Keith Thompson @ 1999-07-18 0:00 UTC (permalink / raw)
William Starner <billeug@worldnet.att.net> writes:
> If I'm trying to interface to a C library that uses this struct
>
> struct BigNum {
> void *ptr;
> unsigned int size;
> unsigned int allocated;
> };
>
> how should I interface to void*?
>
> I have
> private
> type voidpointer is access character;
> type BigNum is record
> ptr: voidpointer;
> size: Interfaces.C.unsigned;
> allocated: Interfaces.C.unsigned;
> end record;
> type RealBigNum is access BigNum;
> type UnboundedInt is new Linear with record
> Value: RealBigNum;
> end record;
> end nla23.linear.bnlib;
> but char* = void* is a C rule, not an Ada rule. It probably works,
> but it's not right. (I don't need to access anything through that
> pointer.)
You're not 100% guaranteed that an Ada type declared as "access
Character" is compatible with a C "char*", or even that Ada's type
Character is the same as C's char.
My suggestion:
type Void_Pointer is private;
Null_Void_Pointer : constant Void_Pointer;
...
type Void_Pointer is new Interfaces.C.Strings.chars_ptr;
Null_Void_Pointer : constant Void_Pointer
:= Interfaces.C.Strings.Null_Ptr;
By making it a private type, you avoid making the operations in
Interfaces.C.Strings visible to clients.
The closest semantic equivalent to C's void* is Ada's System.Address.
This is *almost* certain to be compatible (I doubt that there's an
existing system on which they're different), but the language doesn't
guarantee it.
--
Keith Thompson (The_Other_Keith) kst@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
One of the great tragedies of ancient history is that Helen of Troy
lived before the invention of the champagne bottle.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Interfacing to void *
1999-07-18 0:00 Interfacing to void * William Starner
1999-07-18 0:00 ` Keith Thompson
@ 1999-07-19 0:00 ` jerry
1999-07-19 0:00 ` William Starner
1 sibling, 1 reply; 5+ messages in thread
From: jerry @ 1999-07-19 0:00 UTC (permalink / raw)
William Starner <billeug@worldnet.att.net> wrote:
: If I'm trying to interface to a C library that uses this struct
: struct BigNum {
: void *ptr;
: unsigned int size;
: unsigned int allocated;
: };
: how should I interface to void*?
Depends on what you need to do with the result. In the most basic case
you could do something like:
type BigNum is
record
ptr : System.Address;
size : Interfaces.C.unsigned;
allocated : Interfaces.C.unsigned;
end record;
pragma Convention (C, BigNum);
as a void * basically is a memory address and use
System.Address_To_Access_Conversions to get an access type
to whatever ptr is pointing at.
--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Interfacing to void *
1999-07-19 0:00 ` jerry
@ 1999-07-19 0:00 ` William Starner
1999-07-20 0:00 ` jerry
0 siblings, 1 reply; 5+ messages in thread
From: William Starner @ 1999-07-19 0:00 UTC (permalink / raw)
jerry@jvdsys.stuyts.nl wrote:
>
> William Starner <billeug@worldnet.att.net> wrote:
>
> : If I'm trying to interface to a C library that uses this struct
>
> : struct BigNum {
> : void *ptr;
> : unsigned int size;
> : unsigned int allocated;
> : };
>
> : how should I interface to void*?
>
> Depends on what you need to do with the result.
Absolutely nothing. I just need to be able to declare a pointer to it and
allocate memory for it.
> In the most basic case
> you could do something like:
>
> type BigNum is
> record
> ptr : System.Address;
> size : Interfaces.C.unsigned;
> allocated : Interfaces.C.unsigned;
> end record;
> pragma Convention (C, BigNum);
>
> as a void * basically is a memory address and use
> System.Address_To_Access_Conversions to get an access type
> to whatever ptr is pointing at.
Yes, pragma Convention (C, BigNum). That's probably necessary. I fear that many
bugs are going to appear the first time this gets compiled on a non-GNAT
compiler, with stuff like that (as GNAT uses C convention for almost everything
by default.)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Interfacing to void *
1999-07-19 0:00 ` William Starner
@ 1999-07-20 0:00 ` jerry
0 siblings, 0 replies; 5+ messages in thread
From: jerry @ 1999-07-20 0:00 UTC (permalink / raw)
William Starner <billeug@worldnet.att.net> wrote:
: I fear that many
: bugs are going to appear the first time this gets compiled on a non-GNAT
: compiler, with stuff like that (as GNAT uses C convention for almost everything
: by default.)
a) No, pragma Convention C and Interfaces.C are both standard Ada.
b) GNAT, being part of the gcc suite, makes a point of have the basic
types laid out like the equivalent C types, where appropriate. But this
does _not_ mean that GNAT is somehow using a C convention for its basic
types.
--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1999-07-20 0:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-18 0:00 Interfacing to void * William Starner
1999-07-18 0:00 ` Keith Thompson
1999-07-19 0:00 ` jerry
1999-07-19 0:00 ` William Starner
1999-07-20 0:00 ` jerry
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox