2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / table.adb
blob60f185b0d9642adcc142d099d92758d4adeb644b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- T A B L E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2001 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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;
36 with Output; use Output;
37 pragma Elaborate_All (Output);
38 with System; use System;
39 with Tree_IO; use Tree_IO;
40 with System.Memory; use System.Memory;
41 with System.Address_To_Access_Conversions;
43 package body Table is
44 package body Table is
46 Min : constant Int := Int (Table_Low_Bound);
47 -- Subscript of the minimum entry in the currently allocated table
49 Length : Int := 0;
50 -- Number of entries in currently allocated table. The value of zero
51 -- ensures that we initially allocate the table.
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Reallocate;
58 -- Reallocate the existing table according to the current value stored
59 -- in Max. Works correctly to do an initial allocation if the table
60 -- is currently null.
62 function Tree_Get_Table_Address return Address;
63 -- Return Null_Address if the table length is zero,
64 -- Table (First)'Address if not.
66 package Table_Conversions is
67 new System.Address_To_Access_Conversions (Big_Table_Type);
68 -- Address and Access conversions for a Table object.
70 function To_Address (Table : Table_Ptr) return Address;
71 pragma Inline (To_Address);
72 -- Returns the Address for the Table object.
74 function To_Pointer (Table : Address) return Table_Ptr;
75 pragma Inline (To_Pointer);
76 -- Returns the Access pointer for the Table object.
78 ------------
79 -- Append --
80 ------------
82 procedure Append (New_Val : Table_Component_Type) is
83 begin
84 Increment_Last;
85 Table (Table_Index_Type (Last_Val)) := New_Val;
86 end Append;
88 --------------------
89 -- Decrement_Last --
90 --------------------
92 procedure Decrement_Last is
93 begin
94 Last_Val := Last_Val - 1;
95 end Decrement_Last;
97 ----------
98 -- Free --
99 ----------
101 procedure Free is
102 begin
103 Free (To_Address (Table));
104 Table := null;
105 Length := 0;
106 end Free;
108 --------------------
109 -- Increment_Last --
110 --------------------
112 procedure Increment_Last is
113 begin
114 Last_Val := Last_Val + 1;
116 if Last_Val > Max then
117 Reallocate;
118 end if;
119 end Increment_Last;
121 ----------
122 -- Init --
123 ----------
125 procedure Init is
126 Old_Length : Int := Length;
128 begin
129 Last_Val := Min - 1;
130 Max := Min + (Table_Initial * Opt.Table_Factor) - 1;
131 Length := Max - Min + 1;
133 -- If table is same size as before (happens when table is never
134 -- expanded which is a common case), then simply reuse it. Note
135 -- that this also means that an explicit Init call right after
136 -- the implicit one in the package body is harmless.
138 if Old_Length = Length then
139 return;
141 -- Otherwise we can use Reallocate to get a table of the right size.
142 -- Note that Reallocate works fine to allocate a table of the right
143 -- initial size when it is first allocated.
145 else
146 Reallocate;
147 end if;
148 end Init;
150 ----------
151 -- Last --
152 ----------
154 function Last return Table_Index_Type is
155 begin
156 return Table_Index_Type (Last_Val);
157 end Last;
159 ----------------
160 -- Reallocate --
161 ----------------
163 procedure Reallocate is
164 New_Size : Memory.size_t;
166 begin
167 if Max < Last_Val then
168 pragma Assert (not Locked);
170 -- Make sure that we have at least the initial allocation. This
171 -- is needed in cases where a zero length table is written out.
173 Length := Int'Max (Length, Table_Initial);
175 -- Now increment table length until it is sufficiently large
177 while Max < Last_Val loop
178 Length := Length * (100 + Table_Increment) / 100;
179 Max := Min + Length - 1;
180 end loop;
182 if Debug_Flag_D then
183 Write_Str ("--> Allocating new ");
184 Write_Str (Table_Name);
185 Write_Str (" table, size = ");
186 Write_Int (Max - Min + 1);
187 Write_Eol;
188 end if;
189 end if;
191 New_Size :=
192 Memory.size_t ((Max - Min + 1) *
193 (Table_Type'Component_Size / Storage_Unit));
195 if Table = null then
196 Table := To_Pointer (Alloc (New_Size));
198 elsif New_Size > 0 then
199 Table :=
200 To_Pointer (Realloc (Ptr => To_Address (Table),
201 Size => New_Size));
202 end if;
204 if Length /= 0 and then Table = null then
205 Set_Standard_Error;
206 Write_Str ("available memory exhausted");
207 Write_Eol;
208 Set_Standard_Output;
209 raise Unrecoverable_Error;
210 end if;
212 end Reallocate;
214 -------------
215 -- Release --
216 -------------
218 procedure Release is
219 begin
220 Length := Last_Val - Int (Table_Low_Bound) + 1;
221 Max := Last_Val;
222 Reallocate;
223 end Release;
225 -------------
226 -- Restore --
227 -------------
229 procedure Restore (T : Saved_Table) is
230 begin
231 Free (To_Address (Table));
232 Last_Val := T.Last_Val;
233 Max := T.Max;
234 Table := T.Table;
235 Length := Max - Min + 1;
236 end Restore;
238 ----------
239 -- Save --
240 ----------
242 function Save return Saved_Table is
243 Res : Saved_Table;
245 begin
246 Res.Last_Val := Last_Val;
247 Res.Max := Max;
248 Res.Table := Table;
250 Table := null;
251 Length := 0;
252 Init;
253 return Res;
254 end Save;
256 --------------
257 -- Set_Item --
258 --------------
260 procedure Set_Item
261 (Index : Table_Index_Type;
262 Item : Table_Component_Type)
264 begin
265 if Int (Index) > Max then
266 Set_Last (Index);
267 end if;
269 Table (Index) := Item;
270 end Set_Item;
272 --------------
273 -- Set_Last --
274 --------------
276 procedure Set_Last (New_Val : Table_Index_Type) is
277 begin
278 if Int (New_Val) < Last_Val then
279 Last_Val := Int (New_Val);
280 else
281 Last_Val := Int (New_Val);
283 if Last_Val > Max then
284 Reallocate;
285 end if;
286 end if;
287 end Set_Last;
289 ----------------
290 -- To_Address --
291 ----------------
293 function To_Address (Table : Table_Ptr) return Address is
294 begin
295 return Table_Conversions.To_Address
296 (Table_Conversions.Object_Pointer (Table));
297 end To_Address;
299 ----------------
300 -- To_Pointer --
301 ----------------
303 function To_Pointer (Table : Address) return Table_Ptr is
304 begin
305 return Table_Ptr (Table_Conversions.To_Pointer (Table));
306 end To_Pointer;
308 ----------------------------
309 -- Tree_Get_Table_Address --
310 ----------------------------
312 function Tree_Get_Table_Address return Address is
313 begin
314 if Length = 0 then
315 return Null_Address;
316 else
317 return Table (First)'Address;
318 end if;
319 end Tree_Get_Table_Address;
321 ---------------
322 -- Tree_Read --
323 ---------------
325 -- Note: we allocate only the space required to accommodate the data
326 -- actually written, which means that a Tree_Write/Tree_Read sequence
327 -- does an implicit Release.
329 procedure Tree_Read is
330 begin
331 Tree_Read_Int (Max);
332 Last_Val := Max;
333 Length := Max - Min + 1;
334 Reallocate;
336 Tree_Read_Data
337 (Tree_Get_Table_Address,
338 (Last_Val - Int (First) + 1) *
339 Table_Type'Component_Size / Storage_Unit);
340 end Tree_Read;
342 ----------------
343 -- Tree_Write --
344 ----------------
346 -- Note: we write out only the currently valid data, not the entire
347 -- contents of the allocated array. See note above on Tree_Read.
349 procedure Tree_Write is
350 begin
351 Tree_Write_Int (Int (Last));
352 Tree_Write_Data
353 (Tree_Get_Table_Address,
354 (Last_Val - Int (First) + 1) *
355 Table_Type'Component_Size / Storage_Unit);
356 end Tree_Write;
358 begin
359 Init;
360 end Table;
361 end Table;