OSDN Git Service

2009-08-28 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / table.adb
index 95da3a7..3bf4eb6 100644 (file)
@@ -6,39 +6,41 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---                            $Revision: 1.44 $
---                                                                          --
---          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2009, 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- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
+--                                                                          --
+-- As a special exception under Section 7 of GPL version 3, you are granted --
+-- additional permissions described in the GCC Runtime Library Exception,   --
+-- version 3.1, as published by the Free Software Foundation.               --
 --                                                                          --
--- As a special exception,  if other files  instantiate  generics from this --
--- unit, or you link  this unit with other files  to produce an executable, --
--- this  unit  does not  by itself cause  the resulting  executable  to  be --
--- covered  by the  GNU  General  Public  License.  This exception does not --
--- however invalidate  any other reasons why  the executable file  might be --
--- covered by the  GNU Public License.                                      --
+-- You should have received a copy of the GNU General Public License and    --
+-- a copy of the GCC Runtime Library Exception along with this program;     --
+-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
+-- <http://www.gnu.org/licenses/>.                                          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
--- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
+-- Extensive contributions were provided by Ada Core Technologies Inc.      --
 --                                                                          --
 ------------------------------------------------------------------------------
 
 with Debug;   use Debug;
-with Opt;
+with Opt;     use Opt;
 with Output;  use Output;
 with System;  use System;
 with Tree_IO; use Tree_IO;
 
+with System.Memory; use System.Memory;
+
+with Unchecked_Conversion;
+
+pragma Elaborate_All (Output);
+
 package body Table is
    package body Table is
 
@@ -49,9 +51,6 @@ package body Table is
       --  Number of entries in currently allocated table. The value of zero
       --  ensures that we initially allocate the table.
 
-      procedure free (T : Table_Ptr);
-      pragma Import (C, free);
-
       -----------------------
       -- Local Subprograms --
       -----------------------
@@ -65,16 +64,36 @@ package body Table is
       --  Return Null_Address if the table length is zero,
       --  Table (First)'Address if not.
 
+      pragma Warnings (Off);
+      --  Turn off warnings. The following unchecked conversions are only used
+      --  internally in this package, and cannot never result in any instances
+      --  of improperly aliased pointers for the client of the package.
+
+      function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
+      function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
+
+      pragma Warnings (On);
+
       ------------
       -- Append --
       ------------
 
       procedure Append (New_Val : Table_Component_Type) is
       begin
-         Increment_Last;
-         Table (Table_Index_Type (Last_Val)) := New_Val;
+         Set_Item (Table_Index_Type (Last_Val + 1), New_Val);
       end Append;
 
+      ----------------
+      -- Append_All --
+      ----------------
+
+      procedure Append_All (New_Vals : Table_Type) is
+      begin
+         for J in New_Vals'Range loop
+            Append (New_Vals (J));
+         end loop;
+      end Append_All;
+
       --------------------
       -- Decrement_Last --
       --------------------
@@ -90,7 +109,7 @@ package body Table is
 
       procedure Free is
       begin
-         free (Table);
+         Free (To_Address (Table));
          Table := null;
          Length := 0;
       end Free;
@@ -113,11 +132,12 @@ package body Table is
       ----------
 
       procedure Init is
-         Old_Length : Int := Length;
+         Old_Length : constant Int := Length;
 
       begin
+         Locked   := False;
          Last_Val := Min - 1;
