comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@bix.com
Subject: Re: More C
Date: 2000/01/28
Date: 2000-01-28T00:00:00+00:00	[thread overview]
Message-ID: <kgmk4.14$M83.2117@nnrp3-w.snfc21.pbi.net> (raw)
In-Reply-To: 86skkc$9281@news.cis.okstate.edu

>In memory (assuming 8 bit pointers) we have
>
>num_chars_in_b
>|  pointer    array
>|  |          |
>03 20 ......  01 10 10 23 58 20 48 . . .
>
>The only place in that memory where it tells you the length of the
>array is num_chars_in_b. No matter how you translate that structure
>into Ada, Ada can't give you the length of the array independent of
>num_chars_in_b, because it doesn't have the information. Or so
>I understand it.

Quite so.  But in the message I posted it said:
>I presume you really want Colors to point to a Color_List, not to a
>single color.  Note that Colors will point to a list of N colors,
>where N may, or may not, equal Number_Of_Colors.

in which case the pointer points to an array and the compiler
either knows its bounds or knows where to look to find its
bounds (if they are dynamic).

The case you have is:

>If you really must match the memory layout of the C SDL_Palette,
>
>type Arbitrary_Color_List is array(C.Int range) of aliased SDL_Color;
>type Arbitrary_Color_List_Access is access all Color_List;
>
>type SDL_Palette is record
>        Number_Of_Colors: C.Int;
>        Colors: Arbitrary_Color_List_Access;
>end record;
>
>is probably your best bet.  You then have to do your own index
>checking, just as in C, and you'll want a representation clause.

Since the compiler knows the (unchanging) bounds, here all of C.Int,
it needn't put them in memory anywhere and can set the pointer
to the array Colors to be the identical bits to a pointer
to the first element of Colors, which matches the C layout.
There's no guarantee it will do this, so you must experiment to
see what happens and put big signs around warning of danger, but
it will probably work.  An alternative, more work but likely
more safely portable, is the suggestion from Jeff Carter of

>type SDL_Palette is record
>   Num_Colors : C.Int;
>   Address_Of_Color_Array : System.Address;
>end record;
>
>Given:
>
>   Palette : SDL_Palette;
>
>containing data from C, use
>
>type Color_Set is array (C.Int range <>) of SDL_Color;
>
>Colors : Color_Set (1 .. Palette.Num_Colors);
>for Colors'Address use Palette.Address_Of_Color_Array;

Note, however, that a C pointer is not necessarily the same
as an Ada System.Address (consider various i86 memory models)
so that probably ought to be modified to

type SDL_Palette is record
   Num_Colors : C.Int;
   C_Access_Of_Color_Array : Interfaces.C.Strings.char_array_access;
end record;

and then use System.Address_To_Access_Conversions to convert
C_Access_Of_Color_Array to a System.Address to use in the
  for Colors'Address use the_address_from_C_Access_Of_Color_Array;

PS. I should have said
  type Arbitrary_Color_List is array(C.Int range 0 .. C.Int'last)
  of aliased SDL_Color;
since you probably want index 0 (or maybe 1), rather than C.Int'first,
to indicate the first element.




  reply	other threads:[~2000-01-28  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <vhizotrc4x7.fsf@grotte.ifi.uio.no>
2000-01-28  0:00 ` More C Gautier
2000-01-28  0:00 ` Jeff Carter
2000-01-28  0:00 ` tmoran
2000-01-28  0:00   ` David Starner
2000-01-28  0:00     ` Pascal Obry
2000-01-28  0:00       ` David Starner
2000-01-28  0:00         ` tmoran [this message]
2000-01-30  0:00 ` Nick Roberts
     [not found]   ` <vhioga1r2j8.fsf@grotte.ifi.uio.no>
2000-02-01  0:00     ` tmoran
2000-02-02  0:00     ` Nick Roberts
2000-02-03  0:00     ` Keith 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