Debian Codegraph @

gcc/ada/libgnat/s-secsta__cheri.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . S E C O N D A R Y _ S T A C K --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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.Unchecked_Conversion;
33 with Ada.Unchecked_Deallocation;
34
35 with Interfaces.CHERI; use Interfaces.CHERI;
36 with System.Parameters; use System.Parameters;
37 with System.Soft_Links; use System.Soft_Links;
38 with System.Storage_Elements; use System.Storage_Elements;
39
40 package body System.Secondary_Stack is
41
42 ------------------------------------
43 -- Binder Allocated Stack Support --
44 ------------------------------------
45
46 -- When at least one of the following restrictions
47 --
48 -- No_Implicit_Heap_Allocations
49 -- No_Implicit_Task_Allocations
50 --
51 -- is in effect, the binder creates a static secondary stack pool, where
52 -- each stack has a default size. Assignment of these stacks to tasks is
53 -- performed by SS_Init. The following variables are defined in this unit
54 -- in order to avoid depending on the binder. Their values are set by the
55 -- binder.
56
57 Binder_SS_Count : Natural := 0;
58 pragma Export (Ada, Binder_SS_Count, "__gnat_binder_ss_count");
59 -- The number of secondary stacks in the pool created by the binder
60
61 Binder_Default_SS_Size : Size_Type;
62 pragma Export (Ada, Binder_Default_SS_Size, "__gnat_default_ss_size");
63 -- The default secondary stack size as specified by the binder. The value
64 -- is defined here rather than in init.c or System.Init because the ZFP and
65 -- Ravenscar-ZFP run-times lack these locations.
66
67 Binder_Default_SS_Pool : Address;
68 pragma Export (Ada, Binder_Default_SS_Pool, "__gnat_default_ss_pool");
69 -- The address of the secondary stack pool created by the binder
70
71 Binder_Default_SS_Pool_Index : Natural := 0;
72 -- Index into the secondary stack pool created by the binder
73
74 -----------------------
75 -- Local subprograms --
76 -----------------------
77
78 procedure Allocate_Dynamic
79 (Stack : SS_Stack_Ptr;
80 Mem_Size : Memory_Size;
81 Addr : out Address);
82 pragma Inline (Allocate_Dynamic);
83 -- Allocate enough space on dynamic secondary stack Stack to fit a request
84 -- of size Mem_Size. Addr denotes the address of the first byte of the
85 -- allocation.
86
87 procedure Allocate_On_Chunk
88 (Stack : SS_Stack_Ptr;
89 Prev_Chunk : SS_Chunk_Ptr;
90 Chunk : SS_Chunk_Ptr;
91 Byte : Memory_Index;
92 Mem_Size : Memory_Size;
93 Addr : out Address);
94 pragma Inline (Allocate_On_Chunk);
95 -- Allocate enough space on chunk Chunk to fit a request of size Mem_Size.
96 -- Stack is the owner of the allocation Chunk. Prev_Chunk is the preceding
97 -- chunk of Chunk. Byte indicates the first free byte within Chunk. Addr
98 -- denotes the address of the first byte of the allocation. This routine
99 -- updates the state of Stack.all to reflect the side effects of the
100 -- allocation.
101
102 procedure Allocate_Static
103 (Stack : SS_Stack_Ptr;
104 Mem_Size : Memory_Size;
105 Addr : out Address);
106 pragma Inline (Allocate_Static);
107 -- Allocate enough space on static secondary stack Stack to fit a request
108 -- of size Mem_Size. Addr denotes the address of the first byte of the
109 -- allocation.
110
111 procedure Free is new Ada.Unchecked_Deallocation (SS_Chunk, SS_Chunk_Ptr);
112 -- Free a dynamically allocated chunk
113
114 procedure Free is new Ada.Unchecked_Deallocation (SS_Stack, SS_Stack_Ptr);
115 -- Free a dynamically allocated secondary stack
116
117 function Has_Enough_Free_Memory
118 (Chunk : SS_Chunk_Ptr;
119 Byte : Memory_Index;
120 Mem_Size : Memory_Size) return Boolean;
121 pragma Inline (Has_Enough_Free_Memory);
122 -- Determine whether chunk Chunk has enough room to fit a memory request of
123 -- size Mem_Size, starting from the first free byte of the chunk denoted by
124 -- Byte.
125
126 function Number_Of_Chunks (Stack : SS_Stack_Ptr) return Chunk_Count;
127 pragma Inline (Number_Of_Chunks);
128 -- Count the number of static and dynamic chunks of secondary stack Stack
129
130 function Size_Up_To_And_Including (Chunk : SS_Chunk_Ptr) return Memory_Size;
131 pragma Inline (Size_Up_To_And_Including);
132 -- Calculate the size of secondary stack which houses chunk Chunk, from the
133 -- start of the secondary stack up to and including Chunk itself. The size
134 -- includes the following kinds of memory:
135 --
136 -- * Free memory in used chunks due to alignment holes
137 -- * Occupied memory by allocations
138 --
139 -- This is a constant time operation, regardless of the secondary stack's
140 -- nature.
141
142 function Top_Chunk_Id (Stack : SS_Stack_Ptr) return Chunk_Id_With_Invalid;
143 pragma Inline (Top_Chunk_Id);
144 -- Obtain the Chunk_Id of the chunk indicated by secondary stack Stack's
145 -- pointer.
146
147 function Used_Memory_Size (Stack : SS_Stack_Ptr) return Memory_Size;
148 pragma Inline (Used_Memory_Size);
149 -- Calculate the size of stack Stack's occupied memory usage. This includes
150 -- the following kinds of memory:
151 --
152 -- * Free memory in used chunks due to alignment holes
153 -- * Occupied memory by allocations
154 --
155 -- This is a constant time operation, regardless of the secondary stack's
156 -- nature.
157
158 function Padding_For_Bounds_Alignment
159 (Ptr : Address;
160 Size : Memory_Size)
161 return Memory_Size;
162 pragma Inline (Padding_For_Bounds_Alignment);
163 -- Calculate the amount of padding needed to align an address up to the
164 -- next representable boundary.
165
166 ----------------------
167 -- Allocate_Dynamic --
168 ----------------------
169
170 procedure Allocate_Dynamic
171 (Stack : SS_Stack_Ptr;
172 Mem_Size : Memory_Size;
173 Addr : out Address)
174 is
175 function Allocate_New_Chunk return SS_Chunk_Ptr;
176 pragma Inline (Allocate_New_Chunk);
177 -- Create a new chunk which is big enough to fit a request of size
178 -- Mem_Size.
179
180 ------------------------
181 -- Allocate_New_Chunk --
182 ------------------------
183
184 function Allocate_New_Chunk return SS_Chunk_Ptr is
185 Chunk_Size : Memory_Size;
186
187 begin
188 -- The size of the new chunk must fit the memory request precisely.
189 -- In the case where the memory request is way too small, use the
190 -- default chunk size. This avoids creating multiple tiny chunks.
191
192 Chunk_Size := Mem_Size;
193
194 if Chunk_Size < Stack.Default_Chunk_Size then
195 Chunk_Size := Stack.Default_Chunk_Size;
196 end if;
197
198 return new SS_Chunk (Chunk_Size);
199
200 -- The creation of the new chunk may exhaust the heap. Raise a new
201 -- Storage_Error to indicate that the secondary stack is exhausted
202 -- as well.
203
204 exception
205 when Storage_Error =>
206 raise Storage_Error with "secondary stack exhausted";
207 end Allocate_New_Chunk;
208
209 -- Local variables
210
211 Next_Chunk : SS_Chunk_Ptr;
212
213 -- Start of processing for Allocate_Dynamic
214
215 begin
216 -- Determine whether the chunk indicated by the stack pointer is big
217 -- enough to fit the memory request and if it is, allocate on it.
218
219 if Has_Enough_Free_Memory
220 (Chunk => Stack.Top.Chunk,
221 Byte => Stack.Top.Byte,
222 Mem_Size => Mem_Size)
223 then
224 Allocate_On_Chunk
225 (Stack => Stack,
226 Prev_Chunk => null,
227 Chunk => Stack.Top.Chunk,
228 Byte => Stack.Top.Byte,
229 Mem_Size => Mem_Size,
230 Addr => Addr);
231
232 return;
233 end if;
234
235 -- At this point it is known that the chunk indicated by the stack
236 -- pointer is not big enough to fit the memory request. Examine all
237 -- subsequent chunks, and apply the following criteria:
238 --
239 -- * If the current chunk is too small, free it
240 --
241 -- * If the current chunk is big enough, allocate on it
242 --
243 -- This ensures that no space is wasted. The process is costly, however
244 -- allocation is costly in general. Paying the price here keeps routines
245 -- SS_Mark and SS_Release cheap.
246
247 while Stack.Top.Chunk.Next /= null loop
248
249 -- The current chunk is big enough to fit the memory request,
250 -- allocate on it.
251
252 if Has_Enough_Free_Memory
253 (Chunk => Stack.Top.Chunk.Next,
254 Byte => Stack.Top.Chunk.Next.Memory'First,
255 Mem_Size => Mem_Size)
256 then
257 Allocate_On_Chunk
258 (Stack => Stack,
259 Prev_Chunk => Stack.Top.Chunk,
260 Chunk => Stack.Top.Chunk.Next,
261 Byte => Stack.Top.Chunk.Next.Memory'First,
262 Mem_Size => Mem_Size,
263 Addr => Addr);
264
265 return;
266
267 -- Otherwise the chunk is too small, free it
268
269 else
270 Next_Chunk := Stack.Top.Chunk.Next.Next;
271
272 -- Unchain the chunk from the stack. This keeps the next candidate
273 -- chunk situated immediately after Top.Chunk.
274 --
275 -- Top.Chunk Top.Chunk.Next Top.Chunk.Next.Next
276 -- | | (Next_Chunk)
277 -- v v v
278 -- +-------+ +------------+ +--------------+
279 -- | | --> | | --> | |
280 -- +-------+ +------------+ +--------------+
281 -- to be freed
282
283 Free (Stack.Top.Chunk.Next);
284 Stack.Top.Chunk.Next := Next_Chunk;
285 end if;
286 end loop;
287
288 -- At this point one of the following outcomes took place:
289 --
290 -- * Top.Chunk is the last chunk in the stack
291 --
292 -- * Top.Chunk was not the last chunk originally. It was followed by
293 -- chunks which were too small and as a result were deleted, thus
294 -- making Top.Chunk the last chunk in the stack.
295 --
296 -- Either way, nothing should be hanging off the chunk indicated by the
297 -- stack pointer.
298
299 pragma Assert (Stack.Top.Chunk.Next = null);
300
301 -- Create a new chunk big enough to fit the memory request, and allocate
302 -- on it.
303
304 Stack.Top.Chunk.Next := Allocate_New_Chunk;
305
306 Allocate_On_Chunk
307 (Stack => Stack,
308 Prev_Chunk => Stack.Top.Chunk,
309 Chunk => Stack.Top.Chunk.Next,
310 Byte => Stack.Top.Chunk.Next.Memory'First,
311 Mem_Size => Mem_Size,
312 Addr => Addr);
313 end Allocate_Dynamic;
314
315 -----------------------
316 -- Allocate_On_Chunk --
317 -----------------------
318
319 procedure Allocate_On_Chunk
320 (Stack : SS_Stack_Ptr;
321 Prev_Chunk : SS_Chunk_Ptr;
322 Chunk : SS_Chunk_Ptr;
323 Byte : Memory_Index;
324 Mem_Size : Memory_Size;
325 Addr : out Address)
326 is
327 New_High_Water_Mark : Memory_Size;
328 Padding : Memory_Size;
329
330 begin
331 -- The allocation occurs on a reused or a brand new chunk. Such a chunk
332 -- must always be connected to some previous chunk.
333
334 if Prev_Chunk /= null then
335 pragma Assert (Prev_Chunk.Next = Chunk);
336
337 -- Update the Size_Up_To_Chunk because this value is invalidated for
338 -- reused and new chunks.
339 --
340 -- Prev_Chunk Chunk
341 -- v v
342 -- . . . . . . . +--------------+ +--------
343 -- . --> |##############| --> |
344 -- . . . . . . . +--------------+ +--------
345 -- | |
346 -- -------------------+------------+
347 -- Size_Up_To_Chunk Size
348 --
349 -- The Size_Up_To_Chunk is equal to the size of the whole stack up to
350 -- the previous chunk, plus the size of the previous chunk itself.
351
352 Chunk.Size_Up_To_Chunk := Size_Up_To_And_Including (Prev_Chunk);
353 end if;
354
355 -- The chunk must have enough room to fit the memory request. If this is
356 -- not the case, then a previous step picked the wrong chunk.
357
358 pragma Assert (Has_Enough_Free_Memory (Chunk, Byte, Mem_Size));
359
360 -- The first byte of the allocation is the first free byte within the
361 -- chunk.
362
363 Addr := Chunk.Memory (Byte)'Address;
364
365 -- Align the address to ensure that the CHERI bounds will be
366 -- representable.
367
368 Padding := Padding_For_Bounds_Alignment (Addr, Mem_Size);
369 Addr := Addr + Storage_Offset (Padding);
370
371 -- The chunk becomes the chunk indicated by the stack pointer. This is
372 -- either the currently indicated chunk, an existing chunk, or a brand
373 -- new chunk.
374
375 Stack.Top.Chunk := Chunk;
376
377 -- The next free byte is immediately after the memory request
378 --
379 -- Addr Top.Byte
380 -- | |
381 -- +-----|--------|----+
382 -- |##############| |
383 -- +-------------------+
384
385 -- ??? this calculation may overflow on 32bit targets
386
387 Stack.Top.Byte := Byte + Mem_Size + Padding;
388
389 -- At this point the next free byte cannot go beyond the memory capacity
390 -- of the chunk indicated by the stack pointer, except when the chunk is
391 -- full, in which case it indicates the byte beyond the chunk. Ensure
392 -- that the occupied memory is at most as much as the capacity of the
393 -- chunk. Top.Byte - 1 denotes the last occupied byte.
394
395 pragma Assert (Stack.Top.Byte - 1 <= Stack.Top.Chunk.Size);
396
397 -- Calculate the new high water mark now that the memory request has
398 -- been fulfilled, and update if necessary. The new high water mark is
399 -- technically the size of the used memory by the whole stack.
400
401 New_High_Water_Mark := Used_Memory_Size (Stack);
402
403 if New_High_Water_Mark > Stack.High_Water_Mark then
404 Stack.High_Water_Mark := New_High_Water_Mark;
405 end if;
406 end Allocate_On_Chunk;
407
408 ---------------------
409 -- Allocate_Static --
410 ---------------------
411
412 procedure Allocate_Static
413 (Stack : SS_Stack_Ptr;
414 Mem_Size : Memory_Size;
415 Addr : out Address)
416 is
417 begin
418 -- Static secondary stack allocations are performed only on the static
419 -- chunk. There should be no dynamic chunks following the static chunk.
420
421 pragma Assert (Stack.Top.Chunk = Stack.Static_Chunk'Access);
422 pragma Assert (Stack.Top.Chunk.Next = null);
423
424 -- Raise Storage_Error if the static chunk does not have enough room to
425 -- fit the memory request. This indicates that the stack is about to be
426 -- depleted.
427
428 if not Has_Enough_Free_Memory
429 (Chunk => Stack.Top.Chunk,
430 Byte => Stack.Top.Byte,
431 Mem_Size => Mem_Size)
432 then
433 raise Storage_Error with "secondary stack exhaused";
434 end if;
435
436 Allocate_On_Chunk
437 (Stack => Stack,
438 Prev_Chunk => null,
439 Chunk => Stack.Top.Chunk,
440 Byte => Stack.Top.Byte,
441 Mem_Size => Mem_Size,
442 Addr => Addr);
443 end Allocate_Static;
444
445 --------------------
446 -- Get_Chunk_Info --
447 --------------------
448
449 function Get_Chunk_Info
450 (Stack : SS_Stack_Ptr;
451 C_Id : Chunk_Id) return Chunk_Info
452 is
453 function Find_Chunk return SS_Chunk_Ptr;
454 pragma Inline (Find_Chunk);
455 -- Find the chunk which corresponds to Id. Return null if no such chunk
456 -- exists.
457
458 ----------------
459 -- Find_Chunk --
460 ----------------
461
462 function Find_Chunk return SS_Chunk_Ptr is
463 Chunk : SS_Chunk_Ptr;
464 Id : Chunk_Id;
465
466 begin
467 Chunk := Stack.Static_Chunk'Access;
468 Id := 1;
469 while Chunk /= null loop
470 if Id = C_Id then
471 return Chunk;
472 end if;
473
474 Chunk := Chunk.Next;
475 Id := Id + 1;
476 end loop;
477
478 return null;
479 end Find_Chunk;
480
481 -- Local variables
482
483 Chunk : constant SS_Chunk_Ptr := Find_Chunk;
484
485 -- Start of processing for Get_Chunk_Info
486
487 begin
488 if Chunk = null then
489 return Invalid_Chunk;
490
491 else
492 return (Size => Chunk.Size,
493 Size_Up_To_Chunk => Chunk.Size_Up_To_Chunk);
494 end if;
495 end Get_Chunk_Info;
496
497 --------------------
498 -- Get_Stack_Info --
499 --------------------
500
501 function Get_Stack_Info (Stack : SS_Stack_Ptr) return Stack_Info is
502 Info : Stack_Info;
503
504 begin
505 Info.Default_Chunk_Size := Stack.Default_Chunk_Size;
506 Info.Freeable := Stack.Freeable;
507 Info.High_Water_Mark := Stack.High_Water_Mark;
508 Info.Number_Of_Chunks := Number_Of_Chunks (Stack);
509 Info.Top.Byte := Stack.Top.Byte;
510 Info.Top.Chunk := Top_Chunk_Id (Stack);
511
512 return Info;
513 end Get_Stack_Info;
514
515 ----------------------------
516 -- Has_Enough_Free_Memory --
517 ----------------------------
518
519 function Has_Enough_Free_Memory
520 (Chunk : SS_Chunk_Ptr;
521 Byte : Memory_Index;
522 Mem_Size : Memory_Size) return Boolean
523 is
524 Padding : Memory_Size;
525
526 begin
527 -- First check if the chunk is full (Byte is > Memory'Last in that
528 -- case), then check there is enough free memory.
529
530 -- Byte - 1 denotes the last occupied byte. Subtracting that byte from
531 -- the memory capacity of the chunk yields the size of the free memory
532 -- within the chunk. The chunk can fit the request as long as the free
533 -- memory is as big as the request.
534
535 -- We also need to consider any extra padding needed to align the
536 -- address to ensure that the CHERI lower bound is representable.
537
538 Padding :=
539 Padding_For_Bounds_Alignment (Chunk.Memory (Byte)'Address, Mem_Size);
540
541 return Chunk.Memory'Last >= Byte
542 and then Chunk.Size - (Byte - 1) >= Mem_Size
543 and then Chunk.Size - (Byte - 1) - Mem_Size >= Padding;
544
545 end Has_Enough_Free_Memory;
546
547 ----------------------
548 -- Number_Of_Chunks --
549 ----------------------
550
551 function Number_Of_Chunks (Stack : SS_Stack_Ptr) return Chunk_Count is
552 Chunk : SS_Chunk_Ptr;
553 Count : Chunk_Count;
554
555 begin
556 Chunk := Stack.Static_Chunk'Access;
557 Count := 0;
558 while Chunk /= null loop
559 Chunk := Chunk.Next;
560 Count := Count + 1;
561 end loop;
562
563 return Count;
564 end Number_Of_Chunks;
565
566 ------------------------------
567 -- Size_Up_To_And_Including --
568 ------------------------------
569
570 function Size_Up_To_And_Including
571 (Chunk : SS_Chunk_Ptr) return Memory_Size
572 is
573 begin
574 return Chunk.Size_Up_To_Chunk + Chunk.Size;
575 end Size_Up_To_And_Including;
576
577 -----------------
578 -- SS_Allocate --
579 -----------------
580
581 procedure SS_Allocate
582 (Addr : out Address;
583 Storage_Size : Storage_Count;
584 Alignment : SSE.Storage_Count := Standard'Maximum_Alignment)
585 is
586
587 function Round_Up (Size : Storage_Count) return Memory_Size;
588 pragma Inline (Round_Up);
589 -- Round Size up to the nearest multiple of the maximum alignment
590
591 function Align_Addr (Addr : Address) return Address;
592 pragma Inline (Align_Addr);
593 -- Align Addr to the next multiple of Alignment
594
595 ----------------
596 -- Align_Addr --
597 ----------------
598
599 function Align_Addr (Addr : Address) return Address is
600 begin
601
602 -- L : Alignment
603 -- A : Standard'Maximum_Alignment
604
605 -- Addr
606 -- L | L L
607 -- A--A--A--A--A--A--A--A--A--A--A
608 -- | |
609 -- \----/ | |
610 -- Addr mod L | Addr + L
611 -- |
612 -- Addr + L - (Addr mod L)
613
614 return Addr + (Alignment - (Addr mod Alignment));
615 end Align_Addr;
616
617 --------------
618 -- Round_Up --
619 --------------
620
621 function Round_Up (Size : Storage_Count) return Memory_Size is
622 Algn_MS : constant Memory_Size := Standard'Maximum_Alignment;
623 Size_MS : constant Memory_Size := Memory_Size (Size);
624
625 begin
626 -- Detect a case where the Size is very large and may yield
627 -- a rounded result which is outside the range of Chunk_Memory_Size.
628 -- Treat this case as secondary-stack depletion.
629
630 if Memory_Size'Last - Algn_MS < Size_MS then
631 raise Storage_Error with "secondary stack exhausted";
632 end if;
633
634 return ((Size_MS + Algn_MS - 1) / Algn_MS) * Algn_MS;
635 end Round_Up;
636
637 -- Local variables
638
639 Stack : constant SS_Stack_Ptr := Get_Sec_Stack.all;
640 Mem_Size : Memory_Size;
641
642 Over_Aligning : constant Boolean :=
643 Alignment > Standard'Maximum_Alignment;
644
645 Over_Align_Padding : SSE.Storage_Count := 0;
646
647 Adjusted_Storage_Size : Interfaces.CHERI.Bounds_Length;
648 -- Storage_Size plus padding for over-alignment and extra padding to
649 -- align the capability's upper bound.
650
651 Capability_Lower_Bound : Address;
652
653 -- Start of processing for SS_Allocate
654
655 begin
656 -- Alignment must be a power of two and can be:
657
658 -- - lower than or equal to Maximum_Alignment, in which case the result
659 -- will be aligned on Maximum_Alignment;
660 -- - higher than Maximum_Alignment, in which case the result will be
661 -- dynamically realigned.
662
663 if Over_Aligning then
664 Over_Align_Padding := Alignment;
665 end if;
666
667 -- Raise Storage_Error if the size has overflowed
668
669 if Storage_Size < 0 then
670 raise Storage_Error with "object too large";
671 end if;
672
673 -- Round the requested size (plus the needed padding in case of
674 -- over-alignment) to ensure that the CHERI bounds length will be
675 -- representable.
676
677 Adjusted_Storage_Size :=
678 Representable_Length
679 (Bounds_Length (Storage_Size + Over_Align_Padding));
680
681 -- Round up to the nearest multiple of the default alignment to ensure
682 -- efficient access and that the next available Byte is always aligned
683 -- on the default alignement value.
684
685 Mem_Size := Round_Up (Storage_Count (Adjusted_Storage_Size));
686
687 if Sec_Stack_Dynamic then
688 Allocate_Dynamic (Stack, Mem_Size, Addr);
689 else
690 Allocate_Static (Stack, Mem_Size, Addr);
691 end if;
692
693 -- Restrict the capability bounds to the requested allocation size,
694 -- possibly with some padding for alignment of the bounds.
695
696 Capability_Lower_Bound :=
697 Capability_With_Address_Aligned_Up (Addr, Adjusted_Storage_Size);
698
699 Addr := Capability_With_Exact_Bounds
700 (Capability_Lower_Bound, Adjusted_Storage_Size);
701
702 if Over_Aligning then
703 Addr := Align_Addr (Addr);
704 end if;
705
706 end SS_Allocate;
707
708 -------------
709 -- SS_Free --
710 -------------
711
712 procedure SS_Free (Stack : in out SS_Stack_Ptr) is
713 Static_Chunk : constant SS_Chunk_Ptr := Stack.Static_Chunk'Access;
714 Next_Chunk : SS_Chunk_Ptr;
715
716 begin
717 -- Free all dynamically allocated chunks. The first dynamic chunk is
718 -- found immediately after the static chunk of the stack.
719
720 while Static_Chunk.Next /= null loop
721 Next_Chunk := Static_Chunk.Next.Next;
722 Free (Static_Chunk.Next);
723 Static_Chunk.Next := Next_Chunk;
724 end loop;
725
726 -- At this point one of the following outcomes has taken place:
727 --
728 -- * The stack lacks any dynamic chunks
729 --
730 -- * The stack had dynamic chunks which were all freed
731 --
732 -- Either way, there should be nothing hanging off the static chunk
733
734 pragma Assert (Static_Chunk.Next = null);
735
736 -- Free the stack only when it was dynamically allocated
737
738 if Stack.Freeable then
739 Free (Stack);
740 end if;
741 end SS_Free;
742
743 ----------------
744 -- SS_Get_Max --
745 ----------------
746
747 function SS_Get_Max return Long_Long_Integer is
748 Stack : constant SS_Stack_Ptr := Get_Sec_Stack.all;
749
750 begin
751 return Long_Long_Integer (Stack.High_Water_Mark);
752 end SS_Get_Max;
753
754 -------------
755 -- SS_Info --
756 -------------
757
758 procedure SS_Info is
759 procedure SS_Info_Dynamic (Stack : SS_Stack_Ptr);
760 pragma Inline (SS_Info_Dynamic);
761 -- Output relevant information concerning dynamic secondary stack Stack
762
763 function Total_Memory_Size (Stack : SS_Stack_Ptr) return Memory_Size;
764 pragma Inline (Total_Memory_Size);
765 -- Calculate the size of stack Stack's total memory usage. This includes
766 -- the following kinds of memory:
767 --
768 -- * Free memory in used chunks due to alignment holes
769 -- * Free memory in the topmost chunk due to partial usage
770 -- * Free memory in unused chunks following the chunk indicated by the
771 -- stack pointer.
772 -- * Memory occupied by allocations
773 --
774 -- This is a linear-time operation on the number of chunks.
775
776 ---------------------
777 -- SS_Info_Dynamic --
778 ---------------------
779
780 procedure SS_Info_Dynamic (Stack : SS_Stack_Ptr) is
781 begin
782 Put_Line
783 (" Number of Chunks : " & Number_Of_Chunks (Stack)'Img);
784
785 Put_Line
786 (" Default size of Chunks : " & Stack.Default_Chunk_Size'Img);
787 end SS_Info_Dynamic;
788
789 -----------------------
790 -- Total_Memory_Size --
791 -----------------------
792
793 function Total_Memory_Size (Stack : SS_Stack_Ptr) return Memory_Size is
794 Chunk : SS_Chunk_Ptr;
795 Total : Memory_Size;
796
797 begin
798 -- The total size of the stack is equal to the size of the stack up
799 -- to the chunk indicated by the stack pointer, plus the size of the
800 -- indicated chunk, plus the size of any subsequent chunks.
801
802 Total := Size_Up_To_And_Including (Stack.Top.Chunk);
803
804 Chunk := Stack.Top.Chunk.Next;
805 while Chunk /= null loop
806 Total := Total + Chunk.Size;
807 Chunk := Chunk.Next;
808 end loop;
809
810 return Total;
811 end Total_Memory_Size;
812
813 -- Local variables
814
815 Stack : constant SS_Stack_Ptr := Get_Sec_Stack.all;
816
817 -- Start of processing for SS_Info
818
819 begin
820 Put_Line ("Secondary Stack information:");
821
822 Put_Line
823 (" Total size : "
824 & Total_Memory_Size (Stack)'Img
825 & " bytes");
826
827 Put_Line
828 (" Current allocated space : "
829 & Used_Memory_Size (Stack)'Img
830 & " bytes");
831
832 if Sec_Stack_Dynamic then
833 SS_Info_Dynamic (Stack);
834 end if;
835 end SS_Info;
836
837 -------------
838 -- SS_Init --
839 -------------
840
841 procedure SS_Init
842 (Stack : in out SS_Stack_Ptr;
843 Size : Size_Type := Unspecified_Size)
844 is
845 function Next_Available_Binder_Sec_Stack return SS_Stack_Ptr;
846 pragma Inline (Next_Available_Binder_Sec_Stack);
847 -- Return a pointer to the next available stack from the pool created by
848 -- the binder. This routine updates global Default_Sec_Stack_Pool_Index.
849
850 -------------------------------------
851 -- Next_Available_Binder_Sec_Stack --
852 -------------------------------------
853
854 function Next_Available_Binder_Sec_Stack return SS_Stack_Ptr is
855
856 -- The default-sized secondary stack pool generated by the binder
857 -- is passed to this unit as an Address because it is not possible
858 -- to define a pointer to an array of unconstrained components. The
859 -- pointer is instead obtained using an unchecked conversion to a
860 -- constrained array of secondary stacks with the same size as that
861 -- specified by the binder.
862
863 -- WARNING: The following data structure must be synchronized with
864 -- the one created in Bindgen.Gen_Output_File_Ada. The version in
865 -- bindgen is called Sec_Default_Sized_Stacks.
866
867 type SS_Pool is
868 array (1 .. Binder_SS_Count)
869 of aliased SS_Stack (Binder_Default_SS_Size);
870
871 type SS_Pool_Ptr is access SS_Pool;
872 -- A reference to the secondary stack pool
873
874 function To_SS_Pool_Ptr is
875 new Ada.Unchecked_Conversion (Address, SS_Pool_Ptr);
876
877 -- Use an unchecked conversion to obtain a pointer to one of the
878 -- secondary stacks from the pool generated by the binder. There
879 -- are several reasons for using the conversion:
880 --
881 -- * Accessibility checks prevent a value of a local pointer to be
882 -- stored outside this scope. The conversion is safe because the
883 -- pool is global to the whole application.
884 --
885 -- * Unchecked_Access may circumvent the accessibility checks, but
886 -- it is incompatible with restriction No_Unchecked_Access.
887 --
888 -- * Unrestricted_Access may circumvent the accessibility checks,
889 -- but it is incompatible with pure Ada constructs.
890 -- ??? cannot find the restriction or switch
891
892 pragma Warnings (Off);
893 function To_SS_Stack_Ptr is
894 new Ada.Unchecked_Conversion (Address, SS_Stack_Ptr);
895 pragma Warnings (On);
896
897 Pool : SS_Pool_Ptr;
898
899 begin
900 -- Obtain a typed view of the pool
901
902 Pool := To_SS_Pool_Ptr (Binder_Default_SS_Pool);
903
904 -- Advance the stack index to the next available stack
905
906 Binder_Default_SS_Pool_Index := Binder_Default_SS_Pool_Index + 1;
907
908 -- Return a pointer to the next available stack
909
910 return To_SS_Stack_Ptr (Pool (Binder_Default_SS_Pool_Index)'Address);
911 end Next_Available_Binder_Sec_Stack;
912
913 -- Local variables
914
915 Stack_Size : Memory_Size_With_Invalid;
916
917 -- Start of processing for SS_Init
918
919 begin
920 -- Allocate a new stack on the heap or use one from the pool created by
921 -- the binder.
922
923 if Stack = null then
924
925 -- The caller requested a pool-allocated stack. Determine the proper
926 -- size of the stack based on input from the binder or the runtime in
927 -- case the pool is exhausted.
928
929 if Size = Unspecified_Size then
930
931 -- Use the default secondary stack size as specified by the binder
932 -- only when it has been set. This prevents a bootstrap issue with
933 -- older compilers where the size is never set.
934
935 if Binder_Default_SS_Size > 0 then
936 Stack_Size := Binder_Default_SS_Size;
937
938 -- Otherwise use the default stack size of the particular runtime
939
940 else
941 Stack_Size := Runtime_Default_Sec_Stack_Size;
942 end if;
943
944 -- Otherwise the caller requested a heap-allocated stack. Use the
945 -- specified size directly.
946
947 else
948 Stack_Size := Size;
949 end if;
950
951 -- The caller requested a pool-allocated stack. Use one as long as
952 -- the pool created by the binder has available stacks. This stack
953 -- cannot be deallocated.
954
955 if Size = Unspecified_Size
956 and then Binder_SS_Count > 0
957 and then Binder_Default_SS_Pool_Index < Binder_SS_Count
958 then
959 Stack := Next_Available_Binder_Sec_Stack;
960 Stack.Freeable := False;
961
962 -- Otherwise the caller requested a heap-allocated stack, or the pool
963 -- created by the binder ran out of available stacks. This stack can
964 -- be deallocated.
965
966 else
967 -- It should not be possible to create a stack with a negative
968 -- default chunk size.
969
970 pragma Assert (Stack_Size in Memory_Size);
971
972 Stack := new SS_Stack (Stack_Size);
973 Stack.Freeable := True;
974 end if;
975
976 -- Otherwise the stack was already created either by the compiler or by
977 -- the user, and is about to be reused.
978
979 else
980 null;
981 end if;
982
983 -- The static chunk becomes the chunk indicated by the stack pointer.
984 -- Note that the stack may still hold dynamic chunks, which in turn may
985 -- be reused or freed.
986
987 Stack.Top.Chunk := Stack.Static_Chunk'Access;
988
989 -- The first free byte is the first free byte of the chunk indicated by
990 -- the stack pointer.
991
992 Stack.Top.Byte := Stack.Top.Chunk.Memory'First;
993
994 -- Since the chunk indicated by the stack pointer is also the first
995 -- chunk in the stack, there are no prior chunks, therefore the size
996 -- of the stack up to the chunk is zero.
997
998 Stack.Top.Chunk.Size_Up_To_Chunk := 0;
999
1000 -- Reset the high water mark to account for brand new allocations
1001
1002 Stack.High_Water_Mark := 0;
1003 end SS_Init;
1004
1005 -------------
1006 -- SS_Mark --
1007 -------------
1008
1009 function SS_Mark return Mark_Id is
1010 Stack : constant SS_Stack_Ptr := Get_Sec_Stack.all;
1011
1012 begin
1013 return (Stack => Stack, Top => Stack.Top);
1014 end SS_Mark;
1015
1016 ----------------
1017 -- SS_Release --
1018 ----------------
1019
1020 procedure SS_Release (M : Mark_Id) is
1021 begin
1022 M.Stack.Top := M.Top;
1023 end SS_Release;
1024
1025 ------------------
1026 -- Top_Chunk_Id --
1027 ------------------
1028
1029 function Top_Chunk_Id (Stack : SS_Stack_Ptr) return Chunk_Id_With_Invalid is
1030 Chunk : SS_Chunk_Ptr;
1031 Id : Chunk_Id;
1032
1033 begin
1034 Chunk := Stack.Static_Chunk'Access;
1035 Id := 1;
1036 while Chunk /= null loop
1037 if Chunk = Stack.Top.Chunk then
1038 return Id;
1039 end if;
1040
1041 Chunk := Chunk.Next;
1042 Id := Id + 1;
1043 end loop;
1044
1045 return Invalid_Chunk_Id;
1046 end Top_Chunk_Id;
1047
1048 ----------------------
1049 -- Used_Memory_Size --
1050 ----------------------
1051
1052 function Used_Memory_Size (Stack : SS_Stack_Ptr) return Memory_Size is
1053 begin
1054 -- The size of the occupied memory is equal to the size up to the chunk
1055 -- indicated by the stack pointer, plus the size in use by the indicated
1056 -- chunk itself. Top.Byte - 1 is the last occupied byte.
1057 --
1058 -- Top.Byte
1059 -- |
1060 -- . . . . . . . +--------------|----+
1061 -- . ..> |##############| |
1062 -- . . . . . . . +-------------------+
1063 -- | |
1064 -- -------------------+-------------+
1065 -- Size_Up_To_Chunk size in use
1066
1067 -- ??? this calculation may overflow on 32bit targets
1068
1069 return Stack.Top.Chunk.Size_Up_To_Chunk + Stack.Top.Byte - 1;
1070 end Used_Memory_Size;
1071
1072 ----------------------------------
1073 -- Padding_For_Bounds_Alignment --
1074 ----------------------------------
1075
1076 function Padding_For_Bounds_Alignment
1077 (Ptr : Address;
1078 Size : Memory_Size)
1079 return Memory_Size
1080 is
1081 IA : constant Integer_Address := To_Integer (Ptr);
1082 begin
1083 return Memory_Size (Align_Address_Up (IA, Bounds_Length (Size)) - IA);
1084 end Padding_For_Bounds_Alignment;
1085
1086 end System.Secondary_Stack;