1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  
     | 
    
      ------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                   G N A T . D Y N A M I C _ T A B L E S                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2000-2018, AdaCore                     --
--                                                                          --
-- 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 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.                                     --
--                                                                          --
-- 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.               --
--                                                                          --
-- 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. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------
pragma Compiler_Unit_Warning;
with GNAT.Heap_Sort_G;
with Ada.Unchecked_Deallocation;
with System;
package body GNAT.Dynamic_Tables is
   -----------------------
   -- Local Subprograms --
   -----------------------
   function Last_Allocated (T : Instance) return Table_Last_Type;
   pragma Inline (Last_Allocated);
   --  Return the index of the last allocated element
   procedure Grow (T : in out Instance; New_Last : Table_Last_Type);
   --  This is called when we are about to set the value of Last to a value
   --  that is larger than Last_Allocated. This reallocates the table to the
   --  larger size, as indicated by New_Last. At the time this is called,
   --  Last (T) is still the old value, and this does not modify it.
   --------------
   -- Allocate --
   --------------
   procedure Allocate (T : in out Instance; Num : Integer := 1) is
   begin
      --  Note that Num can be negative
      pragma Assert (not T.Locked);
      Set_Last (T, Last (T) + Table_Index_Type'Base (Num));
   end Allocate;
   ------------
   -- Append --
   ------------
   procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
      pragma Assert (not T.Locked);
      New_Last : constant Table_Last_Type := Last (T) + 1;
   begin
      if New_Last <= Last_Allocated (T) then
         --  Fast path
         T.P.Last := New_Last;
         T.Table (New_Last) := New_Val;
      else
         Set_Item (T, New_Last, New_Val);
      end if;
   end Append;
   ----------------
   -- Append_All --
   ----------------
   procedure Append_All (T : in out Instance; New_Vals : Table_Type) is
   begin
      for J in New_Vals'Range loop
         Append (T, New_Vals (J));
      end loop;
   end Append_All;
   --------------------
   -- Decrement_Last --
   --------------------
   procedure Decrement_Last (T : in out Instance) is
   begin
      pragma Assert (not T.Locked);
      Allocate (T, -1);
   end Decrement_Last;
   -----------
   -- First --
   -----------
   function First return Table_Index_Type is
   begin
      return Table_Low_Bound;
   end First;
   --------------
   -- For_Each --
   --------------
   procedure For_Each (Table : Instance) is
      Quit : Boolean := False;
   begin
      for Index in First .. Last (Table) loop
         Action (Index, Table.Table (Index), Quit);
         exit when Quit;
      end loop;
   end For_Each;
   ----------
   -- Grow --
   ----------
   procedure Grow (T : in out Instance; New_Last : Table_Last_Type) is
      --  Note: Type Alloc_Ptr below needs to be declared locally so we know
      --  the bounds. That means that the collection is local, so is finalized
      --  when leaving Grow. That's why this package doesn't support controlled
      --  types; the table elements would be finalized prematurely. An Ada
      --  implementation would also be within its rights to reclaim the
      --  storage. Fortunately, GNAT doesn't do that.
      pragma Assert (not T.Locked);
      pragma Assert (New_Last > Last_Allocated (T));
      subtype Table_Length_Type is Table_Index_Type'Base
        range 0 .. Table_Index_Type'Base'Last;
      Old_Last_Allocated   : constant Table_Last_Type := Last_Allocated (T);
      Old_Allocated_Length : constant Table_Length_Type :=
                               Old_Last_Allocated - First + 1;
      New_Length : constant Table_Length_Type := New_Last - First + 1;
      New_Allocated_Length : Table_Length_Type;
   begin
      if T.Table = Empty_Table_Ptr then
         New_Allocated_Length := Table_Length_Type (Table_Initial);
      else
         New_Allocated_Length :=
           Table_Length_Type
             (Long_Long_Integer (Old_Allocated_Length) *
               (100 + Long_Long_Integer (Table_Increment)) / 100);
      end if;
      --  Make sure it really did grow
      if New_Allocated_Length <= Old_Allocated_Length then
         New_Allocated_Length := Old_Allocated_Length + 10;
      end if;
      if New_Allocated_Length <= New_Length then
         New_Allocated_Length := New_Length + 10;
      end if;
      pragma Assert (New_Allocated_Length > Old_Allocated_Length);
      pragma Assert (New_Allocated_Length > New_Length);
      T.P.Last_Allocated := First + New_Allocated_Length - 1;
      declare
         subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
         type Old_Alloc_Ptr is access all Old_Alloc_Type;
         procedure Free is
           new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
         function To_Old_Alloc_Ptr is
           new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
         subtype Alloc_Type is
           Table_Type (First .. First + New_Allocated_Length - 1);
         type Alloc_Ptr is access all Alloc_Type;
         function To_Table_Ptr is
           new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
         Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
         New_Table : constant Alloc_Ptr := new Alloc_Type;
      begin
         if T.Table /= Empty_Table_Ptr then
            New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
            Free (Old_Table);
         end if;
         T.Table := To_Table_Ptr (New_Table);
      end;
      pragma Assert (New_Last <= Last_Allocated (T));
      pragma Assert (T.Table /= null);
      pragma Assert (T.Table /= Empty_Table_Ptr);
   end Grow;
   --------------------
   -- Increment_Last --
   --------------------
   procedure Increment_Last (T : in out Instance) is
   begin
      pragma Assert (not T.Locked);
      Allocate (T, 1);
   end Increment_Last;
   ----------
   -- Init --
   ----------
   procedure Init (T : in out Instance) is
      pragma Assert (not T.Locked);
      subtype Alloc_Type is Table_Type (First .. Last_Allocated (T));
      type Alloc_Ptr is access all Alloc_Type;
      procedure Free is new Ada.Unchecked_Deallocation (Alloc_Type, Alloc_Ptr);
      function To_Alloc_Ptr is
        new Ada.Unchecked_Conversion (Table_Ptr, Alloc_Ptr);
      Temp : Alloc_Ptr := To_Alloc_Ptr (T.Table);
   begin
      if T.Table = Empty_Table_Ptr then
         pragma Assert (T.P = (Last_Allocated | Last => First - 1));
         null;
      else
         Free (Temp);
         T.Table := Empty_Table_Ptr;
         T.P := (Last_Allocated | Last => First - 1);
      end if;
   end Init;
   --------------
   -- Is_Empty --
   --------------
   function Is_Empty (T : Instance) return Boolean is
   begin
      return Last (T) = First - 1;
   end Is_Empty;
   ----------
   -- Last --
   ----------
   function Last (T : Instance) return Table_Last_Type is
   begin
      return T.P.Last;
   end Last;
   --------------------
   -- Last_Allocated --
   --------------------
   function Last_Allocated (T : Instance) return Table_Last_Type is
   begin
      return T.P.Last_Allocated;
   end Last_Allocated;
   ----------
   -- Move --
   ----------
   procedure Move (From, To : in out Instance) is
   begin
      pragma Assert (not From.Locked);
      pragma Assert (not To.Locked);
      pragma Assert (Is_Empty (To));
      To := From;
      From.Table            := Empty_Table_Ptr;
      From.Locked           := False;
      From.P.Last_Allocated := First - 1;
      From.P.Last           := First - 1;
      pragma Assert (Is_Empty (From));
   end Move;
   -------------
   -- Release --
   -------------
   procedure Release (T : in out Instance) is
      pragma Assert (not T.Locked);
      Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
      function New_Last_Allocated return Table_Last_Type;
      --  Compute the new value of Last_Allocated. This is normally equal to
      --  Last, but if Release_Threshold /= 0, then we need to take that into
      --  account.
      ------------------------
      -- New_Last_Allocated --
      ------------------------
      function New_Last_Allocated return Table_Last_Type is
         subtype Table_Length_Type is Table_Index_Type'Base
           range 0 .. Table_Index_Type'Base'Last;
         Length : constant Table_Length_Type := Last (T) - First + 1;
         Comp_Size_In_Bytes : constant Table_Length_Type :=
           Table_Type'Component_Size / System.Storage_Unit;
         Length_Threshold : constant Table_Length_Type :=
           Table_Length_Type (Release_Threshold) / Comp_Size_In_Bytes;
      begin
         if Release_Threshold = 0 or else Length < Length_Threshold then
            return Last (T);
         else
            declare
               Extra_Length : constant Table_Length_Type := Length / 1000;
            begin
               return (Length + Extra_Length) - 1 + First;
            end;
         end if;
      end New_Last_Allocated;
      --  Local variables
      New_Last_Alloc : constant Table_Last_Type := New_Last_Allocated;
   --  Start of processing for Release
   begin
      if New_Last_Alloc < Last_Allocated (T) then
         pragma Assert (Last (T) < Last_Allocated (T));
         pragma Assert (T.Table /= Empty_Table_Ptr);
         declare
            subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
            type Old_Alloc_Ptr is access all Old_Alloc_Type;
            procedure Free is
              new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
            function To_Old_Alloc_Ptr is
              new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
            subtype Alloc_Type is Table_Type (First .. New_Last_Alloc);
            type Alloc_Ptr is access all Alloc_Type;
            function To_Table_Ptr is
              new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
            Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
            New_Table : constant Alloc_Ptr := new Alloc_Type;
         begin
            New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
            T.P.Last_Allocated := New_Last_Alloc;
            Free (Old_Table);
            T.Table := To_Table_Ptr (New_Table);
         end;
      end if;
   end Release;
   --------------
   -- Set_Item --
   --------------
   procedure Set_Item
     (T     : in out Instance;
      Index : Valid_Table_Index_Type;
      Item  : Table_Component_Type)
   is
   begin
      pragma Assert (not T.Locked);
      --  If Set_Last is going to reallocate the table, we make a copy of Item,
      --  in case the call was "Set_Item (T, X, T.Table (Y));", and Item is
      --  passed by reference. Without the copy, we would deallocate the array
      --  containing Item, leaving a dangling pointer.
      if Index > Last_Allocated (T) then
         declare
            Item_Copy : constant Table_Component_Type := Item;
         begin
            Set_Last (T, Index);
            T.Table (Index) := Item_Copy;
         end;
      else
         if Index > Last (T) then
            Set_Last (T, Index);
         end if;
         T.Table (Index) := Item;
      end if;
   end Set_Item;
   --------------
   -- Set_Last --
   --------------
   procedure Set_Last (T : in out Instance; New_Val : Table_Last_Type) is
   begin
      pragma Assert (not T.Locked);
      if New_Val > Last_Allocated (T) then
         Grow (T, New_Val);
      end if;
      T.P.Last := New_Val;
   end Set_Last;
   ----------------
   -- Sort_Table --
   ----------------
   procedure Sort_Table (Table : in out Instance) is
      Temp : Table_Component_Type;
      --  A temporary position to simulate index 0
      --  Local subprograms
      function Index_Of (Idx : Natural) return Table_Index_Type'Base;
      --  Return index of Idx'th element of table
      function Lower_Than (Op1, Op2 : Natural) return Boolean;
      --  Compare two components
      procedure Move (From : Natural; To : Natural);
      --  Move one component
      package Heap_Sort is new GNAT.Heap_Sort_G (Move, Lower_Than);
      --------------
      -- Index_Of --
      --------------
      function Index_Of (Idx : Natural) return Table_Index_Type'Base is
         J : constant Integer'Base :=
               Table_Index_Type'Base'Pos (First) + Idx - 1;
      begin
         return Table_Index_Type'Base'Val (J);
      end Index_Of;
      ----------
      -- Move --
      ----------
      procedure Move (From : Natural; To : Natural) is
      begin
         if From = 0 then
            Table.Table (Index_Of (To)) := Temp;
         elsif To = 0 then
            Temp := Table.Table (Index_Of (From));
         else
            Table.Table (Index_Of (To)) :=
              Table.Table (Index_Of (From));
         end if;
      end Move;
      ----------------
      -- Lower_Than --
      ----------------
      function Lower_Than (Op1, Op2 : Natural) return Boolean is
      begin
         if Op1 = 0 then
            return Lt (Temp, Table.Table (Index_Of (Op2)));
         elsif Op2 = 0 then
            return Lt (Table.Table (Index_Of (Op1)), Temp);
         else
            return
              Lt (Table.Table (Index_Of (Op1)), Table.Table (Index_Of (Op2)));
         end if;
      end Lower_Than;
   --  Start of processing for Sort_Table
   begin
      Heap_Sort.Sort (Natural (Last (Table) - First) + 1);
   end Sort_Table;
end GNAT.Dynamic_Tables;
 
     |