From: Johannes Kliemann <kliem...@adacore.com>

The secondary stack allocator needs to take alignment constraints into
account when doing allocations. In the full runtime the secondary stack
is allocated in chunks on the heap and can grow dynamically. As it does
not grow contiguously the "top" of the stack depends on the size of
the allocation. Therefore the alignment of the stack top is not known at
allocation time and the padding needed for a particular alignment needs
to be calculated conservatively to ensure the allocation fits the
requested size after the base address has been aligned.
On more restricted platforms the secondary stack is a contiguous block
of statically allocated memory. Here the conservative mechanism is not
required since the allocations base address is known and the required
padding can be calculated right away. The conservative approach also
sometimes causes an allocation to be slightly larger than it needs to
be. This can be a problem on platforms with limited RAM availability. To
avoid this problem modify the calculation of the required padding on these
platforms to always exactly fit the required size.

gcc/ada/ChangeLog:

        * libgnat/s-secsta.adb (SS_Allocate): Add comment about
        conservative alignment padding calculation.
        * libgnat/s-secsta__cheri.adb (SS_Allocate): Add comment about
        conservative alignment padding calculation.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/libgnat/s-secsta.adb        | 9 +++++++++
 gcc/ada/libgnat/s-secsta__cheri.adb | 9 +++++++++
 2 files changed, 18 insertions(+)

diff --git a/gcc/ada/libgnat/s-secsta.adb b/gcc/ada/libgnat/s-secsta.adb
index 2749658114a..9d78b868b7e 100644
--- a/gcc/ada/libgnat/s-secsta.adb
+++ b/gcc/ada/libgnat/s-secsta.adb
@@ -633,6 +633,15 @@ package body System.Secondary_Stack is
 
       if Over_Aligning then
          Padding := Alignment;
+
+         --  Typically the padding would be
+         --  Alignment - (Addr mod Alignment)
+         --  however Addr in this case is not known yet. It depends on the
+         --  type of the secondary stack (Dynamic/Static). The allocation
+         --  routine for the respective type of stack requires to know the
+         --  allocation size before the address is known. To ensure a
+         --  sufficient allocation size to fit the padding, the padding is
+         --  calculated conservatively.
       end if;
 
       --  Round the requested size (plus the needed padding in case of
diff --git a/gcc/ada/libgnat/s-secsta__cheri.adb 
b/gcc/ada/libgnat/s-secsta__cheri.adb
index a24b50e2f74..9a65ed2879c 100644
--- a/gcc/ada/libgnat/s-secsta__cheri.adb
+++ b/gcc/ada/libgnat/s-secsta__cheri.adb
@@ -662,6 +662,15 @@ package body System.Secondary_Stack is
 
       if Over_Aligning then
          Over_Align_Padding := Alignment;
+
+         --  Typically the padding would be
+         --  Alignment - (Addr mod Alignment)
+         --  however Addr in this case is not known yet. It depends on the
+         --  type of the secondary stack (Dynamic/Static). The allocation
+         --  routine for the respective type of stack requires to know the
+         --  allocation size before the address is known. To ensure a
+         --  sufficient allocation size to fit the padding, the padding is
+         --  calculated conservatively.
       end if;
 
       --  It should not be possible to request an allocation of negative
-- 
2.43.0

Reply via email to