-         Max      := Min + (Table_Initial * Opt.Table_Factor) - 1;
+         Max      := Min + (Table_Initial * Table_Factor) - 1;
          Length   := Max - Min + 1;
 
          --  If table is same size as before (happens when table is never
@@ -151,19 +171,7 @@ package body Table is
       ----------------
 
       procedure Reallocate is
-
-         function realloc
-           (memblock : Table_Ptr;
-            size     : size_t)
-            return     Table_Ptr;
-         pragma Import (C, realloc);
-
-         function malloc
-           (size     : size_t)
-            return     Table_Ptr;
-         pragma Import (C, malloc);
-
-         New_Size : size_t;
+         New_Size   : Memory.size_t;
 
       begin
          if Max < Last_Val then
@@ -174,10 +182,15 @@ package body Table is
 
             Length := Int'Max (Length, Table_Initial);
 
-            --  Now increment table length until it is sufficiently large
+            --  Now increment table length until it is sufficiently large. Use
+            --  the increment value or 10, which ever is larger (the reason
+            --  for the use of 10 here is to ensure that the table does really
+            --  increase in size (which would not be the case for a table of
+            --  length 10 increased by 3% for instance).
 
             while Max < Last_Val loop
-               Length := Length * (100 + Table_Increment) / 100;
+               Length := Int'Max (Length * (100 + Table_Increment) / 100,
+                                  Length + 10);
                Max := Min + Length - 1;
             end loop;
 
@@ -191,17 +204,16 @@ package body Table is
          end if;
 
          New_Size :=
-           size_t ((Max - Min + 1) *
-                   (Table_Type'Component_Size / Storage_Unit));
+           Memory.size_t ((Max - Min + 1) *
+                          (Table_Type'Component_Size / Storage_Unit));
 
          if Table = null then
-            Table := malloc (New_Size);
+            Table := To_Pointer (Alloc (New_Size));
 
          elsif New_Size > 0 then
             Table :=
-              realloc
-                (memblock => Table,
-                 size     => New_Size);
+              To_Pointer (Realloc (Ptr  => To_Address (Table),
+                                   Size => New_Size));
          end if;
 
          if Length /= 0 and then Table = null then
@@ -231,7 +243,7 @@ package body Table is
 
       procedure Restore (T : Saved_Table) is
       begin
-         free (Table);
+         Free (To_Address (Table));
          Last_Val := T.Last_Val;
          Max      := T.Max;
          Table    := T.Table;
@@ -264,12 +276,74 @@ package body Table is
          (Index : Table_Index_Type;
           Item  : Table_Component_Type)
       is
+         --  If Item is a value within the current allocation, and we are going
+         --  to reallocate, then we must preserve an intermediate copy here
+         --  before calling Increment_Last. Otherwise, if Table_Component_Type
+         --  is passed by reference, we are going to end up copying from
+         --  storage that might have been deallocated from Increment_Last
+         --  calling Reallocate.
+
+         subtype Allocated_Table_T is
+           Table_Type (Table'First .. Table_Index_Type (Max + 1));
+         --  A constrained table subtype one element larger than the currently
+         --  allocated table.
+
+         Allocated_Table_Address : constant System.Address :=
+                                     Table.all'Address;
+         --  Used for address clause below (we can't use non-static expression
+         --  Table.all'Address directly in the clause because some older
+         --  versions of the compiler do not allow it).
+
+         Allocated_Table : Allocated_Table_T;
+         pragma Import (Ada, Allocated_Table);
+         pragma Suppress (Range_Check, On => Allocated_Table);
+         for Allocated_Table'Address use Allocated_Table_Address;
+         --  Allocated_Table represents the currently allocated array, plus one
+         --  element (the supplementary element is used to have a convenient
+         --  way of computing the address just past the end of the current
+         --  allocation). Range checks are suppressed because this unit
+         --  uses direct calls to System.Memory for allocation, and this can
+         --  yield misaligned storage (and we cannot rely on the bootstrap
+         --  compiler supporting specifically disabling alignment checks, so we
+         --  need to suppress all range checks). It is safe to suppress this
+         --  check here because we know that a (possibly misaligned) object
+         --  of that type does actually exist at that address.
+         --  ??? We should really improve the allocation circuitry here to
+         --  guarantee proper alignment.
+
+         Need_Realloc : constant Boolean := Int (Index) > Max;
+         --  True if this operation requires storage reallocation (which may
+         --  involve moving table contents around).
+
       begin
-         if Int (Index) > Max then
-            Set_Last (Index);
-         end if;
+         --  If we're going to reallocate, check whether Item references an
+         --  element of the currently allocated table.
+
+         if Need_Realloc
+           and then Allocated_Table'Address <= Item'Address
+           and then Item'Address <
+                      Allocated_Table (Table_Index_Type (Max + 1))'Address
+         then
+            --  If so, save a copy on the stack because Increment_Last will
+            --  reallocate storage and might deallocate the current table.
+
+            declare
+               Item_Copy : constant Table_Component_Type := Item;
+            begin
+               Set_Last (Index);
+               Table (Index) := Item_Copy;
+            end;
 
-         Table (Index) := Item;
+         else
+            --  Here we know that either we won't reallocate (case of Index <
+            --  Max) or that Item is not in the currently allocated table.
+
+            if Int (Index) > Last_Val then
+               Set_Last (Index);
+            end if;
+
+            Table (Index) := Item;
+         end if;
       end Set_Item;
 
       --------------
@@ -280,6 +354,7 @@ package body Table is
       begin
          if Int (New_Val) < Last_Val then
             Last_Val := Int (New_Val);
+
          else
             Last_Val := Int (New_Val);
 
@@ -306,7 +381,7 @@ package body Table is
       -- Tree_Read --
       ---------------
 
-      --  Note: we allocate only the space required to accomodate the data
+      --  Note: we allocate only the space required to accommodate the data
       --  actually written, which means that a Tree_Write/Tree_Read sequence
       --  does an implicit Release.