1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Debug; use Debug;
36 with Output; use Output;
37 with System; use System;
38 with Tree_IO; use Tree_IO;
40 with System.Memory; use System.Memory;
42 with Unchecked_Conversion;
44 pragma Elaborate_All (Output);
49 Min : constant Int := Int (Table_Low_Bound);
50 -- Subscript of the minimum entry in the currently allocated table
53 -- Number of entries in currently allocated table. The value of zero
54 -- ensures that we initially allocate the table.
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
61 -- Reallocate the existing table according to the current value stored
62 -- in Max. Works correctly to do an initial allocation if the table
65 function Tree_Get_Table_Address return Address;
66 -- Return Null_Address if the table length is zero,
67 -- Table (First)'Address if not.
69 function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
70 function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
76 procedure Append (New_Val : Table_Component_Type) is
79 Table (Table_Index_Type (Last_Val)) := New_Val;
86 procedure Decrement_Last is
88 Last_Val := Last_Val - 1;
97 Free (To_Address (Table));
106 procedure Increment_Last is
108 Last_Val := Last_Val + 1;
110 if Last_Val > Max then
120 Old_Length : constant Int := Length;
125 Max := Min + (Table_Initial * Table_Factor) - 1;
126 Length := Max - Min + 1;
128 -- If table is same size as before (happens when table is never
129 -- expanded which is a common case), then simply reuse it. Note
130 -- that this also means that an explicit Init call right after
131 -- the implicit one in the package body is harmless.
133 if Old_Length = Length then
136 -- Otherwise we can use Reallocate to get a table of the right size.
137 -- Note that Reallocate works fine to allocate a table of the right
138 -- initial size when it is first allocated.
149 function Last return Table_Index_Type is
151 return Table_Index_Type (Last_Val);
158 procedure Reallocate is
159 New_Size : Memory.size_t;
162 if Max < Last_Val then
163 pragma Assert (not Locked);
165 -- Make sure that we have at least the initial allocation. This
166 -- is needed in cases where a zero length table is written out.
168 Length := Int'Max (Length, Table_Initial);
170 -- Now increment table length until it is sufficiently large
172 while Max < Last_Val loop
173 Length := Length * (100 + Table_Increment) / 100;
174 Max := Min + Length - 1;
178 Write_Str ("--> Allocating new ");
179 Write_Str (Table_Name);
180 Write_Str (" table, size = ");
181 Write_Int (Max - Min + 1);
187 Memory.size_t ((Max - Min + 1) *
188 (Table_Type'Component_Size / Storage_Unit));
191 Table := To_Pointer (Alloc (New_Size));
193 elsif New_Size > 0 then
195 To_Pointer (Realloc (Ptr => To_Address (Table),
199 if Length /= 0 and then Table = null then
201 Write_Str ("available memory exhausted");
204 raise Unrecoverable_Error;
215 Length := Last_Val - Int (Table_Low_Bound) + 1;
224 procedure Restore (T : Saved_Table) is
226 Free (To_Address (Table));
227 Last_Val := T.Last_Val;
230 Length := Max - Min + 1;
237 function Save return Saved_Table is
241 Res.Last_Val := Last_Val;
256 (Index : Table_Index_Type;
257 Item : Table_Component_Type)
260 if Int (Index) > Max then
264 Table (Index) := Item;
271 procedure Set_Last (New_Val : Table_Index_Type) is
273 if Int (New_Val) < Last_Val then
274 Last_Val := Int (New_Val);
276 Last_Val := Int (New_Val);
278 if Last_Val > Max then
284 ----------------------------
285 -- Tree_Get_Table_Address --
286 ----------------------------
288 function Tree_Get_Table_Address return Address is
293 return Table (First)'Address;
295 end Tree_Get_Table_Address;
301 -- Note: we allocate only the space required to accommodate the data
302 -- actually written, which means that a Tree_Write/Tree_Read sequence
303 -- does an implicit Release.
305 procedure Tree_Read is
309 Length := Max - Min + 1;
313 (Tree_Get_Table_Address,
314 (Last_Val - Int (First) + 1) *
315 Table_Type'Component_Size / Storage_Unit);
322 -- Note: we write out only the currently valid data, not the entire
323 -- contents of the allocated array. See note above on Tree_Read.
325 procedure Tree_Write is
327 Tree_Write_Int (Int (Last));
329 (Tree_Get_Table_Address,
330 (Last_Val - Int (First) + 1) *
331 Table_Type'Component_Size / Storage_Unit);