OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / table.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                T A B L E                                 --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, 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 ------------------------------------------------------------------------------
33
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;
39
40 with System.Memory; use System.Memory;
41
42 with Unchecked_Conversion;
43
44 pragma Elaborate_All (Output);
45
46 package 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       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.
73
74       function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
75       function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
76
77       pragma Warnings (On);
78
79       ------------
80       -- Append --
81       ------------
82
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;
88
89       --------------------
90       -- Decrement_Last --
91       --------------------
92
93       procedure Decrement_Last is
94       begin
95          Last_Val := Last_Val - 1;
96       end Decrement_Last;
97
98       ----------
99       -- Free --
100       ----------
101
102       procedure Free is
103       begin
104          Free (To_Address (Table));
105          Table := null;
106          Length := 0;
107       end Free;
108
109       --------------------
110       -- Increment_Last --
111       --------------------
112
113       procedure Increment_Last is
114       begin
115          Last_Val := Last_Val + 1;
116
117          if Last_Val > Max then
118             Reallocate;
119          end if;
120       end Increment_Last;
121
122       ----------
123       -- Init --
124       ----------
125
126       procedure Init is
127          Old_Length : constant Int := Length;
128
129       begin
130          Locked   := False;
131          Last_Val := Min - 1;
132          Max      := Min + (Table_Initial * Table_Factor) - 1;
133          Length   := Max - Min + 1;
134
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.
139
140          if Old_Length = Length then
141             return;
142
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.
146
147          else
148             Reallocate;
149          end if;
150       end Init;
151
152       ----------
153       -- Last --
154       ----------
155
156       function Last return Table_Index_Type is
157       begin
158          return Table_Index_Type (Last_Val);
159       end Last;
160
161       ----------------
162       -- Reallocate --
163       ----------------
164
165       procedure Reallocate is
166          New_Size   : Memory.size_t;
167
168       begin
169          if Max < Last_Val then
170             pragma Assert (not Locked);
171
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.
174
175             Length := Int'Max (Length, Table_Initial);
176
177             --  Now increment table length until it is sufficiently large. Use
178             --  the increment value or 10, which ever is larger (the reason
179             --  for the use of 10 here is to ensure that the table does really
180             --  increase in size (which would not be the case for a table of
181             --  length 10 increased by 3% for instance).
182
183             while Max < Last_Val loop
184                Length := Int'Max (Length * (100 + Table_Increment) / 100,
185                                   Length + 10);
186                Max := Min + Length - 1;
187             end loop;
188
189             if Debug_Flag_D then
190                Write_Str ("--> Allocating new ");
191                Write_Str (Table_Name);
192                Write_Str (" table, size = ");
193                Write_Int (Max - Min + 1);
194                Write_Eol;
195             end if;
196          end if;
197
198          New_Size :=
199            Memory.size_t ((Max - Min + 1) *
200                           (Table_Type'Component_Size / Storage_Unit));
201
202          if Table = null then
203             Table := To_Pointer (Alloc (New_Size));
204
205          elsif New_Size > 0 then
206             Table :=
207               To_Pointer (Realloc (Ptr  => To_Address (Table),
208                                    Size => New_Size));
209          end if;
210
211          if Length /= 0 and then Table = null then
212             Set_Standard_Error;
213             Write_Str ("available memory exhausted");
214             Write_Eol;
215             Set_Standard_Output;
216             raise Unrecoverable_Error;
217          end if;
218
219       end Reallocate;
220
221       -------------
222       -- Release --
223       -------------
224
225       procedure Release is
226       begin
227          Length := Last_Val - Int (Table_Low_Bound) + 1;
228          Max    := Last_Val;
229          Reallocate;
230       end Release;
231
232       -------------
233       -- Restore --
234       -------------
235
236       procedure Restore (T : Saved_Table) is
237       begin
238          Free (To_Address (Table));
239          Last_Val := T.Last_Val;
240          Max      := T.Max;
241          Table    := T.Table;
242          Length   := Max - Min + 1;
243       end Restore;
244
245       ----------
246       -- Save --
247       ----------
248
249       function Save return Saved_Table is
250          Res : Saved_Table;
251
252       begin
253          Res.Last_Val := Last_Val;
254          Res.Max      := Max;
255          Res.Table    := Table;
256
257          Table  := null;
258          Length := 0;
259          Init;
260          return Res;
261       end Save;
262
263       --------------
264       -- Set_Item --
265       --------------
266
267       procedure Set_Item
268          (Index : Table_Index_Type;
269           Item  : Table_Component_Type)
270       is
271       begin
272          if Int (Index) > Max then
273             Set_Last (Index);
274          end if;
275
276          Table (Index) := Item;
277       end Set_Item;
278
279       --------------
280       -- Set_Last --
281       --------------
282
283       procedure Set_Last (New_Val : Table_Index_Type) is
284       begin
285          if Int (New_Val) < Last_Val then
286             Last_Val := Int (New_Val);
287          else
288             Last_Val := Int (New_Val);
289
290             if Last_Val > Max then
291                Reallocate;
292             end if;
293          end if;
294       end Set_Last;
295
296       ----------------------------
297       -- Tree_Get_Table_Address --
298       ----------------------------
299
300       function Tree_Get_Table_Address return Address is
301       begin
302          if Length = 0 then
303             return Null_Address;
304          else
305             return Table (First)'Address;
306          end if;
307       end Tree_Get_Table_Address;
308
309       ---------------
310       -- Tree_Read --
311       ---------------
312
313       --  Note: we allocate only the space required to accommodate the data
314       --  actually written, which means that a Tree_Write/Tree_Read sequence
315       --  does an implicit Release.
316
317       procedure Tree_Read is
318       begin
319          Tree_Read_Int (Max);
320          Last_Val := Max;
321          Length := Max - Min + 1;
322          Reallocate;
323
324          Tree_Read_Data
325            (Tree_Get_Table_Address,
326              (Last_Val - Int (First) + 1) *
327                Table_Type'Component_Size / Storage_Unit);
328       end Tree_Read;
329
330       ----------------
331       -- Tree_Write --
332       ----------------
333
334       --  Note: we write out only the currently valid data, not the entire
335       --  contents of the allocated array. See note above on Tree_Read.
336
337       procedure Tree_Write is
338       begin
339          Tree_Write_Int (Int (Last));
340          Tree_Write_Data
341            (Tree_Get_Table_Address,
342             (Last_Val - Int (First) + 1) *
343               Table_Type'Component_Size / Storage_Unit);
344       end Tree_Write;
345
346    begin
347       Init;
348    end Table;
349 end Table;