1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . D Y N A M I C _ T A B L E S --
9 -- Copyright (C) 2000-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with GNAT.Heap_Sort_G;
35 with System; use System;
36 with System.Memory; use System.Memory;
38 with Ada.Unchecked_Conversion;
40 package body GNAT.Dynamic_Tables is
42 Min : constant Integer := Integer (Table_Low_Bound);
43 -- Subscript of the minimum entry in the currently allocated table
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Reallocate (T : in out Instance);
50 -- Reallocate the existing table according to the current value stored
51 -- in Max. Works correctly to do an initial allocation if the table
54 pragma Warnings (Off);
55 -- These unchecked conversions are in fact safe, since they never
56 -- generate improperly aliased pointer values.
58 function To_Address is new Ada.Unchecked_Conversion (Table_Ptr, Address);
59 function To_Pointer is new Ada.Unchecked_Conversion (Address, Table_Ptr);
72 T.P.Last_Val := T.P.Last_Val + Num;
74 if T.P.Last_Val > T.P.Max then
83 procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
85 Set_Item (T, Table_Index_Type (T.P.Last_Val + 1), New_Val);
92 procedure Decrement_Last (T : in out Instance) is
94 T.P.Last_Val := T.P.Last_Val - 1;
101 procedure For_Each (Table : Instance) is
102 Quit : Boolean := False;
104 for Index in Table_Low_Bound .. Table_Index_Type (Table.P.Last_Val) loop
105 Action (Index, Table.Table (Index), Quit);
114 procedure Free (T : in out Instance) is
116 Free (To_Address (T.Table));
125 procedure Increment_Last (T : in out Instance) is
127 T.P.Last_Val := T.P.Last_Val + 1;
129 if T.P.Last_Val > T.P.Max then
138 procedure Init (T : in out Instance) is
139 Old_Length : constant Integer := T.P.Length;
142 T.P.Last_Val := Min - 1;
143 T.P.Max := Min + Table_Initial - 1;
144 T.P.Length := T.P.Max - Min + 1;
146 -- If table is same size as before (happens when table is never
147 -- expanded which is a common case), then simply reuse it. Note
148 -- that this also means that an explicit Init call right after
149 -- the implicit one in the package body is harmless.
151 if Old_Length = T.P.Length then
154 -- Otherwise we can use Reallocate to get a table of the right size.
155 -- Note that Reallocate works fine to allocate a table of the right
156 -- initial size when it is first allocated.
167 function Last (T : Instance) return Table_Index_Type is
169 return Table_Index_Type (T.P.Last_Val);
176 procedure Reallocate (T : in out Instance) is
177 New_Length : Integer;
181 if T.P.Max < T.P.Last_Val then
182 while T.P.Max < T.P.Last_Val loop
183 New_Length := T.P.Length * (100 + Table_Increment) / 100;
185 if New_Length > T.P.Length then
186 T.P.Length := New_Length;
188 T.P.Length := T.P.Length + 1;
191 T.P.Max := Min + T.P.Length - 1;
196 size_t ((T.P.Max - Min + 1) *
197 (Table_Type'Component_Size / Storage_Unit));
199 if T.Table = null then
200 T.Table := To_Pointer (Alloc (New_Size));
202 elsif New_Size > 0 then
204 To_Pointer (Realloc (Ptr => To_Address (T.Table),
208 if T.P.Length /= 0 and then T.Table = null then
217 procedure Release (T : in out Instance) is
219 T.P.Length := T.P.Last_Val - Integer (Table_Low_Bound) + 1;
220 T.P.Max := T.P.Last_Val;
229 (T : in out Instance;
230 Index : Table_Index_Type;
231 Item : Table_Component_Type)
233 -- If Item is a value within the current allocation, and we are going to
234 -- reallocate, then we must preserve an intermediate copy here before
235 -- calling Increment_Last. Otherwise, if Table_Component_Type is passed
236 -- by reference, we are going to end up copying from storage that might
237 -- have been deallocated from Increment_Last calling Reallocate.
239 subtype Allocated_Table_T is
240 Table_Type (T.Table'First .. Table_Index_Type (T.P.Max + 1));
241 -- A constrained table subtype one element larger than the currently
244 Allocated_Table_Address : constant System.Address :=
246 -- Used for address clause below (we can't use non-static expression
247 -- Table.all'Address directly in the clause because some older versions
248 -- of the compiler do not allow it).
250 Allocated_Table : Allocated_Table_T;
251 pragma Import (Ada, Allocated_Table);
252 pragma Suppress (Range_Check, On => Allocated_Table);
253 for Allocated_Table'Address use Allocated_Table_Address;
254 -- Allocated_Table represents the currently allocated array, plus one
255 -- element (the supplementary element is used to have a convenient way
256 -- to the address just past the end of the current allocation). Range
257 -- checks are suppressed because this unit uses direct calls to
258 -- System.Memory for allocation, and this can yield misaligned storage
259 -- (and we cannot rely on the bootstrap compiler supporting specifically
260 -- disabling alignment cheks, so we need to suppress all range checks).
261 -- It is safe to suppress this check here because we know that a
262 -- (possibly misaligned) object of that type does actually exist at that
264 -- ??? We should really improve the allocation circuitry here to
265 -- guarantee proper alignment.
267 Need_Realloc : constant Boolean := Integer (Index) > T.P.Max;
268 -- True if this operation requires storage reallocation (which may
269 -- involve moving table contents around).
272 -- If we're going to reallocate, check wheter Item references an
273 -- element of the currently allocated table.
276 and then Allocated_Table'Address <= Item'Address
277 and then Item'Address <
278 Allocated_Table (Table_Index_Type (T.P.Max + 1))'Address
280 -- If so, save a copy on the stack because Increment_Last will
281 -- reallocate storage and might deallocate the current table.
284 Item_Copy : constant Table_Component_Type := Item;
287 T.Table (Index) := Item_Copy;
291 -- Here we know that either we won't reallocate (case of Index < Max)
292 -- or that Item is not in the currently allocated table.
294 if Integer (Index) > T.P.Last_Val then
298 T.Table (Index) := Item;
306 procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type) is
308 if Integer (New_Val) < T.P.Last_Val then
309 T.P.Last_Val := Integer (New_Val);
312 T.P.Last_Val := Integer (New_Val);
314 if T.P.Last_Val > T.P.Max then
324 procedure Sort_Table (Table : in out Instance) is
326 Temp : Table_Component_Type;
327 -- A temporary position to simulate index 0
331 function Index_Of (Idx : Natural) return Table_Index_Type;
332 -- Return index of Idx'th element of table
334 function Lower_Than (Op1, Op2 : Natural) return Boolean;
335 -- Compare two components
337 procedure Move (From : Natural; To : Natural);
338 -- Move one component
340 package Heap_Sort is new GNAT.Heap_Sort_G (Move, Lower_Than);
346 function Index_Of (Idx : Natural) return Table_Index_Type is
347 J : constant Integer'Base :=
348 Table_Index_Type'Pos (First) + Idx - 1;
350 return Table_Index_Type'Val (J);
357 procedure Move (From : Natural; To : Natural) is
360 Table.Table (Index_Of (To)) := Temp;
363 Temp := Table.Table (Index_Of (From));
366 Table.Table (Index_Of (To)) :=
367 Table.Table (Index_Of (From));
375 function Lower_Than (Op1, Op2 : Natural) return Boolean is
378 return Lt (Temp, Table.Table (Index_Of (Op2)));
381 return Lt (Table.Table (Index_Of (Op1)), Temp);
385 Lt (Table.Table (Index_Of (Op1)),
386 Table.Table (Index_Of (Op2)));
390 -- Start of processing for Sort_Table
393 Heap_Sort.Sort (Natural (Last (Table) - First) + 1);
396 end GNAT.Dynamic_Tables;