1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . D Y N A M I C _ T A B L E S --
9 -- Copyright (C) 2000-2024, AdaCore --
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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with GNAT
.Heap_Sort_G
;
34 with Ada
.Unchecked_Deallocation
;
37 package body GNAT
.Dynamic_Tables
is
39 -----------------------
40 -- Local Subprograms --
41 -----------------------
43 function Last_Allocated
(T
: Instance
) return Table_Last_Type
;
44 pragma Inline
(Last_Allocated
);
45 -- Return the index of the last allocated element
47 procedure Grow
(T
: in out Instance
; New_Last
: Table_Last_Type
);
48 -- This is called when we are about to set the value of Last to a value
49 -- that is larger than Last_Allocated. This reallocates the table to the
50 -- larger size, as indicated by New_Last. At the time this is called,
51 -- Last (T) is still the old value, and this does not modify it.
57 procedure Allocate
(T
: in out Instance
; Num
: Integer := 1) is
59 -- Note that Num can be negative
61 pragma Assert
(not T
.Locked
);
62 Set_Last
(T
, Last
(T
) + Table_Index_Type
'Base (Num
));
69 procedure Append
(T
: in out Instance
; New_Val
: Table_Component_Type
) is
70 pragma Assert
(not T
.Locked
);
71 New_Last
: constant Table_Last_Type
:= Last
(T
) + 1;
74 if New_Last
<= Last_Allocated
(T
) then
79 T
.Table
(New_Last
) := New_Val
;
82 Set_Item
(T
, New_Last
, New_Val
);
90 procedure Append_All
(T
: in out Instance
; New_Vals
: Table_Type
) is
92 for J
in New_Vals
'Range loop
93 Append
(T
, New_Vals
(J
));
101 procedure Decrement_Last
(T
: in out Instance
) is
103 pragma Assert
(not T
.Locked
);
111 function First
return Table_Index_Type
is
113 return Table_Low_Bound
;
120 procedure For_Each
(Table
: Instance
) is
121 Quit
: Boolean := False;
123 for Index
in First
.. Last
(Table
) loop
124 Action
(Index
, Table
.Table
(Index
), Quit
);
133 procedure Grow
(T
: in out Instance
; New_Last
: Table_Last_Type
) is
135 -- Note: Type Alloc_Ptr below needs to be declared locally so we know
136 -- the bounds. That means that the collection is local, so is finalized
137 -- when leaving Grow. That's why this package doesn't support controlled
138 -- types; the table elements would be finalized prematurely. An Ada
139 -- implementation would also be within its rights to reclaim the
140 -- storage. Fortunately, GNAT doesn't do that.
142 pragma Assert
(not T
.Locked
);
143 pragma Assert
(New_Last
> Last_Allocated
(T
));
145 subtype Table_Length_Type
is Table_Index_Type
'Base
146 range 0 .. Table_Index_Type
'Base'Last;
148 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
149 Old_Allocated_Length : constant Table_Length_Type :=
150 Old_Last_Allocated - First + 1;
152 New_Length : constant Table_Length_Type := New_Last - First + 1;
153 New_Allocated_Length : Table_Length_Type;
156 if T.Table = Empty_Table_Ptr then
157 New_Allocated_Length := Table_Length_Type (Table_Initial);
159 New_Allocated_Length :=
161 (Long_Long_Integer (Old_Allocated_Length) *
162 (100 + Long_Long_Integer (Table_Increment)) / 100);
165 -- Make sure it really did grow
167 if New_Allocated_Length <= Old_Allocated_Length then
168 New_Allocated_Length := Old_Allocated_Length + 10;
171 if New_Allocated_Length <= New_Length then
172 New_Allocated_Length := New_Length + 10;
175 pragma Assert (New_Allocated_Length > Old_Allocated_Length);
176 pragma Assert (New_Allocated_Length > New_Length);
178 T.P.Last_Allocated := First + New_Allocated_Length - 1;
181 subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
182 type Old_Alloc_Ptr is access all Old_Alloc_Type;
185 new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
186 function To_Old_Alloc_Ptr is
187 new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
189 subtype Alloc_Type is
190 Table_Type (First .. First + New_Allocated_Length - 1);
191 type Alloc_Ptr is access all Alloc_Type;
193 function To_Table_Ptr is
194 new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
196 Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
197 New_Table : constant Alloc_Ptr := new Alloc_Type;
200 if T.Table /= Empty_Table_Ptr then
201 New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
205 T.Table := To_Table_Ptr (New_Table);
208 pragma Assert (New_Last <= Last_Allocated (T));
209 pragma Assert (T.Table /= null);
210 pragma Assert (T.Table /= Empty_Table_Ptr);
217 procedure Increment_Last (T : in out Instance) is
219 pragma Assert (not T.Locked);
227 procedure Init (T : in out Instance) is
228 pragma Assert (not T.Locked);
229 subtype Alloc_Type is Table_Type (First .. Last_Allocated (T));
230 type Alloc_Ptr is access all Alloc_Type;
232 procedure Free is new Ada.Unchecked_Deallocation (Alloc_Type, Alloc_Ptr);
233 function To_Alloc_Ptr is
234 new Ada.Unchecked_Conversion (Table_Ptr, Alloc_Ptr);
236 Temp : Alloc_Ptr := To_Alloc_Ptr (T.Table);
239 if T.Table = Empty_Table_Ptr then
240 pragma Assert (T.P = (Last_Allocated | Last => First - 1));
244 T.Table := Empty_Table_Ptr;
245 T.P := (Last_Allocated | Last => First - 1);
253 function Is_Empty (T : Instance) return Boolean is
255 return Last (T) = First - 1;
262 function Last (T : Instance) return Table_Last_Type is
271 function Last_Allocated (T : Instance) return Table_Last_Type is
273 return T.P.Last_Allocated;
280 procedure Move (From, To : in out Instance) is
282 pragma Assert (not From.Locked);
283 pragma Assert (not To.Locked);
284 pragma Assert (Is_Empty (To));
287 From.Table := Empty_Table_Ptr;
288 From.Locked := False;
289 From.P.Last_Allocated := First - 1;
290 From.P.Last := First - 1;
291 pragma Assert (Is_Empty (From));
298 procedure Release (T : in out Instance) is
299 pragma Assert (not T.Locked);
300 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
302 function New_Last_Allocated return Table_Last_Type;
303 -- Compute the new value of Last_Allocated. This is normally equal to
304 -- Last, but if Release_Threshold /= 0, then we need to take that into
307 ------------------------
308 -- New_Last_Allocated --
309 ------------------------
311 function New_Last_Allocated return Table_Last_Type is
312 subtype Table_Length_Type is Table_Index_Type'Base
313 range 0 .. Table_Index_Type'Base'Last
;
315 Length
: constant Table_Length_Type
:= Last
(T
) - First
+ 1;
317 Comp_Size_In_Bytes
: constant Table_Length_Type
:=
318 Table_Type
'Component_Size / System
.Storage_Unit
;
320 Length_Threshold
: constant Table_Length_Type
:=
321 Table_Length_Type
(Release_Threshold
) / Comp_Size_In_Bytes
;
324 if Release_Threshold
= 0 or else Length
< Length_Threshold
then
328 Extra_Length
: constant Table_Length_Type
:= Length
/ 1000;
330 return (Length
+ Extra_Length
) - 1 + First
;
333 end New_Last_Allocated
;
337 New_Last_Alloc
: constant Table_Last_Type
:= New_Last_Allocated
;
339 -- Start of processing for Release
342 if New_Last_Alloc
< Last_Allocated
(T
) then
343 pragma Assert
(Last
(T
) < Last_Allocated
(T
));
344 pragma Assert
(T
.Table
/= Empty_Table_Ptr
);
347 subtype Old_Alloc_Type
is Table_Type
(First
.. Old_Last_Allocated
);
348 type Old_Alloc_Ptr
is access all Old_Alloc_Type
;
351 new Ada
.Unchecked_Deallocation
(Old_Alloc_Type
, Old_Alloc_Ptr
);
352 function To_Old_Alloc_Ptr
is
353 new Ada
.Unchecked_Conversion
(Table_Ptr
, Old_Alloc_Ptr
);
355 subtype Alloc_Type
is Table_Type
(First
.. New_Last_Alloc
);
356 type Alloc_Ptr
is access all Alloc_Type
;
358 function To_Table_Ptr
is
359 new Ada
.Unchecked_Conversion
(Alloc_Ptr
, Table_Ptr
);
361 Old_Table
: Old_Alloc_Ptr
:= To_Old_Alloc_Ptr
(T
.Table
);
362 New_Table
: constant Alloc_Ptr
:= new Alloc_Type
;
365 New_Table
(First
.. Last
(T
)) := Old_Table
(First
.. Last
(T
));
366 T
.P
.Last_Allocated
:= New_Last_Alloc
;
368 T
.Table
:= To_Table_Ptr
(New_Table
);
378 (T
: in out Instance
;
379 Index
: Valid_Table_Index_Type
;
380 Item
: Table_Component_Type
)
383 pragma Assert
(not T
.Locked
);
385 -- If Set_Last is going to reallocate the table, we make a copy of Item,
386 -- in case the call was "Set_Item (T, X, T.Table (Y));", and Item is
387 -- passed by reference. Without the copy, we would deallocate the array
388 -- containing Item, leaving a dangling pointer.
390 if Index
> Last_Allocated
(T
) then
392 Item_Copy
: constant Table_Component_Type
:= Item
;
395 T
.Table
(Index
) := Item_Copy
;
399 if Index
> Last
(T
) then
403 T
.Table
(Index
) := Item
;
411 procedure Set_Last
(T
: in out Instance
; New_Val
: Table_Last_Type
) is
413 pragma Assert
(not T
.Locked
);
414 if New_Val
> Last_Allocated
(T
) then
425 procedure Sort_Table
(Table
: in out Instance
) is
426 Temp
: Table_Component_Type
;
427 -- A temporary position to simulate index 0
431 function Index_Of
(Idx
: Natural) return Table_Index_Type
'Base;
432 -- Return index of Idx'th element of table
434 function Lower_Than
(Op1
, Op2
: Natural) return Boolean;
435 -- Compare two components
437 procedure Move
(From
: Natural; To
: Natural);
438 -- Move one component
440 package Heap_Sort
is new GNAT
.Heap_Sort_G
(Move
, Lower_Than
);
446 function Index_Of
(Idx
: Natural) return Table_Index_Type
'Base is
447 J
: constant Integer'Base :=
448 Table_Index_Type
'Base'Pos (First) + Idx - 1;
450 return Table_Index_Type'Base'Val
(J
);
457 procedure Move
(From
: Natural; To
: Natural) is
460 Table
.Table
(Index_Of
(To
)) := Temp
;
463 Temp
:= Table
.Table
(Index_Of
(From
));
466 Table
.Table
(Index_Of
(To
)) :=
467 Table
.Table
(Index_Of
(From
));
475 function Lower_Than
(Op1
, Op2
: Natural) return Boolean is
478 return Lt
(Temp
, Table
.Table
(Index_Of
(Op2
)));
481 return Lt
(Table
.Table
(Index_Of
(Op1
)), Temp
);
485 Lt
(Table
.Table
(Index_Of
(Op1
)), Table
.Table
(Index_Of
(Op2
)));
489 -- Start of processing for Sort_Table
492 Heap_Sort
.Sort
(Natural (Last
(Table
) - First
) + 1);
495 end GNAT
.Dynamic_Tables
;