PR c++/29733
[official-gcc.git] / gcc / ada / table.adb
blobb99e6254e9d66b427227e3978b2b950073f9f447
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- T A B L E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004 Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Debug; use Debug;
35 with Opt; use Opt;
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);
46 package body Table is
47 package body Table is
49 Min : constant Int := Int (Table_Low_Bound);
50 -- Subscript of the minimum entry in the currently allocated table
52 Length : Int := 0;
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 -----------------------
60 procedure Reallocate;
61 -- Reallocate the existing table according to the current value stored
62 -- in Max. Works correctly to do an initial allocation if the table
63 -- is currently null.
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 pragma Warnings (Off);
70 -- Turn off warnings. The following unchecked conversions are only used
71 -- internally in this package, and cannot never result in any instances
72 -- of improperly aliased pointers for the client of the package.
74 function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
75 function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
77 pragma Warnings (On);
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 : constant Int := Length;
129 begin
130 Locked := False;
131 Last_Val := Min - 1;
132 Max := Min + (Table_Initial * Table_Factor) - 1;
133 Length := Max - Min + 1;
135 -- If table is same size as before (happens when table is never
136 -- expanded which is a common case), then simply reuse it. Note
137 -- that this also means that an explicit Init call right after
138 -- the implicit one in the package body is harmless.
140 if Old_Length = Length then
141 return;
143 -- Otherwise we can use Reallocate to get a table of the right size.
144 -- Note that Reallocate works fine to allocate a table of the right
145 -- initial size when it is first allocated.
147 else
148 Reallocate;
149 end if;
150 end Init;
152 ----------
153 -- Last --
154 ----------
156 function Last return Table_Index_Type is
157 begin
158 return Table_Index_Type (Last_Val);
159 end Last;
161 ----------------
162 -- Reallocate --
163 ----------------
165 procedure Reallocate is
166 New_Size : Memory.size_t;
168 begin
169 if Max < Last_Val then
170 pragma Assert (not Locked);
172 -- Make sure that we have at least the initial allocation. This
173 -- is needed in cases where a zero length table is written out.
175 Length := Int'Max (Length, Table_Initial);
177 -- Now increment table length until it is sufficiently large
179 while Max < Last_Val loop
180 Length := Length * (100 + Table_Increment) / 100;
181 Max := Min + Length - 1;
182 end loop;
184 if Debug_Flag_D then
185 Write_Str ("--> Allocating new ");
186 Write_Str (Table_Name);
187 Write_Str (" table, size = ");
188 Write_Int (Max - Min + 1);
189 Write_Eol;
190 end if;
191 end if;
193 New_Size :=
194 Memory.size_t ((Max - Min + 1) *
195 (Table_Type'Component_Size / Storage_Unit));
197 if Table = null then
198 Table := To_Pointer (Alloc (New_Size));
200 elsif New_Size > 0 then
201 Table :=
202 To_Pointer (Realloc (Ptr => To_Address (Table),
203 Size => New_Size));
204 end if;
206 if Length /= 0 and then Table = null then
207 Set_Standard_Error;
208 Write_Str ("available memory exhausted");
209 Write_Eol;
210 Set_Standard_Output;
211 raise Unrecoverable_Error;
212 end if;
214 end Reallocate;
216 -------------
217 -- Release --
218 -------------
220 procedure Release is
221 begin
222 Length := Last_Val - Int (Table_Low_Bound) + 1;
223 Max := Last_Val;
224 Reallocate;
225 end Release;
227 -------------
228 -- Restore --
229 -------------
231 procedure Restore (T : Saved_Table) is
232 begin
233 Free (To_Address (Table));
234 Last_Val := T.Last_Val;
235 Max := T.Max;
236 Table := T.Table;
237 Length := Max - Min + 1;
238 end Restore;
240 ----------
241 -- Save --
242 ----------
244 function Save return Saved_Table is
245 Res : Saved_Table;
247 begin
248 Res.Last_Val := Last_Val;
249 Res.Max := Max;
250 Res.Table := Table;
252 Table := null;
253 Length := 0;
254 Init;
255 return Res;
256 end Save;
258 --------------
259 -- Set_Item --
260 --------------
262 procedure Set_Item
263 (Index : Table_Index_Type;
264 Item : Table_Component_Type)
266 begin
267 if Int (Index) > Max then
268 Set_Last (Index);
269 end if;
271 Table (Index) := Item;
272 end Set_Item;
274 --------------
275 -- Set_Last --
276 --------------
278 procedure Set_Last (New_Val : Table_Index_Type) is
279 begin
280 if Int (New_Val) < Last_Val then
281 Last_Val := Int (New_Val);
282 else
283 Last_Val := Int (New_Val);
285 if Last_Val > Max then
286 Reallocate;
287 end if;
288 end if;
289 end Set_Last;
291 ----------------------------
292 -- Tree_Get_Table_Address --
293 ----------------------------
295 function Tree_Get_Table_Address return Address is
296 begin
297 if Length = 0 then
298 return Null_Address;
299 else
300 return Table (First)'Address;
301 end if;
302 end Tree_Get_Table_Address;
304 ---------------
305 -- Tree_Read --
306 ---------------
308 -- Note: we allocate only the space required to accommodate the data
309 -- actually written, which means that a Tree_Write/Tree_Read sequence
310 -- does an implicit Release.
312 procedure Tree_Read is
313 begin
314 Tree_Read_Int (Max);
315 Last_Val := Max;
316 Length := Max - Min + 1;
317 Reallocate;
319 Tree_Read_Data
320 (Tree_Get_Table_Address,
321 (Last_Val - Int (First) + 1) *
322 Table_Type'Component_Size / Storage_Unit);
323 end Tree_Read;
325 ----------------
326 -- Tree_Write --
327 ----------------
329 -- Note: we write out only the currently valid data, not the entire
330 -- contents of the allocated array. See note above on Tree_Read.
332 procedure Tree_Write is
333 begin
334 Tree_Write_Int (Int (Last));
335 Tree_Write_Data
336 (Tree_Get_Table_Address,
337 (Last_Val - Int (First) + 1) *
338 Table_Type'Component_Size / Storage_Unit);
339 end Tree_Write;
341 begin
342 Init;
343 end Table;
344 end Table;