2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / table.adb
blob30c068f6eaec58280429eb13e6ebaf11e4456ebc
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- T A B L E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002 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; 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 function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
70 function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
72 ------------
73 -- Append --
74 ------------
76 procedure Append (New_Val : Table_Component_Type) is
77 begin
78 Increment_Last;
79 Table (Table_Index_Type (Last_Val)) := New_Val;
80 end Append;
82 --------------------
83 -- Decrement_Last --
84 --------------------
86 procedure Decrement_Last is
87 begin
88 Last_Val := Last_Val - 1;
89 end Decrement_Last;
91 ----------
92 -- Free --
93 ----------
95 procedure Free is
96 begin
97 Free (To_Address (Table));
98 Table := null;
99 Length := 0;
100 end Free;
102 --------------------
103 -- Increment_Last --
104 --------------------
106 procedure Increment_Last is
107 begin
108 Last_Val := Last_Val + 1;
110 if Last_Val > Max then
111 Reallocate;
112 end if;
113 end Increment_Last;
115 ----------
116 -- Init --
117 ----------
119 procedure Init is
120 Old_Length : constant Int := Length;
122 begin
123 Locked := False;
124 Last_Val := Min - 1;
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
134 return;
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.
140 else
141 Reallocate;
142 end if;
143 end Init;
145 ----------
146 -- Last --
147 ----------
149 function Last return Table_Index_Type is
150 begin
151 return Table_Index_Type (Last_Val);
152 end Last;
154 ----------------
155 -- Reallocate --
156 ----------------
158 procedure Reallocate is
159 New_Size : Memory.size_t;
161 begin
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;
175 end loop;
177 if Debug_Flag_D then
178 Write_Str ("--> Allocating new ");
179 Write_Str (Table_Name);
180 Write_Str (" table, size = ");
181 Write_Int (Max - Min + 1);
182 Write_Eol;
183 end if;
184 end if;
186 New_Size :=
187 Memory.size_t ((Max - Min + 1) *
188 (Table_Type'Component_Size / Storage_Unit));
190 if Table = null then
191 Table := To_Pointer (Alloc (New_Size));
193 elsif New_Size > 0 then
194 Table :=
195 To_Pointer (Realloc (Ptr => To_Address (Table),
196 Size => New_Size));
197 end if;
199 if Length /= 0 and then Table = null then
200 Set_Standard_Error;
201 Write_Str ("available memory exhausted");
202 Write_Eol;
203 Set_Standard_Output;
204 raise Unrecoverable_Error;
205 end if;
207 end Reallocate;
209 -------------
210 -- Release --
211 -------------
213 procedure Release is
214 begin
215 Length := Last_Val - Int (Table_Low_Bound) + 1;
216 Max := Last_Val;
217 Reallocate;
218 end Release;
220 -------------
221 -- Restore --
222 -------------
224 procedure Restore (T : Saved_Table) is
225 begin
226 Free (To_Address (Table));
227 Last_Val := T.Last_Val;
228 Max := T.Max;
229 Table := T.Table;
230 Length := Max - Min + 1;
231 end Restore;
233 ----------
234 -- Save --
235 ----------
237 function Save return Saved_Table is
238 Res : Saved_Table;
240 begin
241 Res.Last_Val := Last_Val;
242 Res.Max := Max;
243 Res.Table := Table;
245 Table := null;
246 Length := 0;
247 Init;
248 return Res;
249 end Save;
251 --------------
252 -- Set_Item --
253 --------------
255 procedure Set_Item
256 (Index : Table_Index_Type;
257 Item : Table_Component_Type)
259 begin
260 if Int (Index) > Max then
261 Set_Last (Index);
262 end if;
264 Table (Index) := Item;
265 end Set_Item;
267 --------------
268 -- Set_Last --
269 --------------
271 procedure Set_Last (New_Val : Table_Index_Type) is
272 begin
273 if Int (New_Val) < Last_Val then
274 Last_Val := Int (New_Val);
275 else
276 Last_Val := Int (New_Val);
278 if Last_Val > Max then
279 Reallocate;
280 end if;
281 end if;
282 end Set_Last;
284 ----------------------------
285 -- Tree_Get_Table_Address --
286 ----------------------------
288 function Tree_Get_Table_Address return Address is
289 begin
290 if Length = 0 then
291 return Null_Address;
292 else
293 return Table (First)'Address;
294 end if;
295 end Tree_Get_Table_Address;
297 ---------------
298 -- Tree_Read --
299 ---------------
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
306 begin
307 Tree_Read_Int (Max);
308 Last_Val := Max;
309 Length := Max - Min + 1;
310 Reallocate;
312 Tree_Read_Data
313 (Tree_Get_Table_Address,
314 (Last_Val - Int (First) + 1) *
315 Table_Type'Component_Size / Storage_Unit);
316 end Tree_Read;
318 ----------------
319 -- Tree_Write --
320 ----------------
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
326 begin
327 Tree_Write_Int (Int (Last));
328 Tree_Write_Data
329 (Tree_Get_Table_Address,
330 (Last_Val - Int (First) + 1) *
331 Table_Type'Component_Size / Storage_Unit);
332 end Tree_Write;
334 begin
335 Init;
336 end Table;
337 end Table;