1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . F I N A L I Z A T I O N _ I M P L E M E N T A T I O N --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
37 with Ada
.Unchecked_Conversion
;
38 with System
.Storage_Elements
;
39 with System
.Soft_Links
;
41 package body System
.Finalization_Implementation
is
44 use System
.Finalization_Root
;
46 package SSL
renames System
.Soft_Links
;
48 package SSE
renames System
.Storage_Elements
;
49 use type SSE
.Storage_Offset
;
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 function To_Finalizable_Ptr
is
56 new Ada
.Unchecked_Conversion
(Address
, Finalizable_Ptr
);
59 new Ada
.Unchecked_Conversion
(Finalizable_Ptr
, Address
);
61 type RC_Ptr
is access all Record_Controller
;
64 new Ada
.Unchecked_Conversion
(Address
, RC_Ptr
);
66 procedure Raise_Exception_No_Defer
68 Message
: in String := "");
69 pragma Import
(Ada
, Raise_Exception_No_Defer
,
70 "ada__exceptions__raise_exception_no_defer");
71 pragma No_Return
(Raise_Exception_No_Defer
);
72 -- Raise an exception without deferring abort. Note that we have to
73 -- use this rather kludgy Ada Import interface, since this subprogram
74 -- is not available in the visible spec of Ada.Exceptions.
76 procedure Raise_From_Finalize
79 E_Occ
: Exception_Occurrence
);
80 -- Deal with an exception raised during finalization of a list. L is a
81 -- pointer to the list of element not yet finalized. From_Abort is true
82 -- if the finalization actions come from an abort rather than a normal
83 -- exit. E_Occ represents the exception being raised.
85 function RC_Offset
(T
: Ada
.Tags
.Tag
) return SSE
.Storage_Offset
;
86 pragma Import
(Ada
, RC_Offset
, "ada__tags__get_rc_offset");
88 function Parent_Size
(Obj
: Address
) return SSE
.Storage_Count
;
89 pragma Import
(Ada
, Parent_Size
, "ada__tags__parent_size");
91 function Get_RC_Dynamically
(Obj
: Address
) return Address
;
92 -- Given an the address of an object (obj) of a tagged extension with
93 -- controlled component, computes the address of the record controller
94 -- located just after the _parent field
100 procedure Adjust
(Object
: in out Record_Controller
) is
102 First_Comp
: Finalizable_Ptr
;
103 My_Offset
: constant SSE
.Storage_Offset
:=
104 Object
.My_Address
- Object
'Address;
106 procedure Ptr_Adjust
(Ptr
: in out Finalizable_Ptr
);
107 -- Subtract the offset to the pointer
109 procedure Reverse_Adjust
(P
: Finalizable_Ptr
);
110 -- Adjust the components in the reverse order in which they are stored
111 -- on the finalization list. (Adjust and Finalization are not done in
114 procedure Ptr_Adjust
(Ptr
: in out Finalizable_Ptr
) is
117 Ptr
:= To_Finalizable_Ptr
(To_Addr
(Ptr
) - My_Offset
);
121 procedure Reverse_Adjust
(P
: Finalizable_Ptr
) is
125 Reverse_Adjust
(P
.Next
);
127 Object
.F
:= P
; -- Successfully adjusted, so place in list.
131 -- Start of processing for Adjust
134 -- Adjust the components and their finalization pointers next.
135 -- We must protect against an exception in some call to Adjust, so
136 -- we keep pointing to the list of successfully adjusted components,
137 -- which can be finalized if an exception is raised.
139 First_Comp
:= Object
.F
;
140 Object
.F
:= null; -- nothing adjusted yet.
141 Ptr_Adjust
(First_Comp
); -- set addresss of first component.
142 Reverse_Adjust
(First_Comp
);
144 -- Then Adjust the controller itself
146 Object
.My_Address
:= Object
'Address;
150 -- Finalize those components that were successfully adjusted, and
151 -- propagate exception. The object itself is not yet attached to
152 -- global finalization list, so we cannot rely on the outer call
153 -- to Clean to take care of these components.
159 --------------------------
160 -- Attach_To_Final_List --
161 --------------------------
163 procedure Attach_To_Final_List
164 (L
: in out Finalizable_Ptr
;
165 Obj
: in out Finalizable
;
166 Nb_Link
: Short_Short_Integer)
169 -- Simple case: attachement to a one way list
173 L
:= Obj
'Unchecked_Access;
175 -- Dynamically allocated objects: they are attached to a doubly
176 -- linked list, so that an element can be finalized at any moment
177 -- by means of an unchecked deallocation. Attachement is
178 -- protected against multi-threaded access.
180 elsif Nb_Link
= 2 then
182 Locked_Processing
: begin
185 Obj
.Prev
:= L
.Next
.Prev
;
186 L
.Next
.Prev
:= Obj
'Unchecked_Access;
187 L
.Next
:= Obj
'Unchecked_Access;
194 end Locked_Processing
;
196 -- Attachement of arrays to the final list (used only for objects
197 -- returned by function). Obj, in this case is the last element,
198 -- but all other elements are already threaded after it. We just
199 -- attach the rest of the final list at the end of the array list.
201 elsif Nb_Link
= 3 then
203 P
: Finalizable_Ptr
:= Obj
'Unchecked_Access;
206 while P
.Next
/= null loop
211 L
:= Obj
'Unchecked_Access;
215 end Attach_To_Final_List
;
217 ---------------------
218 -- Deep_Tag_Adjust --
219 ---------------------
221 procedure Deep_Tag_Adjust
222 (L
: in out SFR
.Finalizable_Ptr
;
224 B
: Short_Short_Integer)
226 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
227 Offset
: constant SSE
.Storage_Offset
:= RC_Offset
(V
'Tag);
232 -- Has controlled components
236 Controller
:= To_RC_Ptr
(A
+ Offset
);
238 Controller
:= To_RC_Ptr
(Get_RC_Dynamically
(A
));
241 Adjust
(Controller
.all);
242 Attach_To_Final_List
(L
, Controller
.all, B
);
246 elsif V
.all in Finalizable
then
248 Attach_To_Final_List
(L
, Finalizable
(V
.all), 1);
252 ---------------------
253 -- Deep_Tag_Attach --
254 ----------------------
256 procedure Deep_Tag_Attach
257 (L
: in out SFR
.Finalizable_Ptr
;
259 B
: Short_Short_Integer)
261 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
262 Offset
: constant SSE
.Storage_Offset
:= RC_Offset
(V
'Tag);
269 Controller
:= To_RC_Ptr
(A
+ Offset
);
271 Controller
:= To_RC_Ptr
(Get_RC_Dynamically
(A
));
274 Attach_To_Final_List
(L
, Controller
.all, B
);
278 elsif V
.all in Finalizable
then
279 Attach_To_Final_List
(L
, V
.all, B
);
283 -----------------------
284 -- Deep_Tag_Finalize --
285 -----------------------
287 procedure Deep_Tag_Finalize
288 (L
: in out SFR
.Finalizable_Ptr
;
292 pragma Warnings
(Off
, L
);
294 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
295 Offset
: constant SSE
.Storage_Offset
:= RC_Offset
(V
'Tag);
300 -- Has controlled components
304 Controller
:= To_RC_Ptr
(A
+ Offset
);
306 Controller
:= To_RC_Ptr
(Get_RC_Dynamically
(A
));
310 Finalize_One
(Controller
.all);
312 Finalize
(Controller
.all);
317 elsif V
.all in Finalizable
then
319 Finalize_One
(V
.all);
324 end Deep_Tag_Finalize
;
326 -------------------------
327 -- Deep_Tag_Initialize --
328 -------------------------
330 procedure Deep_Tag_Initialize
331 (L
: in out SFR
.Finalizable_Ptr
;
333 B
: Short_Short_Integer)
335 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
336 Offset
: constant SSE
.Storage_Offset
:= RC_Offset
(V
'Tag);
341 -- This procedure should not be called if the object has no
342 -- controlled components
348 -- Has controlled components
352 Controller
:= To_RC_Ptr
(A
+ Offset
);
354 Controller
:= To_RC_Ptr
(Get_RC_Dynamically
(A
));
358 Initialize
(Controller
.all);
359 Attach_To_Final_List
(L
, Controller
.all, B
);
363 if V
.all in Finalizable
then
365 Attach_To_Final_List
(Controller
.F
, Finalizable
(Controller
.all), 1);
367 end Deep_Tag_Initialize
;
369 -----------------------------
370 -- Detach_From_Final_List --
371 -----------------------------
373 -- We know that the detach object is neither at the beginning nor at the
374 -- end of the list, thank's to the dummy First and Last Elements but the
375 -- object may not be attached at all if it is Finalize_Storage_Only
377 procedure Detach_From_Final_List
(Obj
: in out Finalizable
) is
380 -- When objects are not properly attached to a doubly linked
381 -- list do not try to detach them. The only case where it can
382 -- happen is when dealing with Finalize_Storage_Only objects
383 -- which are not always attached.
385 if Obj
.Next
/= null and then Obj
.Prev
/= null then
387 Obj
.Next
.Prev
:= Obj
.Prev
;
388 Obj
.Prev
.Next
:= Obj
.Next
;
396 end Detach_From_Final_List
;
402 procedure Finalize
(Object
: in out Limited_Record_Controller
) is
404 Finalize_List
(Object
.F
);
407 --------------------------
408 -- Finalize_Global_List --
409 --------------------------
411 procedure Finalize_Global_List
is
413 -- There are three case here:
414 -- a. the application uses tasks, in which case Finalize_Global_Tasks
415 -- will defer abortion
416 -- b. the application doesn't use tasks but uses other tasking
417 -- constructs, such as ATCs and protected objects. In this case,
418 -- the binder will call Finalize_Global_List instead of
419 -- Finalize_Global_Tasks, letting abort undeferred, and leading
420 -- to assertion failures in the GNULL
421 -- c. the application doesn't use any tasking construct in which case
422 -- deferring abort isn't necessary.
424 -- Until another solution is found to deal with case b, we need to
425 -- call abort_defer here to pass the checks, but we do not need to
426 -- undefer abortion, since Finalize_Global_List is the last procedure
427 -- called before exiting the partition.
430 Finalize_List
(Global_Final_List
);
431 end Finalize_Global_List
;
437 procedure Finalize_List
(L
: Finalizable_Ptr
) is
438 P
: Finalizable_Ptr
:= L
;
441 type Fake_Exception_Occurrence
is record
444 type Ptr
is access all Fake_Exception_Occurrence
;
446 -- Let's get the current exception before starting to finalize in
447 -- order to check if we are in the abort case if an exception is
450 function To_Ptr
is new
451 Ada
.Unchecked_Conversion
(Exception_Occurrence_Access
, Ptr
);
453 To_Ptr
(System
.Soft_Links
.Get_Current_Excep
.all).Id
;
463 when E_Occ
: others =>
464 Raise_From_Finalize
(
466 X
= Standard
'Abort_Signal'Identity,
474 procedure Finalize_One (Obj : in out Finalizable) is
476 Detach_From_Final_List (Obj);
480 when E_Occ : others => Raise_From_Finalize (null, False, E_Occ);
483 ------------------------
484 -- Get_RC_Dynamically --
485 ------------------------
487 function Get_RC_Dynamically (Obj : Address) return Address is
489 -- define a faked record controller to avoid generating
490 -- unnecessary expanded code for controlled types
492 type Faked_Record_Controller is record
493 Tag, Prec, Next : Address;
496 -- Reconstruction of a type with characteristics
497 -- comparable to the original type
499 D : constant := Storage_Unit - 1;
501 type Faked_Type_Of_Obj is record
502 Parent : SSE.Storage_Array
503 (1 .. (Parent_Size (Obj) + D) / Storage_Unit);
504 Controller : Faked_Record_Controller;
507 type Obj_Ptr is access all Faked_Type_Of_Obj;
508 function To_Obj_Ptr is new Ada.Unchecked_Conversion (Address, Obj_Ptr);
511 return To_Obj_Ptr (Obj).Controller'Address;
512 end Get_RC_Dynamically;
518 procedure Initialize (Object : in out Limited_Record_Controller) is
519 pragma Warnings (Off, Object);
525 procedure Initialize (Object : in out Record_Controller) is
527 Object.My_Address := Object'Address;
530 -------------------------
531 -- Raise_From_Finalize --
532 -------------------------
534 procedure Raise_From_Finalize
535 (L : Finalizable_Ptr;
536 From_Abort : Boolean;
537 E_Occ : Exception_Occurrence)
539 Msg : constant String := Exception_Message (E_Occ);
540 P : Finalizable_Ptr := L;
544 -- We already got an exception. We now finalize the remainder of
545 -- the list, ignoring all further exceptions.
559 -- If finalization from an Abort, then nothing to do
564 -- If no message, then add our own message saying what happened
567 Raise_Exception_No_Defer
568 (E => Program_Error'Identity,
569 Message => "exception " &
570 Exception_Name (E_Occ) &
571 " raised during finalization");
573 -- If there was a message, pass it on
576 Raise_Exception_No_Defer (Program_Error'Identity, Msg);
578 end Raise_From_Finalize;
580 -- Initialization of package, set Adafinal soft link
583 SSL.Adafinal := Finalize_Global_List'Access;
585 end System.Finalization_Implementation;