1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . T A B L E --
9 -- Copyright (C) 1998-2014, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with GNAT.Heap_Sort_G;
34 with System; use System;
35 with System.Memory; use System.Memory;
37 with Ada.Unchecked_Conversion;
39 package body GNAT.Table is
41 Min : constant Integer := Integer (Table_Low_Bound);
42 -- Subscript of the minimum entry in the currently allocated table
45 -- Subscript of the maximum entry in the currently allocated table
47 Length : Integer := 0;
48 -- Number of entries in currently allocated table. The value of zero
49 -- ensures that we initially allocate the table.
52 -- Current value of Last
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
59 -- Reallocate the existing table according to the current value stored
60 -- in Max. Works correctly to do an initial allocation if the table
63 pragma Warnings (Off);
64 -- Turn off warnings. The following unchecked conversions are only used
65 -- internally in this package, and cannot never result in any instances
66 -- of improperly aliased pointers for the client of the package.
68 function To_Address is new Ada.Unchecked_Conversion (Table_Ptr, Address);
69 function To_Pointer is new Ada.Unchecked_Conversion (Address, Table_Ptr);
77 function Allocate (Num : Integer := 1) return Table_Index_Type is
78 Old_Last : constant Integer := Last_Val;
81 Last_Val := Last_Val + Num;
83 if Last_Val > Max then
87 return Table_Index_Type (Old_Last + 1);
94 procedure Append (New_Val : Table_Component_Type) is
96 Set_Item (Table_Index_Type (Last_Val + 1), New_Val);
103 procedure Append_All (New_Vals : Table_Type) is
105 for J in New_Vals'Range loop
106 Append (New_Vals (J));
114 procedure Decrement_Last is
116 Last_Val := Last_Val - 1;
123 procedure For_Each is
124 Quit : Boolean := False;
126 for Index in Table_Low_Bound .. Table_Index_Type (Last_Val) loop
127 Action (Index, Table (Index), Quit);
138 Free (To_Address (Table));
147 procedure Increment_Last is
149 Last_Val := Last_Val + 1;
151 if Last_Val > Max then
161 Old_Length : constant Integer := Length;
165 Max := Min + Table_Initial - 1;
166 Length := Max - Min + 1;
168 -- If table is same size as before (happens when table is never
169 -- expanded which is a common case), then simply reuse it. Note
170 -- that this also means that an explicit Init call right after
171 -- the implicit one in the package body is harmless.
173 if Old_Length = Length then
176 -- Otherwise we can use Reallocate to get a table of the right size.
177 -- Note that Reallocate works fine to allocate a table of the right
178 -- initial size when it is first allocated.
189 function Last return Table_Index_Type is
191 return Table_Index_Type (Last_Val);
198 procedure Reallocate is
200 New_Length : Long_Long_Integer;
203 if Max < Last_Val then
204 pragma Assert (not Locked);
206 -- Now increment table length until it is sufficiently large. Use
207 -- the increment value or 10, which ever is larger (the reason
208 -- for the use of 10 here is to ensure that the table does really
209 -- increase in size (which would not be the case for a table of
210 -- length 10 increased by 3% for instance). Do the intermediate
211 -- calculation in Long_Long_Integer to avoid overflow.
213 while Max < Last_Val loop
215 Long_Long_Integer (Length) *
216 (100 + Long_Long_Integer (Table_Increment)) / 100;
217 Length := Integer'Max (Integer (New_Length), Length + 10);
218 Max := Min + Length - 1;
223 size_t ((Max - Min + 1) *
224 (Table_Type'Component_Size / Storage_Unit));
227 Table := To_Pointer (Alloc (New_Size));
229 elsif New_Size > 0 then
231 To_Pointer (Realloc (Ptr => To_Address (Table),
235 if Length /= 0 and then Table = null then
247 Length := Last_Val - Integer (Table_Low_Bound) + 1;
257 (Index : Table_Index_Type;
258 Item : Table_Component_Type)
260 -- If Item is a value within the current allocation, and we are going to
261 -- reallocate, then we must preserve an intermediate copy here before
262 -- calling Increment_Last. Otherwise, if Table_Component_Type is passed
263 -- by reference, we are going to end up copying from storage that might
264 -- have been deallocated from Increment_Last calling Reallocate.
266 subtype Allocated_Table_T is
267 Table_Type (Table'First .. Table_Index_Type (Max + 1));
268 -- A constrained table subtype one element larger than the currently
271 Allocated_Table_Address : constant System.Address :=
273 -- Used for address clause below (we can't use non-static expression
274 -- Table.all'Address directly in the clause because some older versions
275 -- of the compiler do not allow it).
277 Allocated_Table : Allocated_Table_T;
278 pragma Import (Ada, Allocated_Table);
279 pragma Suppress (Range_Check, On => Allocated_Table);
280 for Allocated_Table'Address use Allocated_Table_Address;
281 -- Allocated_Table represents the currently allocated array, plus one
282 -- element (the supplementary element is used to have a convenient
283 -- way of computing the address just past the end of the current
284 -- allocation). Range checks are suppressed because this unit uses
285 -- direct calls to System.Memory for allocation, and this can yield
286 -- misaligned storage (and we cannot rely on the bootstrap compiler
287 -- supporting specifically disabling alignment checks, so we need to
288 -- suppress all range checks). It is safe to suppress this check here
289 -- because we know that a (possibly misaligned) object of that type
290 -- does actually exist at that address. ??? We should really improve
291 -- the allocation circuitry here to
292 -- guarantee proper alignment.
294 Need_Realloc : constant Boolean := Integer (Index) > Max;
295 -- True if this operation requires storage reallocation (which may
296 -- involve moving table contents around).
299 -- If we're going to reallocate, check whether Item references an
300 -- element of the currently allocated table.
303 and then Allocated_Table'Address <= Item'Address
304 and then Item'Address <
305 Allocated_Table (Table_Index_Type (Max + 1))'Address
307 -- If so, save a copy on the stack because Increment_Last will
308 -- reallocate storage and might deallocate the current table.
311 Item_Copy : constant Table_Component_Type := Item;
314 Table (Index) := Item_Copy;
318 -- Here we know that either we won't reallocate (case of Index < Max)
319 -- or that Item is not in the currently allocated table.
321 if Integer (Index) > Last_Val then
325 Table (Index) := Item;
333 procedure Set_Last (New_Val : Table_Index_Type) is
335 if Integer (New_Val) < Last_Val then
336 Last_Val := Integer (New_Val);
338 Last_Val := Integer (New_Val);
340 if Last_Val > Max then
350 procedure Sort_Table is
352 Temp : Table_Component_Type;
353 -- A temporary position to simulate index 0
357 function Index_Of (Idx : Natural) return Table_Index_Type;
358 -- Return index of Idx'th element of table
360 function Lower_Than (Op1, Op2 : Natural) return Boolean;
361 -- Compare two components
363 procedure Move (From : Natural; To : Natural);
364 -- Move one component
366 package Heap_Sort is new GNAT.Heap_Sort_G (Move, Lower_Than);
372 function Index_Of (Idx : Natural) return Table_Index_Type is
373 J : constant Integer'Base := Table_Index_Type'Pos (First) + Idx - 1;
375 return Table_Index_Type'Val (J);
382 procedure Move (From : Natural; To : Natural) is
385 Table (Index_Of (To)) := Temp;
387 Temp := Table (Index_Of (From));
389 Table (Index_Of (To)) := Table (Index_Of (From));
397 function Lower_Than (Op1, Op2 : Natural) return Boolean is
400 return Lt (Temp, Table (Index_Of (Op2)));
402 return Lt (Table (Index_Of (Op1)), Temp);
404 return Lt (Table (Index_Of (Op1)), Table (Index_Of (Op2)));
408 -- Start of processing for Sort_Table
411 Heap_Sort.Sort (Natural (Last - First) + 1);