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 _ M A S T E R S          --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2011-2020, 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
32with Ada.Finalization;
33with System.Storage_Elements;
34with System.Storage_Pools;
35
36pragma Compiler_Unit_Warning;
37
38package System.Finalization_Masters is
39   pragma Preelaborate;
40
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.
46
47   type Finalize_Address_Ptr is access procedure (Obj : System.Address);
48
49   --  Heterogeneous collection type structure
50
51   type FM_Node is private;
52   type FM_Node_Ptr is access all FM_Node;
53   pragma No_Strict_Aliasing (FM_Node_Ptr);
54
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.
57
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;
61
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 accommodate heterogeneous objects.
66
67   type Finalization_Master is
68     new Ada.Finalization.Limited_Controlled with private;
69
70   --  A reference to a finalization master. Since this type may not be used
71   --  to allocate objects, its storage size is zero.
72
73   type Finalization_Master_Ptr is access all Finalization_Master;
74   for Finalization_Master_Ptr'Storage_Size use 0;
75
76   procedure Attach (N : not null FM_Node_Ptr; L : not null FM_Node_Ptr);
77   --  Compiler interface, do not call from withing the run-time. Prepend a
78   --  node to a specific finalization master.
79
80   procedure Attach_Unprotected
81     (N : not null FM_Node_Ptr;
82      L : not null FM_Node_Ptr);
83   --  Prepend a node to a specific finalization master
84
85   procedure Delete_Finalize_Address_Unprotected (Obj : System.Address);
86   --  Destroy the relation pair object - Finalize_Address from the internal
87   --  hash table.
88
89   procedure Detach_Unprotected (N : not null FM_Node_Ptr);
90   --  Remove a node from an arbitrary finalization master
91
92   overriding procedure Finalize (Master : in out Finalization_Master);
93   --  Lock the master to prevent allocations during finalization. Iterate over
94   --  the list of allocated controlled objects, finalizing each one by calling
95   --  its specific Finalize_Address. In the end, deallocate the dummy head.
96
97   function Finalize_Address
98     (Master : Finalization_Master) return Finalize_Address_Ptr;
99   --  Return a reference to the TSS primitive Finalize_Address associated with
100   --  a master.
101
102   function Finalize_Address_Unprotected
103     (Obj : System.Address) return Finalize_Address_Ptr;
104   --  Retrieve the Finalize_Address primitive associated with a particular
105   --  object.
106
107   function Finalization_Started (Master : Finalization_Master) return Boolean;
108   --  Return the finalization status of a master
109
110   function Header_Size return System.Storage_Elements.Storage_Count;
111   --  Return the size of type FM_Node as Storage_Count
112
113   function Is_Homogeneous (Master : Finalization_Master) return Boolean;
114   --  Return the behavior flag of a master
115
116   function Objects (Master : Finalization_Master) return FM_Node_Ptr;
117   --  Return the header of the doubly-linked list of controlled objects
118
119   procedure Print_Master (Master : Finalization_Master);
120   --  Debug routine, outputs the contents of a master
121
122   procedure Set_Finalize_Address
123     (Master       : in out Finalization_Master;
124      Fin_Addr_Ptr : Finalize_Address_Ptr);
125   --  Compiler interface, do not call from within the run-time. Set the clean
126   --  up routine of a finalization master
127
128   procedure Set_Finalize_Address_Unprotected
129     (Master       : in out Finalization_Master;
130      Fin_Addr_Ptr : Finalize_Address_Ptr);
131   --  Set the clean up routine of a finalization master
132
133   procedure Set_Heterogeneous_Finalize_Address_Unprotected
134     (Obj          : System.Address;
135      Fin_Addr_Ptr : Finalize_Address_Ptr);
136   --  Add a relation pair object - Finalize_Address to the internal hash
137   --  table. This is done in the context of allocation on a heterogeneous
138   --  finalization master where a single master services multiple anonymous
139   --  access-to-controlled types.
140
141   procedure Set_Is_Heterogeneous (Master : in out Finalization_Master);
142   --  Mark the master as being a heterogeneous collection of objects
143
144private
145   --  Heterogeneous collection type structure
146
147   type FM_Node is record
148      Prev : FM_Node_Ptr := null;
149      Next : FM_Node_Ptr := null;
150   end record;
151
152   --  Finalization master type structure. A unique master is associated with
153   --  each access-to-controlled or access-to-class-wide type. Masters also act
154   --  as components of subpools. By default, a master contains objects of the
155   --  same designated type but it may also accommodate heterogeneous objects.
156
157   type Finalization_Master is
158     new Ada.Finalization.Limited_Controlled with
159   record
160      Is_Homogeneous : Boolean := True;
161      --  A flag which controls the behavior of the master. A value of False
162      --  denotes a heterogeneous collection.
163
164      Base_Pool : Any_Storage_Pool_Ptr := null;
165      --  A reference to the pool which this finalization master services. This
166      --  field is used in conjunction with the build-in-place machinery.
167
168      Objects : aliased FM_Node;
169      --  A doubly linked list which contains the headers of all controlled
170      --  objects allocated in a [sub]pool.
171
172      Finalize_Address : Finalize_Address_Ptr := null;
173      --  A reference to the routine reponsible for object finalization. This
174      --  is used only when the master is in homogeneous mode.
175
176      Finalization_Started : Boolean := False;
177      --  A flag used to detect allocations which occur during the finalization
178      --  of a master. The allocations must raise Program_Error. This scenario
179      --  may arise in a multitask environment.
180   end record;
181
182   --  Since RTSfind cannot contain names of the form RE_"+", the following
183   --  routine serves as a wrapper around System.Storage_Elements."+".
184
185   function Add_Offset_To_Address
186     (Addr   : System.Address;
187      Offset : System.Storage_Elements.Storage_Offset) return System.Address;
188
189   function Base_Pool
190     (Master : Finalization_Master) return Any_Storage_Pool_Ptr;
191   --  Return a reference to the underlying storage pool on which the master
192   --  operates.
193
194   overriding procedure Initialize (Master : in out Finalization_Master);
195   --  Initialize the dummy head of a finalization master
196
197   procedure Set_Base_Pool
198     (Master   : in out Finalization_Master;
199      Pool_Ptr : Any_Storage_Pool_Ptr);
200   --  Set the underlying pool of a finalization master
201
202end System.Finalization_Masters;
203