Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / ada / g-debpoo.ads
blob3d558a8f2693a31a80a72efb4201e9148ab2b78e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D E B U G _ P O O L S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2005, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 packages provides a special implementation of the Ada95 storage pools
36 -- The goal of this debug pool is to detect incorrect uses of memory
37 -- (multiple deallocations, access to invalid memory,...). Errors are reported
38 -- in one of two ways: either by immediately raising an exception, or by
39 -- printing a message on standard output.
41 -- You need to instrument your code to use this package: for each access type
42 -- you want to monitor, you need to add a clause similar to:
44 -- type Integer_Access is access Integer;
45 -- for Integer_Access'Storage_Pool use Pool;
47 -- where Pool is a tagged object declared with
49 -- Pool : GNAT.Debug_Pools.Debug_Pool;
51 -- This package was designed to be as efficient as possible, but still has an
52 -- impact on the performance of your code, which depends on the number of
53 -- allocations, deallocations and, somewhat less, dereferences that your
54 -- application performs.
56 -- For each faulty memory use, this debug pool will print several lines
57 -- of information, including things like the location where the memory
58 -- was initially allocated, the location where it was freed etc.
60 -- Physical allocations and deallocations are done through the usual system
61 -- calls. However, in order to provide proper checks, the debug pool will not
62 -- release the memory immediately. It keeps released memory around (the amount
63 -- kept around is configurable) so that it can distinguish between memory that
64 -- has not been allocated and memory that has been allocated but freed. This
65 -- also means that this memory cannot be reallocated, preventing what would
66 -- otherwise be a false indication that freed memory is now allocated.
68 -- In addition, this package presents several subprograms that help analyze
69 -- the behavior of your program, by reporting memory leaks, the total amount
70 -- of memory that was allocated. The pool is also designed to work correctly
71 -- in conjunction with gnatmem.
73 -- Finally, a subprogram Print_Pool is provided for use from the debugger
75 -- Limitations
76 -- ===========
78 -- Current limitation of this debug pool: if you use this debug pool for a
79 -- general access type ("access all"), the pool might report invalid
80 -- dereferences if the access object is pointing to another object on the
81 -- stack which was not allocated through a call to "new".
83 -- This debug pool will respect all alignments specified in your code, but
84 -- it does that by aligning all objects using Standard'Maximum_Alignment.
85 -- This allows faster checks, and limits the performance impact of using
86 -- this pool.
88 with System; use System;
89 with System.Storage_Elements; use System.Storage_Elements;
90 with System.Checked_Pools;
92 package GNAT.Debug_Pools is
94 type Debug_Pool is new System.Checked_Pools.Checked_Pool with private;
95 -- The new debug pool
97 subtype SSC is System.Storage_Elements.Storage_Count;
99 Default_Max_Freed : constant SSC := 50_000_000;
100 Default_Stack_Trace_Depth : constant Natural := 20;
101 Default_Reset_Content : constant Boolean := False;
102 Default_Raise_Exceptions : constant Boolean := True;
103 Default_Advanced_Scanning : constant Boolean := False;
104 Default_Min_Freed : constant SSC := 0;
105 -- The above values are constants used for the parameters to Configure
106 -- if not overridden in the call. See description of Configure for full
107 -- details on these parameters. If these defaults are not satisfactory,
108 -- then you need to call Configure to change the default values.
110 procedure Configure
111 (Pool : in out Debug_Pool;
112 Stack_Trace_Depth : Natural := Default_Stack_Trace_Depth;
113 Maximum_Logically_Freed_Memory : SSC := Default_Max_Freed;
114 Minimum_To_Free : SSC := Default_Min_Freed;
115 Reset_Content_On_Free : Boolean := Default_Reset_Content;
116 Raise_Exceptions : Boolean := Default_Raise_Exceptions;
117 Advanced_Scanning : Boolean := Default_Advanced_Scanning);
118 -- Subprogram used to configure the debug pool.
120 -- Stack_Trace_Depth. This parameter controls the maximum depth of stack
121 -- traces that are output to indicate locations of actions for error
122 -- conditions such as bad allocations. If set to zero, the debug pool
123 -- will not try to compute backtraces. This is more efficient but gives
124 -- less information on problem locations
126 -- Maximum_Logically_Freed_Memory: maximum amount of memory (bytes)
127 -- that should be kept before starting to physically deallocate some.
128 -- This value should be non-zero, since having memory that is logically
129 -- but not physically freed helps to detect invalid memory accesses.
131 -- Minimum_To_Free is the minimum amount of memory that should be freed
132 -- every time the pool starts physically releasing memory. The algorithm
133 -- to compute which block should be physically released needs some
134 -- expensive initialization (see Advanced_Scanning below), and this
135 -- parameter can be used to limit the performance impact by ensuring
136 -- that a reasonable amount of memory is freed each time. Even in the
137 -- advanced scanning mode, marked blocks may be released to match this
138 -- Minimum_To_Free parameter.
140 -- Reset_Content_On_Free: If true, then the contents of the freed memory
141 -- is reset to the pattern 16#DEADBEEF#, following an old IBM convention.
142 -- This helps in detecting invalid memory references from the debugger.
144 -- Raise_Exceptions: If true, the exceptions below will be raised every
145 -- time an error is detected. If you set this to False, then the action
146 -- is to generate output on standard error, noting the errors, but to
147 -- keep running if possible (of course if storage is badly damaged, this
148 -- attempt may fail. This helps to detect more than one error in a run.
150 -- Advanced_Scanning: If true, the pool will check the contents of all
151 -- allocated blocks before physically releasing memory. Any possible
152 -- reference to a logically free block will prevent its deallocation.
153 -- Note that this algorithm is approximate, and it is recommended
154 -- that you set Minimum_To_Free to a non-zero value to save time.
156 -- All instantiations of this pool use the same internal tables. However,
157 -- they do not store the same amount of information for the tracebacks,
158 -- and they have different counters for maximum logically freed memory.
160 Accessing_Not_Allocated_Storage : exception;
161 -- Exception raised if Raise_Exception is True, and an attempt is made
162 -- to access storage that was never allocated.
164 Accessing_Deallocated_Storage : exception;
165 -- Exception raised if Raise_Exception is True, and an attempt is made
166 -- to access storage that was allocated but has been deallocated.
168 Freeing_Not_Allocated_Storage : exception;
169 -- Exception raised if Raise_Exception is True, and an attempt is made
170 -- to free storage that had not been previously allocated.
172 Freeing_Deallocated_Storage : exception;
173 -- Exception raised if Raise_Exception is True, and an attempt is made
174 -- to free storage that had already been freed.
176 -- Note on the above exceptions. The distinction between not allocated
177 -- and deallocated storage is not guaranteed to be accurate in the case
178 -- where storage is allocated, and then physically freed. Larger values
179 -- of the parameter Maximum_Logically_Freed_Memory will help to guarantee
180 -- that this distinction is made more accurately.
182 generic
183 with procedure Put_Line (S : String) is <>;
184 with procedure Put (S : String) is <>;
185 procedure Print_Info
186 (Pool : Debug_Pool;
187 Cumulate : Boolean := False;
188 Display_Slots : Boolean := False;
189 Display_Leaks : Boolean := False);
190 -- Print out information about the High Water Mark, the current and
191 -- total number of bytes allocated and the total number of bytes
192 -- deallocated.
194 -- If Display_Slots is true, this subprogram prints a list of all the
195 -- locations in the application that have done at least one allocation or
196 -- deallocation. The result might be used to detect places in the program
197 -- where lots of allocations are taking place. This output is not in any
198 -- defined order.
200 -- If Cumulate if True, then each stack trace will display the number of
201 -- allocations that were done either directly, or by the subprograms called
202 -- at that location (e.g: if there were two physical allocations at a->b->c
203 -- and a->b->d, then a->b would be reported as performing two allocations).
205 -- If Display_Leaks is true, then each block that has not been deallocated
206 -- (often called a "memory leak") will be listed, along with the traceback
207 -- showing where it was allocated. Not that no grouping of the blocks is
208 -- done, you should use the Dump_Gnatmem procedure below in conjunction
209 -- with the gnatmem utility.
211 procedure Print_Info_Stdout
212 (Pool : Debug_Pool;
213 Cumulate : Boolean := False;
214 Display_Slots : Boolean := False;
215 Display_Leaks : Boolean := False);
216 -- Standard instantiation of Print_Info to print on standard_output. More
217 -- convenient to use where this is the intended location, and in particular
218 -- easier to use from the debugger.
220 procedure Dump_Gnatmem (Pool : Debug_Pool; File_Name : String);
221 -- Create an external file on the disk, which can be processed by gnatmem
222 -- to display the location of memory leaks.
224 -- This provides a nicer output that Print_Info above, and groups similar
225 -- stack traces together. This also provides an easy way to save the memory
226 -- status of your program for post-mortem analysis.
228 -- To use this file, use the following command line:
229 -- gnatmem 5 -i <File_Name> <Executable_Name>
230 -- If you want all the stack traces to be displayed with 5 levels.
232 procedure Print_Pool (A : System.Address);
233 pragma Export (C, Print_Pool, "print_pool");
234 -- This subprogram is meant to be used from a debugger. Given an address in
235 -- memory, it will print on standard output the known information about
236 -- this address (provided, of course, the matching pointer is handled by
237 -- the Debug_Pool).
239 -- The information includes the stacktrace for the allocation or
240 -- deallocation of that memory chunck, its current status (allocated or
241 -- logically freed), etc.
243 private
244 -- The following are the standard primitive subprograms for a pool
246 procedure Allocate
247 (Pool : in out Debug_Pool;
248 Storage_Address : out Address;
249 Size_In_Storage_Elements : Storage_Count;
250 Alignment : Storage_Count);
251 -- Allocate a new chunk of memory, and set it up so that the debug pool
252 -- can check accesses to its data, and report incorrect access later on.
253 -- The parameters have the same semantics as defined in the ARM95.
255 procedure Deallocate
256 (Pool : in out Debug_Pool;
257 Storage_Address : Address;
258 Size_In_Storage_Elements : Storage_Count;
259 Alignment : Storage_Count);
260 -- Mark a block of memory as invalid. It might not be physically removed
261 -- immediately, depending on the setup of the debug pool, so that checks
262 -- are still possible. The parameters have the same semantics as defined
263 -- in the RM.
265 function Storage_Size (Pool : Debug_Pool) return SSC;
266 -- Return the maximal size of data that can be allocated through Pool.
267 -- Since Pool uses the malloc() system call, all the memory is accessible
268 -- through the pool
270 procedure Dereference
271 (Pool : in out Debug_Pool;
272 Storage_Address : System.Address;
273 Size_In_Storage_Elements : Storage_Count;
274 Alignment : Storage_Count);
275 -- Check whether a derefence statement is valid, ie whether the pointer
276 -- was allocated through Pool. As documented above, errors will be
277 -- reported either by a special error message or an exception, depending
278 -- on the setup of the storage pool.
279 -- The parameters have the same semantics as defined in the ARM95.
281 type Byte_Count is mod System.Max_Binary_Modulus;
282 -- Type used for maintaining byte counts, needs to be large enough
283 -- to accomodate counts allowing for repeated use of the same memory.
285 type Debug_Pool is new System.Checked_Pools.Checked_Pool with record
286 Stack_Trace_Depth : Natural := Default_Stack_Trace_Depth;
287 Maximum_Logically_Freed_Memory : SSC := Default_Max_Freed;
288 Reset_Content_On_Free : Boolean := Default_Reset_Content;
289 Raise_Exceptions : Boolean := Default_Raise_Exceptions;
290 Minimum_To_Free : SSC := Default_Min_Freed;
291 Advanced_Scanning : Boolean := Default_Advanced_Scanning;
293 Allocated : Byte_Count := 0;
294 -- Total number of bytes allocated in this pool
296 Logically_Deallocated : Byte_Count := 0;
297 -- Total number of bytes logically deallocated in this pool. This is the
298 -- memory that the application has released, but that the pool has not
299 -- yet physically released through a call to free(), to detect later
300 -- accesed to deallocated memory.
302 Physically_Deallocated : Byte_Count := 0;
303 -- Total number of bytes that were free()-ed
305 Marked_Blocks_Deallocated : Boolean := False;
306 -- Set to true if some mark blocks had to be deallocated in the advanced
307 -- scanning scheme. Since this is potentially dangereous, this is
308 -- reported to the user, who might want to rerun his program with a
309 -- lower Minimum_To_Free value.
311 High_Water : Byte_Count := 0;
312 -- Maximum of Allocated - Logically_Deallocated - Physically_Deallocated
314 First_Free_Block : System.Address := System.Null_Address;
315 Last_Free_Block : System.Address := System.Null_Address;
316 -- Pointers to the first and last logically freed blocks
318 First_Used_Block : System.Address := System.Null_Address;
319 -- Pointer to the list of currently allocated blocks. This list is
320 -- used to list the memory leaks in the application on exit, as well as
321 -- for the advanced freeing algorithms that needs to traverse all these
322 -- blocks to find possible references to the block being physically
323 -- freed.
324 end record;
325 end GNAT.Debug_Pools;