1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . S E C O N D A R Y _ S T A C K --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 pragma Compiler_Unit_Warning
;
34 with System
.Soft_Links
;
35 with System
.Parameters
;
37 with Ada
.Unchecked_Conversion
;
38 with Ada
.Unchecked_Deallocation
;
40 package body System
.Secondary_Stack
is
42 package SSL
renames System
.Soft_Links
;
44 use type SSE
.Storage_Offset
;
45 use type System
.Parameters
.Size_Type
;
47 SS_Ratio_Dynamic
: constant Boolean :=
48 Parameters
.Sec_Stack_Percentage
= Parameters
.Dynamic
;
49 -- There are two entirely different implementations of the secondary
50 -- stack mechanism in this unit, and this Boolean is used to select
51 -- between them (at compile time, so the generated code will contain
52 -- only the code for the desired variant). If SS_Ratio_Dynamic is
53 -- True, then the secondary stack is dynamically allocated from the
54 -- heap in a linked list of chunks. If SS_Ration_Dynamic is False,
55 -- then the secondary stack is allocated statically by grabbing a
56 -- section of the primary stack and using it for this purpose.
58 type Memory
is array (SS_Ptr
range <>) of SSE
.Storage_Element
;
59 for Memory
'Alignment use Standard
'Maximum_Alignment;
60 -- This is the type used for actual allocation of secondary stack
61 -- areas. We require maximum alignment for all such allocations.
63 ---------------------------------------------------------------
64 -- Data Structures for Dynamically Allocated Secondary Stack --
65 ---------------------------------------------------------------
67 -- The following is a diagram of the data structures used for the
68 -- case of a dynamically allocated secondary stack, where the stack
69 -- is allocated as a linked list of chunks allocated from the heap.
71 -- +------------------+
73 -- +------------------+
81 -- +------------------+
83 -- | +--------- | ------+
87 -- | +------ | ---------+
89 -- | +------------------+
93 -- +-----------------+ | +------->| U |
94 -- | Current_Chunk ----+ | | N |
95 -- +-----------------+ | | K |
96 -- | Top --------+ | | First (1)
97 -- +-----------------+ +------------------+
98 -- | Default_Size | | Prev |
99 -- +-----------------+ +------------------+
102 type Chunk_Id
(First
, Last
: SS_Ptr
);
103 type Chunk_Ptr
is access all Chunk_Id
;
105 type Chunk_Id
(First
, Last
: SS_Ptr
) is record
106 Prev
, Next
: Chunk_Ptr
;
107 Mem
: Memory
(First
.. Last
);
110 type Stack_Id
is record
112 Default_Size
: SSE
.Storage_Count
;
113 Current_Chunk
: Chunk_Ptr
;
116 type Stack_Ptr
is access Stack_Id
;
117 -- Pointer to record used to represent a dynamically allocated secondary
118 -- stack descriptor for a secondary stack chunk.
120 procedure Free
is new Ada
.Unchecked_Deallocation
(Chunk_Id
, Chunk_Ptr
);
121 -- Free a dynamically allocated chunk
123 function To_Stack_Ptr
is new
124 Ada
.Unchecked_Conversion
(Address
, Stack_Ptr
);
125 function To_Addr
is new
126 Ada
.Unchecked_Conversion
(Stack_Ptr
, Address
);
127 -- Convert to and from address stored in task data structures
129 --------------------------------------------------------------
130 -- Data Structures for Statically Allocated Secondary Stack --
131 --------------------------------------------------------------
133 -- For the static case, the secondary stack is a single contiguous
134 -- chunk of storage, carved out of the primary stack, and represented
135 -- by the following data structure
137 type Fixed_Stack_Id
is record
139 -- Index of next available location in Mem. This is initialized to
140 -- 0, and then incremented on Allocate, and Decremented on Release.
143 -- Length of usable Mem array, which is thus the index past the
144 -- last available location in Mem. Mem (Last-1) can be used. This
145 -- is used to check that the stack does not overflow.
148 -- Maximum value of Top. Initialized to 0, and then may be incremented
149 -- on Allocate, but is never Decremented. The last used location will
150 -- be Mem (Max - 1), so Max is the maximum count of used stack space.
152 Mem
: Memory
(0 .. 0);
153 -- This is the area that is actually used for the secondary stack.
154 -- Note that the upper bound is a dummy value properly defined by
155 -- the value of Last. We never actually allocate objects of type
156 -- Fixed_Stack_Id, so the bounds declared here do not matter.
159 Dummy_Fixed_Stack
: Fixed_Stack_Id
;
160 pragma Warnings
(Off
, Dummy_Fixed_Stack
);
161 -- Well it is not quite true that we never allocate an object of the
162 -- type. This dummy object is allocated for the purpose of getting the
163 -- offset of the Mem field via the 'Position attribute (such a nuisance
164 -- that we cannot apply this to a field of a type).
166 type Fixed_Stack_Ptr
is access Fixed_Stack_Id
;
167 -- Pointer to record used to describe statically allocated sec stack
169 function To_Fixed_Stack_Ptr
is new
170 Ada
.Unchecked_Conversion
(Address
, Fixed_Stack_Ptr
);
171 -- Convert from address stored in task data structures
173 ----------------------------------
174 -- Minimum_Secondary_Stack_Size --
175 ----------------------------------
177 function Minimum_Secondary_Stack_Size
return Natural is
179 return Dummy_Fixed_Stack
.Mem
'Position;
180 end Minimum_Secondary_Stack_Size
;
186 procedure SS_Allocate
188 Storage_Size
: SSE
.Storage_Count
)
190 Max_Align
: constant SS_Ptr
:= SS_Ptr
(Standard
'Maximum_Alignment);
191 Max_Size
: constant SS_Ptr
:=
192 ((SS_Ptr
(Storage_Size
) + Max_Align
- 1) / Max_Align
) *
196 -- Case of fixed allocation secondary stack
198 if not SS_Ratio_Dynamic
then
200 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
201 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
204 -- Check if max stack usage is increasing
206 if Fixed_Stack
.Top
+ Max_Size
> Fixed_Stack
.Max
then
208 -- If so, check if max size is exceeded
210 if Fixed_Stack
.Top
+ Max_Size
> Fixed_Stack
.Last
then
214 -- Record new max usage
216 Fixed_Stack
.Max
:= Fixed_Stack
.Top
+ Max_Size
;
219 -- Set resulting address and update top of stack pointer
221 Addr
:= Fixed_Stack
.Mem
(Fixed_Stack
.Top
)'Address;
222 Fixed_Stack
.Top
:= Fixed_Stack
.Top
+ Max_Size
;
225 -- Case of dynamically allocated secondary stack
229 Stack
: constant Stack_Ptr
:=
230 To_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
233 To_Be_Released_Chunk
: Chunk_Ptr
;
236 Chunk
:= Stack
.Current_Chunk
;
238 -- The Current_Chunk may not be the good one if a lot of release
239 -- operations have taken place. Go down the stack if necessary.
241 while Chunk
.First
> Stack
.Top
loop
245 -- Find out if the available memory in the current chunk is
246 -- sufficient, if not, go to the next one and eventually create
247 -- the necessary room.
249 while Chunk
.Last
- Stack
.Top
+ 1 < Max_Size
loop
250 if Chunk
.Next
/= null then
252 -- Release unused non-first empty chunk
254 if Chunk
.Prev
/= null and then Chunk
.First
= Stack
.Top
then
255 To_Be_Released_Chunk
:= Chunk
;
257 Chunk
.Next
:= To_Be_Released_Chunk
.Next
;
258 To_Be_Released_Chunk
.Next
.Prev
:= Chunk
;
259 Free
(To_Be_Released_Chunk
);
262 -- Create new chunk of default size unless it is not sufficient
263 -- to satisfy the current request.
265 elsif SSE
.Storage_Count
(Max_Size
) <= Stack
.Default_Size
then
268 (First
=> Chunk
.Last
+ 1,
269 Last
=> Chunk
.Last
+ SS_Ptr
(Stack
.Default_Size
));
271 Chunk
.Next
.Prev
:= Chunk
;
273 -- Otherwise create new chunk of requested size
278 (First
=> Chunk
.Last
+ 1,
279 Last
=> Chunk
.Last
+ Max_Size
);
281 Chunk
.Next
.Prev
:= Chunk
;
285 Stack
.Top
:= Chunk
.First
;
288 -- Resulting address is the address pointed by Stack.Top
290 Addr
:= Chunk
.Mem
(Stack
.Top
)'Address;
291 Stack
.Top
:= Stack
.Top
+ Max_Size
;
292 Stack
.Current_Chunk
:= Chunk
;
301 procedure SS_Free
(Stk
: in out Address
) is
303 -- Case of statically allocated secondary stack, nothing to free
305 if not SS_Ratio_Dynamic
then
308 -- Case of dynamically allocated secondary stack
312 Stack
: Stack_Ptr
:= To_Stack_Ptr
(Stk
);
316 new Ada
.Unchecked_Deallocation
(Stack_Id
, Stack_Ptr
);
319 Chunk
:= Stack
.Current_Chunk
;
321 while Chunk
.Prev
/= null loop
325 while Chunk
.Next
/= null loop
341 function SS_Get_Max
return Long_Long_Integer is
343 if SS_Ratio_Dynamic
then
347 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
348 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
350 return Long_Long_Integer (Fixed_Stack
.Max
);
361 Put_Line
("Secondary Stack information:");
363 -- Case of fixed secondary stack
365 if not SS_Ratio_Dynamic
then
367 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
368 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
371 Put_Line
(" Total size : "
372 & SS_Ptr
'Image (Fixed_Stack
.Last
)
375 Put_Line
(" Current allocated space : "
376 & SS_Ptr
'Image (Fixed_Stack
.Top
)
380 -- Case of dynamically allocated secondary stack
384 Stack
: constant Stack_Ptr
:=
385 To_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
386 Nb_Chunks
: Integer := 1;
387 Chunk
: Chunk_Ptr
:= Stack
.Current_Chunk
;
390 while Chunk
.Prev
/= null loop
394 while Chunk
.Next
/= null loop
395 Nb_Chunks
:= Nb_Chunks
+ 1;
399 -- Current Chunk information
401 -- Note that First of each chunk is one more than Last of the
402 -- previous one, so Chunk.Last is the total size of all chunks; we
403 -- don't need to walk all the chunks to compute the total size.
405 Put_Line
(" Total size : "
406 & SS_Ptr
'Image (Chunk
.Last
)
409 Put_Line
(" Current allocated space : "
410 & SS_Ptr
'Image (Stack
.Top
- 1)
413 Put_Line
(" Number of Chunks : "
414 & Integer'Image (Nb_Chunks
));
416 Put_Line
(" Default size of Chunks : "
417 & SSE
.Storage_Count
'Image (Stack
.Default_Size
));
427 (Stk
: in out Address
;
428 Size
: Natural := Default_Secondary_Stack_Size
)
431 -- Case of fixed size secondary stack
433 if not SS_Ratio_Dynamic
then
435 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
436 To_Fixed_Stack_Ptr
(Stk
);
439 Fixed_Stack
.Top
:= 0;
440 Fixed_Stack
.Max
:= 0;
442 if Size
<= Dummy_Fixed_Stack
.Mem
'Position then
443 Fixed_Stack
.Last
:= 0;
446 SS_Ptr
(Size
) - Dummy_Fixed_Stack
.Mem
'Position;
450 -- Case of dynamically allocated secondary stack
456 Stack
:= new Stack_Id
;
457 Stack
.Current_Chunk
:= new Chunk_Id
(1, SS_Ptr
(Size
));
459 Stack
.Default_Size
:= SSE
.Storage_Count
(Size
);
460 Stk
:= To_Addr
(Stack
);
469 function SS_Mark
return Mark_Id
is
470 Sstk
: constant System
.Address
:= SSL
.Get_Sec_Stack_Addr
.all;
472 if SS_Ratio_Dynamic
then
473 return (Sstk
=> Sstk
, Sptr
=> To_Stack_Ptr
(Sstk
).Top
);
475 return (Sstk
=> Sstk
, Sptr
=> To_Fixed_Stack_Ptr
(Sstk
).Top
);
483 procedure SS_Release
(M
: Mark_Id
) is
485 if SS_Ratio_Dynamic
then
486 To_Stack_Ptr
(M
.Sstk
).Top
:= M
.Sptr
;
488 To_Fixed_Stack_Ptr
(M
.Sstk
).Top
:= M
.Sptr
;
492 -------------------------
493 -- Package Elaboration --
494 -------------------------
496 -- Allocate a secondary stack for the main program to use
498 -- We make sure that the stack has maximum alignment. Some systems require
499 -- this (e.g. Sparc), and in any case it is a good idea for efficiency.
501 Stack
: aliased Stack_Id
;
502 for Stack
'Alignment use Standard
'Maximum_Alignment;
504 Static_Secondary_Stack_Size
: constant := 10 * 1024;
505 -- Static_Secondary_Stack_Size must be static so that Chunk is allocated
506 -- statically, and not via dynamic memory allocation.
508 Chunk
: aliased Chunk_Id
(1, Static_Secondary_Stack_Size
);
509 for Chunk
'Alignment use Standard
'Maximum_Alignment;
510 -- Default chunk used, unless gnatbind -D is specified with a value greater
511 -- than Static_Secondary_Stack_Size.
515 Chunk_Address
: Address
;
516 Chunk_Access
: Chunk_Ptr
;
519 if Default_Secondary_Stack_Size
<= Static_Secondary_Stack_Size
then
521 -- Normally we allocate the secondary stack for the main program
522 -- statically, using the default secondary stack size.
524 Chunk_Access
:= Chunk
'Access;
527 -- Default_Secondary_Stack_Size was increased via gnatbind -D, so we
528 -- need to allocate a chunk dynamically.
531 new Chunk_Id
(1, SS_Ptr
(Default_Secondary_Stack_Size
));
534 if SS_Ratio_Dynamic
then
536 Stack
.Current_Chunk
:= Chunk_Access
;
537 Stack
.Default_Size
:=
538 SSE
.Storage_Offset
(Default_Secondary_Stack_Size
);
539 System
.Soft_Links
.Set_Sec_Stack_Addr_NT
(Stack
'Address);
542 Chunk_Address
:= Chunk_Access
.all'Address;
543 SS_Init
(Chunk_Address
, Default_Secondary_Stack_Size
);
544 System
.Soft_Links
.Set_Sec_Stack_Addr_NT
(Chunk_Address
);
547 end System
.Secondary_Stack
;