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-2018, 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 pragma Compiler_Unit_Warning
;
34 with GNAT
.Heap_Sort_G
;
36 with Ada
.Unchecked_Deallocation
;
39 package body GNAT
.Dynamic_Tables
is
41 -----------------------
42 -- Local Subprograms --
43 -----------------------
45 function Last_Allocated
(T
: Instance
) return Table_Last_Type
;
46 pragma Inline
(Last_Allocated
);
47 -- Return the index of the last allocated element
49 procedure Grow
(T
: in out Instance
; New_Last
: Table_Last_Type
);
50 -- This is called when we are about to set the value of Last to a value
51 -- that is larger than Last_Allocated. This reallocates the table to the
52 -- larger size, as indicated by New_Last. At the time this is called,
53 -- Last (T) is still the old value, and this does not modify it.
59 procedure Allocate
(T
: in out Instance
; Num
: Integer := 1) is
61 -- Note that Num can be negative
63 pragma Assert
(not T
.Locked
);
64 Set_Last
(T
, Last
(T
) + Table_Index_Type
'Base (Num
));
71 procedure Append
(T
: in out Instance
; New_Val
: Table_Component_Type
) is
72 pragma Assert
(not T
.Locked
);
73 New_Last
: constant Table_Last_Type
:= Last
(T
) + 1;
76 if New_Last
<= Last_Allocated
(T
) then
81 T
.Table
(New_Last
) := New_Val
;
84 Set_Item
(T
, New_Last
, New_Val
);
92 procedure Append_All
(T
: in out Instance
; New_Vals
: Table_Type
) is
94 for J
in New_Vals
'Range loop
95 Append
(T
, New_Vals
(J
));
103 procedure Decrement_Last
(T
: in out Instance
) is
105 pragma Assert
(not T
.Locked
);
113 function First
return Table_Index_Type
is
115 return Table_Low_Bound
;
122 procedure For_Each
(Table
: Instance
) is
123 Quit
: Boolean := False;
125 for Index
in First
.. Last
(Table
) loop
126 Action
(Index
, Table
.Table
(Index
), Quit
);
135 procedure Grow
(T
: in out Instance
; New_Last
: Table_Last_Type
) is
137 -- Note: Type Alloc_Ptr below needs to be declared locally so we know
138 -- the bounds. That means that the collection is local, so is finalized
139 -- when leaving Grow. That's why this package doesn't support controlled
140 -- types; the table elements would be finalized prematurely. An Ada
141 -- implementation would also be within its rights to reclaim the
142 -- storage. Fortunately, GNAT doesn't do that.
144 pragma Assert
(not T
.Locked
);
145 pragma Assert
(New_Last
> Last_Allocated
(T
));
147 subtype Table_Length_Type
is Table_Index_Type
'Base
148 range 0 .. Table_Index_Type
'Base'Last;
150 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
151 Old_Allocated_Length : constant Table_Length_Type :=
152 Old_Last_Allocated - First + 1;
154 New_Length : constant Table_Length_Type := New_Last - First + 1;
155 New_Allocated_Length : Table_Length_Type;
158 if T.Table = Empty_Table_Ptr then
159 New_Allocated_Length := Table_Length_Type (Table_Initial);
161 New_Allocated_Length :=
163 (Long_Long_Integer (Old_Allocated_Length) *
164 (100 + Long_Long_Integer (Table_Increment)) / 100);
167 -- Make sure it really did grow
169 if New_Allocated_Length <= Old_Allocated_Length then
170 New_Allocated_Length := Old_Allocated_Length + 10;
173 if New_Allocated_Length <= New_Length then
174 New_Allocated_Length := New_Length + 10;
177 pragma Assert (New_Allocated_Length > Old_Allocated_Length);
178 pragma Assert (New_Allocated_Length > New_Length);
180 T.P.Last_Allocated := First + New_Allocated_Length - 1;
183 subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
184 type Old_Alloc_Ptr is access all Old_Alloc_Type;
187 new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
188 function To_Old_Alloc_Ptr is
189 new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
191 subtype Alloc_Type is
192 Table_Type (First .. First + New_Allocated_Length - 1);
193 type Alloc_Ptr is access all Alloc_Type;
195 function To_Table_Ptr is
196 new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
198 Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
199 New_Table : constant Alloc_Ptr := new Alloc_Type;
202 if T.Table /= Empty_Table_Ptr then
203 New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
207 T.Table := To_Table_Ptr (New_Table);
210 pragma Assert (New_Last <= Last_Allocated (T));
211 pragma Assert (T.Table /= null);
212 pragma Assert (T.Table /= Empty_Table_Ptr);
219 procedure Increment_Last (T : in out Instance) is
221 pragma Assert (not T.Locked);
229 procedure Init (T : in out Instance) is
230 pragma Assert (not T.Locked);
231 subtype Alloc_Type is Table_Type (First .. Last_Allocated (T));
232 type Alloc_Ptr is access all Alloc_Type;
234 procedure Free is new Ada.Unchecked_Deallocation (Alloc_Type, Alloc_Ptr);
235 function To_Alloc_Ptr is
236 new Ada.Unchecked_Conversion (Table_Ptr, Alloc_Ptr);
238 Temp : Alloc_Ptr := To_Alloc_Ptr (T.Table);
241 if T.Table = Empty_Table_Ptr then
242 pragma Assert (T.P = (Last_Allocated | Last => First - 1));
246 T.Table := Empty_Table_Ptr;
247 T.P := (Last_Allocated | Last => First - 1);
255 function Is_Empty (T : Instance) return Boolean is
257 return Last (T) = First - 1;
264 function Last (T : Instance) return Table_Last_Type is
273 function Last_Allocated (T : Instance) return Table_Last_Type is
275 return T.P.Last_Allocated;
282 procedure Move (From, To : in out Instance) is
284 pragma Assert (not From.Locked);
285 pragma Assert (not To.Locked);
286 pragma Assert (Is_Empty (To));
289 From.Table := Empty_Table_Ptr;
290 From.Locked := False;
291 From.P.Last_Allocated := First - 1;
292 From.P.Last := First - 1;
293 pragma Assert (Is_Empty (From));
300 procedure Release (T : in out Instance) is
301 pragma Assert (not T.Locked);
302 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
304 function New_Last_Allocated return Table_Last_Type;
305 -- Compute the new value of Last_Allocated. This is normally equal to
306 -- Last, but if Release_Threshold /= 0, then we need to take that into
309 ------------------------
310 -- New_Last_Allocated --
311 ------------------------
313 function New_Last_Allocated return Table_Last_Type is
314 subtype Table_Length_Type is Table_Index_Type'Base
315 range 0 .. Table_Index_Type'Base'Last
;
317 Length
: constant Table_Length_Type
:= Last
(T
) - First
+ 1;
319 Comp_Size_In_Bytes
: constant Table_Length_Type
:=
320 Table_Type
'Component_Size / System
.Storage_Unit
;
322 Length_Threshold
: constant Table_Length_Type
:=
323 Table_Length_Type
(Release_Threshold
) / Comp_Size_In_Bytes
;
326 if Release_Threshold
= 0 or else Length
< Length_Threshold
then
330 Extra_Length
: constant Table_Length_Type
:= Length
/ 1000;
332 return (Length
+ Extra_Length
) - 1 + First
;
335 end New_Last_Allocated
;
339 New_Last_Alloc
: constant Table_Last_Type
:= New_Last_Allocated
;
341 -- Start of processing for Release
344 if New_Last_Alloc
< Last_Allocated
(T
) then
345 pragma Assert
(Last
(T
) < Last_Allocated
(T
));
346 pragma Assert
(T
.Table
/= Empty_Table_Ptr
);
349 subtype Old_Alloc_Type
is Table_Type
(First
.. Old_Last_Allocated
);
350 type Old_Alloc_Ptr
is access all Old_Alloc_Type
;
353 new Ada
.Unchecked_Deallocation
(Old_Alloc_Type
, Old_Alloc_Ptr
);
354 function To_Old_Alloc_Ptr
is
355 new Ada
.Unchecked_Conversion
(Table_Ptr
, Old_Alloc_Ptr
);
357 subtype Alloc_Type
is Table_Type
(First
.. New_Last_Alloc
);
358 type Alloc_Ptr
is access all Alloc_Type
;
360 function To_Table_Ptr
is
361 new Ada
.Unchecked_Conversion
(Alloc_Ptr
, Table_Ptr
);
363 Old_Table
: Old_Alloc_Ptr
:= To_Old_Alloc_Ptr
(T
.Table
);
364 New_Table
: constant Alloc_Ptr
:= new Alloc_Type
;
367 New_Table
(First
.. Last
(T
)) := Old_Table
(First
.. Last
(T
));
368 T
.P
.Last_Allocated
:= New_Last_Alloc
;
370 T
.Table
:= To_Table_Ptr
(New_Table
);
380 (T
: in out Instance
;
381 Index
: Valid_Table_Index_Type
;
382 Item
: Table_Component_Type
)
385 pragma Assert
(not T
.Locked
);
387 -- If Set_Last is going to reallocate the table, we make a copy of Item,
388 -- in case the call was "Set_Item (T, X, T.Table (Y));", and Item is
389 -- passed by reference. Without the copy, we would deallocate the array
390 -- containing Item, leaving a dangling pointer.
392 if Index
> Last_Allocated
(T
) then
394 Item_Copy
: constant Table_Component_Type
:= Item
;
397 T
.Table
(Index
) := Item_Copy
;
401 if Index
> Last
(T
) then
405 T
.Table
(Index
) := Item
;
413 procedure Set_Last
(T
: in out Instance
; New_Val
: Table_Last_Type
) is
415 pragma Assert
(not T
.Locked
);
416 if New_Val
> Last_Allocated
(T
) then
427 procedure Sort_Table
(Table
: in out Instance
) is
428 Temp
: Table_Component_Type
;
429 -- A temporary position to simulate index 0
433 function Index_Of
(Idx
: Natural) return Table_Index_Type
'Base;
434 -- Return index of Idx'th element of table
436 function Lower_Than
(Op1
, Op2
: Natural) return Boolean;
437 -- Compare two components
439 procedure Move
(From
: Natural; To
: Natural);
440 -- Move one component
442 package Heap_Sort
is new GNAT
.Heap_Sort_G
(Move
, Lower_Than
);
448 function Index_Of
(Idx
: Natural) return Table_Index_Type
'Base is
449 J
: constant Integer'Base :=
450 Table_Index_Type
'Base'Pos (First) + Idx - 1;
452 return Table_Index_Type'Base'Val
(J
);
459 procedure Move
(From
: Natural; To
: Natural) is
462 Table
.Table
(Index_Of
(To
)) := Temp
;
465 Temp
:= Table
.Table
(Index_Of
(From
));
468 Table
.Table
(Index_Of
(To
)) :=
469 Table
.Table
(Index_Of
(From
));
477 function Lower_Than
(Op1
, Op2
: Natural) return Boolean is
480 return Lt
(Temp
, Table
.Table
(Index_Of
(Op2
)));
483 return Lt
(Table
.Table
(Index_Of
(Op1
)), Temp
);
487 Lt
(Table
.Table
(Index_Of
(Op1
)), Table
.Table
(Index_Of
(Op2
)));
491 -- Start of processing for Sort_Table
494 Heap_Sort
.Sort
(Natural (Last
(Table
) - First
) + 1);
497 end GNAT
.Dynamic_Tables
;