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 --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
37 with System
.Soft_Links
;
39 with Unchecked_Conversion
;
40 with System
.Restrictions
;
42 package body System
.Finalization_Implementation
is
45 use System
.Finalization_Root
;
47 package SSL
renames System
.Soft_Links
;
49 use type SSE
.Storage_Offset
;
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 type RC_Ptr
is access all Record_Controller
;
58 new Unchecked_Conversion
(Address
, RC_Ptr
);
60 procedure Raise_Exception_No_Defer
62 Message
: String := "");
63 pragma Import
(Ada
, Raise_Exception_No_Defer
,
64 "ada__exceptions__raise_exception_no_defer");
65 pragma No_Return
(Raise_Exception_No_Defer
);
66 -- Raise an exception without deferring abort. Note that we have to
67 -- use this rather kludgy Ada Import interface, since this subprogram
68 -- is not available in the visible spec of Ada.Exceptions.
70 procedure Raise_From_Finalize
73 E_Occ
: Exception_Occurrence
);
74 -- Deal with an exception raised during finalization of a list. L is a
75 -- pointer to the list of element not yet finalized. From_Abort is true
76 -- if the finalization actions come from an abort rather than a normal
77 -- exit. E_Occ represents the exception being raised.
79 function RC_Offset
(T
: Ada
.Tags
.Tag
) return SSE
.Storage_Offset
;
80 pragma Import
(Ada
, RC_Offset
, "ada__tags__get_rc_offset");
82 function Parent_Size
(Obj
: Address
; T
: Ada
.Tags
.Tag
)
83 return SSE
.Storage_Count
;
84 pragma Import
(Ada
, Parent_Size
, "ada__tags__parent_size");
86 function Get_Deep_Controller
(Obj
: System
.Address
) return RC_Ptr
;
87 -- Given the address (obj) of a tagged object, return a
88 -- pointer to the record controller of this object.
94 procedure Adjust
(Object
: in out Record_Controller
) is
96 First_Comp
: Finalizable_Ptr
;
97 My_Offset
: constant SSE
.Storage_Offset
:=
98 Object
.My_Address
- Object
'Address;
100 procedure Ptr_Adjust
(Ptr
: in out Finalizable_Ptr
);
101 -- Subtract the offset to the pointer
103 procedure Reverse_Adjust
(P
: Finalizable_Ptr
);
104 -- Ajust the components in the reverse order in which they are stored
105 -- on the finalization list. (Adjust and Finalization are not done in
112 procedure Ptr_Adjust
(Ptr
: in out Finalizable_Ptr
) is
115 Ptr
:= To_Finalizable_Ptr
(To_Addr
(Ptr
) - My_Offset
);
123 procedure Reverse_Adjust
(P
: Finalizable_Ptr
) is
127 Reverse_Adjust
(P
.Next
);
129 Object
.F
:= P
; -- Successfully adjusted, so place in list.
133 -- Start of processing for Adjust
136 -- Adjust the components and their finalization pointers next. We must
137 -- protect against an exception in some call to Adjust, so we keep
138 -- pointing to the list of successfully adjusted components, which can
139 -- be finalized if an exception is raised.
141 First_Comp
:= Object
.F
;
142 Object
.F
:= null; -- nothing adjusted yet.
143 Ptr_Adjust
(First_Comp
); -- set addresss of first component.
144 Reverse_Adjust
(First_Comp
);
146 -- Then Adjust the controller itself
148 Object
.My_Address
:= Object
'Address;
152 -- Finalize those components that were successfully adjusted, and
153 -- propagate exception. The object itself is not yet attached to
154 -- global finalization list, so we cannot rely on the outer call to
155 -- Clean to take care of these components.
161 --------------------------
162 -- Attach_To_Final_List --
163 --------------------------
165 procedure Attach_To_Final_List
166 (L
: in out Finalizable_Ptr
;
167 Obj
: in out Finalizable
;
168 Nb_Link
: Short_Short_Integer)
171 -- Simple case: attachement to a one way list
175 L
:= Obj
'Unchecked_Access;
177 -- Dynamically allocated objects: they are attached to a doubly linked
178 -- list, so that an element can be finalized at any moment by means of
179 -- an unchecked deallocation. Attachement is protected against
180 -- multi-threaded access.
182 elsif Nb_Link
= 2 then
184 -- Raise Program_Error if we're trying to allocate an object in a
185 -- collection whose finalization has already started.
187 if L
= Collection_Finalization_Started
then
188 raise Program_Error
with
189 "allocation after collection finalization started";
192 Locked_Processing
: begin
195 Obj
.Prev
:= L
.Next
.Prev
;
196 L
.Next
.Prev
:= Obj
'Unchecked_Access;
197 L
.Next
:= Obj
'Unchecked_Access;
204 end Locked_Processing
;
206 -- Attachement of arrays to the final list (used only for objects
207 -- returned by function). Obj, in this case is the last element,
208 -- but all other elements are already threaded after it. We just
209 -- attach the rest of the final list at the end of the array list.
211 elsif Nb_Link
= 3 then
213 P
: Finalizable_Ptr
:= Obj
'Unchecked_Access;
216 while P
.Next
/= null loop
221 L
:= Obj
'Unchecked_Access;
224 -- Make the object completely unattached (case of a library-level,
225 -- Finalize_Storage_Only object).
227 elsif Nb_Link
= 4 then
231 end Attach_To_Final_List
;
233 ---------------------
234 -- Deep_Tag_Adjust --
235 ---------------------
237 procedure Deep_Tag_Adjust
238 (L
: in out SFR
.Finalizable_Ptr
;
240 B
: Short_Short_Integer)
242 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
243 Controller
: constant RC_Ptr
:= Get_Deep_Controller
(A
);
246 if Controller
/= null then
247 Adjust
(Controller
.all);
248 Attach_To_Final_List
(L
, Controller
.all, B
);
253 if V
.all in Finalizable
then
255 Attach_To_Final_List
(L
, Finalizable
(V
.all), 1);
259 ---------------------
260 -- Deep_Tag_Attach --
261 ----------------------
263 procedure Deep_Tag_Attach
264 (L
: in out SFR
.Finalizable_Ptr
;
266 B
: Short_Short_Integer)
268 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
269 Controller
: constant RC_Ptr
:= Get_Deep_Controller
(A
);
272 if Controller
/= null then
273 Attach_To_Final_List
(L
, Controller
.all, B
);
278 if 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 Controller
: constant RC_Ptr
:= Get_Deep_Controller
(A
);
298 if Controller
/= null then
300 Finalize_One
(Controller
.all);
302 Finalize
(Controller
.all);
308 if V
.all in Finalizable
then
310 Finalize_One
(V
.all);
315 end Deep_Tag_Finalize
;
317 -------------------------
318 -- Deep_Tag_Initialize --
319 -------------------------
321 procedure Deep_Tag_Initialize
322 (L
: in out SFR
.Finalizable_Ptr
;
324 B
: Short_Short_Integer)
326 V
: constant SFR
.Finalizable_Ptr
:= To_Finalizable_Ptr
(A
);
327 Controller
: constant RC_Ptr
:= Get_Deep_Controller
(A
);
330 -- This procedure should not be called if the object has no
331 -- controlled components
333 if Controller
= null then
336 -- Has controlled components
339 Initialize
(Controller
.all);
340 Attach_To_Final_List
(L
, Controller
.all, B
);
345 if V
.all in Finalizable
then
347 Attach_To_Final_List
(Controller
.F
, Finalizable
(Controller
.all), 1);
349 end Deep_Tag_Initialize
;
351 -----------------------------
352 -- Detach_From_Final_List --
353 -----------------------------
355 -- We know that the detach object is neither at the beginning nor at the
356 -- end of the list, thank's to the dummy First and Last Elements but the
357 -- object may not be attached at all if it is Finalize_Storage_Only
359 procedure Detach_From_Final_List
(Obj
: in out Finalizable
) is
362 -- When objects are not properly attached to a doubly linked list do
363 -- not try to detach them. The only case where it can happen is when
364 -- dealing with Finalize_Storage_Only objects which are not always
365 -- attached to the finalization list.
367 if Obj
.Next
/= null and then Obj
.Prev
/= null then
369 Obj
.Next
.Prev
:= Obj
.Prev
;
370 Obj
.Prev
.Next
:= Obj
.Next
;
378 end Detach_From_Final_List
;
384 procedure Finalize
(Object
: in out Limited_Record_Controller
) is
386 Finalize_List
(Object
.F
);
389 --------------------------
390 -- Finalize_Global_List --
391 --------------------------
393 procedure Finalize_Global_List
is
395 -- There are three case here:
397 -- a. the application uses tasks, in which case Finalize_Global_Tasks
400 -- b. the application doesn't use tasks but uses other tasking
401 -- constructs, such as ATCs and protected objects. In this case,
402 -- the binder will call Finalize_Global_List instead of
403 -- Finalize_Global_Tasks, letting abort undeferred, and leading
404 -- to assertion failures in the GNULL
406 -- c. the application doesn't use any tasking construct in which case
407 -- deferring abort isn't necessary.
409 -- Until another solution is found to deal with case b, we need to
410 -- call abort_defer here to pass the checks, but we do not need to
411 -- undefer abort, since Finalize_Global_List is the last procedure
412 -- called before exiting the partition.
415 Finalize_List
(Global_Final_List
);
416 end Finalize_Global_List
;
422 procedure Finalize_List
(L
: Finalizable_Ptr
) is
423 P
: Finalizable_Ptr
:= L
;
426 type Fake_Exception_Occurence
is record
429 type Ptr
is access all Fake_Exception_Occurence
;
431 function To_Ptr
is new
432 Unchecked_Conversion
(Exception_Occurrence_Access
, Ptr
);
434 X
: Exception_Id
:= Null_Id
;
437 -- If abort is allowed, we get the current exception before starting
438 -- to finalize in order to check if we are in the abort case if an
439 -- exception is raised. When abort is not allowed, avoid accessing the
440 -- current exception since this can be a pretty costly operation in
441 -- programs using controlled types heavily.
443 if System
.Restrictions
.Abort_Allowed
then
444 X
:= To_Ptr
(System
.Soft_Links
.Get_Current_Excep
.all).Id
;
454 when E_Occ
: others =>
455 Raise_From_Finalize
(
457 X
= Standard
'Abort_Signal'Identity,
465 procedure Finalize_One (Obj : in out Finalizable) is
467 Detach_From_Final_List (Obj);
470 when E_Occ : others => Raise_From_Finalize (null, False, E_Occ);
473 -------------------------
474 -- Get_Deep_Controller --
475 -------------------------
477 function Get_Deep_Controller (Obj : System.Address) return RC_Ptr is
478 The_Tag : Ada.Tags.Tag := To_Finalizable_Ptr (Obj)'Tag;
479 Offset : SSE.Storage_Offset := RC_Offset (The_Tag);
482 -- Fetch the controller from the Parent or above if necessary
483 -- when there are no controller at this level
485 while Offset = -2 loop
486 The_Tag := Ada.Tags.Parent_Tag (The_Tag);
487 Offset := RC_Offset (The_Tag);
490 -- No Controlled component case
495 -- The _controller Offset is known statically
497 elsif Offset > 0 then
498 return To_RC_Ptr (Obj + Offset);
500 -- At this stage, we know that the controller is part of the
501 -- ancestor corresponding to the tag "The_Tag" and that its parent
502 -- is variable sized. We assume that the _controller is the first
503 -- compoment right after the parent.
505 -- ??? note that it may not be true if there are new discriminants
510 -- define a faked record controller to avoid generating
511 -- unnecessary expanded code for controlled types
513 type Faked_Record_Controller is record
514 Tag, Prec, Next : Address;
517 -- Reconstruction of a type with characteristics
518 -- comparable to the original type
520 D : constant := SSE.Storage_Offset (Storage_Unit - 1);
522 type Parent_Type is new SSE.Storage_Array
523 (1 .. (Parent_Size (Obj, The_Tag) + D) /
524 SSE.Storage_Offset (Storage_Unit));
525 for Parent_Type'Alignment use Address'Alignment;
527 type Faked_Type_Of_Obj is record
528 Parent : Parent_Type;
529 Controller : Faked_Record_Controller;
532 type Obj_Ptr is access all Faked_Type_Of_Obj;
533 function To_Obj_Ptr is
534 new Unchecked_Conversion (Address, Obj_Ptr);
537 return To_RC_Ptr (To_Obj_Ptr (Obj).Controller'Address);
540 end Get_Deep_Controller;
546 procedure Initialize (Object : in out Limited_Record_Controller) is
547 pragma Warnings (Off, Object);
552 procedure Initialize (Object : in out Record_Controller) is
554 Object.My_Address := Object'Address;
557 -------------------------
558 -- Raise_From_Finalize --
559 -------------------------
561 procedure Raise_From_Finalize
562 (L : Finalizable_Ptr;
563 From_Abort : Boolean;
564 E_Occ : Exception_Occurrence)
566 Msg : constant String := Exception_Message (E_Occ);
567 P : Finalizable_Ptr := L;
571 -- We already got an exception. We now finalize the remainder of
572 -- the list, ignoring all further exceptions.
586 -- If finalization from an Abort, then nothing to do
591 -- If no message, then add our own message saying what happened
594 Raise_Exception_No_Defer
595 (E => Program_Error'Identity,
596 Message => "exception " &
597 Exception_Name (E_Occ) &
598 " raised during finalization");
600 -- If there was a message, pass it on
603 Raise_Exception_No_Defer (Program_Error'Identity, Msg);
605 end Raise_From_Finalize;
607 -- Initialization of package, set Adafinal soft link
610 SSL.Finalize_Global_List := Finalize_Global_List'Access;
612 end System.Finalization_Implementation;