1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . F I N A L I Z A T I O N _ M A S T E R S --
9 -- Copyright (C) 2011, 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 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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Finalization;
33 with System.Storage_Elements;
34 with System.Storage_Pools;
38 package System.Finalization_Masters is
41 -- A reference to primitive Finalize_Address. The expander generates an
42 -- implementation of this procedure for each controlled and class-wide
43 -- type. Since controlled objects are simply viewed as addresses once
44 -- allocated through a master, Finalize_Address provides a backward
45 -- indirection from an address to a type-specific context.
47 type Finalize_Address_Ptr is access procedure (Obj : System.Address);
49 -- Heterogeneous collection type structure
51 type FM_Node is private;
52 type FM_Node_Ptr is access all FM_Node;
53 pragma No_Strict_Aliasing (FM_Node_Ptr);
55 -- A reference to any derivation from Root_Storage_Pool. Since this type
56 -- may not be used to allocate objects, its storage size is zero.
58 type Any_Storage_Pool_Ptr is
59 access System.Storage_Pools.Root_Storage_Pool'Class;
60 for Any_Storage_Pool_Ptr'Storage_Size use 0;
62 -- Finalization master type structure. A unique master is associated with
63 -- each access-to-controlled or access-to-class-wide type. Masters also act
64 -- as components of subpools. By default, a master contains objects of the
65 -- same designated type but it may also accomodate heterogeneous objects.
67 type Finalization_Master is
68 new Ada.Finalization.Limited_Controlled with private;
70 -- A reference to a finalization master. Since this type may not be used
71 -- to allocate objects, its storage size is zero.
73 type Finalization_Master_Ptr is access all Finalization_Master;
74 for Finalization_Master_Ptr'Storage_Size use 0;
76 procedure Attach (N : not null FM_Node_Ptr; L : not null FM_Node_Ptr);
77 -- Prepend a node to a specific finalization master
79 procedure Delete_Finalize_Address (Obj : System.Address);
80 -- Destroy the relation pair object - Finalize_Address from the internal
83 procedure Detach (N : not null FM_Node_Ptr);
84 -- Remove a node from an arbitrary finalization master
86 overriding procedure Finalize (Master : in out Finalization_Master);
87 -- Lock the master to prevent allocations during finalization. Iterate over
88 -- the list of allocated controlled objects, finalizing each one by calling
89 -- its specific Finalize_Address. In the end, deallocate the dummy head.
91 function Finalize_Address
92 (Master : Finalization_Master) return Finalize_Address_Ptr;
93 -- Return a reference to the TSS primitive Finalize_Address associated with
96 function Finalize_Address
97 (Obj : System.Address) return Finalize_Address_Ptr;
98 -- Retrieve the Finalize_Address primitive associated with a particular
101 function Finalization_Started (Master : Finalization_Master) return Boolean;
102 -- Return the finalization status of a master
104 function Header_Offset return System.Storage_Elements.Storage_Offset;
105 -- Return the size of type FM_Node as Storage_Offset
107 function Header_Size return System.Storage_Elements.Storage_Count;
108 -- Return the size of type FM_Node as Storage_Count
110 function Is_Homogeneous (Master : Finalization_Master) return Boolean;
111 -- Return the behavior flag of a master
113 function Objects (Master : Finalization_Master) return FM_Node_Ptr;
114 -- Return the header of the doubly-linked list of controlled objects
116 procedure Print_Master (Master : Finalization_Master);
117 -- Debug routine, outputs the contents of a master
119 procedure Set_Finalize_Address
120 (Master : in out Finalization_Master;
121 Fin_Addr_Ptr : Finalize_Address_Ptr);
122 -- Set the clean up routine of a finalization master
124 procedure Set_Heterogeneous_Finalize_Address
125 (Obj : System.Address;
126 Fin_Addr_Ptr : Finalize_Address_Ptr);
127 -- Add a relation pair object - Finalize_Address to the internal hash
128 -- table. This is done in the context of allocation on a heterogeneous
129 -- finalization master where a single master services multiple anonymous
130 -- access-to-controlled types.
132 procedure Set_Is_Heterogeneous (Master : in out Finalization_Master);
133 -- Mark the master as being a heterogeneous collection of objects
136 -- Heterogeneous collection type structure
138 type FM_Node is record
139 Prev : FM_Node_Ptr := null;
140 Next : FM_Node_Ptr := null;
143 -- Finalization master type structure. A unique master is associated with
144 -- each access-to-controlled or access-to-class-wide type. Masters also act
145 -- as components of subpools. By default, a master contains objects of the
146 -- same designated type but it may also accomodate heterogeneous objects.
148 type Finalization_Master is
149 new Ada.Finalization.Limited_Controlled with
151 Is_Homogeneous : Boolean := True;
152 -- A flag which controls the behavior of the master. A value of False
153 -- denotes a heterogeneous collection.
155 Base_Pool : Any_Storage_Pool_Ptr := null;
156 -- A reference to the pool which this finalization master services. This
157 -- field is used in conjunction with the build-in-place machinery.
159 Objects : aliased FM_Node;
160 -- A doubly linked list which contains the headers of all controlled
161 -- objects allocated in a [sub]pool.
163 Finalize_Address : Finalize_Address_Ptr := null;
164 -- A reference to the routine reponsible for object finalization. This
165 -- is used only when the master is in homogeneous mode.
167 Finalization_Started : Boolean := False;
168 pragma Atomic (Finalization_Started);
169 -- A flag used to detect allocations which occur during the finalization
170 -- of a master. The allocations must raise Program_Error. This scenario
171 -- may arise in a multitask environment. The flag is atomic because it
172 -- is accessed without Lock_Task / Unlock_Task.
175 -- Since RTSfind cannot contain names of the form RE_"+", the following
176 -- routine serves as a wrapper around System.Storage_Elements."+".
178 function Add_Offset_To_Address
179 (Addr : System.Address;
180 Offset : System.Storage_Elements.Storage_Offset) return System.Address;
183 (Master : Finalization_Master) return Any_Storage_Pool_Ptr;
184 -- Return a reference to the underlying storage pool on which the master
187 overriding procedure Initialize (Master : in out Finalization_Master);
188 -- Initialize the dummy head of a finalization master
190 procedure Set_Base_Pool
191 (Master : in out Finalization_Master;
192 Pool_Ptr : Any_Storage_Pool_Ptr);
193 -- Set the underlying pool of a finalization master
195 end System.Finalization_Masters;