1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Debug
; use Debug
;
38 with Output
; use Output
;
39 with System
; use System
;
40 with Tree_IO
; use Tree_IO
;
45 Min
: constant Int
:= Int
(Table_Low_Bound
);
46 -- Subscript of the minimum entry in the currently allocated table
49 -- Number of entries in currently allocated table. The value of zero
50 -- ensures that we initially allocate the table.
52 procedure free
(T
: Table_Ptr
);
53 pragma Import
(C
, free
);
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
60 -- Reallocate the existing table according to the current value stored
61 -- in Max. Works correctly to do an initial allocation if the table
64 function Tree_Get_Table_Address
return Address
;
65 -- Return Null_Address if the table length is zero,
66 -- Table (First)'Address if not.
72 procedure Append
(New_Val
: Table_Component_Type
) is
75 Table
(Table_Index_Type
(Last_Val
)) := New_Val
;
82 procedure Decrement_Last
is
84 Last_Val
:= Last_Val
- 1;
102 procedure Increment_Last
is
104 Last_Val
:= Last_Val
+ 1;
106 if Last_Val
> Max
then
116 Old_Length
: Int
:= Length
;
120 Max
:= Min
+ (Table_Initial
* Opt
.Table_Factor
) - 1;
121 Length
:= Max
- Min
+ 1;
123 -- If table is same size as before (happens when table is never
124 -- expanded which is a common case), then simply reuse it. Note
125 -- that this also means that an explicit Init call right after
126 -- the implicit one in the package body is harmless.
128 if Old_Length
= Length
then
131 -- Otherwise we can use Reallocate to get a table of the right size.
132 -- Note that Reallocate works fine to allocate a table of the right
133 -- initial size when it is first allocated.
144 function Last
return Table_Index_Type
is
146 return Table_Index_Type
(Last_Val
);
153 procedure Reallocate
is
156 (memblock
: Table_Ptr
;
159 pragma Import
(C
, realloc
);
164 pragma Import
(C
, malloc
);
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 size_t
((Max
- Min
+ 1) *
195 (Table_Type
'Component_Size / Storage_Unit
));
198 Table
:= malloc
(New_Size
);
200 elsif New_Size
> 0 then
207 if Length
/= 0 and then Table
= null then
209 Write_Str
("available memory exhausted");
212 raise Unrecoverable_Error
;
223 Length
:= Last_Val
- Int
(Table_Low_Bound
) + 1;
232 procedure Restore
(T
: Saved_Table
) is
235 Last_Val
:= T
.Last_Val
;
238 Length
:= Max
- Min
+ 1;
245 function Save
return Saved_Table
is
249 Res
.Last_Val
:= Last_Val
;
264 (Index
: Table_Index_Type
;
265 Item
: Table_Component_Type
)
268 if Int
(Index
) > Max
then
272 Table
(Index
) := Item
;
279 procedure Set_Last
(New_Val
: Table_Index_Type
) is
281 if Int
(New_Val
) < Last_Val
then
282 Last_Val
:= Int
(New_Val
);
284 Last_Val
:= Int
(New_Val
);
286 if Last_Val
> Max
then
292 ----------------------------
293 -- Tree_Get_Table_Address --
294 ----------------------------
296 function Tree_Get_Table_Address
return Address
is
301 return Table
(First
)'Address;
303 end Tree_Get_Table_Address
;
309 -- Note: we allocate only the space required to accommodate the data
310 -- actually written, which means that a Tree_Write/Tree_Read sequence
311 -- does an implicit Release.
313 procedure Tree_Read
is
317 Length
:= Max
- Min
+ 1;
321 (Tree_Get_Table_Address
,
322 (Last_Val
- Int
(First
) + 1) *
323 Table_Type
'Component_Size / Storage_Unit
);
330 -- Note: we write out only the currently valid data, not the entire
331 -- contents of the allocated array. See note above on Tree_Read.
333 procedure Tree_Write
is
335 Tree_Write_Int
(Int
(Last
));
337 (Tree_Get_Table_Address
,
338 (Last_Val
- Int
(First
) + 1) *
339 Table_Type
'Component_Size / Storage_Unit
);