* c-decl.c (duplicate_decls): Conditionalize DECL_SAVED_TREE copy.
[official-gcc.git] / gcc / ada / g-dyntab.adb
blob02c1bc19636bef61be8f613fdba527cfa8fcd55c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D Y N A M I C _ T A B L E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.4 $
10 -- --
11 -- Copyright (C) 2000-2001 Ada Core Technologies, Inc. --
12 -- --
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. --
23 -- --
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. --
30 -- --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 -- --
33 ------------------------------------------------------------------------------
35 with System; use System;
37 package body GNAT.Dynamic_Tables is
39 Min : constant Integer := Integer (Table_Low_Bound);
40 -- Subscript of the minimum entry in the currently allocated table
42 type size_t is new Integer;
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Reallocate (T : in out Instance);
49 -- Reallocate the existing table according to the current value stored
50 -- in Max. Works correctly to do an initial allocation if the table
51 -- is currently null.
53 --------------
54 -- Allocate --
55 --------------
57 procedure Allocate
58 (T : in out Instance;
59 Num : Integer := 1)
61 begin
62 T.P.Last_Val := T.P.Last_Val + Num;
64 if T.P.Last_Val > T.P.Max then
65 Reallocate (T);
66 end if;
67 end Allocate;
69 ------------
70 -- Append --
71 ------------
73 procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
74 begin
75 Increment_Last (T);
76 T.Table (Table_Index_Type (T.P.Last_Val)) := New_Val;
77 end Append;
79 --------------------
80 -- Decrement_Last --
81 --------------------
83 procedure Decrement_Last (T : in out Instance) is
84 begin
85 T.P.Last_Val := T.P.Last_Val - 1;
86 end Decrement_Last;
88 ----------
89 -- Free --
90 ----------
92 procedure Free (T : in out Instance) is
93 procedure free (T : Table_Ptr);
94 pragma Import (C, free);
96 begin
97 free (T.Table);
98 T.Table := null;
99 T.P.Length := 0;
100 end Free;
102 --------------------
103 -- Increment_Last --
104 --------------------
106 procedure Increment_Last (T : in out Instance) is
107 begin
108 T.P.Last_Val := T.P.Last_Val + 1;
110 if T.P.Last_Val > T.P.Max then
111 Reallocate (T);
112 end if;
113 end Increment_Last;
115 ----------
116 -- Init --
117 ----------
119 procedure Init (T : in out Instance) is
120 Old_Length : constant Integer := T.P.Length;
122 begin
123 T.P.Last_Val := Min - 1;
124 T.P.Max := Min + Table_Initial - 1;
125 T.P.Length := T.P.Max - Min + 1;
127 -- If table is same size as before (happens when table is never
128 -- expanded which is a common case), then simply reuse it. Note
129 -- that this also means that an explicit Init call right after
130 -- the implicit one in the package body is harmless.
132 if Old_Length = T.P.Length then
133 return;
135 -- Otherwise we can use Reallocate to get a table of the right size.
136 -- Note that Reallocate works fine to allocate a table of the right
137 -- initial size when it is first allocated.
139 else
140 Reallocate (T);
141 end if;
142 end Init;
144 ----------
145 -- Last --
146 ----------
148 function Last (T : in Instance) return Table_Index_Type is
149 begin
150 return Table_Index_Type (T.P.Last_Val);
151 end Last;
153 ----------------
154 -- Reallocate --
155 ----------------
157 procedure Reallocate (T : in out Instance) is
159 function realloc
160 (memblock : Table_Ptr;
161 size : size_t)
162 return Table_Ptr;
163 pragma Import (C, realloc);
165 function malloc
166 (size : size_t)
167 return Table_Ptr;
168 pragma Import (C, malloc);
170 New_Size : size_t;
172 begin
173 if T.P.Max < T.P.Last_Val then
174 while T.P.Max < T.P.Last_Val loop
175 T.P.Length := T.P.Length * (100 + Table_Increment) / 100;
176 T.P.Max := Min + T.P.Length - 1;
177 end loop;
178 end if;
180 New_Size :=
181 size_t ((T.P.Max - Min + 1) *
182 (Table_Type'Component_Size / Storage_Unit));
184 if T.Table = null then
185 T.Table := malloc (New_Size);
187 elsif New_Size > 0 then
188 T.Table :=
189 realloc
190 (memblock => T.Table,
191 size => New_Size);
192 end if;
194 if T.P.Length /= 0 and then T.Table = null then
195 raise Storage_Error;
196 end if;
198 end Reallocate;
200 -------------
201 -- Release --
202 -------------
204 procedure Release (T : in out Instance) is
205 begin
206 T.P.Length := T.P.Last_Val - Integer (Table_Low_Bound) + 1;
207 T.P.Max := T.P.Last_Val;
208 Reallocate (T);
209 end Release;
211 --------------
212 -- Set_Item --
213 --------------
215 procedure Set_Item
216 (T : in out Instance;
217 Index : Table_Index_Type;
218 Item : Table_Component_Type)
220 begin
221 if Integer (Index) > T.P.Max then
222 Set_Last (T, Index);
223 end if;
225 T.Table (Index) := Item;
226 end Set_Item;
228 --------------
229 -- Set_Last --
230 --------------
232 procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type) is
233 begin
234 if Integer (New_Val) < T.P.Last_Val then
235 T.P.Last_Val := Integer (New_Val);
237 else
238 T.P.Last_Val := Integer (New_Val);
240 if T.P.Last_Val > T.P.Max then
241 Reallocate (T);
242 end if;
243 end if;
244 end Set_Last;
246 end GNAT.Dynamic_Tables;