comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Need help mapping a C struct to Ada
Date: Wed, 21 Mar 2001 21:25:40 GMT
Date: 2001-03-21T21:25:40+00:00	[thread overview]
Message-ID: <o%8u6.56105$zV3.4715130@news1.frmt1.sfba.home.com> (raw)
In-Reply-To: WH7u6.40$Je5.495@newsfeed.slurp.net

>  type element_table (count: positive) is record
>     num_elements:   integer;
>     the_elements:   element_array(0..count-1);
>  end record;
>That failed because Ada allocated space in element_table for the
>field 'count'.  The C code, knowing nothing about 'count' proceded

  An Ada description of the C data structure is

   type element_table (count: Interfaces.C.Int) is record
      the_elements:   element_array(Interfaces.C.Unsigned);
   end record;

There is a single "count", and the number of elements is potentially
very large.  If C routines allocate these objects, they will pass back
pointers, not massive structures, so there's no worry about space, but
it's up to you to do the subscript range checking.  If you actually need
to allocate one of these things in Ada, clearly the
"element_array(Interfaces.C.Unsigned)" is unreasonable, so you will have
to figure out some maximum on the number of elements and do

   Max_Possible_Index:constant Interfaces.C.Int := ???
   type element_table (count: Interfaces.C.Int) is record
      the_elements:   element_array(0 .. Max_Possible_Index);
   end record;

>In this case I'm just crossing my fingers and hoping that the
>Ada compiler lays out the element_table record the same way the
>C compiler does.

  A very last resort.  Think "when the C compiler sees one of these
objects being used, what information does it know?".  It knows the first
part of storage is an int, followed by any number of "element"s.  If the
Ada compiler knows more, such as a value for "num_elements" that could
differ from "count", or a dynamic upper bound for the "element_array",
clearly the Ada compiler will have to store that additional information
someplace, and thus the storage arrangements must necessarily differ.
You must inform the Ada compiler of just what (how little) you know when
you get one of these things from a C call.

  You should also use a representation clause

   for element_table use record
     count at 0 range 0 .. Interfaces.C.Int'size - 1;
     the_elements at Interfaces.C.Int'size/System.Storage_Unit
       range 0 .. (Max_Possible_Index+1)*Interfaces.C.Int'size - 1;
   end record;

That will tell the compiler what you want, tell future program maintainers
what you want, and let the compiler tell you if what it's going to do
differs from what you want.



  reply	other threads:[~2001-03-21 21:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-21 19:56 Need help mapping a C struct to Ada (null)
2001-03-21 21:25 ` tmoran [this message]
2001-03-22 20:53   ` tmoran
2001-03-21 22:21 ` Jeffrey Carter
2001-03-21 23:12   ` Florian Weimer
2001-03-30  5:59 ` David Thompson
replies disabled

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