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
177 procedure SS_Allocate
179 Storage_Size
: SSE
.Storage_Count
)
181 Max_Align
: constant SS_Ptr
:= SS_Ptr
(Standard
'Maximum_Alignment);
182 Max_Size
: constant SS_Ptr
:=
183 ((SS_Ptr
(Storage_Size
) + Max_Align
- 1) / Max_Align
) *
187 -- Case of fixed allocation secondary stack
189 if not SS_Ratio_Dynamic
then
191 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
192 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
195 -- Check if max stack usage is increasing
197 if Fixed_Stack
.Top
+ Max_Size
> Fixed_Stack
.Max
then
199 -- If so, check if max size is exceeded
201 if Fixed_Stack
.Top
+ Max_Size
> Fixed_Stack
.Last
then
205 -- Record new max usage
207 Fixed_Stack
.Max
:= Fixed_Stack
.Top
+ Max_Size
;
210 -- Set resulting address and update top of stack pointer
212 Addr
:= Fixed_Stack
.Mem
(Fixed_Stack
.Top
)'Address;
213 Fixed_Stack
.Top
:= Fixed_Stack
.Top
+ Max_Size
;
216 -- Case of dynamically allocated secondary stack
220 Stack
: constant Stack_Ptr
:=
221 To_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
224 To_Be_Released_Chunk
: Chunk_Ptr
;
227 Chunk
:= Stack
.Current_Chunk
;
229 -- The Current_Chunk may not be the good one if a lot of release
230 -- operations have taken place. Go down the stack if necessary.
232 while Chunk
.First
> Stack
.Top
loop
236 -- Find out if the available memory in the current chunk is
237 -- sufficient, if not, go to the next one and eventually create
238 -- the necessary room.
240 while Chunk
.Last
- Stack
.Top
+ 1 < Max_Size
loop
241 if Chunk
.Next
/= null then
243 -- Release unused non-first empty chunk
245 if Chunk
.Prev
/= null and then Chunk
.First
= Stack
.Top
then
246 To_Be_Released_Chunk
:= Chunk
;
248 Chunk
.Next
:= To_Be_Released_Chunk
.Next
;
249 To_Be_Released_Chunk
.Next
.Prev
:= Chunk
;
250 Free
(To_Be_Released_Chunk
);
253 -- Create new chunk of default size unless it is not sufficient
254 -- to satisfy the current request.
256 elsif SSE
.Storage_Count
(Max_Size
) <= Stack
.Default_Size
then
259 (First
=> Chunk
.Last
+ 1,
260 Last
=> Chunk
.Last
+ SS_Ptr
(Stack
.Default_Size
));
262 Chunk
.Next
.Prev
:= Chunk
;
264 -- Otherwise create new chunk of requested size
269 (First
=> Chunk
.Last
+ 1,
270 Last
=> Chunk
.Last
+ Max_Size
);
272 Chunk
.Next
.Prev
:= Chunk
;
276 Stack
.Top
:= Chunk
.First
;
279 -- Resulting address is the address pointed by Stack.Top
281 Addr
:= Chunk
.Mem
(Stack
.Top
)'Address;
282 Stack
.Top
:= Stack
.Top
+ Max_Size
;
283 Stack
.Current_Chunk
:= Chunk
;
292 procedure SS_Free
(Stk
: in out Address
) is
294 -- Case of statically allocated secondary stack, nothing to free
296 if not SS_Ratio_Dynamic
then
299 -- Case of dynamically allocated secondary stack
303 Stack
: Stack_Ptr
:= To_Stack_Ptr
(Stk
);
307 new Ada
.Unchecked_Deallocation
(Stack_Id
, Stack_Ptr
);
310 Chunk
:= Stack
.Current_Chunk
;
312 while Chunk
.Prev
/= null loop
316 while Chunk
.Next
/= null loop
332 function SS_Get_Max
return Long_Long_Integer is
334 if SS_Ratio_Dynamic
then
338 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
339 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
341 return Long_Long_Integer (Fixed_Stack
.Max
);
352 Put_Line
("Secondary Stack information:");
354 -- Case of fixed secondary stack
356 if not SS_Ratio_Dynamic
then
358 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
359 To_Fixed_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
364 & SS_Ptr
'Image (Fixed_Stack
.Last
)
368 " Current allocated space : "
369 & SS_Ptr
'Image (Fixed_Stack
.Top
- 1)
373 -- Case of dynamically allocated secondary stack
377 Stack
: constant Stack_Ptr
:=
378 To_Stack_Ptr
(SSL
.Get_Sec_Stack_Addr
.all);
379 Nb_Chunks
: Integer := 1;
380 Chunk
: Chunk_Ptr
:= Stack
.Current_Chunk
;
383 while Chunk
.Prev
/= null loop
387 while Chunk
.Next
/= null loop
388 Nb_Chunks
:= Nb_Chunks
+ 1;
392 -- Current Chunk information
396 & SS_Ptr
'Image (Chunk
.Last
)
400 " Current allocated space : "
401 & SS_Ptr
'Image (Stack
.Top
- 1)
405 " Number of Chunks : "
406 & Integer'Image (Nb_Chunks
));
409 " Default size of Chunks : "
410 & SSE
.Storage_Count
'Image (Stack
.Default_Size
));
420 (Stk
: in out Address
;
421 Size
: Natural := Default_Secondary_Stack_Size
)
424 -- Case of fixed size secondary stack
426 if not SS_Ratio_Dynamic
then
428 Fixed_Stack
: constant Fixed_Stack_Ptr
:=
429 To_Fixed_Stack_Ptr
(Stk
);
432 Fixed_Stack
.Top
:= 0;
433 Fixed_Stack
.Max
:= 0;
435 if Size
< Dummy_Fixed_Stack
.Mem
'Position then
436 Fixed_Stack
.Last
:= 0;
439 SS_Ptr
(Size
) - Dummy_Fixed_Stack
.Mem
'Position;
443 -- Case of dynamically allocated secondary stack
449 Stack
:= new Stack_Id
;
450 Stack
.Current_Chunk
:= new Chunk_Id
(1, SS_Ptr
(Size
));
452 Stack
.Default_Size
:= SSE
.Storage_Count
(Size
);
453 Stk
:= To_Addr
(Stack
);
462 function SS_Mark
return Mark_Id
is
463 Sstk
: constant System
.Address
:= SSL
.Get_Sec_Stack_Addr
.all;
465 if SS_Ratio_Dynamic
then
466 return (Sstk
=> Sstk
, Sptr
=> To_Stack_Ptr
(Sstk
).Top
);
468 return (Sstk
=> Sstk
, Sptr
=> To_Fixed_Stack_Ptr
(Sstk
).Top
);
476 procedure SS_Release
(M
: Mark_Id
) is
478 if SS_Ratio_Dynamic
then
479 To_Stack_Ptr
(M
.Sstk
).Top
:= M
.Sptr
;
481 To_Fixed_Stack_Ptr
(M
.Sstk
).Top
:= M
.Sptr
;
485 -------------------------
486 -- Package Elaboration --
487 -------------------------
489 -- Allocate a secondary stack for the main program to use
491 -- We make sure that the stack has maximum alignment. Some systems require
492 -- this (e.g. Sparc), and in any case it is a good idea for efficiency.
494 Stack
: aliased Stack_Id
;
495 for Stack
'Alignment use Standard
'Maximum_Alignment;
497 Static_Secondary_Stack_Size
: constant := 10 * 1024;
498 -- Static_Secondary_Stack_Size must be static so that Chunk is allocated
499 -- statically, and not via dynamic memory allocation.
501 Chunk
: aliased Chunk_Id
(1, Static_Secondary_Stack_Size
);
502 for Chunk
'Alignment use Standard
'Maximum_Alignment;
503 -- Default chunk used, unless gnatbind -D is specified with a value greater
504 -- than Static_Secondary_Stack_Size.
508 Chunk_Address
: Address
;
509 Chunk_Access
: Chunk_Ptr
;
512 if Default_Secondary_Stack_Size
<= Static_Secondary_Stack_Size
then
514 -- Normally we allocate the secondary stack for the main program
515 -- statically, using the default secondary stack size.
517 Chunk_Access
:= Chunk
'Access;
520 -- Default_Secondary_Stack_Size was increased via gnatbind -D, so we
521 -- need to allocate a chunk dynamically.
524 new Chunk_Id
(1, SS_Ptr
(Default_Secondary_Stack_Size
));
527 if SS_Ratio_Dynamic
then
529 Stack
.Current_Chunk
:= Chunk_Access
;
530 Stack
.Default_Size
:=
531 SSE
.Storage_Offset
(Default_Secondary_Stack_Size
);
532 System
.Soft_Links
.Set_Sec_Stack_Addr_NT
(Stack
'Address);
535 Chunk_Address
:= Chunk_Access
.all'Address;
536 SS_Init
(Chunk_Address
, Default_Secondary_Stack_Size
);
537 System
.Soft_Links
.Set_Sec_Stack_Addr_NT
(Chunk_Address
);
540 end System
.Secondary_Stack
;