Patchwork [Ada] Add new attribute System_Allocator_Alignment

login
register
mail settings
Submitter Arnaud Charlet
Date Aug. 29, 2011, 9:25 a.m.
Message ID <20110829092506.GA19226@adacore.com>
Download mbox | patch
Permalink /patch/111997/
State New
Headers show

Comments

Arnaud Charlet - Aug. 29, 2011, 9:25 a.m.
This attribute returns the alignment honored by malloc.
The following program should display this value:
with Ada.Text_IO; use Ada.Text_IO;

procedure Alloc_Algn is
begin
   Put_Line ("malloc alignment:"
             & Integer'Image (Standard'System_Allocator_Alignment));
end Alloc_Algn;

Tested on x86_64-pc-linux-gnu, committed on trunk

2011-08-29  Tristan Gingold  <gingold@adacore.com>

	* sem_attr.adb, exp_attr.adb: Add handling of
	Attribute_System_Allocator_Alignment
	* snames.ads-tmpl: Add Name_System_Allocator_Alignment and
	Attribute_System_Allocator_Alignment.
	* ttypes.ads, get_targ.ads: Add Get_System_Allocator_Alignment.
	* gcc-interface/targtyps.c, gcc-interface/utils2.c,
	gcc-interface/gigi.h: Renames get_target_default_allocator_alignment to
	get_target_system_allocator_alignment.

Patch

Index: gnat_rm.texi
===================================================================
--- gnat_rm.texi	(revision 178168)
+++ gnat_rm.texi	(working copy)
@@ -270,6 +270,7 @@ 
 * Small::
 * Storage_Unit::
 * Stub_Type::
+* System_Allocator_Alignment::
 * Target_Name::
 * Tick::
 * To_Address::
@@ -5752,6 +5753,7 @@ 
 * Small::
 * Storage_Unit::
 * Stub_Type::
+* System_Allocator_Alignment::
 * Target_Name::
 * Tick::
 * To_Address::
@@ -6490,6 +6492,18 @@ 
 unit @code{System.Partition_Interface}. Use of this attribute will create
 an implicit dependency on this unit.
 
+@node System_Allocator_Alignment
+@unnumberedsec System_Allocator_Alignment
+@cindex Alignment, allocator
+@findex System_Allocator_Alignment
+@noindent
+@code{Standard'System_Allocator_Alignment} (@code{Standard} is the only
+permissible prefix) provides the observable guaranted to be honored by
+the system allocator (malloc). This is a static value that can be used
+in user storage pools based on malloc either to reject allocation
+with alignment too large or to enable a realignment circuitry if the
+alignment request is larger than this value.
+
 @node Target_Name
 @unnumberedsec Target_Name
 @findex Target_Name
Index: exp_attr.adb
===================================================================
--- exp_attr.adb	(revision 178166)
+++ exp_attr.adb	(working copy)
@@ -5379,6 +5379,7 @@ 
            Attribute_Small                        |
            Attribute_Storage_Unit                 |
            Attribute_Stub_Type                    |
+           Attribute_System_Allocator_Alignment   |
            Attribute_Target_Name                  |
            Attribute_Type_Class                   |
            Attribute_Type_Key                     |
Index: sem_attr.adb
===================================================================
--- sem_attr.adb	(revision 178166)
+++ sem_attr.adb	(working copy)
@@ -4563,6 +4563,13 @@ 
             end if;
          end if;
 
+      --------------------------------
+      -- System_Allocator_Alignment --
+      --------------------------------
+
+      when Attribute_System_Allocator_Alignment =>
+         Standard_Attribute (Ttypes.System_Allocator_Alignment);
+
       ---------
       -- Tag --
       ---------
@@ -7698,61 +7705,62 @@ 
       --  Note that in some cases, the values have already been folded as
       --  a result of the processing in Analyze_Attribute.
 
