2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / s-pooloc.adb
blob8272ae333bb33a8a2a57126a7d4f8414f0120b89
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . P O O L _ L O C A L --
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 with System.Memory;
35 with System.Storage_Elements;
36 with System.Address_To_Access_Conversions;
38 package body System.Pool_Local is
40 package SSE renames System.Storage_Elements;
41 use type SSE.Storage_Offset;
43 Pointer_Size : constant SSE.Storage_Offset := Address'Size / Storage_Unit;
44 Pointers_Size : constant SSE.Storage_Offset := 2 * Pointer_Size;
46 type Acc_Address is access all Address;
47 package Addr is new Address_To_Access_Conversions (Address);
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 function Next (A : Address) return Acc_Address;
54 -- Given an address of a block, return an access to the next block
56 function Prev (A : Address) return Acc_Address;
57 -- Given an address of a block, return an access to the previous block
59 --------------
60 -- Allocate --
61 --------------
63 procedure Allocate
64 (Pool : in out Unbounded_Reclaim_Pool;
65 Address : out System.Address;
66 Storage_Size : SSE.Storage_Count;
67 Alignment : SSE.Storage_Count)
69 pragma Warnings (Off, Alignment);
71 Allocated : constant System.Address :=
72 Memory.Alloc
73 (Memory.size_t (Storage_Size + Pointers_Size));
75 begin
76 -- The call to Alloc returns an address whose alignment is compatible
77 -- with the worst case alignment requirement for the machine; thus the
78 -- Alignment argument can be safely ignored.
80 if Allocated = Null_Address then
81 raise Storage_Error;
82 else
83 Address := Allocated + Pointers_Size;
84 Next (Allocated).all := Pool.First;
85 Prev (Allocated).all := Null_Address;
87 if Pool.First /= Null_Address then
88 Prev (Pool.First).all := Allocated;
89 end if;
91 Pool.First := Allocated;
92 end if;
93 end Allocate;
95 ----------------
96 -- Deallocate --
97 ----------------
99 procedure Deallocate
100 (Pool : in out Unbounded_Reclaim_Pool;
101 Address : System.Address;
102 Storage_Size : SSE.Storage_Count;
103 Alignment : SSE.Storage_Count)
105 pragma Warnings (Off, Storage_Size);
106 pragma Warnings (Off, Alignment);
108 Allocated : constant System.Address := Address - Pointers_Size;
110 begin
111 if Prev (Allocated).all = Null_Address then
112 Pool.First := Next (Allocated).all;
113 Prev (Pool.First).all := Null_Address;
114 else
115 Next (Prev (Allocated).all).all := Next (Allocated).all;
116 end if;
118 if Next (Allocated).all /= Null_Address then
119 Prev (Next (Allocated).all).all := Prev (Allocated).all;
120 end if;
122 Memory.Free (Allocated);
123 end Deallocate;
125 --------------
126 -- Finalize --
127 --------------
129 procedure Finalize (Pool : in out Unbounded_Reclaim_Pool) is
130 N : System.Address := Pool.First;
131 Allocated : System.Address;
133 begin
134 while N /= Null_Address loop
135 Allocated := N;
136 N := Next (N).all;
137 Memory.Free (Allocated);
138 end loop;
139 end Finalize;
141 ----------
142 -- Next --
143 ----------
145 function Next (A : Address) return Acc_Address is
146 begin
147 return Acc_Address (Addr.To_Pointer (A));
148 end Next;
150 ----------
151 -- Prev --
152 ----------
154 function Prev (A : Address) return Acc_Address is
155 begin
156 return Acc_Address (Addr.To_Pointer (A + Pointer_Size));
157 end Prev;
159 end System.Pool_Local;