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 --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with System.Soft_Links;
37 with System.Parameters;
38 with Unchecked_Conversion;
39 with Unchecked_Deallocation;
41 package body System.Secondary_Stack is
43 package SSL renames System.Soft_Links;
45 use type SSE.Storage_Offset;
46 use type System.Parameters.Size_Type;
48 SS_Ratio_Dynamic : constant Boolean :=
49 Parameters.Sec_Stack_Ratio = Parameters.Dynamic;
51 -- +------------------+
53 -- +------------------+
61 -- +------------------+
63 -- | +----------+-------+
67 -- | +-------+----------+
69 -- | +------------------+
73 -- +-----------------+ | +-------->| U |
74 -- | Current_Chunk -|--+ | | N |
75 -- +-----------------+ | | K |
76 -- | Top -|-----+ | | First (1)
77 -- +-----------------+ +------------------+
78 -- | Default_Size | | Prev |
79 -- +-----------------+ +------------------+
82 type Memory is array (Mark_Id range <>) of SSE.Storage_Element;
84 type Chunk_Id (First, Last : Mark_Id);
85 type Chunk_Ptr is access all Chunk_Id;
87 type Chunk_Id (First, Last : Mark_Id) is record
88 Prev, Next : Chunk_Ptr;
89 Mem : Memory (First .. Last);
92 type Stack_Id is record
94 Default_Size : SSE.Storage_Count;
95 Current_Chunk : Chunk_Ptr;
98 type Fixed_Stack_Id is record
101 Mem : Memory (1 .. Mark_Id'Last / 2 - 1);
102 -- This should really be 1 .. Mark_Id'Last, but there is a bug in gigi
103 -- with this type, introduced Sep 2001, that causes gigi to reject this
104 -- type because its size in bytes overflows ???
107 type Stack_Ptr is access Stack_Id;
108 type Fixed_Stack_Ptr is access Fixed_Stack_Id;
110 function From_Addr is new Unchecked_Conversion (Address, Stack_Ptr);
111 function To_Addr is new Unchecked_Conversion (Stack_Ptr, System.Address);
112 function To_Stack is new Unchecked_Conversion (Fixed_Stack_Ptr, Stack_Ptr);
113 function To_Fixed is new Unchecked_Conversion (Stack_Ptr, Fixed_Stack_Ptr);
115 procedure Free is new Unchecked_Deallocation (Chunk_Id, Chunk_Ptr);
121 procedure SS_Allocate
122 (Address : out System.Address;
123 Storage_Size : SSE.Storage_Count)
125 Stack : constant Stack_Ptr :=
126 From_Addr (SSL.Get_Sec_Stack_Addr.all);
127 Fixed_Stack : Fixed_Stack_Ptr;
129 Max_Align : constant Mark_Id := Mark_Id (Standard'Maximum_Alignment);
130 Max_Size : constant Mark_Id :=
131 ((Mark_Id (Storage_Size) + Max_Align - 1) / Max_Align)
134 Count_Unreleased_Chunks : Natural;
135 To_Be_Released_Chunk : Chunk_Ptr;
138 -- If the secondary stack is fixed in the primary stack, then the
139 -- handling becomes simple
141 if not SS_Ratio_Dynamic then
142 Fixed_Stack := To_Fixed (Stack);
144 if Fixed_Stack.Top + Max_Size > Fixed_Stack.Last then
148 Address := Fixed_Stack.Mem (Fixed_Stack.Top)'Address;
149 Fixed_Stack.Top := Fixed_Stack.Top + Mark_Id (Max_Size);
153 Chunk := Stack.Current_Chunk;
155 -- The Current_Chunk may not be the good one if a lot of release
156 -- operations have taken place. So go down the stack if necessary
158 while Chunk.First > Stack.Top loop
162 -- Find out if the available memory in the current chunk is sufficient.
163 -- if not, go to the next one and eventally create the necessary room
165 Count_Unreleased_Chunks := 0;
167 while Chunk.Last - Stack.Top + 1 < Max_Size loop
168 if Chunk.Next /= null then
170 -- Release unused non-first empty chunk
172 if Chunk.Prev /= null and then Chunk.First = Stack.Top then
173 To_Be_Released_Chunk := Chunk;
175 Chunk.Next := To_Be_Released_Chunk.Next;
176 To_Be_Released_Chunk.Next.Prev := Chunk;
177 Free (To_Be_Released_Chunk);
180 -- Create new chunk of the default size unless it is not sufficient
182 elsif SSE.Storage_Count (Max_Size) <= Stack.Default_Size then
183 Chunk.Next := new Chunk_Id (
184 First => Chunk.Last + 1,
185 Last => Chunk.Last + Mark_Id (Stack.Default_Size));
187 Chunk.Next.Prev := Chunk;
190 Chunk.Next := new Chunk_Id (
191 First => Chunk.Last + 1,
192 Last => Chunk.Last + Max_Size);
194 Chunk.Next.Prev := Chunk;
198 Stack.Top := Chunk.First;
201 -- Resulting address is the address pointed by Stack.Top
203 Address := Chunk.Mem (Stack.Top)'Address;
204 Stack.Top := Stack.Top + Max_Size;
205 Stack.Current_Chunk := Chunk;
212 procedure SS_Free (Stk : in out System.Address) is
216 procedure Free is new Unchecked_Deallocation (Stack_Id, Stack_Ptr);
219 if not SS_Ratio_Dynamic then
223 Stack := From_Addr (Stk);
224 Chunk := Stack.Current_Chunk;
226 while Chunk.Prev /= null loop
230 while Chunk.Next /= null loop
245 Stack : constant Stack_Ptr :=
246 From_Addr (SSL.Get_Sec_Stack_Addr.all);
247 Fixed_Stack : Fixed_Stack_Ptr;
248 Nb_Chunks : Integer := 1;
249 Chunk : Chunk_Ptr := Stack.Current_Chunk;
252 Put_Line ("Secondary Stack information:");
254 if not SS_Ratio_Dynamic then
255 Fixed_Stack := To_Fixed (Stack);
258 & Mark_Id'Image (Fixed_Stack.Last)
261 " Current allocated space : "
262 & Mark_Id'Image (Fixed_Stack.Top - 1)
267 while Chunk.Prev /= null loop
271 while Chunk.Next /= null loop
272 Nb_Chunks := Nb_Chunks + 1;
276 -- Current Chunk information
280 & Mark_Id'Image (Chunk.Last)
283 " Current allocated space : "
284 & Mark_Id'Image (Stack.Top - 1)
288 " Number of Chunks : "
289 & Integer'Image (Nb_Chunks));
292 " Default size of Chunks : "
293 & SSE.Storage_Count'Image (Stack.Default_Size));
301 (Stk : in out System.Address;
302 Size : Natural := Default_Secondary_Stack_Size)
305 Fixed_Stack : Fixed_Stack_Ptr;
308 if not SS_Ratio_Dynamic then
309 Fixed_Stack := To_Fixed (From_Addr (Stk));
310 Fixed_Stack.Top := Fixed_Stack.Mem'First;
312 if Size < 2 * Mark_Id'Max_Size_In_Storage_Elements then
313 Fixed_Stack.Last := 0;
315 Fixed_Stack.Last := Mark_Id (Size) -
316 2 * Mark_Id'Max_Size_In_Storage_Elements;
322 Stack := new Stack_Id;
323 Stack.Current_Chunk := new Chunk_Id (1, Mark_Id (Size));
325 Stack.Default_Size := SSE.Storage_Count (Size);
327 Stk := To_Addr (Stack);
334 function SS_Mark return Mark_Id is
336 return From_Addr (SSL.Get_Sec_Stack_Addr.all).Top;
343 procedure SS_Release (M : Mark_Id) is
345 From_Addr (SSL.Get_Sec_Stack_Addr.all).Top := M;
348 -------------------------
349 -- Package Elaboration --
350 -------------------------
352 -- Allocate a secondary stack for the main program to use.
353 -- We make sure that the stack has maximum alignment. Some systems require
354 -- this (e.g. Sun), and in any case it is a good idea for efficiency.
356 Stack : aliased Stack_Id;
357 for Stack'Alignment use Standard'Maximum_Alignment;
359 Chunk : aliased Chunk_Id (1, Default_Secondary_Stack_Size);
360 for Chunk'Alignment use Standard'Maximum_Alignment;
362 Chunk_Address : System.Address;
365 if SS_Ratio_Dynamic then
367 Stack.Current_Chunk := Chunk'Access;
368 Stack.Default_Size := Default_Secondary_Stack_Size;
369 System.Soft_Links.Set_Sec_Stack_Addr_NT (Stack'Address);
372 Chunk_Address := Chunk'Address;
373 SS_Init (Chunk_Address, Default_Secondary_Stack_Size);
374 System.Soft_Links.Set_Sec_Stack_Addr_NT (Chunk_Address);
376 end System.Secondary_Stack;