comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Alignment issue
Date: Sat, 16 Feb 2019 19:40:38 +0000
Date: 2019-02-16T19:40:38+00:00	[thread overview]
Message-ID: <ly36onplmx.fsf@pushface.org> (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)

             reply	other threads:[~2019-02-16 19:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-16 19:40 Simon Wright [this message]
2019-02-18 23:01 ` Alignment issue Randy Brukardt
replies disabled

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