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-2003 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;
35 with System.Soft_Links;
37 with Unchecked_Conversion;
39 package body System.Pool_Size is
41 package SSE renames System.Storage_Elements;
42 use type SSE.Storage_Offset;
44 -- Even though these storage pools are typically only used
45 -- by a single task, if multiple tasks are declared at the
46 -- same or a more nested scope as the storage pool, there
47 -- still may be concurrent access. The current implementation
48 -- of Stack_Bounded_Pool always uses a global lock for protecting
49 -- access. This should eventually be replaced by an atomic
50 -- linked list implementation for efficiency reasons.
52 package SSL renames System.Soft_Links;
54 type Storage_Count_Access is access SSE.Storage_Count;
55 function To_Storage_Count_Access is
56 new Unchecked_Conversion (Address, Storage_Count_Access);
58 SC_Size : constant := SSE.Storage_Count'Object_Size / System.Storage_Unit;
60 package Variable_Size_Management is
62 -- Embedded pool that manages allocation of variable-size data.
64 -- This pool is used as soon as the Elmt_sizS of the pool object is 0.
66 -- Allocation is done on the first chunk long enough for the request.
67 -- Deallocation just puts the freed chunk at the beginning of the list.
69 procedure Initialize (Pool : in out Stack_Bounded_Pool);
71 (Pool : in out Stack_Bounded_Pool;
72 Address : out System.Address;
73 Storage_Size : SSE.Storage_Count;
74 Alignment : SSE.Storage_Count);
77 (Pool : in out Stack_Bounded_Pool;
78 Address : System.Address;
79 Storage_Size : SSE.Storage_Count;
80 Alignment : SSE.Storage_Count);
81 end Variable_Size_Management;
83 package Vsize renames Variable_Size_Management;
90 (Pool : in out Stack_Bounded_Pool;
91 Address : out System.Address;
92 Storage_Size : SSE.Storage_Count;
93 Alignment : SSE.Storage_Count)
98 if Pool.Elmt_Size = 0 then
99 Vsize.Allocate (Pool, Address, Storage_Size, Alignment);
101 elsif Pool.First_Free /= 0 then
102 Address := Pool.The_Pool (Pool.First_Free)'Address;
103 Pool.First_Free := To_Storage_Count_Access (Address).all;
106 Pool.First_Empty <= (Pool.Pool_Size - Pool.Aligned_Elmt_Size + 1)
108 Address := Pool.The_Pool (Pool.First_Empty)'Address;
109 Pool.First_Empty := Pool.First_Empty + Pool.Aligned_Elmt_Size;
128 (Pool : in out Stack_Bounded_Pool;
129 Address : System.Address;
130 Storage_Size : SSE.Storage_Count;
131 Alignment : SSE.Storage_Count)
136 if Pool.Elmt_Size = 0 then
137 Vsize.Deallocate (Pool, Address, Storage_Size, Alignment);
140 To_Storage_Count_Access (Address).all := Pool.First_Free;
141 Pool.First_Free := Address - Pool.The_Pool'Address + 1;
155 procedure Initialize (Pool : in out Stack_Bounded_Pool) is
156 Align : constant SSE.Storage_Count :=
157 SSE.Storage_Count'Max (SSE.Storage_Count'Alignment, Pool.Alignment);
160 if Pool.Elmt_Size = 0 then
161 Vsize.Initialize (Pool);
164 Pool.First_Free := 0;
165 Pool.First_Empty := 1;
167 -- Compute the size to allocate given the size of the element and
168 -- the possible Alignment clause
170 Pool.Aligned_Elmt_Size :=
171 SSE.Storage_Count'Max (SC_Size,
172 ((Pool.Elmt_Size + Align - 1) / Align) * Align);
180 function Storage_Size
181 (Pool : Stack_Bounded_Pool)
182 return SSE.Storage_Count
185 return Pool.Pool_Size;
188 ------------------------------
189 -- Variable_Size_Management --
190 ------------------------------
192 package body Variable_Size_Management is
194 Minimum_Size : constant := 2 * SC_Size;
197 (Pool : Stack_Bounded_Pool;
198 Chunk, Size : SSE.Storage_Count);
199 -- Update the field 'size' of a chunk of available storage
202 (Pool : Stack_Bounded_Pool;
203 Chunk, Next : SSE.Storage_Count);
204 -- Update the field 'next' of a chunk of available storage
207 (Pool : Stack_Bounded_Pool;
208 Chunk : SSE.Storage_Count)
209 return SSE.Storage_Count;
210 -- Fetch the field 'size' of a chunk of available storage
213 (Pool : Stack_Bounded_Pool;
214 Chunk : SSE.Storage_Count)
215 return SSE.Storage_Count;
216 -- Fetch the field 'next' of a chunk of available storage
219 (Pool : Stack_Bounded_Pool;
220 Addr : System.Address)
221 return SSE.Storage_Count;
222 -- Give the chunk number in the pool from its Address
229 (Pool : in out Stack_Bounded_Pool;
230 Address : out System.Address;
231 Storage_Size : SSE.Storage_Count;
232 Alignment : SSE.Storage_Count)
234 Chunk : SSE.Storage_Count;
235 New_Chunk : SSE.Storage_Count;
236 Prev_Chunk : SSE.Storage_Count;
237 Our_Align : constant SSE.Storage_Count :=
238 SSE.Storage_Count'Max (SSE.Storage_Count'Alignment,
240 Align_Size : constant SSE.Storage_Count :=
241 SSE.Storage_Count'Max (
243 ((Storage_Size + Our_Align - 1) / Our_Align) *
247 -- Look for the first big enough chunk
249 Prev_Chunk := Pool.First_Free;
250 Chunk := Next (Pool, Prev_Chunk);
252 while Chunk /= 0 and then Size (Pool, Chunk) < Align_Size loop
254 Chunk := Next (Pool, Chunk);
257 -- Raise storage_error if no big enough chunk available
263 -- When the chunk is bigger than what is needed, take appropraite
264 -- amount and build a new shrinked chunk with the remainder.
266 if Size (Pool, Chunk) - Align_Size > Minimum_Size then
267 New_Chunk := Chunk + Align_Size;
268 Set_Size (Pool, New_Chunk, Size (Pool, Chunk) - Align_Size);
269 Set_Next (Pool, New_Chunk, Next (Pool, Chunk));
270 Set_Next (Pool, Prev_Chunk, New_Chunk);
272 -- If the chunk is the right size, just delete it from the chain
275 Set_Next (Pool, Prev_Chunk, Next (Pool, Chunk));
278 Address := Pool.The_Pool (Chunk)'Address;
286 (Pool : Stack_Bounded_Pool;
287 Addr : System.Address)
288 return SSE.Storage_Count
291 return 1 + abs (Addr - Pool.The_Pool (1)'Address);
299 (Pool : in out Stack_Bounded_Pool;
300 Address : System.Address;
301 Storage_Size : SSE.Storage_Count;
302 Alignment : SSE.Storage_Count)
304 Align_Size : constant SSE.Storage_Count :=
305 ((Storage_Size + Alignment - 1) / Alignment) *
307 Chunk : constant SSE.Storage_Count := Chunk_Of (Pool, Address);
310 -- Attach the freed chunk to the chain
312 Set_Size (Pool, Chunk,
313 SSE.Storage_Count'Max (Align_Size, Minimum_Size));
314 Set_Next (Pool, Chunk, Next (Pool, Pool.First_Free));
315 Set_Next (Pool, Pool.First_Free, Chunk);
323 procedure Initialize (Pool : in out Stack_Bounded_Pool) is
325 Pool.First_Free := 1;
327 if Pool.Pool_Size > Minimum_Size then
328 Set_Next (Pool, Pool.First_Free, Pool.First_Free + Minimum_Size);
329 Set_Size (Pool, Pool.First_Free, 0);
330 Set_Size (Pool, Pool.First_Free + Minimum_Size,
331 Pool.Pool_Size - Minimum_Size);
332 Set_Next (Pool, Pool.First_Free + Minimum_Size, 0);
341 (Pool : Stack_Bounded_Pool;
342 Chunk : SSE.Storage_Count)
343 return SSE.Storage_Count
346 pragma Warnings (Off);
347 -- Kill alignment warnings, we are careful to make sure
348 -- that the alignment is correct.
350 return To_Storage_Count_Access
351 (Pool.The_Pool (Chunk + SC_Size)'Address).all;
353 pragma Warnings (On);
361 (Pool : Stack_Bounded_Pool;
362 Chunk, Next : SSE.Storage_Count)
365 pragma Warnings (Off);
366 -- Kill alignment warnings, we are careful to make sure
367 -- that the alignment is correct.
369 To_Storage_Count_Access
370 (Pool.The_Pool (Chunk + SC_Size)'Address).all := Next;
372 pragma Warnings (On);
380 (Pool : Stack_Bounded_Pool;
381 Chunk, Size : SSE.Storage_Count)
384 pragma Warnings (Off);
385 -- Kill alignment warnings, we are careful to make sure
386 -- that the alignment is correct.
388 To_Storage_Count_Access
389 (Pool.The_Pool (Chunk)'Address).all := Size;
391 pragma Warnings (On);
399 (Pool : Stack_Bounded_Pool;
400 Chunk : SSE.Storage_Count)
401 return SSE.Storage_Count
404 pragma Warnings (Off);
405 -- Kill alignment warnings, we are careful to make sure
406 -- that the alignment is correct.
408 return To_Storage_Count_Access (Pool.The_Pool (Chunk)'Address).all;
410 pragma Warnings (On);
413 end Variable_Size_Management;
414 end System.Pool_Size;