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------------------------------------------------------------------------------
33
34with Debug;   use Debug;
35with Opt;     use Opt;
36with Output;  use Output;
37with System;  use System;
38with Tree_IO; use Tree_IO;
39
40with System.Memory; use System.Memory;
41
42with Unchecked_Conversion;
43
44pragma Elaborate_All (Output);
45
46package body Table is
47   package body Table is
48
49      Min : constant Int := Int (Table_Low_Bound);
50      --  Subscript of the minimum entry in the currently allocated table
51
52      Length : Int := 0;
53      --  Number of entries in currently allocated table. The value of zero
54      --  ensures that we initially allocate the table.
55
56      -----------------------
57      -- Local Subprograms --
58      -----------------------
59
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.
64
65      function Tree_Get_Table_Address return Address;
66      --  Return Null_Address if the table length is zero,
67      --  Table (First)'Address if not.
68
69      function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
70      function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
71
72      ------------
73      -- Append --
74      ------------
75
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;
81
82      --------------------
83      -- Decrement_Last --
84      --------------------
85
86      procedure Decrement_Last is
87      begin
88         Last_Val := Last_Val - 1;
89      end Decrement_Last;
90
91      ----------
92      -- Free --
93      ----------
94
95      procedure Free is
96      begin
97         Free (To_Address (Table));
98         Table := null;
99         Length := 0;
100      end Free;
101
102      --------------------
103      -- Increment_Last --
104      --------------------
105
106      procedure Increment_Last is
107      begin
108         Last_Val := Last_Val + 1;
109
110         if Last_Val > Max then
111            Reallocate;
112         end if;
113      end Increment_Last;
114
115      ----------
116      -- Init --
117      ----------
118
119      procedure Init is
120         Old_Length : constant Int := Length;
121
122      begin
123         Locked   := False;
124         Last_Val := Min - 1;
125         Max      := Min + (Table_Initial * Table_Factor) - 1;
126         Length   := Max - Min + 1;
127
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.
132
133         if Old_Length = Length then
134            return;
135
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.
139
140         else
141            Reallocate;
142         end if;
143      end Init;
144
145      ----------
146      -- Last --
147      ----------
148
149      function Last return Table_Index_Type is
150      begin
151         return Table_Index_Type (Last_Val);
152      end Last;
153
154      ----------------
155      -- Reallocate --
156      ----------------
157
158      procedure Reallocate is
159         New_Size : Memory.size_t;
160
161      begin
162         if Max < Last_Val then
163            pragma Assert (not Locked);
164
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.
167
168            Length := Int'Max (Length, Table_Initial);
169
170            --  Now increment table length until it is sufficiently large
171
172            while Max < Last_Val loop
173               Length := Length * (100 + Table_Increment) / 100;
174               Max := Min + Length - 1;
175            end loop;
176
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;
185
186         New_Size :=
187           Memory.size_t ((Max - Min + 1) *
188                          (Table_Type'Component_Size / Storage_Unit));
189
190         if Table = null then
191            Table := To_Pointer (Alloc (New_Size));
192
193         elsif New_Size > 0 then
194            Table :=
195              To_Pointer (Realloc (Ptr  => To_Address (Table),
196                                   Size => New_Size));
197         end if;
198
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;
206
207      end Reallocate;
208
209      -------------
210      -- Release --
211      -------------
212
213      procedure Release is
214      begin
215         Length := Last_Val - Int (Table_Low_Bound) + 1;
216         Max    := Last_Val;
217         Reallocate;
218      end Release;
219
220      -------------
221      -- Restore --
222      -------------
223
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;
232
233      ----------
234      -- Save --
235      ----------
236
237      function Save return Saved_Table is
238         Res : Saved_Table;
239
240      begin
241         Res.Last_Val := Last_Val;
242         Res.Max      := Max;
243         Res.Table    := Table;
244
245         Table  := null;
246         Length := 0;
247         Init;
248         return Res;
249      end Save;
250
251      --------------
252      -- Set_Item --
253      --------------
254
255      procedure Set_Item
256         (Index : Table_Index_Type;
257          Item  : Table_Component_Type)
258      is
259      begin
260         if Int (Index) > Max then
261            Set_Last (Index);
262         end if;
263
264         Table (Index) := Item;
265      end Set_Item;
266
267      --------------
268      -- Set_Last --
269      --------------
270
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);
277
278            if Last_Val > Max then
279               Reallocate;
280            end if;
281         end if;
282      end Set_Last;
283
284      ----------------------------
285      -- Tree_Get_Table_Address --
286      ----------------------------
287
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;
296
297      ---------------
298      -- Tree_Read --
299      ---------------
300
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.
304
305      procedure Tree_Read is
306      begin
307         Tree_Read_Int (Max);
308         Last_Val := Max;
309         Length := Max - Min + 1;
310         Reallocate;
311
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;
317
318      ----------------
319      -- Tree_Write --
320      ----------------
321
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.
324
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;
333
334   begin
335      Init;
336   end Table;
337end Table;
338