111
|
1 ------------------------------------------------------------------------------
|
|
2 -- --
|
|
3 -- GNAT COMPILER COMPONENTS --
|
|
4 -- --
|
|
5 -- T A B L E --
|
|
6 -- --
|
|
7 -- S p e c --
|
|
8 -- --
|
145
|
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
|
111
|
10 -- --
|
|
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. --
|
|
17 -- --
|
|
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. --
|
|
21 -- --
|
|
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/>. --
|
|
26 -- --
|
|
27 -- GNAT was originally developed by the GNAT team at New York University. --
|
|
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
|
|
29 -- --
|
|
30 ------------------------------------------------------------------------------
|
|
31
|
|
32 -- This package provides an implementation of dynamically resizable one
|
|
33 -- dimensional arrays. The idea is to mimic the normal Ada semantics for
|
|
34 -- arrays as closely as possible with the one additional capability of
|
|
35 -- dynamically modifying the value of the Last attribute.
|
|
36
|
|
37 -- This package uses a very efficient memory management scheme and any
|
|
38 -- change must be carefully evaluated on compilation of real software.
|
|
39
|
|
40 -- Note that this interface should remain synchronized with those in
|
|
41 -- GNAT.Table and GNAT.Dynamic_Tables to keep coherency between these
|
|
42 -- three related units.
|
|
43
|
|
44 with Types; use Types;
|
|
45
|
|
46 package Table is
|
|
47 pragma Elaborate_Body;
|
|
48
|
|
49 generic
|
|
50 type Table_Component_Type is private;
|
|
51 type Table_Index_Type is range <>;
|
|
52
|
|
53 Table_Low_Bound : Table_Index_Type;
|
|
54 Table_Initial : Pos;
|
|
55 Table_Increment : Nat;
|
|
56 Table_Name : String;
|
|
57 Release_Threshold : Nat := 0;
|
|
58
|
|
59 package Table is
|
|
60
|
|
61 -- Table_Component_Type and Table_Index_Type specify the type of the
|
|
62 -- array, Table_Low_Bound is the lower bound. Table_Index_Type must be
|
|
63 -- an integer type. The effect is roughly to declare:
|
|
64
|
|
65 -- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
|
|
66 -- of Table_Component_Type;
|
|
67
|
|
68 -- Note: since the upper bound can be one less than the lower
|
|
69 -- bound for an empty array, the table index type must be able
|
|
70 -- to cover this range, e.g. if the lower bound is 1, then the
|
|
71 -- Table_Index_Type should be Natural rather than Positive.
|
|
72
|
|
73 -- Table_Component_Type may be any Ada type, except that controlled
|
|
74 -- types are not supported. Note however that default initialization
|
|
75 -- will NOT occur for array components.
|
|
76
|
|
77 -- The Table_Initial values controls the allocation of the table when
|
|
78 -- it is first allocated, either by default, or by an explicit Init
|
|
79 -- call. The value used is Opt.Table_Factor * Table_Initial.
|
|
80
|
|
81 -- The Table_Increment value controls the amount of increase, if the
|
|
82 -- table has to be increased in size. The value given is a percentage
|
|
83 -- value (e.g. 100 = increase table size by 100%, i.e. double it).
|
|
84
|
|
85 -- The Table_Name parameter is simply use in debug output messages it
|
|
86 -- has no other usage, and is not referenced in non-debugging mode.
|
|
87
|
|
88 -- The Last and Set_Last subprograms provide control over the current
|
|
89 -- logical allocation. They are quite efficient, so they can be used
|
|
90 -- freely (expensive reallocation occurs only at major granularity
|
|
91 -- chunks controlled by the allocation parameters).
|
|
92
|
|
93 -- Note: We do not make the table components aliased, since this would
|
|
94 -- restrict the use of table for discriminated types. If it is necessary
|
|
95 -- to take the access of a table element, use Unrestricted_Access.
|
|
96
|
|
97 -- WARNING: On HPPA, the virtual addressing approach used in this unit
|
|
98 -- is incompatible with the indexing instructions on the HPPA. So when
|
|
99 -- using this unit, compile your application with -mdisable-indexing.
|
|
100
|
|
101 -- WARNING: If the table is reallocated, then the address of all its
|
|
102 -- components will change. So do not capture the address of an element
|
|
103 -- and then use the address later after the table may be reallocated.
|
|
104 -- One tricky case of this is passing an element of the table to a
|
|
105 -- subprogram by reference where the table gets reallocated during
|
|
106 -- the execution of the subprogram. The best rule to follow is never
|
|
107 -- to pass a table element as a parameter except for the case of IN
|
|
108 -- mode parameters with scalar values.
|
|
109
|
|
110 type Table_Type is
|
|
111 array (Table_Index_Type range <>) of Table_Component_Type;
|
|
112
|
|
113 subtype Big_Table_Type is
|
|
114 Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
|
|
115 -- We work with pointers to a bogus array type that is constrained
|
|
116 -- with the maximum possible range bound. This means that the pointer
|
|
117 -- is a thin pointer, which is more efficient. Since subscript checks
|
|
118 -- in any case must be on the logical, rather than physical bounds,
|
|
119 -- safety is not compromised by this approach.
|
|
120
|
|
121 type Table_Ptr is access all Big_Table_Type;
|
|
122 for Table_Ptr'Storage_Size use 0;
|
|
123 -- The table is actually represented as a pointer to allow reallocation
|
|
124
|
|
125 Table : aliased Table_Ptr := null;
|
|
126 -- The table itself. The lower bound is the value of Low_Bound.
|
|
127 -- Logically the upper bound is the current value of Last (although
|
|
128 -- the actual size of the allocated table may be larger than this).
|
|
129 -- The program may only access and modify Table entries in the range
|
|
130 -- First .. Last.
|
|
131
|
|
132 Locked : Boolean := False;
|
145
|
133 -- Increasing the value of Last is permitted only if this switch is set
|
|
134 -- to False. A client may set Locked to True, in which case any attempt
|
|
135 -- to increase the value of Last (which might expand the table) will
|
|
136 -- cause an assertion failure. Note that while a table is locked, its
|
|
137 -- address in memory remains fixed and unchanging. This feature is used
|
|
138 -- to control table expansion during Gigi processing. Gigi assumes that
|
|
139 -- tables other than the Uint and Ureal tables do not move during
|
|
140 -- processing, which means that they cannot be expanded. The Locked
|
|
141 -- flag is used to enforce this restriction.
|
111
|
142
|
|
143 procedure Init;
|
|
144 -- This procedure allocates a new table of size Initial (freeing any
|
|
145 -- previously allocated larger table). It is not necessary to call
|
|
146 -- Init when a table is first instantiated (since the instantiation does
|
|
147 -- the same initialization steps). However, it is harmless to do so, and
|
|
148 -- Init is convenient in reestablishing a table for new use.
|
|
149
|
|
150 function Last return Table_Index_Type;
|
|
151 pragma Inline (Last);
|
|
152 -- Returns the current value of the last used entry in the table, which
|
|
153 -- can then be used as a subscript for Table. Note that the only way to
|
|
154 -- modify Last is to call the Set_Last procedure. Last must always be
|
|
155 -- used to determine the logically last entry.
|
|
156
|
|
157 procedure Release;
|
|
158 -- Storage is allocated in chunks according to the values given in the
|
|
159 -- Initial and Increment parameters. If Release_Threshold is 0 or the
|
|
160 -- length of the table does not exceed this threshold then a call to
|
|
161 -- Release releases all storage that is allocated, but is not logically
|
|
162 -- part of the current array value; otherwise the call to Release leaves
|
|
163 -- the current array value plus 0.1% of the current table length free
|
|
164 -- elements located at the end of the table (this parameter facilitates
|
|
165 -- reopening large tables and adding a few elements without allocating a
|
|
166 -- chunk of memory). In both cases current array values are not affected
|
|
167 -- by this call.
|
|
168
|
|
169 procedure Free;
|
|
170 -- Free all allocated memory for the table. A call to init is required
|
|
171 -- before any use of this table after calling Free.
|
|
172
|
|
173 First : constant Table_Index_Type := Table_Low_Bound;
|
|
174 -- Export First as synonym for Low_Bound (parallel with use of Last)
|
|
175
|
|
176 procedure Set_Last (New_Val : Table_Index_Type);
|
|
177 pragma Inline (Set_Last);
|
|
178 -- This procedure sets Last to the indicated value. If necessary the
|
|
179 -- table is reallocated to accommodate the new value (i.e. on return
|
|
180 -- the allocated table has an upper bound of at least Last). If Set_Last
|
|
181 -- reduces the size of the table, then logically entries are removed
|
|
182 -- from the table. If Set_Last increases the size of the table, then
|
|
183 -- new entries are logically added to the table.
|
|
184
|
|
185 procedure Increment_Last;
|
|
186 pragma Inline (Increment_Last);
|
|
187 -- Adds 1 to Last (same as Set_Last (Last + 1)
|
|
188
|
|
189 procedure Decrement_Last;
|
|
190 pragma Inline (Decrement_Last);
|
|
191 -- Subtracts 1 from Last (same as Set_Last (Last - 1)
|
|
192
|
|
193 procedure Append (New_Val : Table_Component_Type);
|
|
194 pragma Inline (Append);
|
|
195 -- Equivalent to:
|
|
196 -- x.Increment_Last;
|
|
197 -- x.Table (x.Last) := New_Val;
|
|
198 -- i.e. the table size is increased by one, and the given new item
|
|
199 -- stored in the newly created table element.
|
|
200
|
|
201 procedure Append_All (New_Vals : Table_Type);
|
|
202 -- Appends all components of New_Vals
|
|
203
|
|
204 procedure Set_Item
|
|
205 (Index : Table_Index_Type;
|
|
206 Item : Table_Component_Type);
|
|
207 pragma Inline (Set_Item);
|
|
208 -- Put Item in the table at position Index. The table is expanded if
|
|
209 -- current table length is less than Index and in that case Last is set
|
|
210 -- to Index. Item will replace any value already present in the table
|
|
211 -- at this position.
|
|
212
|
|
213 type Saved_Table is private;
|
|
214 -- Type used for Save/Restore subprograms
|
|
215
|
|
216 function Save return Saved_Table;
|
|
217 -- Resets table to empty, but saves old contents of table in returned
|
|
218 -- value, for possible later restoration by a call to Restore.
|
|
219
|
|
220 procedure Restore (T : Saved_Table);
|
|
221 -- Given a Saved_Table value returned by a prior call to Save, restores
|
|
222 -- the table to the state it was in at the time of the Save call.
|
|
223
|
|
224 procedure Tree_Write;
|
|
225 -- Writes out contents of table using Tree_IO
|
|
226
|
|
227 procedure Tree_Read;
|
|
228 -- Initializes table by reading contents previously written with the
|
|
229 -- Tree_Write call (also using Tree_IO).
|
|
230
|
|
231 private
|
|
232
|
|
233 Last_Val : Int;
|
|
234 -- Current value of Last. Note that we declare this in the private part
|
|
235 -- because we don't want the client to modify Last except through one of
|
|
236 -- the official interfaces (since a modification to Last may require a
|
|
237 -- reallocation of the table).
|
|
238
|
|
239 Max : Int;
|
|
240 -- Subscript of the maximum entry in the currently allocated table
|
|
241
|
|
242 type Saved_Table is record
|
|
243 Last_Val : Int;
|
|
244 Max : Int;
|
|
245 Table : Table_Ptr;
|
|
246 end record;
|
|
247
|
|
248 end Table;
|
|
249 end Table;
|