Debian Codegraph @

gcc/ada/libgnat/s-stposu.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011-2025, 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
32 with Ada.Exceptions; use Ada.Exceptions;
33
34 with System.Address_Image;
35 with System.IO; use System.IO;
36 with System.Soft_Links; use System.Soft_Links;
37 with System.Storage_Elements; use System.Storage_Elements;
38
39 with System.Storage_Pools.Subpools.Finalization;
40 use System.Storage_Pools.Subpools.Finalization;
41
42 package body System.Storage_Pools.Subpools is
43
44 procedure Attach (N : not null SP_Node_Ptr; L : not null SP_Node_Ptr);
45 -- Attach a subpool node to a pool
46
47 -----------------------------------
48 -- Adjust_Controlled_Dereference --
49 -----------------------------------
50
51 procedure Adjust_Controlled_Dereference
52 (Addr : in out System.Address;
53 Storage_Size : in out System.Storage_Elements.Storage_Count;
54 Alignment : System.Storage_Elements.Storage_Count)
55 is
56 Header_And_Padding : constant Storage_Offset :=
57 Header_Size_With_Padding (Alignment);
58 begin
59 -- Expose the header and its padding by shifting the address from the
60 -- start of the object to the beginning of the padding.
61
62 Addr := Addr - Header_And_Padding;
63
64 -- Update the size to include the header and its padding
65
66 Storage_Size := Storage_Size + Header_And_Padding;
67 end Adjust_Controlled_Dereference;
68
69 --------------
70 -- Allocate --
71 --------------
72
73 overriding procedure Allocate
74 (Pool : in out Root_Storage_Pool_With_Subpools;
75 Storage_Address : out System.Address;
76 Size_In_Storage_Elements : System.Storage_Elements.Storage_Count;
77 Alignment : System.Storage_Elements.Storage_Count)
78 is
79 begin
80 -- Dispatch to the user-defined implementations of Allocate_From_Subpool
81 -- and Default_Subpool_For_Pool.
82
83 Allocate_From_Subpool
84 (Root_Storage_Pool_With_Subpools'Class (Pool),
85 Storage_Address,
86 Size_In_Storage_Elements,
87 Alignment,
88 Default_Subpool_For_Pool
89 (Root_Storage_Pool_With_Subpools'Class (Pool)));
90 end Allocate;
91
92 -----------------------------
93 -- Allocate_Any_Controlled --
94 -----------------------------
95
96 procedure Allocate_Any_Controlled
97 (Pool : in out Root_Storage_Pool'Class;
98 Named_Subpool : Subpool_Handle;
99 Collection : in out
100 Finalization_Primitives.Finalization_Collection_Ptr;
101 Addr : out System.Address;
102 Storage_Size : System.Storage_Elements.Storage_Count;
103 Alignment : System.Storage_Elements.Storage_Count;
104 Is_Controlled : Boolean;
105 On_Subpool : Boolean)
106 is
107 use type System.Finalization_Primitives.Finalization_Collection_Ptr;
108
109 Is_Subpool_Allocation : constant Boolean :=
110 Pool in Root_Storage_Pool_With_Subpools'Class;
111
112 N_Addr : Address;
113 N_Alignment : Storage_Count;
114 N_Size : Storage_Count;
115 Subpool : Subpool_Handle;
116
117 Header_And_Padding : Storage_Offset;
118 -- This offset includes the size of a header plus an additional padding
119 -- due to a larger alignment of the object.
120
121 begin
122 -- Step 1: Pool-related runtime checks
123
124 -- Allocation on a pool_with_subpools. In this scenario there is a
125 -- collection for each subpool. That of the access type is ignored.
126
127 if Is_Subpool_Allocation then
128
129 -- Case of an allocation without a Subpool_Handle. Dispatch to the
130 -- implementation of Default_Subpool_For_Pool.
131
132 if Named_Subpool = null then
133 Subpool :=
134 Default_Subpool_For_Pool
135 (Root_Storage_Pool_With_Subpools'Class (Pool));
136
137 -- Allocation with a Subpool_Handle
138
139 else
140 Subpool := Named_Subpool;
141 end if;
142
143 -- Ensure proper ownership and chaining of the subpool
144
145 if Subpool.Owner /=
146 Root_Storage_Pool_With_Subpools'Class (Pool)'Unchecked_Access
147 or else Subpool.Node = null
148 or else Subpool.Node.Prev = null
149 or else Subpool.Node.Next = null
150 then
151 raise Program_Error with "incorrect owner of subpool";
152 end if;
153
154 Collection := Subpool.Collection'Unchecked_Access;
155
156 -- Allocation on a simple pool. In this scenario there is a collection
157 -- for each access-to-controlled type. No context subpool is allowed.
158
159 else
160 -- If the collection is missing, then the expansion of the access
161 -- type has failed to create one. This is a compiler bug.
162
163 pragma Assert
164 (Collection /= null, "no collection in pool allocation");
165
166 -- If a subpool is present, then this is the result of erroneous
167 -- allocator expansion. This is not a serious error, but it should
168 -- still be detected.
169
170 if Named_Subpool /= null then
171 raise Program_Error
172 with "subpool not required in pool allocation";
173 end if;
174
175 -- If the allocation is intended to be on a subpool, but the access
176 -- type's pool does not support subpools, then this is the result of
177 -- incorrect end-user code.
178
179 if On_Subpool then
180 raise Program_Error
181 with "pool of access type does not support subpools";
182 end if;
183 end if;
184
185 -- Step 2: Size and alignment calculations
186
187 -- Allocation of a descendant from [Limited_]Controlled, a class-wide
188 -- object or a record with controlled components.
189
190 if Is_Controlled then
191 -- The size must account for the hidden header before the object.
192 -- Account for possible padding space before the header due to a
193 -- larger alignment of the object.
194
195 Header_And_Padding := Header_Size_With_Padding (Alignment);
196
197 N_Size := Storage_Size + Header_And_Padding;
198
199 -- The alignment must account for the hidden header before the object
200
201 N_Alignment :=
202 System.Storage_Elements.Storage_Count'Max
203 (Alignment, System.Finalization_Primitives.Header_Alignment);
204
205 -- Non-controlled allocation
206
207 else
208 N_Size := Storage_Size;
209 N_Alignment := Alignment;
210 end if;
211
212 -- Step 3: Allocation of object
213
214 -- For descendants of Root_Storage_Pool_With_Subpools, dispatch to the
215 -- implementation of Allocate_From_Subpool.
216
217 if Is_Subpool_Allocation then
218 Allocate_From_Subpool
219 (Root_Storage_Pool_With_Subpools'Class (Pool),
220 N_Addr, N_Size, N_Alignment, Subpool);
221
222 -- For descendants of Root_Storage_Pool, dispatch to the implementation
223 -- of Allocate.
224
225 else
226 Allocate (Pool, N_Addr, N_Size, N_Alignment);
227 end if;
228
229 -- Step 4: Displacement of address
230
231 if Is_Controlled then
232 -- Move the address from the hidden list header to the start of the
233 -- object. If there is padding due to larger alignment of the object,
234 -- the padding is placed at the beginning. This effectively hides the
235 -- list header:
236
237 -- N_Addr Addr
238 -- | |
239 -- V V
240 -- +-------+---------------+----------------------+
241 -- |Padding| Header | Object |
242 -- +-------+---------------+----------------------+
243 -- ^ ^ ^
244 -- | +- Header_Size -+
245 -- | |
246 -- +- Header_And_Padding --+
247
248 Addr := N_Addr + Header_And_Padding;
249
250 -- Non-controlled allocation
251
252 else
253 Addr := N_Addr;
254 end if;
255 end Allocate_Any_Controlled;
256
257 ------------
258 -- Attach --
259 ------------
260
261 procedure Attach (N : not null SP_Node_Ptr; L : not null SP_Node_Ptr) is
262 begin
263 -- Ensure that the node has not been attached already
264
265 pragma Assert (N.Prev = null and then N.Next = null);
266
267 Lock_Task.all;
268
269 L.Next.Prev := N;
270 N.Next := L.Next;
271 L.Next := N;
272 N.Prev := L;
273
274 Unlock_Task.all;
275
276 -- Note: No need to unlock in case of an exception because the above
277 -- code can never raise one.
278 end Attach;
279
280 -------------------------------
281 -- Deallocate_Any_Controlled --
282 -------------------------------
283
284 procedure Deallocate_Any_Controlled
285 (Pool : in out Root_Storage_Pool'Class;
286 Addr : System.Address;
287 Storage_Size : System.Storage_Elements.Storage_Count;
288 Alignment : System.Storage_Elements.Storage_Count;
289 Is_Controlled : Boolean)
290 is
291 N_Addr : Address;
292 N_Alignment : Storage_Count;
293 N_Size : Storage_Count;
294
295 Header_And_Padding : Storage_Offset;
296 -- This offset includes the size of a header plus an additional padding
297 -- due to a larger alignment of the object.
298
299 begin
300 -- Step 1: Displacement of address
301
302 if Is_Controlled then
303 -- Account for possible padding space before the header due to a
304 -- larger alignment.
305
306 Header_And_Padding := Header_Size_With_Padding (Alignment);
307
308 -- N_Addr Addr
309 -- | |
310 -- V V
311 -- +-------+---------------+----------------------+
312 -- |Padding| Header | Object |
313 -- +-------+---------------+----------------------+
314 -- ^ ^ ^
315 -- | +- Header_Size -+
316 -- | |
317 -- +- Header_And_Padding --+
318
319 -- Move the address from the object to the beginning of the header
320
321 N_Addr := Addr - Header_And_Padding;
322
323 -- The size of the deallocated object must include that of the header
324
325 N_Size := Storage_Size + Header_And_Padding;
326
327 -- The alignment must account for the hidden header before the object
328
329 N_Alignment :=
330 System.Storage_Elements.Storage_Count'Max
331 (Alignment, System.Finalization_Primitives.Header_Alignment);
332
333 else
334 N_Addr := Addr;
335 N_Size := Storage_Size;
336 N_Alignment := Alignment;
337 end if;
338
339 -- Step 2: Deallocation of object
340
341 -- Dispatch to the proper implementation of Deallocate. This action
342 -- covers both Root_Storage_Pool and Root_Storage_Pool_With_Subpools
343 -- implementations.
344
345 Deallocate (Pool, N_Addr, N_Size, N_Alignment);
346 end Deallocate_Any_Controlled;
347
348 ------------------------------
349 -- Default_Subpool_For_Pool --
350 ------------------------------
351
352 function Default_Subpool_For_Pool
353 (Pool : in out Root_Storage_Pool_With_Subpools)
354 return not null Subpool_Handle
355 is
356 pragma Unreferenced (Pool);
357 begin
358 return raise Program_Error with
359 "default Default_Subpool_For_Pool called; must be overridden";
360 end Default_Subpool_For_Pool;
361
362 ------------
363 -- Detach --
364 ------------
365
366 procedure Detach (N : not null SP_Node_Ptr) is
367 begin
368 -- Ensure that the node is attached to some list
369
370 pragma Assert (N.Next /= null and then N.Prev /= null);
371
372 Lock_Task.all;
373
374 N.Prev.Next := N.Next;
375 N.Next.Prev := N.Prev;
376 N.Prev := null;
377 N.Next := null;
378
379 Unlock_Task.all;
380
381 -- Note: No need to unlock in case of an exception because the above
382 -- code can never raise one.
383 end Detach;
384
385 --------------
386 -- Finalize --
387 --------------
388
389 overriding procedure Finalize (Controller : in out Pool_Controller) is
390 begin
391 Finalize_Pool (Controller.Enclosing_Pool.all);
392 end Finalize;
393
394 -------------------
395 -- Finalize_Pool --
396 -------------------
397
398 procedure Finalize_Pool (Pool : in out Root_Storage_Pool_With_Subpools) is
399 Curr_Ptr : SP_Node_Ptr;
400 Ex_Occur : Exception_Occurrence;
401 Handle : Subpool_Handle;
402 Raised : Boolean := False;
403
404 function Is_Empty_List (L : not null SP_Node_Ptr) return Boolean;
405 -- Determine whether a list contains only one element, the dummy head
406
407 -------------------
408 -- Is_Empty_List --
409 -------------------
410
411 function Is_Empty_List (L : not null SP_Node_Ptr) return Boolean is
412 begin
413 return L.Next = L and then L.Prev = L;
414 end Is_Empty_List;
415
416 -- Start of processing for Finalize_Pool
417
418 begin
419 -- It is possible for multiple tasks to cause the finalization of a
420 -- common pool. Allow only one task to finalize the contents.
421
422 if Pool.Finalization_Started then
423 return;
424 end if;
425
426 -- Lock the pool to prevent the creation of additional subpools while
427 -- the available ones are finalized. The pool remains locked because
428 -- either it is about to be deallocated or the associated access type
429 -- is about to go out of scope.
430
431 Pool.Finalization_Started := True;
432
433 while not Is_Empty_List (Pool.Subpools'Unchecked_Access) loop
434 Curr_Ptr := Pool.Subpools.Next;
435
436 -- Finalize and deallocate the subpool. Beware that the node pointed
437 -- to by Curr_Ptr will be deallocated so may not be passed as actual
438 -- in the call, since the formal parameter is In Out.
439
440 begin
441 Handle := Curr_Ptr.Subpool;
442 Finalize_And_Deallocate (Handle);
443
444 exception
445 when Fin_Occur : others =>
446 if not Raised then
447 Raised := True;
448 Save_Occurrence (Ex_Occur, Fin_Occur);
449 end if;
450 end;
451 end loop;
452
453 -- If the finalization of a particular collection failed, reraise the
454 -- exception now.
455
456 if Raised then
457 Reraise_Occurrence (Ex_Occur);
458 end if;
459 end Finalize_Pool;
460
461 ------------------------------
462 -- Header_Size_With_Padding --
463 ------------------------------
464
465 function Header_Size_With_Padding
466 (Alignment : System.Storage_Elements.Storage_Count)
467 return System.Storage_Elements.Storage_Count
468 is
469 Size : constant Storage_Count :=
470 System.Finalization_Primitives.Header_Size;
471
472 begin
473 if Size mod Alignment = 0 then
474 return Size;
475
476 -- Add enough padding to reach the nearest multiple of the alignment
477 -- rounding up.
478
479 else
480 return ((Size + Alignment - 1) / Alignment) * Alignment;
481 end if;
482 end Header_Size_With_Padding;
483
484 ----------------
485 -- Initialize --
486 ----------------
487
488 overriding procedure Initialize (Controller : in out Pool_Controller) is
489 begin
490 Initialize_Pool (Controller.Enclosing_Pool.all);
491 end Initialize;
492
493 ---------------------
494 -- Initialize_Pool --
495 ---------------------
496
497 procedure Initialize_Pool (Pool : in out Root_Storage_Pool_With_Subpools) is
498 begin
499 -- The dummy head must point to itself in both directions
500
501 Pool.Subpools.Next := Pool.Subpools'Unchecked_Access;
502 Pool.Subpools.Prev := Pool.Subpools'Unchecked_Access;
503 end Initialize_Pool;
504
505 ---------------------
506 -- Pool_Of_Subpool --
507 ---------------------
508
509 function Pool_Of_Subpool
510 (Subpool : not null Subpool_Handle)
511 return access Root_Storage_Pool_With_Subpools'Class
512 is
513 begin
514 return Subpool.Owner;
515 end Pool_Of_Subpool;
516
517 ----------------
518 -- Print_Pool --
519 ----------------
520
521 procedure Print_Pool (Pool : Root_Storage_Pool_With_Subpools) is
522 Head : constant SP_Node_Ptr := Pool.Subpools'Unrestricted_Access;
523 Head_Seen : Boolean := False;
524 SP_Ptr : SP_Node_Ptr;
525
526 begin
527 -- Output the contents of the pool
528
529 -- Pool : 0x123456789
530 -- Subpools : 0x123456789
531 -- Fin_Start : TRUE <or> FALSE
532 -- Controller: OK <or> NOK
533
534 Put ("Pool : ");
535 Put_Line (Address_Image (Pool'Address));
536
537 Put ("Subpools : ");
538 Put_Line (Address_Image (Pool.Subpools'Address));
539
540 Put ("Fin_Start : ");
541 Put_Line (Pool.Finalization_Started'Img);
542
543 Put ("Controlled: ");
544 if Pool.Controller.Enclosing_Pool = Pool'Unrestricted_Access then
545 Put_Line ("OK");
546 else
547 Put_Line ("NOK (ERROR)");
548 end if;
549
550 SP_Ptr := Head;
551 while SP_Ptr /= null loop -- Should never be null
552 Put_Line ("V");
553
554 -- We see the head initially; we want to exit when we see the head a
555 -- second time.
556
557 if SP_Ptr = Head then
558 exit when Head_Seen;
559
560 Head_Seen := True;
561 end if;
562
563 -- The current element is null. This should never happend since the
564 -- list is circular.
565
566 if SP_Ptr.Prev = null then
567 Put_Line ("null (ERROR)");
568
569 -- The current element points back to the correct element
570
571 elsif SP_Ptr.Prev.Next = SP_Ptr then
572 Put_Line ("^");
573
574 -- The current element points to an erroneous element
575
576 else
577 Put_Line ("? (ERROR)");
578 end if;
579
580 -- Output the contents of the node
581
582 Put ("|Header: ");
583 Put (Address_Image (SP_Ptr.all'Address));
584 if SP_Ptr = Head then
585 Put_Line (" (dummy head)");
586 else
587 Put_Line ("");
588 end if;
589
590 Put ("| Prev: ");
591
592 if SP_Ptr.Prev = null then
593 Put_Line ("null");
594 else
595 Put_Line (Address_Image (SP_Ptr.Prev.all'Address));
596 end if;
597
598 Put ("| Next: ");
599
600 if SP_Ptr.Next = null then
601 Put_Line ("null");
602 else
603 Put_Line (Address_Image (SP_Ptr.Next.all'Address));
604 end if;
605
606 Put ("| Subp: ");
607
608 if SP_Ptr.Subpool = null then
609 Put_Line ("null");
610 else
611 Put_Line (Address_Image (SP_Ptr.Subpool.all'Address));
612 end if;
613
614 SP_Ptr := SP_Ptr.Next;
615 end loop;
616 end Print_Pool;
617
618 -------------------
619 -- Print_Subpool --
620 -------------------
621
622 procedure Print_Subpool (Subpool : Subpool_Handle) is
623 begin
624 if Subpool = null then
625 Put_Line ("null");
626 return;
627 end if;
628
629 -- Output the contents of a subpool
630
631 -- Owner : 0x123456789
632 -- Collection: 0x123456789
633 -- Node : 0x123456789
634
635 Put ("Owner : ");
636 if Subpool.Owner = null then
637 Put_Line ("null");
638 else
639 Put_Line (Address_Image (Subpool.Owner'Address));
640 end if;
641
642 Put ("Collection: ");
643 Put_Line (Address_Image (Subpool.Collection'Address));
644
645 Put ("Node : ");
646 if Subpool.Node = null then
647 Put ("null");
648
649 if Subpool.Owner = null then
650 Put_Line (" OK");
651 else
652 Put_Line (" (ERROR)");
653 end if;
654 else
655 Put_Line (Address_Image (Subpool.Node'Address));
656 end if;
657 end Print_Subpool;
658
659 -------------------------
660 -- Set_Pool_Of_Subpool --
661 -------------------------
662
663 procedure Set_Pool_Of_Subpool
664 (Subpool : not null Subpool_Handle;
665 To : in out Root_Storage_Pool_With_Subpools'Class)
666 is
667 N_Ptr : SP_Node_Ptr;
668
669 begin
670 -- If the subpool is already owned, raise Program_Error. This is a
671 -- direct violation of the RM rules.
672
673 if Subpool.Owner /= null then
674 raise Program_Error with "subpool already belongs to a pool";
675 end if;
676
677 -- Prevent the creation of a new subpool while the owner is being
678 -- finalized. This is a serious error.
679
680 if To.Finalization_Started then
681 raise Program_Error
682 with "subpool creation after finalization started";
683 end if;
684
685 Subpool.Owner := To'Unchecked_Access;
686
687 -- Create a subpool node and decorate it. Since this node is not
688 -- allocated on the owner's pool, it must be explicitly destroyed by
689 -- Finalize_And_Detach.
690
691 N_Ptr := new SP_Node;
692 N_Ptr.Subpool := Subpool;
693 Subpool.Node := N_Ptr;
694
695 Attach (N_Ptr, To.Subpools'Unchecked_Access);
696 end Set_Pool_Of_Subpool;
697
698 end System.Storage_Pools.Subpools;