-      when Attribute_Abort_Signal             |
-           Attribute_Access                   |
-           Attribute_Address                  |
-           Attribute_Address_Size             |
-           Attribute_Asm_Input                |
-           Attribute_Asm_Output               |
-           Attribute_Base                     |
-           Attribute_Bit_Order                |
-           Attribute_Bit_Position             |
-           Attribute_Callable                 |
-           Attribute_Caller                   |
-           Attribute_Class                    |
-           Attribute_Code_Address             |
-           Attribute_Compiler_Version         |
-           Attribute_Count                    |
-           Attribute_Default_Bit_Order        |
-           Attribute_Elaborated               |
-           Attribute_Elab_Body                |
-           Attribute_Elab_Spec                |
-           Attribute_Elab_Subp_Body           |
-           Attribute_Enabled                  |
-           Attribute_External_Tag             |
-           Attribute_Fast_Math                |
-           Attribute_First_Bit                |
-           Attribute_Input                    |
-           Attribute_Last_Bit                 |
-           Attribute_Maximum_Alignment        |
-           Attribute_Old                      |
-           Attribute_Output                   |
-           Attribute_Partition_ID             |
-           Attribute_Pool_Address             |
-           Attribute_Position                 |
-           Attribute_Priority                 |
-           Attribute_Read                     |
-           Attribute_Result                   |
-           Attribute_Storage_Pool             |
-           Attribute_Storage_Size             |
-           Attribute_Storage_Unit             |
-           Attribute_Stub_Type                |
-           Attribute_Tag                      |
-           Attribute_Target_Name              |
-           Attribute_Terminated               |
-           Attribute_To_Address               |
-           Attribute_Type_Key                 |
-           Attribute_UET_Address              |
-           Attribute_Unchecked_Access         |
-           Attribute_Universal_Literal_String |
-           Attribute_Unrestricted_Access      |
-           Attribute_Valid                    |
-           Attribute_Value                    |
-           Attribute_Wchar_T_Size             |
-           Attribute_Wide_Value               |
-           Attribute_Wide_Wide_Value          |
-           Attribute_Word_Size                |
-           Attribute_Write                    =>
+      when Attribute_Abort_Signal               |
+           Attribute_Access                     |
+           Attribute_Address                    |
+           Attribute_Address_Size               |
+           Attribute_Asm_Input                  |
+           Attribute_Asm_Output                 |
+           Attribute_Base                       |
+           Attribute_Bit_Order                  |
+           Attribute_Bit_Position               |
+           Attribute_Callable                   |
+           Attribute_Caller                     |
+           Attribute_Class                      |
+           Attribute_Code_Address               |
+           Attribute_Compiler_Version           |
+           Attribute_Count                      |
+           Attribute_Default_Bit_Order          |
+           Attribute_Elaborated                 |
+           Attribute_Elab_Body                  |
+           Attribute_Elab_Spec                  |
+           Attribute_Elab_Subp_Body             |
+           Attribute_Enabled                    |
+           Attribute_External_Tag               |
+           Attribute_Fast_Math                  |
+           Attribute_First_Bit                  |
+           Attribute_Input                      |
+           Attribute_Last_Bit                   |
+           Attribute_Maximum_Alignment          |
+           Attribute_Old                        |
+           Attribute_Output                     |
+           Attribute_Partition_ID               |
+           Attribute_Pool_Address               |
+           Attribute_Position                   |
+           Attribute_Priority                   |
+           Attribute_Read                       |
+           Attribute_Result                     |
+           Attribute_Storage_Pool               |
+           Attribute_Storage_Size               |
+           Attribute_Storage_Unit               |
+           Attribute_Stub_Type                  |
+           Attribute_System_Allocator_Alignment |
+           Attribute_Tag                        |
+           Attribute_Target_Name                |
+           Attribute_Terminated                 |
+           Attribute_To_Address                 |
+           Attribute_Type_Key                   |
+           Attribute_UET_Address                |
+           Attribute_Unchecked_Access           |
+           Attribute_Universal_Literal_String   |
+           Attribute_Unrestricted_Access        |
+           Attribute_Valid                      |
+           Attribute_Value                      |
+           Attribute_Wchar_T_Size               |
+           Attribute_Wide_Value                 |
+           Attribute_Wide_Wide_Value            |
+           Attribute_Word_Size                  |
+           Attribute_Write                      =>
 
          raise Program_Error;
       end case;
Index: ttypes.ads
===================================================================
--- ttypes.ads	(revision 178155)
+++ ttypes.ads	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -188,6 +188,10 @@ 
    --  The maximum alignment, in storage units, that an object or
    --  type may require on the target machine.
 
+   System_Allocator_Alignment : constant Pos :=
+     Get_System_Allocator_Alignment;
+   --  The alignment, in storage units, of addresses returned by malloc.
+
    Max_Unaligned_Field : constant Pos := Get_Max_Unaligned_Field;
    --  The maximum supported size in bits for a field that is not aligned
    --  on a storage unit boundary.
Index: get_targ.ads
===================================================================
--- get_targ.ads	(revision 178155)
+++ get_targ.ads	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -95,6 +95,10 @@ 
    function Get_Strict_Alignment return Nat;
    pragma Import (C, Get_Strict_Alignment, "get_target_strict_alignment");
 
+   function Get_System_Allocator_Alignment return Nat;
+   pragma Import (C, Get_System_Allocator_Alignment,
+                  "get_target_system_allocator_alignment");
+
    function Get_Double_Float_Alignment return Nat;
    pragma Import (C, Get_Double_Float_Alignment,
                   "get_target_double_float_alignment");
