FSF GCC merge 02/23/03
[official-gcc.git] / gcc / ada / table.adb
blobe9acfc278ef943e6b82403ba12fb2744db5fe7c7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- T A B L E --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 with Debug; use Debug;
36 with Opt;
37 with Output; use Output;
38 pragma Elaborate_All (Output);
39 with System; use System;
40 with Tree_IO; use Tree_IO;
41 with System.Memory; use System.Memory;
42 with System.Address_To_Access_Conversions;
44 package body Table is
45 package body Table is
47 Min : constant Int := Int (Table_Low_Bound);
48 -- Subscript of the minimum entry in the currently allocated table
50 Length : Int := 0;
51 -- Number of entries in currently allocated table. The value of zero
52 -- ensures that we initially allocate the table.
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Reallocate;
59 -- Reallocate the existing table according to the current value stored
60 -- in Max. Works correctly to do an initial allocation if the table
61 -- is currently null.
63 function Tree_Get_Table_Address return Address;
64 -- Return Null_Address if the table length is zero,
65 -- Table (First)'Address if not.
67 package Table_Conversions is
68 new System.Address_To_Access_Conversions (Big_Table_Type);
69 -- Address and Access conversions for a Table object.
71 function To_Address (Table : Table_Ptr) return Address;
72 pragma Inline (To_Address);
73 -- Returns the Address for the Table object.
75 function To_Pointer (Table : Address) return Table_Ptr;
76 pragma Inline (To_Pointer);
77 -- Returns the Access pointer for the Table object.
79 ------------
80 -- Append --
81 ------------
83 procedure Append (New_Val : Table_Component_Type) is
84 begin
85 Increment_Last;
86 Table (Table_Index_Type (Last_Val)) := New_Val;
87 end Append;
89 --------------------
90 -- Decrement_Last --
91 --------------------
93 procedure Decrement_Last is
94 begin
95 Last_Val := Last_Val - 1;
96 end Decrement_Last;
98 ----------
99 -- Free --
100 ----------
102 procedure Free is
103 begin
104 Free (To_Address (Table));
105 Table := null;
106 Length := 0;
107 end Free;
109 --------------------
110 -- Increment_Last --
111 --------------------
113 procedure Increment_Last is
114 begin
115 Last_Val := Last_Val + 1;
117 if Last_Val > Max then
118 Reallocate;
119 end if;
120 end Increment_Last;
122 ----------
123 -- Init --
124 ----------
126 procedure Init is
127 Old_Length : Int := Length;
129 begin
130 Last_Val := Min - 1;
131 Max := Min + (Table_Initial * Opt.Table_Factor) - 1;
132 Length := Max - Min + 1;
134 -- If table is same size as before (happens when table is never
135 -- expanded which is a common case), then simply reuse it. Note
136 -- that this also means that an explicit Init call right after
137 -- the implicit one in the package body is harmless.
139 if Old_Length = Length then
140 return;
142 -- Otherwise we can use Reallocate to get a table of the right size.
143 -- Note that Reallocate works fine to allocate a table of the right
144 -- initial size when it is first allocated.
146 else
147 Reallocate;
148 end if;
149 end Init;
151 ----------
152 -- Last --
153 ----------
155 function Last return Table_Index_Type is
156 begin
157 return Table_Index_Type (Last_Val);
158 end Last;
160 ----------------
161 -- Reallocate --
162 ----------------
164 procedure Reallocate is
165 New_Size : Memory.size_t;
167 begin
168 if Max < Last_Val then
169 pragma Assert (not Locked);
171 -- Make sure that we have at least the initial allocation. This
172 -- is needed in cases where a zero length table is written out.
174 Length := Int'Max (Length, Table_Initial);
176 -- Now increment table length until it is sufficiently large
178 while Max < Last_Val loop
179 Length := Length * (100 + Table_Increment) / 100;
180 Max := Min + Length - 1;
181 end loop;
183 if Debug_Flag_D then
184 Write_Str ("--> Allocating new ");
185 Write_Str (Table_Name);
186 Write_Str (" table, size = ");
187 Write_Int (Max - Min + 1);
188 Write_Eol;
189 end if;
190 end if;
192 New_Size :=
193 Memory.size_t ((Max - Min + 1) *
194 (Table_Type'Component_Size / Storage_Unit));
196 if Table = null then
197 Table := To_Pointer (Alloc (New_Size));
199 elsif New_Size > 0 then
200 Table :=
201 To_Pointer (Realloc (Ptr => To_Address (Table),
202 Size => New_Size));
203 end if;
205 if Length /= 0 and then Table = null then
206 Set_Standard_Error;
207 Write_Str ("available memory exhausted");
208 Write_Eol;
209 Set_Standard_Output;
210 raise Unrecoverable_Error;
211 end if;
213 end Reallocate;
215 -------------
216 -- Release --
217 -------------
219 procedure Release is
220 begin
221 Length := Last_Val - Int (Table_Low_Bound) + 1;
222 Max := Last_Val;
223 Reallocate;
224 end Release;
226 -------------
227 -- Restore --
228 -------------
230 procedure Restore (T : Saved_Table) is
231 begin
232 Free (To_Address (Table));
233 Last_Val := T.Last_Val;
234 Max := T.Max;
235 Table := T.Table;
236 Length := Max - Min + 1;
237 end Restore;
239 ----------
240 -- Save --
241 ----------
243 function Save return Saved_Table is
244 Res : Saved_Table;
246 begin
247 Res.Last_Val := Last_Val;
248 Res.Max := Max;
249 Res.Table := Table;
251 Table := null;
252 Length := 0;
253 Init;
254 return Res;
255 end Save;
257 --------------
258 -- Set_Item --
259 --------------
261 procedure Set_Item
262 (Index : Table_Index_Type;
263 Item : Table_Component_Type)
265 begin
266 if Int (Index) > Max then
267 Set_Last (Index);
268 end if;
270 Table (Index) := Item;
271 end Set_Item;
273 --------------
274 -- Set_Last --
275 --------------
277 procedure Set_Last (New_Val : Table_Index_Type) is
278 begin
279 if Int (New_Val) < Last_Val then
280 Last_Val := Int (New_Val);
281 else
282 Last_Val := Int (New_Val);
284 if Last_Val > Max then
285 Reallocate;
286 end if;
287 end if;
288 end Set_Last;
290 ----------------
291 -- To_Address --
292 ----------------
294 function To_Address (Table : Table_Ptr) return Address is
295 begin
296 return Table_Conversions.To_Address
297 (Table_Conversions.Object_Pointer (Table));
298 end To_Address;
300 ----------------
301 -- To_Pointer --
302 ----------------
304 function To_Pointer (Table : Address) return Table_Ptr is
305 begin
306 return Table_Ptr (Table_Conversions.To_Pointer (Table));
307 end To_Pointer;
309 ----------------------------
310 -- Tree_Get_Table_Address --
311 ----------------------------
313 function Tree_Get_Table_Address return Address is
314 begin
315 if Length = 0 then
316 return Null_Address;
317 else
318 return Table (First)'Address;
319 end if;
320 end Tree_Get_Table_Address;
322 ---------------
323 -- Tree_Read --
324 ---------------
326 -- Note: we allocate only the space required to accommodate the data
327 -- actually written, which means that a Tree_Write/Tree_Read sequence
328 -- does an implicit Release.
330 procedure Tree_Read is
331 begin
332 Tree_Read_Int (Max);
333 Last_Val := Max;
334 Length := Max - Min + 1;
335 Reallocate;
337 Tree_Read_Data
338 (Tree_Get_Table_Address,
339 (Last_Val - Int (First) + 1) *
340 Table_Type'Component_Size / Storage_Unit);
341 end Tree_Read;
343 ----------------
344 -- Tree_Write --
345 ----------------
347 -- Note: we write out only the currently valid data, not the entire
348 -- contents of the allocated array. See note above on Tree_Read.
350 procedure Tree_Write is
351 begin
352 Tree_Write_Int (Int (Last));
353 Tree_Write_Data
354 (Tree_Get_Table_Address,
355 (Last_Val - Int (First) + 1) *
356 Table_Type'Component_Size / Storage_Unit);
357 end Tree_Write;
359 begin
360 Init;
361 end Table;
362 end Table;