1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- A D A . F I N A L I Z A T I O N . H E A P _ M A N A G E M E N T --
9 -- Copyright (C) 2008-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.Exceptions; use Ada.Exceptions;
33 with Ada.Unchecked_Conversion;
35 with System; use System;
36 with System.Address_Image;
37 with System.IO; use System.IO;
38 with System.Soft_Links; use System.Soft_Links;
39 with System.Storage_Elements; use System.Storage_Elements;
40 with System.Storage_Pools; use System.Storage_Pools;
42 package body Ada.Finalization.Heap_Management is
44 Header_Size : constant Storage_Count := Node'Size / Storage_Unit;
45 -- Size of the header in bytes. Added to Storage_Size requested by
46 -- Allocate/Deallocate to determine the Storage_Size passed to the
49 Header_Offset : constant Storage_Offset := Header_Size;
50 -- Offset from the header to the actual object. Used to get from the
51 -- address of a header to the address of the actual object, and vice-versa.
53 function Address_To_Node_Ptr is
54 new Ada.Unchecked_Conversion (Address, Node_Ptr);
56 procedure Attach (N : Node_Ptr; L : Node_Ptr);
57 -- Prepend a node to a list
59 procedure Detach (N : Node_Ptr);
60 -- Unhook a node from an arbitrary list
62 ---------------------------
63 -- Add_Offset_To_Address --
64 ---------------------------
66 function Add_Offset_To_Address
67 (Addr : System.Address;
68 Offset : System.Storage_Elements.Storage_Offset) return System.Address
71 return System.Storage_Elements."+" (Addr, Offset);
72 end Add_Offset_To_Address;
79 (Collection : in out Finalization_Collection;
80 Addr : out System.Address;
81 Storage_Size : System.Storage_Elements.Storage_Count;
82 Alignment : System.Storage_Elements.Storage_Count;
83 Needs_Header : Boolean := True)
86 -- Allocation of an object with controlled parts
90 -- Do not allow the allocation of controlled objects while the
91 -- associated collection is being finalized.
93 if Collection.Finalization_Started then
94 raise Program_Error with "allocation after finalization started";
102 -- Use the underlying pool to allocate enough space for the object
103 -- and the list header. The returned address points to the list
104 -- header. If locking is necessary, it will be done by the
108 (Collection.Base_Pool.all,
110 Storage_Size + Header_Size,
113 -- Map the allocated memory into a Node record. This converts the
114 -- top of the allocated bits into a list header.
116 N_Ptr := Address_To_Node_Ptr (N_Addr);
117 Attach (N_Ptr, Collection.Objects'Unchecked_Access);
119 -- Move the address from Prev to the start of the object. This
120 -- operation effectively hides the list header.
122 Addr := N_Addr + Header_Offset;
125 -- Allocation of a non-controlled object
129 (Collection.Base_Pool.all,
140 procedure Attach (N : Node_Ptr; L : Node_Ptr) is
162 (Collection : Finalization_Collection) return Any_Storage_Pool_Ptr
165 return Collection.Base_Pool;
173 (Collection : in out Finalization_Collection;
174 Addr : System.Address;
175 Storage_Size : System.Storage_Elements.Storage_Count;
176 Alignment : System.Storage_Elements.Storage_Count;
177 Has_Header : Boolean := True)
180 -- Deallocation of an object with controlled parts
188 -- Move the address from the object to the beginning of the list
191 N_Addr := Addr - Header_Offset;
193 -- Converts the bits preceding the object into a list header
195 N_Ptr := Address_To_Node_Ptr (N_Addr);
198 -- Use the underlying pool to destroy the object along with the
202 (Collection.Base_Pool.all,
204 Storage_Size + Header_Size,
208 -- Deallocation of a non-controlled object
212 (Collection.Base_Pool.all,
223 procedure Detach (N : Node_Ptr) is
228 and then N.Next /= null
230 N.Prev.Next := N.Next;
231 N.Next.Prev := N.Prev;
248 overriding procedure Finalize
249 (Collection : in out Finalization_Collection)
251 function Node_Ptr_To_Address (N : Node_Ptr) return Address;
252 -- Not the reverse of Address_To_Node_Ptr. Return the address of the
253 -- object following the list header.
255 -------------------------
256 -- Node_Ptr_To_Address --
257 -------------------------
259 function Node_Ptr_To_Address (N : Node_Ptr) return Address is
261 return N.all'Address + Header_Offset;
262 end Node_Ptr_To_Address;
264 Curr_Ptr : Node_Ptr := Collection.Objects.Next; -- skip dummy head
265 Ex_Occur : Exception_Occurrence;
266 Raised : Boolean := False;
268 -- Start of processing for Finalize
271 -- Set Finalization_Started to prevent any allocations of objects with
272 -- controlled parts during finalization. The associated access type is
273 -- about to go out of scope; Finalization_Started is never again
276 Collection.Finalization_Started := True;
278 -- Go through the Objects list, and finalize each one. There is no need
279 -- to detach items from the list, because the whole collection is about
282 while Curr_Ptr /= Collection.Objects'Unchecked_Access loop
283 -- ??? Kludge: Don't do anything until the proper place to set
284 -- primitive Finalize_Address has been determined.
286 if Collection.Finalize_Address /= null then
288 Collection.Finalize_Address (Node_Ptr_To_Address (Curr_Ptr));
291 when Fin_Except : others =>
294 Save_Occurrence (Ex_Occur, Fin_Except);
299 Curr_Ptr := Curr_Ptr.Next;
302 -- If the finalization of a particular node raised an exception, reraise
303 -- it after the remainder of the list has been finalized.
306 Reraise_Occurrence (Ex_Occur);
314 overriding procedure Initialize
315 (Collection : in out Finalization_Collection)
318 -- The dummy head must point to itself in both directions
320 Collection.Objects.Next := Collection.Objects'Unchecked_Access;
321 Collection.Objects.Prev := Collection.Objects'Unchecked_Access;
328 procedure pcol (Collection : Finalization_Collection) is
329 Head : constant Node_Ptr := Collection.Objects'Unrestricted_Access;
330 -- "Unrestricted", because we're evilly getting access-to-variable of a
331 -- constant! OK for debugging code.
333 Head_Seen : Boolean := False;
337 -- Output the basic contents of the collection
339 -- Collection: 0x123456789
340 -- Base_Pool : null <or> 0x123456789
341 -- Fin_Addr : null <or> 0x123456789
342 -- Fin_Start : TRUE <or> FALSE
344 Put ("Collection: ");
345 Put_Line (Address_Image (Collection'Address));
347 Put ("Base_Pool : ");
348 if Collection.Base_Pool = null then
351 Put_Line (Address_Image (Collection.Base_Pool'Address));
355 if Collection.Finalize_Address = null then
358 Put_Line (Address_Image (Collection.Finalize_Address'Address));
361 Put ("Fin_Start : ");
362 Put_Line (Collection.Finalization_Started'Img);
364 -- Output all chained elements. The format is the following:
366 -- ^ <or> ? <or> null
367 -- |Header: 0x123456789 (dummy head)
368 -- | Prev: 0x123456789
369 -- | Next: 0x123456789
372 -- ^ - the current element points back to the correct element
373 -- ? - the current element points back to an erroneous element
374 -- n - the current element points back to null
376 -- Header - the address of the list header
377 -- Prev - the address of the list header which the current element
379 -- Next - the address of the list header which the current element
381 -- (dummy head) - present if dummy head
385 while N_Ptr /= null loop -- Should never be null; we being defensive
388 -- We see the head initially; we want to exit when we see the head a
397 -- The current element is null. This should never happen since the
400 if N_Ptr.Prev = null then
401 Put_Line ("null (ERROR)");
403 -- The current element points back to the correct element
405 elsif N_Ptr.Prev.Next = N_Ptr then
408 -- The current element points to an erroneous element
411 Put_Line ("? (ERROR)");
414 -- Output the header and fields
417 Put (Address_Image (N_Ptr.all'Address));
419 -- Detect the dummy head
422 Put_Line (" (dummy head)");
428 if N_Ptr.Prev = null then
431 Put_Line (Address_Image (N_Ptr.Prev.all'Address));
435 if N_Ptr.Next = null then
438 Put_Line (Address_Image (N_Ptr.Next.all'Address));
445 ------------------------------
446 -- Set_Finalize_Address_Ptr --
447 ------------------------------
449 procedure Set_Finalize_Address_Ptr
450 (Collection : in out Finalization_Collection;
451 Proc_Ptr : Finalize_Address_Ptr)
454 Collection.Finalize_Address := Proc_Ptr;
455 end Set_Finalize_Address_Ptr;
457 --------------------------
458 -- Set_Storage_Pool_Ptr --
459 --------------------------
461 procedure Set_Storage_Pool_Ptr
462 (Collection : in out Finalization_Collection;
463 Pool_Ptr : Any_Storage_Pool_Ptr)
466 Collection.Base_Pool := Pool_Ptr;
467 end Set_Storage_Pool_Ptr;
469 end Ada.Finalization.Heap_Management;