Index: snames.ads-tmpl
===================================================================
--- snames.ads-tmpl	(revision 178165)
+++ snames.ads-tmpl	(working copy)
@@ -814,6 +814,7 @@ 
    Name_Storage_Size                   : constant Name_Id := N + $;
    Name_Storage_Unit                   : constant Name_Id := N + $; -- GNAT
    Name_Stream_Size                    : constant Name_Id := N + $; -- Ada 05
+   Name_System_Allocator_Alignment     : constant Name_Id := N + $; -- GNAT
    Name_Tag                            : constant Name_Id := N + $;
    Name_Target_Name                    : constant Name_Id := N + $; -- GNAT
    Name_Terminated                     : constant Name_Id := N + $;
@@ -1354,6 +1355,7 @@ 
       Attribute_Storage_Size,
       Attribute_Storage_Unit,
       Attribute_Stream_Size,
+      Attribute_System_Allocator_Alignment,
       Attribute_Tag,
       Attribute_Target_Name,
       Attribute_Terminated,
Index: gcc-interface/targtyps.c
===================================================================
--- gcc-interface/targtyps.c	(revision 178155)
+++ gcc-interface/targtyps.c	(working copy)
@@ -6,7 +6,7 @@ 
  *                                                                          *
  *                                  Body                                    *
  *                                                                          *
- *          Copyright (C) 1992-2010, Free Software Foundation, Inc.         *
+ *          Copyright (C) 1992-2011, Free Software Foundation, Inc.         *
  *                                                                          *
  * GNAT is free software;  you can  redistribute it  and/or modify it under *
  * terms of the  GNU General Public License as published  by the Free Soft- *
@@ -149,7 +149,7 @@ 
   return BIGGEST_ALIGNMENT / BITS_PER_UNIT;
 }
 
-/* Standard'Default_Allocator_Alignment.  Alignment guaranteed to be honored
+/* Standard'System_Allocator_Alignment.  Alignment guaranteed to be honored
    by the default allocator (System.Memory.Alloc or malloc if we have no
    run-time library at hand).
 
@@ -172,7 +172,7 @@ 
 #endif
 
 Pos
-get_target_default_allocator_alignment (void)
+get_target_system_allocator_alignment (void)
 {
   return MALLOC_ALIGNMENT / BITS_PER_UNIT;
 }
Index: gcc-interface/utils2.c
===================================================================
--- gcc-interface/utils2.c	(revision 178155)
+++ gcc-interface/utils2.c	(working copy)
@@ -1907,13 +1907,13 @@ 
      stored just in front.  */
 
   unsigned int data_align = TYPE_ALIGN (data_type);
-  unsigned int default_allocator_alignment
-      = get_target_default_allocator_alignment () * BITS_PER_UNIT;
+  unsigned int system_allocator_alignment
+      = get_target_system_allocator_alignment () * BITS_PER_UNIT;
 
   tree aligning_type
-    = ((data_align > default_allocator_alignment)
+    = ((data_align > system_allocator_alignment)
        ? make_aligning_type (data_type, data_align, data_size,
-			     default_allocator_alignment,
+			     system_allocator_alignment,
 			     POINTER_SIZE / BITS_PER_UNIT)
        : NULL_TREE);
 
@@ -1986,12 +1986,12 @@ 
      return value, stored in front of the data block at allocation time.  */
 
   unsigned int data_align = TYPE_ALIGN (data_type);
-  unsigned int default_allocator_alignment
-      = get_target_default_allocator_alignment () * BITS_PER_UNIT;
+  unsigned int system_allocator_alignment
+      = get_target_system_allocator_alignment () * BITS_PER_UNIT;
 
   tree free_ptr;
 
-  if (data_align > default_allocator_alignment)
+  if (data_align > system_allocator_alignment)
     {
       /* DATA_FRONT_PTR (void *)
 	 = (void *)DATA_PTR - (void *)sizeof (void *))  */
Index: gcc-interface/gigi.h
===================================================================
--- gcc-interface/gigi.h	(revision 178155)
+++ gcc-interface/gigi.h	(working copy)
@@ -954,7 +954,7 @@ 
 extern Pos get_target_long_double_size (void);
 extern Pos get_target_pointer_size (void);
 extern Pos get_target_maximum_default_alignment (void);
-extern Pos get_target_default_allocator_alignment (void);
+extern Pos get_target_system_allocator_alignment (void);
 extern Pos get_target_maximum_allowed_alignment (void);
 extern Pos get_target_maximum_alignment (void);
 extern Nat get_float_words_be (void);