2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / 1ssecsta.adb
blob3f3bf78be90c3e93c9bb29bf9287a26536995390
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-2001 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is the HI-E version of this package.
36 with Unchecked_Conversion;
38 package body System.Secondary_Stack is
40 use type SSE.Storage_Offset;
42 type Memory is array (Mark_Id range <>) of SSE.Storage_Element;
44 type Stack_Id is record
45 Top : Mark_Id;
46 Last : Mark_Id;
47 Mem : Memory (1 .. Mark_Id'Last);
48 end record;
49 pragma Suppress_Initialization (Stack_Id);
51 type Stack_Ptr is access Stack_Id;
53 function From_Addr is new Unchecked_Conversion (Address, Stack_Ptr);
55 function Get_Sec_Stack return Stack_Ptr;
56 pragma Import (C, Get_Sec_Stack, "__gnat_get_secondary_stack");
57 -- Return the address of the secondary stack.
58 -- In a multi-threaded environment, Sec_Stack should be a thread-local
59 -- variable.
61 -- Possible implementation of Get_Sec_Stack in a single-threaded
62 -- environment:
64 -- Chunk : aliased Memory (1 .. Default_Secondary_Stack_Size);
65 -- for Chunk'Alignment use Standard'Maximum_Alignment;
66 -- -- The secondary stack.
68 -- function Get_Sec_Stack return Stack_Ptr is
69 -- begin
70 -- return From_Addr (Chunk'Address);
71 -- end Get_Sec_Stack;
73 -- begin
74 -- SS_Init (Chunk'Address, Default_Secondary_Stack_Size);
75 -- end System.Secondary_Stack;
77 -----------------
78 -- SS_Allocate --
79 -----------------
81 procedure SS_Allocate
82 (Address : out System.Address;
83 Storage_Size : SSE.Storage_Count)
85 Max_Align : constant Mark_Id := Mark_Id (Standard'Maximum_Alignment);
86 Max_Size : constant Mark_Id :=
87 ((Mark_Id (Storage_Size) + Max_Align - 1) / Max_Align)
88 * Max_Align;
89 Sec_Stack : constant Stack_Ptr := Get_Sec_Stack;
91 begin
92 if Sec_Stack.Top + Max_Size > Sec_Stack.Last then
93 raise Storage_Error;
94 end if;
96 Address := Sec_Stack.Mem (Sec_Stack.Top)'Address;
97 Sec_Stack.Top := Sec_Stack.Top + Mark_Id (Max_Size);
98 end SS_Allocate;
100 -------------
101 -- SS_Free --
102 -------------
104 procedure SS_Free (Stk : in out System.Address) is
105 begin
106 Stk := Null_Address;
107 end SS_Free;
109 -------------
110 -- SS_Init --
111 -------------
113 procedure SS_Init
114 (Stk : System.Address;
115 Size : Natural := Default_Secondary_Stack_Size)
117 Stack : Stack_Ptr := From_Addr (Stk);
118 begin
119 pragma Assert (Size >= 2 * Mark_Id'Max_Size_In_Storage_Elements);
121 Stack.Top := Stack.Mem'First;
122 Stack.Last := Mark_Id (Size) - 2 * Mark_Id'Max_Size_In_Storage_Elements;
123 end SS_Init;
125 -------------
126 -- SS_Mark --
127 -------------
129 function SS_Mark return Mark_Id is
130 begin
131 return Get_Sec_Stack.Top;
132 end SS_Mark;
134 ----------------
135 -- SS_Release --
136 ----------------
138 procedure SS_Release (M : Mark_Id) is
139 begin
140 Get_Sec_Stack.Top := M;
141 end SS_Release;
143 end System.Secondary_Stack;