| 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 |
-- B o d y -- |
| 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 Ada.Exceptions; use Ada.Exceptions; |
| 33 |
with Ada.Unchecked_Conversion; |
| 34 |
|
| 35 |
with System.Soft_Links; use System.Soft_Links; |
| 36 |
|
| 37 |
package body System.Finalization_Primitives is |
| 38 |
|
| 39 |
procedure Raise_From_Controlled_Operation (X : Exception_Occurrence); |
| 40 |
pragma Import (Ada, Raise_From_Controlled_Operation, |
| 41 |
"__gnat_raise_from_controlled_operation"); |
| 42 |
|
| 43 |
function To_Collection_Node_Ptr is |
| 44 |
new Ada.Unchecked_Conversion (Address, Collection_Node_Ptr); |
| 45 |
|
| 46 |
procedure Detach_Node_From_Collection (Node : not null Collection_Node_Ptr); |
| 47 |
-- Remove a collection node from its associated finalization collection. |
| 48 |
-- Calls to the procedure with a Node that has already been detached have |
| 49 |
-- no effects. |
| 50 |
|
| 51 |
procedure Lock_Collection (Collection : in out Finalization_Collection); |
| 52 |
-- Lock the finalization collection. Upon return, the caller owns the lock |
| 53 |
-- to the collection and no other call with the same actual parameter will |
| 54 |
-- return until a corresponding call to Unlock_Collection has been made by |
| 55 |
-- the caller. This means that it is not possible to call Lock_Collection |
| 56 |
-- more than once on a collection without a call to Unlock_Collection in |
| 57 |
-- between. |
| 58 |
|
| 59 |
procedure Unlock_Collection (Collection : in out Finalization_Collection); |
| 60 |
-- Unlock the finalization collection, i.e. relinquish ownership of the |
| 61 |
-- lock to the collection. |
| 62 |
|
| 63 |
--------------------------------- |
| 64 |
-- Attach_Object_To_Collection -- |
| 65 |
--------------------------------- |
| 66 |
|
| 67 |
procedure Attach_Object_To_Collection |
| 68 |
(Object_Address : System.Address; |
| 69 |
Finalize_Address : not null Finalize_Address_Ptr; |
| 70 |
Collection : in out Finalization_Collection) |
| 71 |
is |
| 72 |
Node : constant Collection_Node_Ptr := |
| 73 |
To_Collection_Node_Ptr (Object_Address - Header_Size); |
| 74 |
|
| 75 |
begin |
| 76 |
Lock_Collection (Collection); |
| 77 |
|
| 78 |
-- Do not allow the attachment of controlled objects while the |
| 79 |
-- associated collection is being finalized. |
| 80 |
|
| 81 |
-- Synchronization: |
| 82 |
-- Read - attachment, finalization |
| 83 |
-- Write - finalization |
| 84 |
|
| 85 |
if Collection.Finalization_Started then |
| 86 |
raise Program_Error with "attachment after finalization started"; |
| 87 |
end if; |
| 88 |
|
| 89 |
-- Check whether primitive Finalize_Address is available. If it is |
| 90 |
-- not, then either the expansion of the designated type failed or |
| 91 |
-- the expansion of the allocator failed. This is a compiler bug. |
| 92 |
|
| 93 |
pragma Assert |
| 94 |
(Finalize_Address /= null, "primitive Finalize_Address not available"); |
| 95 |
|
| 96 |
Node.Enclosing_Collection := Collection'Unrestricted_Access; |
| 97 |
Node.Finalize_Address := Finalize_Address; |
| 98 |
Node.Prev := Collection.Head'Unchecked_Access; |
| 99 |
Node.Next := Collection.Head.Next; |
| 100 |
|
| 101 |
Collection.Head.Next.Prev := Node; |
| 102 |
Collection.Head.Next := Node; |
| 103 |
|
| 104 |
Unlock_Collection (Collection); |
| 105 |
|
| 106 |
exception |
| 107 |
when others => |
| 108 |
|
| 109 |
-- Unlock the collection in case the attachment failed and reraise |
| 110 |
-- the exception. |
| 111 |
|
| 112 |
Unlock_Collection (Collection); |
| 113 |
raise; |
| 114 |
end Attach_Object_To_Collection; |
| 115 |
|
| 116 |
----------------------------- |
| 117 |
-- Attach_Object_To_Master -- |
| 118 |
----------------------------- |
| 119 |
|
| 120 |
procedure Attach_Object_To_Master |
| 121 |
(Object_Address : System.Address; |
| 122 |
Finalize_Address : not null Finalize_Address_Ptr; |
| 123 |
Node : not null Master_Node_Ptr; |
| 124 |
Master : in out Finalization_Master) |
| 125 |
is |
| 126 |
begin |
| 127 |
Attach_Object_To_Node (Object_Address, Finalize_Address, Node.all); |
| 128 |
Chain_Node_To_Master (Node, Master); |
| 129 |
end Attach_Object_To_Master; |
| 130 |
|
| 131 |
--------------------------- |
| 132 |
-- Attach_Object_To_Node -- |
| 133 |
--------------------------- |
| 134 |
|
| 135 |
procedure Attach_Object_To_Node |
| 136 |
(Object_Address : System.Address; |
| 137 |
Finalize_Address : not null Finalize_Address_Ptr; |
| 138 |
Node : in out Master_Node) |
| 139 |
is |
| 140 |
begin |
| 141 |
pragma Assert (Node.Object_Address = Null_Address |
| 142 |
and then Node.Finalize_Address = null); |
| 143 |
|
| 144 |
Node.Object_Address := Object_Address; |
| 145 |
Node.Finalize_Address := Finalize_Address; |
| 146 |
end Attach_Object_To_Node; |
| 147 |
|
| 148 |
-------------------------- |
| 149 |
-- Chain_Node_To_Master -- |
| 150 |
-------------------------- |
| 151 |
|
| 152 |
procedure Chain_Node_To_Master |
| 153 |
(Node : not null Master_Node_Ptr; |
| 154 |
Master : in out Finalization_Master) |
| 155 |
is |
| 156 |
begin |
| 157 |
Node.Next := Master.Head; |
| 158 |
Master.Head := Node; |
| 159 |
end Chain_Node_To_Master; |
| 160 |
|
| 161 |
--------------------------------- |
| 162 |
-- Detach_Node_From_Collection -- |
| 163 |
--------------------------------- |
| 164 |
|
| 165 |
procedure Detach_Node_From_Collection |
| 166 |
(Node : not null Collection_Node_Ptr) |
| 167 |
is |
| 168 |
begin |
| 169 |
if Node.Prev /= null and then Node.Next /= null then |
| 170 |
Node.Prev.Next := Node.Next; |
| 171 |
Node.Next.Prev := Node.Prev; |
| 172 |
Node.Prev := null; |
| 173 |
Node.Next := null; |
| 174 |
end if; |
| 175 |
end Detach_Node_From_Collection; |
| 176 |
|
| 177 |
----------------------------------- |
| 178 |
-- Detach_Object_From_Collection -- |
| 179 |
----------------------------------- |
| 180 |
|
| 181 |
procedure Detach_Object_From_Collection |
| 182 |
(Object_Address : System.Address) |
| 183 |
is |
| 184 |
Node : constant Collection_Node_Ptr := |
| 185 |
To_Collection_Node_Ptr (Object_Address - Header_Size); |
| 186 |
|
| 187 |
begin |
| 188 |
Lock_Collection (Node.Enclosing_Collection.all); |
| 189 |
|
| 190 |
Detach_Node_From_Collection (Node); |
| 191 |
|
| 192 |
Unlock_Collection (Node.Enclosing_Collection.all); |
| 193 |
end Detach_Object_From_Collection; |
| 194 |
|
| 195 |
-------------- |
| 196 |
-- Finalize -- |
| 197 |
-------------- |
| 198 |
|
| 199 |
procedure Finalize (Collection : in out Finalization_Collection) is |
| 200 |
Curr_Ptr : Collection_Node_Ptr; |
| 201 |
Exc_Occur : Exception_Occurrence; |
| 202 |
Finalization_Exception_Raised : Boolean := False; |
| 203 |
Obj_Addr : Address; |
| 204 |
|
| 205 |
function Is_Empty_List (L : not null Collection_Node_Ptr) return Boolean; |
| 206 |
-- Determine whether a list contains only one element, the dummy head |
| 207 |
|
| 208 |
------------------- |
| 209 |
-- Is_Empty_List -- |
| 210 |
------------------- |
| 211 |
|
| 212 |
function Is_Empty_List (L : not null Collection_Node_Ptr) return Boolean |
| 213 |
is |
| 214 |
begin |
| 215 |
return L.Next = L and then L.Prev = L; |
| 216 |
end Is_Empty_List; |
| 217 |
|
| 218 |
begin |
| 219 |
Lock_Collection (Collection); |
| 220 |
|
| 221 |
-- Synchronization: |
| 222 |
-- Read - attachment, finalization |
| 223 |
-- Write - finalization |
| 224 |
|
| 225 |
if Collection.Finalization_Started then |
| 226 |
Unlock_Collection (Collection); |
| 227 |
|
| 228 |
-- Double finalization may occur during the handling of stand-alone |
| 229 |
-- libraries or the finalization of a pool with subpools. |
| 230 |
|
| 231 |
return; |
| 232 |
end if; |
| 233 |
|
| 234 |
-- Lock the collection to prevent any attachment while the objects are |
| 235 |
-- being finalized. The collection remains locked because either it is |
| 236 |
-- explicitly deallocated or the associated access type is about to go |
| 237 |
-- out of scope. |
| 238 |
|
| 239 |
-- Synchronization: |
| 240 |
-- Read - attachment, finalization |
| 241 |
-- Write - finalization |
| 242 |
|
| 243 |
Collection.Finalization_Started := True; |
| 244 |
|
| 245 |
-- Note that we cannot walk the list while finalizing its elements |
| 246 |
-- because the finalization of one may call Unchecked_Deallocation |
| 247 |
-- on another and, therefore, detach it from anywhere on the list. |
| 248 |
-- Instead, we empty the list by repeatedly finalizing the first |
| 249 |
-- element (after the dummy head) and detaching it from the list. |
| 250 |
|
| 251 |
while not Is_Empty_List (Collection.Head'Unchecked_Access) loop |
| 252 |
Curr_Ptr := Collection.Head.Next; |
| 253 |
|
| 254 |
-- Synchronization: |
| 255 |
-- Write - attachment, detachment, finalization |
| 256 |
|
| 257 |
Detach_Node_From_Collection (Curr_Ptr); |
| 258 |
|
| 259 |
-- Skip the list header in order to offer proper object layout for |
| 260 |
-- finalization. |
| 261 |
|
| 262 |
Obj_Addr := Curr_Ptr.all'Address + Header_Size; |
| 263 |
|
| 264 |
-- Temporarily release the lock because the call to Finalize_Address |
| 265 |
-- may ultimately invoke Detach_Object_From_Collection. |
| 266 |
|
| 267 |
Unlock_Collection (Collection); |
| 268 |
|
| 269 |
begin |
| 270 |
Curr_Ptr.Finalize_Address (Obj_Addr); |
| 271 |
exception |
| 272 |
when Fin_Occur : others => |
| 273 |
if not Finalization_Exception_Raised then |
| 274 |
Finalization_Exception_Raised := True; |
| 275 |
Save_Occurrence (Exc_Occur, Fin_Occur); |
| 276 |
end if; |
| 277 |
end; |
| 278 |
|
| 279 |
-- Retake the lock for the next iteration |
| 280 |
|
| 281 |
Lock_Collection (Collection); |
| 282 |
end loop; |
| 283 |
|
| 284 |
Unlock_Collection (Collection); |
| 285 |
|
| 286 |
-- If one of the finalization actions raised an exception, reraise it |
| 287 |
|
| 288 |
if Finalization_Exception_Raised then |
| 289 |
Raise_From_Controlled_Operation (Exc_Occur); |
| 290 |
end if; |
| 291 |
end Finalize; |
| 292 |
|
| 293 |
--------------------- |
| 294 |
-- Finalize_Master -- |
| 295 |
--------------------- |
| 296 |
|
| 297 |
procedure Finalize_Master (Master : in out Finalization_Master) is |
| 298 |
Exc_Occur : Exception_Occurrence; |
| 299 |
Finalization_Exception_Raised : Boolean := False; |
| 300 |
Node : Master_Node_Ptr; |
| 301 |
|
| 302 |
begin |
| 303 |
Node := Master.Head; |
| 304 |
|
| 305 |
-- If exceptions are enabled, we catch them locally and reraise one |
| 306 |
-- once all the finalization actions have been completed. |
| 307 |
|
| 308 |
if Master.Exceptions_OK then |
| 309 |
while Node /= null loop |
| 310 |
begin |
| 311 |
Finalize_Object (Node.all, Node.Finalize_Address); |
| 312 |
|
| 313 |
exception |
| 314 |
when Exc : others => |
| 315 |
if not Finalization_Exception_Raised then |
| 316 |
Finalization_Exception_Raised := True; |
| 317 |
|
| 318 |
if Master.Library_Level then |
| 319 |
if Master.Extra_Info then |
| 320 |
Save_Library_Occurrence (Exc'Unrestricted_Access); |
| 321 |
else |
| 322 |
Save_Library_Occurrence (null); |
| 323 |
end if; |
| 324 |
|
| 325 |
elsif Master.Extra_Info then |
| 326 |
Save_Occurrence (Exc_Occur, Exc); |
| 327 |
end if; |
| 328 |
end if; |
| 329 |
end; |
| 330 |
|
| 331 |
Node := Node.Next; |
| 332 |
end loop; |
| 333 |
|
| 334 |
-- Otherwise we call finalization procedures without protection |
| 335 |
|
| 336 |
else |
| 337 |
while Node /= null loop |
| 338 |
Finalize_Object (Node.all, Node.Finalize_Address); |
| 339 |
|
| 340 |
Node := Node.Next; |
| 341 |
end loop; |
| 342 |
end if; |
| 343 |
|
| 344 |
Master.Head := null; |
| 345 |
|
| 346 |
-- If one of the finalization actions raised an exception, and we are |
| 347 |
-- not at library level, then reraise the exception. |
| 348 |
|
| 349 |
if Finalization_Exception_Raised and then not Master.Library_Level then |
| 350 |
if Master.Extra_Info then |
| 351 |
Raise_From_Controlled_Operation (Exc_Occur); |
| 352 |
else |
| 353 |
raise Program_Error with "finalize/adjust raised exception"; |
| 354 |
end if; |
| 355 |
end if; |
| 356 |
end Finalize_Master; |
| 357 |
|
| 358 |
--------------------- |
| 359 |
-- Finalize_Object -- |
| 360 |
--------------------- |
| 361 |
|
| 362 |
procedure Finalize_Object |
| 363 |
(Node : in out Master_Node; |
| 364 |
Finalize_Address : Finalize_Address_Ptr) |
| 365 |
is |
| 366 |
Addr : constant System.Address := Node.Object_Address; |
| 367 |
|
| 368 |
begin |
| 369 |
if Addr /= Null_Address then |
| 370 |
Node.Object_Address := Null_Address; |
| 371 |
|
| 372 |
pragma Assert (Node.Finalize_Address = Finalize_Address); |
| 373 |
Finalize_Address (Addr); |
| 374 |
end if; |
| 375 |
end Finalize_Object; |
| 376 |
|
| 377 |
---------------- |
| 378 |
-- Initialize -- |
| 379 |
---------------- |
| 380 |
|
| 381 |
procedure Initialize (Collection : in out Finalization_Collection) is |
| 382 |
begin |
| 383 |
-- The dummy head must point to itself in both directions |
| 384 |
|
| 385 |
Collection.Head.Prev := Collection.Head'Unchecked_Access; |
| 386 |
Collection.Head.Next := Collection.Head'Unchecked_Access; |
| 387 |
|
| 388 |
Initialize_RTS_Lock (Collection.Lock'Address); |
| 389 |
|
| 390 |
Collection.Finalization_Started := False; |
| 391 |
end Initialize; |
| 392 |
|
| 393 |
--------------------- |
| 394 |
-- Lock_Collection -- |
| 395 |
--------------------- |
| 396 |
|
| 397 |
procedure Lock_Collection (Collection : in out Finalization_Collection) is |
| 398 |
begin |
| 399 |
Acquire_RTS_Lock (Collection.Lock'Address); |
| 400 |
end Lock_Collection; |
| 401 |
|
| 402 |
------------------------------------- |
| 403 |
-- Suppress_Object_Finalize_At_End -- |
| 404 |
------------------------------------- |
| 405 |
|
| 406 |
procedure Suppress_Object_Finalize_At_End (Node : in out Master_Node) is |
| 407 |
begin |
| 408 |
Node.Object_Address := Null_Address; |
| 409 |
end Suppress_Object_Finalize_At_End; |
| 410 |
|
| 411 |
----------------------- |
| 412 |
-- Unlock_Collection -- |
| 413 |
----------------------- |
| 414 |
|
| 415 |
procedure Unlock_Collection (Collection : in out Finalization_Collection) is |
| 416 |
begin |
| 417 |
Release_RTS_Lock (Collection.Lock'Address); |
| 418 |
end Unlock_Collection; |
| 419 |
|
| 420 |
end System.Finalization_Primitives; |