1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Debug
; use Debug
;
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
);
49 Min
: constant Int
:= Int
(Table_Low_Bound
);
50 -- Subscript of the minimum entry in the currently allocated table
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 -----------------------
61 -- Reallocate the existing table according to the current value stored
62 -- in Max. Works correctly to do an initial allocation if the table
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 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.
74 function To_Address
is new Unchecked_Conversion
(Table_Ptr
, Address
);
75 function To_Pointer
is new Unchecked_Conversion
(Address
, Table_Ptr
);
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
: constant Int
:= Length
;
132 Max
:= Min
+ (Table_Initial
* Table_Factor
) - 1;
133 Length
:= Max
- Min
+ 1;
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.
140 if Old_Length
= Length
then
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.
156 function Last
return Table_Index_Type
is
158 return Table_Index_Type
(Last_Val
);
165 procedure Reallocate
is
166 New_Size
: Memory
.size_t
;
169 if Max
< Last_Val
then
170 pragma Assert
(not Locked
);
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.
175 Length
:= Int
'Max (Length
, Table_Initial
);
177 -- Now increment table length until it is sufficiently large
179 while Max
< Last_Val
loop
180 Length
:= Length
* (100 + Table_Increment
) / 100;
181 Max
:= Min
+ Length
- 1;
185 Write_Str
("--> Allocating new ");
186 Write_Str
(Table_Name
);
187 Write_Str
(" table, size = ");
188 Write_Int
(Max
- Min
+ 1);
194 Memory
.size_t
((Max
- Min
+ 1) *
195 (Table_Type
'Component_Size / Storage_Unit
));
198 Table
:= To_Pointer
(Alloc
(New_Size
));
200 elsif New_Size
> 0 then
202 To_Pointer
(Realloc
(Ptr
=> To_Address
(Table
),
206 if Length
/= 0 and then Table
= null then
208 Write_Str
("available memory exhausted");
211 raise Unrecoverable_Error
;
222 Length
:= Last_Val
- Int
(Table_Low_Bound
) + 1;
231 procedure Restore
(T
: Saved_Table
) is
233 Free
(To_Address
(Table
));
234 Last_Val
:= T
.Last_Val
;
237 Length
:= Max
- Min
+ 1;
244 function Save
return Saved_Table
is
248 Res
.Last_Val
:= Last_Val
;
263 (Index
: Table_Index_Type
;
264 Item
: Table_Component_Type
)
267 if Int
(Index
) > Max
then
271 Table
(Index
) := Item
;
278 procedure Set_Last
(New_Val
: Table_Index_Type
) is
280 if Int
(New_Val
) < Last_Val
then
281 Last_Val
:= Int
(New_Val
);
283 Last_Val
:= Int
(New_Val
);
285 if Last_Val
> Max
then
291 ----------------------------
292 -- Tree_Get_Table_Address --
293 ----------------------------
295 function Tree_Get_Table_Address
return Address
is
300 return Table
(First
)'Address;
302 end Tree_Get_Table_Address
;
308 -- Note: we allocate only the space required to accommodate the data
309 -- actually written, which means that a Tree_Write/Tree_Read sequence
310 -- does an implicit Release.
312 procedure Tree_Read
is
316 Length
:= Max
- Min
+ 1;
320 (Tree_Get_Table_Address
,
321 (Last_Val
- Int
(First
) + 1) *
322 Table_Type
'Component_Size / Storage_Unit
);
329 -- Note: we write out only the currently valid data, not the entire
330 -- contents of the allocated array. See note above on Tree_Read.
332 procedure Tree_Write
is
334 Tree_Write_Int
(Int
(Last
));
336 (Tree_Get_Table_Address
,
337 (Last_Val
- Int
(First
) + 1) *
338 Table_Type
'Component_Size / Storage_Unit
);