1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- G N A T . T A B L E --
11 -- Copyright (C) 1998-2001 Ada Core Technologies, 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with System
; use System
;
37 package body GNAT
.Table
is
39 Min
: constant Integer := Integer (Table_Low_Bound
);
40 -- Subscript of the minimum entry in the currently allocated table
43 -- Subscript of the maximum entry in the currently allocated table
45 Length
: Integer := 0;
46 -- Number of entries in currently allocated table. The value of zero
47 -- ensures that we initially allocate the table.
50 -- Current value of Last.
52 type size_t
is new Integer;
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
67 function Allocate
(Num
: Integer := 1) return Table_Index_Type
is
68 Old_Last
: constant Integer := Last_Val
;
71 Last_Val
:= Last_Val
+ Num
;
73 if Last_Val
> Max
then
77 return Table_Index_Type
(Old_Last
+ 1);
84 procedure Append
(New_Val
: Table_Component_Type
) is
87 Table
(Table_Index_Type
(Last_Val
)) := New_Val
;
94 procedure Decrement_Last
is
96 Last_Val
:= Last_Val
- 1;
104 procedure free
(T
: Table_Ptr
);
105 pragma Import
(C
, free
);
117 procedure Increment_Last
is
119 Last_Val
:= Last_Val
+ 1;
121 if Last_Val
> Max
then
131 Old_Length
: Integer := Length
;
135 Max
:= Min
+ Table_Initial
- 1;
136 Length
:= Max
- Min
+ 1;
138 -- If table is same size as before (happens when table is never
139 -- expanded which is a common case), then simply reuse it. Note
140 -- that this also means that an explicit Init call right after
141 -- the implicit one in the package body is harmless.
143 if Old_Length
= Length
then
146 -- Otherwise we can use Reallocate to get a table of the right size.
147 -- Note that Reallocate works fine to allocate a table of the right
148 -- initial size when it is first allocated.
159 function Last
return Table_Index_Type
is
161 return Table_Index_Type
(Last_Val
);
168 procedure Reallocate
is
171 (memblock
: Table_Ptr
;
174 pragma Import
(C
, realloc
);
179 pragma Import
(C
, malloc
);
184 if Max
< Last_Val
then
185 pragma Assert
(not Locked
);
187 while Max
< Last_Val
loop
189 -- Increase length using the table increment factor, but make
190 -- sure that we add at least ten elements (this avoids a loop
191 -- for silly small increment values)
193 Length
:= Integer'Max
194 (Length
* (100 + Table_Increment
) / 100,
196 Max
:= Min
+ Length
- 1;
201 size_t
((Max
- Min
+ 1) *
202 (Table_Type
'Component_Size / Storage_Unit
));
205 Table
:= malloc
(New_Size
);
207 elsif New_Size
> 0 then
214 if Length
/= 0 and then Table
= null then
226 Length
:= Last_Val
- Integer (Table_Low_Bound
) + 1;
236 (Index
: Table_Index_Type
;
237 Item
: Table_Component_Type
)
240 if Integer (Index
) > Max
then
244 Table
(Index
) := Item
;
251 procedure Set_Last
(New_Val
: Table_Index_Type
) is
253 if Integer (New_Val
) < Last_Val
then
254 Last_Val
:= Integer (New_Val
);
256 Last_Val
:= Integer (New_Val
);
258 if Last_Val
> Max
then