Debian Codegraph @

gcc/ada/libgnat/s-finpri.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . F I N A L I Z A T I O N _ P R I M I T I V E S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2023-2025, Free Software Foundation, Inc. --
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 with System.OS_Locks;
33 with System.Storage_Elements;
34
35 -- This package encapsulates the types and operations used by the compiler
36 -- to support finalization of objects of finalizable types (types derived
37 -- from standard Ada types Controlled and Limited_Controlled, as well as
38 -- types declared with the GNAT Finalizable aspect).
39
40 package System.Finalization_Primitives with Preelaborate is
41
42 use type System.Storage_Elements.Storage_Offset;
43
44 type Finalize_Address_Ptr is access procedure (Obj : System.Address);
45 -- Values of this type denote finalization procedures associated with
46 -- objects that have controlled parts. For convenience, such objects
47 -- are simply referred to as controlled objects in the remainder of
48 -- this package.
49
50 type Master_Node is private;
51 -- Each controlled object associated with a finalization master has an
52 -- associated master node created by the compiler.
53
54 type Master_Node_Ptr is access all Master_Node;
55 for Master_Node_Ptr'Storage_Size use 0;
56 -- A reference to a master node. Since this type may not be used to
57 -- allocate objects, its storage size is zero.
58
59 --------------------------------------------------------------------------
60 -- Types and operations of finalization masters: A finalization master
61 -- is used to manage a set of controlled objects declared at the library
62 -- level of the program or associated with the declarative part of a
63 -- subprogram or other closed scopes (block statements, for example).
64
65 type Finalization_Master
66 (Exceptions_OK : Boolean;
67 Extra_Info : Boolean;
68 Library_Level : Boolean) is limited private;
69 -- Objects of this type encapsulate an ordered list of zero or more master
70 -- nodes, each of which is associated with a controlled object.
71
72 procedure Attach_Object_To_Master
73 (Object_Address : System.Address;
74 Finalize_Address : not null Finalize_Address_Ptr;
75 Node : not null Master_Node_Ptr;
76 Master : in out Finalization_Master);
77 -- Associates a controlled object and its master node with a given master.
78 -- Finalize_Address denotes the operation to be called to finalize the
79 -- object (which could be a user-declared Finalize procedure or a procedure
80 -- generated by the compiler). An object can be associated with at most one
81 -- finalization master.
82
83 procedure Attach_Object_To_Node
84 (Object_Address : System.Address;
85 Finalize_Address : not null Finalize_Address_Ptr;
86 Node : in out Master_Node);
87 -- Associates a controlled object with its master node only. This is used
88 -- when there is a single object to be finalized in the context, as well as
89 -- for objects that need special processing (return object in an extended
90 -- return statement or transient objects).
91
92 procedure Chain_Node_To_Master
93 (Node : not null Master_Node_Ptr;
94 Master : in out Finalization_Master);
95 -- Chain a master node to the given master. This is used to chain the node
96 -- to the master of the enclosing scope for the objects that need special
97 -- processing mentioned for Attach_Object_To_Node.
98
99 procedure Finalize_Master (Master : in out Finalization_Master);
100 -- Finalizes each of the controlled objects associated with Master, in the
101 -- reverse of the order in which they were attached. Calls to the procedure
102 -- with a Master that has already been finalized have no effects.
103
104 procedure Finalize_Object
105 (Node : in out Master_Node;
106 Finalize_Address : Finalize_Address_Ptr);
107 -- Finalizes the controlled object attached to Node by generating a call to
108 -- Finalize_Address on it, which has to be equal to Node.Finalize_Address.
109 -- The weird redundancy is intended to help the optimizer turn an indirect
110 -- call to Finalize_Address into a direct one and then inline it if needed,
111 -- after having inlined Finalize_Object itself. Calls to the procedure with
112 -- a Node that has already been finalized have no effects.
113
114 procedure Suppress_Object_Finalize_At_End (Node : in out Master_Node);
115 -- Changes the state of Node to effectively suppress a call to Node's
116 -- associated object's Finalize procedure. This is called at the end
117 -- of an extended return statement of a function whose result type
118 -- needs finalization, to ensure that the function's return object is
119 -- not finalized within the function in the case the return statement
120 -- is completed normally (it will still be finalized if an exception
121 -- is raised before the normal completion of the return statement).
122
123 --------------------------------------------------------------------------
124 -- Types and operations of finalization collections: A finalization
125 -- collection is used to manage a set of controlled objects associated
126 -- with an access type. Such collections are always associated with a
127 -- finalization master, either at the library-level or within a subprogram,
128 -- depending on where the access type is declared, and the collection
129 -- object itself is managed via a Master_Node attached to its finalization
130 -- master.
131
132 type Finalization_Collection is limited private
133 with Finalizable => (Initialize => Initialize,
134 Finalize => Finalize,
135 Relaxed_Finalization => Standard.False);
136 -- Objects of this type encapsulate a set of zero or more controlled
137 -- objects associated with an access type. The compiler ensures that
138 -- each finalization collection is in turn associated with a finalization
139 -- master.
140
141 type Finalization_Collection_Ptr is access all Finalization_Collection;
142 for Finalization_Collection_Ptr'Storage_Size use 0;
143 -- A reference to a collection. Since this type may not be used to
144 -- allocate objects, its storage size is zero.
145
146 procedure Initialize (Collection : in out Finalization_Collection);
147 -- Initializes the dummy head of a collection
148
149 procedure Finalize (Collection : in out Finalization_Collection);
150 -- Finalizes each object that has been associated with a finalization
151 -- collection, in some arbitrary order. Calls to this procedure with
152 -- a collection that has already been finalized have no effect.
153
154 procedure Attach_Object_To_Collection
155 (Object_Address : System.Address;
156 Finalize_Address : not null Finalize_Address_Ptr;
157 Collection : in out Finalization_Collection);
158 -- Associates a controlled object allocated for some access type with a
159 -- given finalization collection. Finalize_Address denotes the operation
160 -- to be called to finalize the object (which could be a user-declared
161 -- Finalize procedure or a procedure generated by the compiler). An object
162 -- can be associated with at most one finalization collection.
163
164 procedure Detach_Object_From_Collection (Object_Address : System.Address);
165 -- Removes a controlled object from its associated finalization collection.
166 -- Calls to the procedure with an object that has already been detached
167 -- have no effects.
168
169 function Header_Alignment return System.Storage_Elements.Storage_Count;
170 -- Return the alignment of the header to be placed immediately in front of
171 -- a controlled object allocated for some access type, in storage units.
172
173 function Header_Size return System.Storage_Elements.Storage_Count;
174 -- Return the size of the header to be placed immediately in front of a
175 -- controlled object allocated for some access type, in storage units.
176
177 private
178
179 -- Finalization masters:
180
181 -- Master node type structure. Finalize_Address comes first because it is
182 -- an access-to-subprogram and, therefore, might be twice as large and as
183 -- aligned as an access-to-object on some platforms.
184
185 type Master_Node is record
186 Finalize_Address : Finalize_Address_Ptr := null;
187 Object_Address : System.Address := Null_Address;
188 Next : Master_Node_Ptr := null;
189 end record;
190
191 -- Finalization master type structure. A unique master is associated
192 -- with each scope containing controlled objects.
193
194 type Finalization_Master
195 (Exceptions_OK : Boolean;
196 Extra_Info : Boolean;
197 Library_Level : Boolean) is limited
198 record
199 Head : Master_Node_Ptr := null;
200 end record;
201
202 -- These operations need to be performed in line for maximum performance
203
204 pragma Inline (Attach_Object_To_Master);
205 pragma Inline (Attach_Object_To_Node);
206 pragma Inline (Chain_Node_To_Master);
207 pragma Inline (Finalize_Object);
208 pragma Inline (Suppress_Object_Finalize_At_End);
209
210 -- Finalization collections:
211
212 type Collection_Node;
213 -- Each controlled object associated with a finalization collection has
214 -- an associated object of this type.
215
216 type Collection_Node_Ptr is access all Collection_Node;
217 for Collection_Node_Ptr'Storage_Size use 0;
218 pragma No_Strict_Aliasing (Collection_Node_Ptr);
219 -- A reference to a collection node. Since this type may not be used to
220 -- allocate objects, its storage size is zero.
221
222 -- Collection node type structure. Finalize_Address comes first because it
223 -- is an access-to-subprogram and, therefore, might be twice as large and
224 -- as aligned as an access-to-object on some platforms.
225
226 type Collection_Node is record
227 Finalize_Address : Finalize_Address_Ptr := null;
228 -- A pointer to the Finalize_Address procedure of the object
229
230 Enclosing_Collection : Finalization_Collection_Ptr := null;
231 -- A pointer to the collection to which the node is attached
232
233 Prev : Collection_Node_Ptr := null;
234 Next : Collection_Node_Ptr := null;
235 -- Collection nodes are managed as a circular doubly-linked list
236 end record;
237
238 function Header_Alignment return System.Storage_Elements.Storage_Count is
239 (Collection_Node'Alignment);
240
241 function Header_Size return System.Storage_Elements.Storage_Count is
242 (Collection_Node'Object_Size / Storage_Unit);
243
244 -- Finalization collection type structure
245
246 type Finalization_Collection is limited record
247 Head : aliased Collection_Node;
248 -- The head of the circular doubly-linked list of collection nodes
249
250 Lock : aliased System.OS_Locks.RTS_Lock;
251 -- A lock to synchronize concurrent accesses to the collection
252
253 Finalization_Started : Boolean;
254 -- A flag used to detect allocations which occur during the finalization
255 -- of a collection. The allocations must raise Program_Error. This may
256 -- arise in a multitask environment.
257 end record;
258
259 -- This operation is very simple and thus can be performed in line
260
261 pragma Inline (Initialize);
262
263 end System.Finalization_Primitives;