1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . P O O L _ S I Z E --
9 -- Copyright (C) 1992-2002 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 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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with System.Storage_Elements;
36 with Unchecked_Conversion;
38 package body System.Pool_Size is
40 package SSE renames System.Storage_Elements;
41 use type SSE.Storage_Offset;
43 type Storage_Count_Access is access SSE.Storage_Count;
44 function To_Storage_Count_Access is
45 new Unchecked_Conversion (Address, Storage_Count_Access);
47 SC_Size : constant := SSE.Storage_Count'Object_Size / System.Storage_Unit;
49 package Variable_Size_Management is
51 -- Embedded pool that manages allocation of variable-size data.
53 -- This pool is used as soon as the Elmt_sizS of the pool object is 0.
55 -- Allocation is done on the first chunk long enough for the request.
56 -- Deallocation just puts the freed chunk at the beginning of the list.
58 procedure Initialize (Pool : in out Stack_Bounded_Pool);
60 (Pool : in out Stack_Bounded_Pool;
61 Address : out System.Address;
62 Storage_Size : SSE.Storage_Count;
63 Alignment : SSE.Storage_Count);
66 (Pool : in out Stack_Bounded_Pool;
67 Address : System.Address;
68 Storage_Size : SSE.Storage_Count;
69 Alignment : SSE.Storage_Count);
70 end Variable_Size_Management;
72 package Vsize renames Variable_Size_Management;
79 (Pool : in out Stack_Bounded_Pool;
80 Address : out System.Address;
81 Storage_Size : SSE.Storage_Count;
82 Alignment : SSE.Storage_Count)
85 if Pool.Elmt_Size = 0 then
86 Vsize.Allocate (Pool, Address, Storage_Size, Alignment);
88 elsif Pool.First_Free /= 0 then
89 Address := Pool.The_Pool (Pool.First_Free)'Address;
90 Pool.First_Free := To_Storage_Count_Access (Address).all;
93 Pool.First_Empty <= (Pool.Pool_Size - Pool.Aligned_Elmt_Size + 1)
95 Address := Pool.The_Pool (Pool.First_Empty)'Address;
96 Pool.First_Empty := Pool.First_Empty + Pool.Aligned_Elmt_Size;
108 (Pool : in out Stack_Bounded_Pool;
109 Address : System.Address;
110 Storage_Size : SSE.Storage_Count;
111 Alignment : SSE.Storage_Count)
114 if Pool.Elmt_Size = 0 then
115 Vsize.Deallocate (Pool, Address, Storage_Size, Alignment);
118 To_Storage_Count_Access (Address).all := Pool.First_Free;
119 Pool.First_Free := Address - Pool.The_Pool'Address + 1;
127 procedure Initialize (Pool : in out Stack_Bounded_Pool) is
128 Align : constant SSE.Storage_Count :=
129 SSE.Storage_Count'Max (SSE.Storage_Count'Alignment, Pool.Alignment);
132 if Pool.Elmt_Size = 0 then
133 Vsize.Initialize (Pool);
136 Pool.First_Free := 0;
137 Pool.First_Empty := 1;
139 -- Compute the size to allocate given the size of the element and
140 -- the possible Alignment clause
142 Pool.Aligned_Elmt_Size :=
143 SSE.Storage_Count'Max (SC_Size,
144 ((Pool.Elmt_Size + Align - 1) / Align) * Align);
152 function Storage_Size
153 (Pool : Stack_Bounded_Pool)
154 return SSE.Storage_Count
157 return Pool.Pool_Size;
160 ------------------------------
161 -- Variable_Size_Management --
162 ------------------------------
164 package body Variable_Size_Management is
166 Minimum_Size : constant := 2 * SC_Size;
169 (Pool : Stack_Bounded_Pool;
170 Chunk, Size : SSE.Storage_Count);
171 -- Update the field 'size' of a chunk of available storage
174 (Pool : Stack_Bounded_Pool;
175 Chunk, Next : SSE.Storage_Count);
176 -- Update the field 'next' of a chunk of available storage
179 (Pool : Stack_Bounded_Pool;
180 Chunk : SSE.Storage_Count)
181 return SSE.Storage_Count;
182 -- Fetch the field 'size' of a chunk of available storage
185 (Pool : Stack_Bounded_Pool;
186 Chunk : SSE.Storage_Count)
187 return SSE.Storage_Count;
188 -- Fetch the field 'next' of a chunk of available storage
191 (Pool : Stack_Bounded_Pool;
192 Addr : System.Address)
193 return SSE.Storage_Count;
194 -- Give the chunk number in the pool from its Address
201 (Pool : in out Stack_Bounded_Pool;
202 Address : out System.Address;
203 Storage_Size : SSE.Storage_Count;
204 Alignment : SSE.Storage_Count)
206 Chunk : SSE.Storage_Count;
207 New_Chunk : SSE.Storage_Count;
208 Prev_Chunk : SSE.Storage_Count;
209 Our_Align : constant SSE.Storage_Count :=
210 SSE.Storage_Count'Max (SSE.Storage_Count'Alignment,
212 Align_Size : constant SSE.Storage_Count :=
213 SSE.Storage_Count'Max (
215 ((Storage_Size + Our_Align - 1) / Our_Align) *
219 -- Look for the first big enough chunk
221 Prev_Chunk := Pool.First_Free;
222 Chunk := Next (Pool, Prev_Chunk);
224 while Chunk /= 0 and then Size (Pool, Chunk) < Align_Size loop
226 Chunk := Next (Pool, Chunk);
229 -- Raise storage_error if no big enough chunk available
235 -- When the chunk is bigger than what is needed, take appropraite
236 -- amount and build a new shrinked chunk with the remainder.
238 if Size (Pool, Chunk) - Align_Size > Minimum_Size then
239 New_Chunk := Chunk + Align_Size;
240 Set_Size (Pool, New_Chunk, Size (Pool, Chunk) - Align_Size);
241 Set_Next (Pool, New_Chunk, Next (Pool, Chunk));
242 Set_Next (Pool, Prev_Chunk, New_Chunk);
244 -- If the chunk is the right size, just delete it from the chain
247 Set_Next (Pool, Prev_Chunk, Next (Pool, Chunk));
250 Address := Pool.The_Pool (Chunk)'Address;
258 (Pool : Stack_Bounded_Pool;
259 Addr : System.Address)
260 return SSE.Storage_Count
263 return 1 + abs (Addr - Pool.The_Pool (1)'Address);
271 (Pool : in out Stack_Bounded_Pool;
272 Address : System.Address;
273 Storage_Size : SSE.Storage_Count;
274 Alignment : SSE.Storage_Count)
276 Align_Size : constant SSE.Storage_Count :=
277 ((Storage_Size + Alignment - 1) / Alignment) *
279 Chunk : constant SSE.Storage_Count := Chunk_Of (Pool, Address);
282 -- Attach the freed chunk to the chain
284 Set_Size (Pool, Chunk,
285 SSE.Storage_Count'Max (Align_Size, Minimum_Size));
286 Set_Next (Pool, Chunk, Next (Pool, Pool.First_Free));
287 Set_Next (Pool, Pool.First_Free, Chunk);
295 procedure Initialize (Pool : in out Stack_Bounded_Pool) is
297 Pool.First_Free := 1;
299 if Pool.Pool_Size > Minimum_Size then
300 Set_Next (Pool, Pool.First_Free, Pool.First_Free + Minimum_Size);
301 Set_Size (Pool, Pool.First_Free, 0);
302 Set_Size (Pool, Pool.First_Free + Minimum_Size,
303 Pool.Pool_Size - Minimum_Size);
304 Set_Next (Pool, Pool.First_Free + Minimum_Size, 0);
313 (Pool : Stack_Bounded_Pool;
314 Chunk : SSE.Storage_Count)
315 return SSE.Storage_Count
318 pragma Warnings (Off);
319 -- Kill alignment warnings, we are careful to make sure
320 -- that the alignment is correct.
322 return To_Storage_Count_Access
323 (Pool.The_Pool (Chunk + SC_Size)'Address).all;
325 pragma Warnings (On);
333 (Pool : Stack_Bounded_Pool;
334 Chunk, Next : SSE.Storage_Count)
337 pragma Warnings (Off);
338 -- Kill alignment warnings, we are careful to make sure
339 -- that the alignment is correct.
341 To_Storage_Count_Access
342 (Pool.The_Pool (Chunk + SC_Size)'Address).all := Next;
344 pragma Warnings (On);
352 (Pool : Stack_Bounded_Pool;
353 Chunk, Size : SSE.Storage_Count)
356 pragma Warnings (Off);
357 -- Kill alignment warnings, we are careful to make sure
358 -- that the alignment is correct.
360 To_Storage_Count_Access
361 (Pool.The_Pool (Chunk)'Address).all := Size;
363 pragma Warnings (On);
371 (Pool : Stack_Bounded_Pool;
372 Chunk : SSE.Storage_Count)
373 return SSE.Storage_Count
376 pragma Warnings (Off);
377 -- Kill alignment warnings, we are careful to make sure
378 -- that the alignment is correct.
380 return To_Storage_Count_Access (Pool.The_Pool (Chunk)'Address).all;
382 pragma Warnings (On);
385 end Variable_Size_Management;
386 end System.Pool_Size;