1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Debug
; use Debug
;
37 with Output
; use Output
;
38 pragma Elaborate_All
(Output
);
39 with System
; use System
;
40 with Tree_IO
; use Tree_IO
;
41 with System
.Memory
; use System
.Memory
;
42 with System
.Address_To_Access_Conversions
;
47 Min
: constant Int
:= Int
(Table_Low_Bound
);
48 -- Subscript of the minimum entry in the currently allocated table
51 -- Number of entries in currently allocated table. The value of zero
52 -- ensures that we initially allocate the table.
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
59 -- Reallocate the existing table according to the current value stored
60 -- in Max. Works correctly to do an initial allocation if the table
63 function Tree_Get_Table_Address
return Address
;
64 -- Return Null_Address if the table length is zero,
65 -- Table (First)'Address if not.
67 package Table_Conversions
is
68 new System
.Address_To_Access_Conversions
(Big_Table_Type
);
69 -- Address and Access conversions for a Table object.
71 function To_Address
(Table
: Table_Ptr
) return Address
;
72 pragma Inline
(To_Address
);
73 -- Returns the Address for the Table object.
75 function To_Pointer
(Table
: Address
) return Table_Ptr
;
76 pragma Inline
(To_Pointer
);
77 -- Returns the Access pointer for the Table object.
83 procedure Append
(New_Val
: Table_Component_Type
) is
86 Table
(Table_Index_Type
(Last_Val
)) := New_Val
;
93 procedure Decrement_Last
is
95 Last_Val
:= Last_Val
- 1;
104 Free
(To_Address
(Table
));
113 procedure Increment_Last
is
115 Last_Val
:= Last_Val
+ 1;
117 if Last_Val
> Max
then
127 Old_Length
: Int
:= Length
;
131 Max
:= Min
+ (Table_Initial
* Opt
.Table_Factor
) - 1;
132 Length
:= Max
- Min
+ 1;
134 -- If table is same size as before (happens when table is never
135 -- expanded which is a common case), then simply reuse it. Note
136 -- that this also means that an explicit Init call right after
137 -- the implicit one in the package body is harmless.
139 if Old_Length
= Length
then
142 -- Otherwise we can use Reallocate to get a table of the right size.
143 -- Note that Reallocate works fine to allocate a table of the right
144 -- initial size when it is first allocated.
155 function Last
return Table_Index_Type
is
157 return Table_Index_Type
(Last_Val
);
164 procedure Reallocate
is
165 New_Size
: Memory
.size_t
;
168 if Max
< Last_Val
then
169 pragma Assert
(not Locked
);
171 -- Make sure that we have at least the initial allocation. This
172 -- is needed in cases where a zero length table is written out.
174 Length
:= Int
'Max (Length
, Table_Initial
);
176 -- Now increment table length until it is sufficiently large
178 while Max
< Last_Val
loop
179 Length
:= Length
* (100 + Table_Increment
) / 100;
180 Max
:= Min
+ Length
- 1;
184 Write_Str
("--> Allocating new ");
185 Write_Str
(Table_Name
);
186 Write_Str
(" table, size = ");
187 Write_Int
(Max
- Min
+ 1);
193 Memory
.size_t
((Max
- Min
+ 1) *
194 (Table_Type
'Component_Size / Storage_Unit
));
197 Table
:= To_Pointer
(Alloc
(New_Size
));
199 elsif New_Size
> 0 then
201 To_Pointer
(Realloc
(Ptr
=> To_Address
(Table
),
205 if Length
/= 0 and then Table
= null then
207 Write_Str
("available memory exhausted");
210 raise Unrecoverable_Error
;
221 Length
:= Last_Val
- Int
(Table_Low_Bound
) + 1;
230 procedure Restore
(T
: Saved_Table
) is
232 Free
(To_Address
(Table
));
233 Last_Val
:= T
.Last_Val
;
236 Length
:= Max
- Min
+ 1;
243 function Save
return Saved_Table
is
247 Res
.Last_Val
:= Last_Val
;
262 (Index
: Table_Index_Type
;
263 Item
: Table_Component_Type
)
266 if Int
(Index
) > Max
then
270 Table
(Index
) := Item
;
277 procedure Set_Last
(New_Val
: Table_Index_Type
) is
279 if Int
(New_Val
) < Last_Val
then
280 Last_Val
:= Int
(New_Val
);
282 Last_Val
:= Int
(New_Val
);
284 if Last_Val
> Max
then
294 function To_Address
(Table
: Table_Ptr
) return Address
is
296 return Table_Conversions
.To_Address
297 (Table_Conversions
.Object_Pointer
(Table
));
304 function To_Pointer
(Table
: Address
) return Table_Ptr
is
306 return Table_Ptr
(Table_Conversions
.To_Pointer
(Table
));
309 ----------------------------
310 -- Tree_Get_Table_Address --
311 ----------------------------
313 function Tree_Get_Table_Address
return Address
is
318 return Table
(First
)'Address;
320 end Tree_Get_Table_Address
;
326 -- Note: we allocate only the space required to accommodate the data
327 -- actually written, which means that a Tree_Write/Tree_Read sequence
328 -- does an implicit Release.
330 procedure Tree_Read
is
334 Length
:= Max
- Min
+ 1;
338 (Tree_Get_Table_Address
,
339 (Last_Val
- Int
(First
) + 1) *
340 Table_Type
'Component_Size / Storage_Unit
);
347 -- Note: we write out only the currently valid data, not the entire
348 -- contents of the allocated array. See note above on Tree_Read.
350 procedure Tree_Write
is
352 Tree_Write_Int
(Int
(Last
));
354 (Tree_Get_Table_Address
,
355 (Last_Val
- Int
(First
) + 1) *
356 Table_Type
'Component_Size / Storage_Unit
);