From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Interfacing C type, unconstrained array with record
Date: Sat, 16 Oct 2010 17:41:40 -0400
Date: 2010-10-16T17:41:40-04:00 [thread overview]
Message-ID: <wcc4oclg6i3.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: xtj9ihygde9q$.t30hjfo8ikh2.dlg@40tude.net
"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> You need a flat array, e.g.
>
> with Interfaces; use Interfaces;
> with Interfaces.C; use Interfaces.C;
I'd probably write:
with Interfaces.C; use Interfaces;
and then refer to C.int instead of Int.
No big deal.
> type SDL_Color is record
> R, G, B, Unused : Unsigned_8;
> end record;
> pragma Convention (C, SDL_Color);
> type SDL_Color_Array is array (Positive) of aliased SDL_Color;
I think you want this indexed by the same type as N_Colors.
And why does it need to have aliased components?
So perhaps:
type SDL_Color_Array is array (Int range 1..1_000_000) of SDL_Color;
> pragma Convention (C, SDL_Color_Array);
> type SDL_Palette is record
> N_Colors : Int;
> Colors : SDL_Color_Array;
> end record;
> pragma Convention (C, SDL_Palette);
>
> Now, you cannot directly declare a SDL_Palette variable, because it is too
> large. You should do it as you would do in C, i.e. allocate some amount of
> memory and then set structure pointer to it. You do not need 'new' or
> access types in Ada. It can be on the stack like this:
>
> with System.Storage_Elements; use System.Storage_Elements;
> ...
> Raw_Memory : Storage_Array (1..1_024); -- Memory
> Palette : SDL_Palette;
> for Palette'Address use Raw_Memory'Address;
I think you need to worry about alignment here.
Perhaps:
pragma Assert(SDL_Palette'Alignment = C.int'Alignment);
for Raw_Memory'Alignment use C.int'Alignment;
> begin
> Palette.N_Colors := 20;
>
> Of course computing memory size from N_Colors is up to you (as in C).
You might want to say:
Colors: SDL_Color_Array renames Palette.Colors(1..Palette.N_Colors);
and then refer to components of Colors, so you get array bounds checking.
- Bob
next prev parent reply other threads:[~2010-10-16 21:41 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-16 17:35 Interfacing C type, unconstrained array with record Ron Wills
2010-10-16 18:36 ` Dmitry A. Kazakov
2010-10-16 19:19 ` Ron Wills
2010-10-16 22:15 ` Jeffrey Carter
2010-10-17 10:20 ` Simon Wright
2010-10-17 12:38 ` Robert A Duff
2010-10-17 8:35 ` Dmitry A. Kazakov
2010-10-17 12:30 ` Robert A Duff
2010-10-16 21:41 ` Robert A Duff [this message]
2010-10-16 23:34 ` tmoran
2010-10-17 6:59 ` J-P. Rosen
2010-10-17 12:34 ` Robert A Duff
2010-10-17 7:45 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox