1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . S T O R A G E _ P O O L S . S U B P O O L S --
9 -- Copyright (C) 2011-2024, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 with Ada
.Finalization
;
37 with System
.Finalization_Primitives
;
38 with System
.Storage_Elements
;
40 package System
.Storage_Pools
.Subpools
is
43 type Root_Storage_Pool_With_Subpools
is abstract
44 new Root_Storage_Pool
with private;
45 pragma Preelaborable_Initialization
(Root_Storage_Pool_With_Subpools
);
46 -- The base for all implementations of Storage_Pool_With_Subpools. This
47 -- type is Limited_Controlled by derivation. To use subpools, an access
48 -- type must be associated with an implementation descending from type
49 -- Root_Storage_Pool_With_Subpools.
51 type Root_Subpool
is abstract tagged limited private;
52 pragma Preelaborable_Initialization
(Root_Subpool
);
53 -- The base for all implementations of Subpool. Objects of this type are
54 -- managed by the pool_with_subpools.
56 type Subpool_Handle
is access all Root_Subpool
'Class;
57 for Subpool_Handle
'Storage_Size use 0;
58 -- Since subpools are limited types by definition, a handle is instead used
59 -- to manage subpool abstractions.
61 overriding
procedure Allocate
62 (Pool
: in out Root_Storage_Pool_With_Subpools
;
63 Storage_Address
: out System
.Address
;
64 Size_In_Storage_Elements
: System
.Storage_Elements
.Storage_Count
;
65 Alignment
: System
.Storage_Elements
.Storage_Count
);
66 -- Allocate an object described by Size_In_Storage_Elements and Alignment
67 -- on the default subpool of Pool. Controlled types allocated through this
68 -- routine will NOT be handled properly.
70 procedure Allocate_From_Subpool
71 (Pool
: in out Root_Storage_Pool_With_Subpools
;
72 Storage_Address
: out System
.Address
;
73 Size_In_Storage_Elements
: System
.Storage_Elements
.Storage_Count
;
74 Alignment
: System
.Storage_Elements
.Storage_Count
;
75 Subpool
: not null Subpool_Handle
) is abstract;
77 -- ??? This precondition causes errors in simple tests, disabled for now
79 -- with Pre'Class => Pool_Of_Subpool (Subpool) = Pool'Access;
80 -- This routine requires implementation. Allocate an object described by
81 -- Size_In_Storage_Elements and Alignment on a subpool.
83 function Create_Subpool
84 (Pool
: in out Root_Storage_Pool_With_Subpools
)
85 return not null Subpool_Handle
is abstract;
86 -- This routine requires implementation. Create a subpool within the given
87 -- pool_with_subpools.
89 overriding
procedure Deallocate
90 (Pool
: in out Root_Storage_Pool_With_Subpools
;
91 Storage_Address
: System
.Address
;
92 Size_In_Storage_Elements
: System
.Storage_Elements
.Storage_Count
;
93 Alignment
: System
.Storage_Elements
.Storage_Count
)
96 procedure Deallocate_Subpool
97 (Pool
: in out Root_Storage_Pool_With_Subpools
;
98 Subpool
: in out Subpool_Handle
)
100 -- This precondition causes errors in simple tests, disabled for now???
101 -- with Pre'Class => Pool_Of_Subpool (Subpool) = Pool'Access;
103 -- This routine requires implementation. Reclaim the storage a particular
104 -- subpool occupies in a pool_with_subpools. This routine is called by
105 -- Ada.Unchecked_Deallocate_Subpool.
107 function Default_Subpool_For_Pool
108 (Pool
: in out Root_Storage_Pool_With_Subpools
)
109 return not null Subpool_Handle
;
110 -- Return a common subpool which is used for object allocations without a
111 -- Subpool_Handle_Name in the allocator. The default implementation of this
112 -- routine raises Program_Error.
114 function Pool_Of_Subpool
115 (Subpool
: not null Subpool_Handle
)
116 return access Root_Storage_Pool_With_Subpools
'Class;
117 -- Return the owner of the subpool
119 procedure Set_Pool_Of_Subpool
120 (Subpool
: not null Subpool_Handle
;
121 To
: in out Root_Storage_Pool_With_Subpools
'Class);
122 -- Set the owner of the subpool. This is intended to be called from
123 -- Create_Subpool or similar subpool constructors. Raises Program_Error
124 -- if the subpool already belongs to a pool.
126 overriding
function Storage_Size
127 (Pool
: Root_Storage_Pool_With_Subpools
)
128 return System
.Storage_Elements
.Storage_Count
130 (System
.Storage_Elements
.Storage_Count
'Last);
133 -- SP_Nodes are created on the heap, while collection nodes and associated
134 -- objects are created on the pool_with_subpools.
136 type Any_Storage_Pool_With_Subpools_Ptr
137 is access all Root_Storage_Pool_With_Subpools
'Class;
138 for Any_Storage_Pool_With_Subpools_Ptr
'Storage_Size use 0;
140 -- A pool controller is a special controlled object which ensures the
141 -- proper initialization and finalization of the enclosing pool.
143 type Pool_Controller
(Enclosing_Pool
: Any_Storage_Pool_With_Subpools_Ptr
)
144 is new Ada
.Finalization
.Limited_Controlled
with null record;
146 -- Subpool list types. Each pool_with_subpools contains a list of subpools.
147 -- This is an indirect doubly linked list since subpools are not supposed
148 -- to be allocatable by language design.
151 type SP_Node_Ptr
is access all SP_Node
;
153 type SP_Node
is record
154 Prev
: SP_Node_Ptr
:= null;
155 Next
: SP_Node_Ptr
:= null;
156 Subpool
: Subpool_Handle
:= null;
159 -- Root_Storage_Pool_With_Subpools internal structure. The type uses a
160 -- special controller to perform initialization and finalization actions
161 -- on itself. This is necessary because the end user of this package may
162 -- decide to override Initialize and Finalize, thus disabling the desired
165 -- Pool_With_Subpools SP_Node SP_Node SP_Node
166 -- +-->+--------------------+ +-----+ +-----+ +-----+
167 -- | | Subpools -------->| ------->| ------->| ------->
168 -- | +--------------------+ +-----+ +-----+ +-----+
169 -- | |Finalization_Started| : : : : : :
170 -- | +--------------------+
171 -- +--- Controller.Encl_Pool|
172 -- +--------------------+
176 type Root_Storage_Pool_With_Subpools
is abstract
177 new Root_Storage_Pool
with
179 Subpools
: aliased SP_Node
;
180 -- A doubly linked list of subpools
182 Finalization_Started
: Boolean := False;
183 pragma Atomic
(Finalization_Started
);
184 -- A flag which prevents the creation of new subpools while the parent
185 -- pool is being finalized. The flag needs to be atomic because it is
186 -- accessed without Lock_Task / Unlock_Task.
188 Controller
: Pool_Controller
189 (Root_Storage_Pool_With_Subpools
'Unchecked_Access);
190 -- A component which ensures that the enclosing pool is initialized and
191 -- finalized at the appropriate places.
194 -- A subpool is an abstraction layer which sits on top of a pool. It
195 -- contains links to all controlled objects allocated on a particular
198 -- Pool_With_Subpools SP_Node SP_Node SP_Node
199 -- +-->+--------------------+ +-----+ +-----+ +-----+
200 -- | | Subpools -------->| ------->| ------->| ------->
201 -- | +--------------------+ +-----+ +-----+ +-----+
202 -- | |Finalization_Started|<------ |<------- |<------- |<---
203 -- | +--------------------+ +-----+ +-----+ +-----+
204 -- +--- Controller.Encl_Pool| | nul | | + | | + |
205 -- | +--------------------+ +-----+ +--|--+ +--:--+
208 -- | Root_Subpool V |
209 -- | +-------------+ |
210 -- +-------------------------------- Owner | |
211 -- Collection nodes +-------------+ |
212 -- +-----+ +-----+<-- | Collection |
213 -- <------ |<------ | +-------------+ |
214 -- +-----+ +-----+ | Node -------+
215 -- | ------>| -----> +-------------+
216 -- +-----+ +-----+ : :
221 type Root_Subpool
is abstract tagged limited record
222 Owner
: Any_Storage_Pool_With_Subpools_Ptr
:= null;
223 -- A reference to the parent pool_with_subpools
225 Collection
: aliased Finalization_Primitives
.Finalization_Collection
;
226 -- A collection of controlled objects
228 Node
: SP_Node_Ptr
:= null;
229 -- A link to the doubly linked list node which contains the subpool.
230 -- This back pointer is used in subpool deallocation.
233 procedure Adjust_Controlled_Dereference
234 (Addr
: in out System
.Address
;
235 Storage_Size
: in out System
.Storage_Elements
.Storage_Count
;
236 Alignment
: System
.Storage_Elements
.Storage_Count
);
237 -- Given the memory attributes of a heap-allocated object that is known to
238 -- be controlled, adjust the address and size of the object to include the
239 -- hidden header inserted by the finalization machinery and its padding.
241 -- ??? Once Storage_Pools.Allocate_Any is removed, this should be renamed
244 procedure Allocate_Any_Controlled
245 (Pool
: in out Root_Storage_Pool
'Class;
246 Named_Subpool
: Subpool_Handle
;
248 Finalization_Primitives
.Finalization_Collection_Ptr
;
249 Addr
: out System
.Address
;
250 Storage_Size
: System
.Storage_Elements
.Storage_Count
;
251 Alignment
: System
.Storage_Elements
.Storage_Count
;
252 Is_Controlled
: Boolean;
253 On_Subpool
: Boolean);
254 -- Compiler interface. This version of Allocate handles all possible cases,
255 -- either on a pool or a pool_with_subpools, regardless of the controlled
256 -- status of the allocated object. Parameter usage:
258 -- * Pool - The pool associated with the access type. Pool can be any
259 -- derivation from Root_Storage_Pool, including a pool_with_subpools.
261 -- * Named_Subpool - The subpool identified by the handle name of an
262 -- allocator. If no handle name is present, the actual would be null.
264 -- * Collection - The finalization collection associated with the access
265 -- type if its designated type is controlled. If it is not, the actual
266 -- would be null. If the object is allocated on a subpool, the parameter
267 -- is updated to the collection of the subpool.
269 -- * Addr - The address of the allocated object.
271 -- * Storage_Size - The size of the allocated object.
273 -- * Alignment - The alignment of the allocated object.
275 -- * Is_Controlled - A flag which determines whether the allocated object
276 -- is controlled. When set to True, the machinery allocates more space
277 -- and returns a displaced address.
279 -- * On_Subpool - A flag which determines whether the a subpool handle
280 -- name is present at the point of allocation. This is used for error
283 procedure Deallocate_Any_Controlled
284 (Pool
: in out Root_Storage_Pool
'Class;
285 Addr
: System
.Address
;
286 Storage_Size
: System
.Storage_Elements
.Storage_Count
;
287 Alignment
: System
.Storage_Elements
.Storage_Count
;
288 Is_Controlled
: Boolean);
289 -- Compiler interface. This version of Deallocate handles all possible
290 -- cases, either from a pool or a pool_with_subpools, regardless of the
291 -- controlled status of the deallocated object. Parameter usage:
293 -- * Pool - The pool associated with the access type. Pool can be any
294 -- derivation from Root_Storage_Pool, including a pool_with_subpools.
296 -- * Addr - The address of the allocated object.
298 -- * Storage_Size - The size of the allocated object.
300 -- * Alignment - The alignment of the allocated object.
302 -- * Is_Controlled - A flag which determines whether the allocated object
303 -- is controlled. When set to True, the address must be displaced.
305 procedure Detach
(N
: not null SP_Node_Ptr
);
306 -- Unhook a subpool node from an arbitrary subpool list
308 overriding
procedure Finalize
(Controller
: in out Pool_Controller
);
309 -- Buffer routine, calls Finalize_Pool
311 procedure Finalize_Pool
(Pool
: in out Root_Storage_Pool_With_Subpools
);
312 -- Iterate over all subpools of Pool, detach them one by one and finalize
313 -- their collections. This action first detaches a controlled object from a
314 -- particular collection, then invokes its Finalize_Address primitive.
316 function Header_Size_With_Padding
317 (Alignment
: System
.Storage_Elements
.Storage_Count
)
318 return System
.Storage_Elements
.Storage_Count
;
319 -- Given an arbitrary alignment, calculate the size of the header which
320 -- precedes a controlled object as the nearest multiple rounded up of the
323 overriding
procedure Initialize
(Controller
: in out Pool_Controller
);
324 -- Buffer routine, calls Initialize_Pool
326 procedure Initialize_Pool
(Pool
: in out Root_Storage_Pool_With_Subpools
);
327 -- Setup the doubly linked list of subpools
329 procedure Print_Pool
(Pool
: Root_Storage_Pool_With_Subpools
);
330 -- Debug routine, output the contents of a pool_with_subpools
332 procedure Print_Subpool
(Subpool
: Subpool_Handle
);
333 -- Debug routine, output the contents of a subpool
335 end System
.Storage_Pools
.Subpools
;