comp.lang.ada
 help / color / mirror / Atom feed
* Alignment issue
@ 2019-02-16 19:40 Simon Wright
  2019-02-18 23:01 ` Randy Brukardt
  0 siblings, 1 reply; 2+ messages in thread
From: Simon Wright @ 2019-02-16 19:40 UTC (permalink / raw)


I have code like this (written while working on a StackOverflow
question), and GNAT ignores apparent alignment requests.

   with System.Storage_Pools;
   with System.Storage_Elements;
   package Alignment_Issue is

      type Data_Store is new System.Storage_Elements.Storage_Array
      with Alignment => 16;  --  Standard'Maximum_Alignment;

      type User_Pool (Size : System.Storage_Elements.Storage_Count)
         is  record
            Flag          : Boolean;
            Data          : Data_Store (1 .. Size);
         end record
      with Alignment => 16;  --  Standard'Maximum_Alignment;

   end Alignment_Issue;

(Standard'Maximum_Alignment is a GNAT special) and compiling with GNAT
CE 2018 (and other GNAT compilers) I see

   $ /opt/gnat-ce-2018/bin/gnatmake -c -u -f -gnatR alignment_issue.ads 
   gcc -c -gnatR alignment_issue.ads

   Representation information for unit Alignment_Issue (spec)
   ----------------------------------------------------------

   for Data_Store'Alignment use 16;
   for Data_Store'Component_Size use 8;

   for User_Pool'Object_Size use ??;
   for User_Pool'Value_Size use ??;
   for User_Pool'Alignment use 16;
   for User_Pool use record
      Size at 0 range  0 .. 63;
      Flag at 8 range  0 ..  7;
      Data at 9 range  0 .. ??;
   end record;

which means that GNAT has ignored the alignment specified for Data_Store
when setting up User_Pool.Data.

Is this expected? OK?

I found a workround of sorts:

   type Data_Store (Size : System.Storage_Elements.Storage_Count) is record
      Data : System.Storage_Elements.Storage_Array (1 .. Size);
   end record
   with Alignment => 16;  --  Standard'Maximum_Alignment;

   type User_Pool (Size : System.Storage_Elements.Storage_Count)
      is record
         Flag  : Boolean;
         Stack : Data_Store (Size);
      end record;

giving

   Representation information for unit Alignment_Issue (spec)
   ----------------------------------------------------------

   for Data_Store'Object_Size use ??;
   for Data_Store'Value_Size use ??;
   for Data_Store'Alignment use 16;
   for Data_Store use record
      Size at 0 range  0 .. 63;
      Data at 8 range  0 .. ??;
   end record;

   for User_Pool'Object_Size use ??;
   for User_Pool'Value_Size use ??;
   for User_Pool'Alignment use 16;
   for User_Pool use record
      Size  at  0 range  0 .. 63;
      Flag  at  8 range  0 ..  7;
      Stack at 16 range  0 .. ??;
   end record;

(but even then I see that Stack.Data is offset by 8 bytes because of the
discriminant)

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

end of thread, other threads:[~2019-02-18 23:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-16 19:40 Alignment issue Simon Wright
2019-02-18 23:01 ` Randy Brukardt

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