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