comp.lang.ada
 help / color / mirror / Atom feed
* Re: How to supply pointer to C?
  1996-08-27  0:00 How to supply pointer to C? Greg Baker
@ 1996-08-27  0:00 ` David C. Hoos, Sr.
  1996-08-28  0:00   ` Robert Dewar
  0 siblings, 1 reply; 4+ messages in thread
From: David C. Hoos, Sr. @ 1996-08-27  0:00 UTC (permalink / raw)



Hi Greg,
This solution is a portable (i.e. Ada83/Ada95) solution, based on the
assumption that Ada's System.ADDRESS and C's pointer are the same size, and
have identical representations, and that Ada's INTEGER type and C's size_t
type are the same size, and have identical representations.  These
assumptions are usually true.
with System;
--The Ada record could be:
type WHATEVER is 
  record
    Data_Address : System.ADDRESS;
    Data_Size     : NATURAL;
end record;
-- A representation clause insures that the Ada compiler's representation
of the record
-- is the same as the C compiler's representation of the record.
-- Now, some may find fault with this "belt plus suspenders" approach, but
I've been burned
-- by so many compiler differences (even by variations between releases of
the
-- same compiler), that I feel this care is warranted.
for WHATEVER use
  record at mod 4;
    -- NOTE:  We make the assumption here that INTEGERs and ADDRESSes are
the same
    -- size, because in Ada 95, System.ADDRESS'SIZE is not a statuic
expression,
    -- hence can't be used in the record representation clause.
    Data_Address at 0 range 0 .. INTEGER'SIZE -1;
    Data_Size    at 0 range INTEGER'SIZE .. 2 * INTEGER'SIZE - 1;
  end record;

--Now, then if the data items are of some type, say, ITEM_TYPE, then
objects can be
--declared as
An_Item : ITEM_TYPE;
--and a record object can be declared as
A_Whatever : WHATEVER :=
--and the record can be initialized with the following aggregate expression
    (Data_Address => An_Item'ADDRESS, Data_Size => An_Item'SIZE /
System.STORAGE_UNIT);

Hope this helps.

David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

Greg Baker <gbaker@walleroo.rp.csiro.au> wrote in article
<85ybj1zcdu.fsf@walleroo.rp.CSIRO.AU>...
> Interfaces.C.Pointers.Value can get you from a C array to an Ada
> array,  and Interfaces.C.To_C can get you from a String to a
> null-terminated block of chars,  but what if I want to get a block of
> ints,  for examples?
> 
> More generally, what if I have some object and I want to pass its
> address to some C function.  (The question arises as I am trying to
> write an Ada gdbm interface - gdbm_store takes as arguments structs of
> the form-
>   struct { char * dptr;   int data_size; }.
> (i.e. record 
>          dptr : Interfaces.C.Pointers.???
>          data_size : Interfaces.C.int
>       end;
> )
> 
> I'm kind of hoping to do something like
> 
>   x.dptr := Magical_Conversion_function ( object'Access ); 
>   x.data_size = object'Size;
> 
> Any suggestions?
> 
> -- 
> Gregory D. Baker        gbaker@rp.csiro.au         CSIRO Radiophysics 
>    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
>    - predictions of the future - quantum computers - silly poetry  -
>    - the "langue musicale universale" (Solresol)  revival project  - 
>    - - - - - - - - http://www.rp.csiro.au/~gbaker  - - - - - - - - -
> I'm an eccentric mathematician/IT guru/communicator,  and I'm looking
>                for a new job in Sydney,  Australia.
> 




^ permalink raw reply	[flat|nested] 4+ messages in thread

* How to supply pointer to C?
@ 1996-08-27  0:00 Greg Baker
  1996-08-27  0:00 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Baker @ 1996-08-27  0:00 UTC (permalink / raw)



Interfaces.C.Pointers.Value can get you from a C array to an Ada
array,  and Interfaces.C.To_C can get you from a String to a
null-terminated block of chars,  but what if I want to get a block of
ints,  for examples?

More generally, what if I have some object and I want to pass its
address to some C function.  (The question arises as I am trying to
write an Ada gdbm interface - gdbm_store takes as arguments structs of
the form-
  struct { char * dptr;   int data_size; }.
(i.e. record 
         dptr : Interfaces.C.Pointers.???
         data_size : Interfaces.C.int
      end;
)

I'm kind of hoping to do something like

  x.dptr := Magical_Conversion_function ( object'Access ); 
  x.data_size = object'Size;

Any suggestions?

-- 
Gregory D. Baker        gbaker@rp.csiro.au         CSIRO Radiophysics 
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
   - predictions of the future - quantum computers - silly poetry  -
   - the "langue musicale universale" (Solresol)  revival project  - 
   - - - - - - - - http://www.rp.csiro.au/~gbaker  - - - - - - - - -
I'm an eccentric mathematician/IT guru/communicator,  and I'm looking
               for a new job in Sydney,  Australia.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to supply pointer to C?
  1996-08-27  0:00 ` David C. Hoos, Sr.
@ 1996-08-28  0:00   ` Robert Dewar
  1996-08-28  0:00     ` Larry Kilgallen
  0 siblings, 1 reply; 4+ messages in thread
From: Robert Dewar @ 1996-08-28  0:00 UTC (permalink / raw)



David said

"This solution is a portable (i.e. Ada83/Ada95) solution, based on the
assumption that Ada's System.ADDRESS and C's pointer are the same size, and
have identical representations, and that Ada's INTEGER type and C's size_t
type are the same size, and have identical representations.  These
assumptions are usually true."

The size_t assumption is very dangerous, it will typically NOT be true
on any 64-bit machine, for example it is false in the DEC Unix
implementation of GNAT.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to supply pointer to C?
  1996-08-28  0:00   ` Robert Dewar
@ 1996-08-28  0:00     ` Larry Kilgallen
  0 siblings, 0 replies; 4+ messages in thread
From: Larry Kilgallen @ 1996-08-28  0:00 UTC (permalink / raw)



In article <dewar.841212360@schonberg>, dewar@cs.nyu.edu (Robert Dewar) writes:
> David said
> 
> "This solution is a portable (i.e. Ada83/Ada95) solution, based on the
> assumption that Ada's System.ADDRESS and C's pointer are the same size, and
> have identical representations, and that Ada's INTEGER type and C's size_t
> type are the same size, and have identical representations.  These
> assumptions are usually true."
> 
> The size_t assumption is very dangerous, it will typically NOT be true
> on any 64-bit machine, for example it is false in the DEC Unix
> implementation of GNAT.

It is also not true on Alpha VMS using DEC Ada and DEC C...

...but some of us want DEC to change that aspect of DEC Ada...

...reinforcing the danger of such assumptions.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-08-28  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-27  0:00 How to supply pointer to C? Greg Baker
1996-08-27  0:00 ` David C. Hoos, Sr.
1996-08-28  0:00   ` Robert Dewar
1996-08-28  0:00     ` Larry Kilgallen

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