1 | /** @file
|
---|
2 | * IPRT - Memory Tracker.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010-2017 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_memtracker_h
|
---|
27 | #define ___iprt_memtracker_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/list.h>
|
---|
32 |
|
---|
33 | RT_C_DECLS_BEGIN
|
---|
34 |
|
---|
35 | /** @defgroup grp_rt_memtracker RTMemTracker - Memory Allocation Tracker.
|
---|
36 | * @ingroup grp_rt
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * The allocation/free method.
|
---|
42 | */
|
---|
43 | typedef enum RTMEMTRACKERMETHOD
|
---|
44 | {
|
---|
45 | RTMEMTRACKERMETHOD_INVALID = 0,
|
---|
46 | RTMEMTRACKERMETHOD_ALLOC,
|
---|
47 | RTMEMTRACKERMETHOD_ALLOCZ,
|
---|
48 | RTMEMTRACKERMETHOD_REALLOC_PREP, /**< Internal, don't use. */
|
---|
49 | RTMEMTRACKERMETHOD_REALLOC_DONE, /**< Internal, don't use. */
|
---|
50 | RTMEMTRACKERMETHOD_REALLOC_FAILED, /**< Internal, don't use. */
|
---|
51 | RTMEMTRACKERMETHOD_FREE,
|
---|
52 |
|
---|
53 | RTMEMTRACKERMETHOD_NEW,
|
---|
54 | RTMEMTRACKERMETHOD_NEW_ARRAY,
|
---|
55 | RTMEMTRACKERMETHOD_DELETE,
|
---|
56 | RTMEMTRACKERMETHOD_DELETE_ARRAY,
|
---|
57 | RTMEMTRACKERMETHOD_END,
|
---|
58 | RTMEMTRACKERMETHOD_32BIT_HACK = 0x7fffffff
|
---|
59 | } RTMEMTRACKERMETHOD;
|
---|
60 |
|
---|
61 | /** Pointer to a tag structure. */
|
---|
62 | typedef struct RTMEMTRACKERTAG *PRTMEMTRACKERTAG;
|
---|
63 |
|
---|
64 | /** Pointer to a user structure. */
|
---|
65 | typedef struct RTMEMTRACKERUSER *PRTMEMTRACKERUSER;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Memory Tracking Header for use with RTMemTrackerHdrAlloc,
|
---|
69 | * RTMemTrackerHdrReallocPrep, RTMemTrackerHdrReallocDone and
|
---|
70 | * RTMemTrackerHdrFree.
|
---|
71 | */
|
---|
72 | typedef struct RTMEMTRACKERHDR
|
---|
73 | {
|
---|
74 | /** Magic value / eye catcher (RTMEMTRACKERHDR_MAGIC). */
|
---|
75 | size_t uMagic;
|
---|
76 | /** The allocation size, user data only. */
|
---|
77 | size_t cbUser;
|
---|
78 | /** The list entry. */
|
---|
79 | RTLISTNODE ListEntry;
|
---|
80 | /** Pointer to the user structure where this header is linked. */
|
---|
81 | PRTMEMTRACKERUSER pUser;
|
---|
82 | /** Pointer to the per-tag structure. */
|
---|
83 | PRTMEMTRACKERTAG pTag;
|
---|
84 | /** The tag string. */
|
---|
85 | const char *pszTag;
|
---|
86 | /** The caller address. */
|
---|
87 | void *pvCaller;
|
---|
88 | /** Pointer to the user data we're tracking. */
|
---|
89 | void *pvUser;
|
---|
90 | /** Alignment padding. */
|
---|
91 | size_t uReserved;
|
---|
92 | } RTMEMTRACKERHDR;
|
---|
93 | /** Pointer to a memory tracker header. */
|
---|
94 | typedef RTMEMTRACKERHDR *PRTMEMTRACKERHDR;
|
---|
95 | /** Pointer to a const memory tracker header. */
|
---|
96 | typedef RTMEMTRACKERHDR *PPRTMEMTRACKERHDR;
|
---|
97 |
|
---|
98 | /** Magic value for RTMEMTRACKERHDR::uMagic (Kelly Link). */
|
---|
99 | #if ARCH_BITS == 64
|
---|
100 | # define RTMEMTRACKERHDR_MAGIC UINT64_C(0x1907691919690719)
|
---|
101 | #else
|
---|
102 | # define RTMEMTRACKERHDR_MAGIC UINT32_C(0x19690719)
|
---|
103 | #endif
|
---|
104 | /** Magic number used when reallocated. */
|
---|
105 | #if ARCH_BITS == 64
|
---|
106 | # define RTMEMTRACKERHDR_MAGIC_REALLOC UINT64_C(0x0000691919690000)
|
---|
107 | #else
|
---|
108 | # define RTMEMTRACKERHDR_MAGIC_REALLOC UINT32_C(0x19690000)
|
---|
109 | #endif
|
---|
110 | /** Magic number used when freed. */
|
---|
111 | #define RTMEMTRACKERHDR_MAGIC_FREE (~RTMEMTRACKERHDR_MAGIC)
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Initializes the allocation header and links it to the relevant tag.
|
---|
116 | *
|
---|
117 | * @returns Pointer to the user data part.
|
---|
118 | * @param pv The header + user data block. This must be at
|
---|
119 | * least @a cb + sizeof(RTMEMTRACKERHDR).
|
---|
120 | * @param cbUser The user data size (bytes).
|
---|
121 | * @param pszTag The tag string.
|
---|
122 | * @param pvCaller The return address.
|
---|
123 | * @param enmMethod The method that the user called.
|
---|
124 | */
|
---|
125 | RTDECL(void *) RTMemTrackerHdrAlloc(void *pv, size_t cbUser, const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Prepares for a realloc, i.e. invalidates the header.
|
---|
129 | *
|
---|
130 | * @returns Pointer to the user data part.
|
---|
131 | * @param pvOldUser Pointer to the old user data.
|
---|
132 | * @param cbOldUser The size of the old user data, 0 if not
|
---|
133 | * known.
|
---|
134 | * @param pszTag The tag string.
|
---|
135 | * @param pvCaller The return address.
|
---|
136 | */
|
---|
137 | RTDECL(void *) RTMemTrackerHdrReallocPrep(void *pvOldUser, size_t cbOldUser, const char *pszTag, void *pvCaller);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Initializes the allocation header and links it to the relevant tag.
|
---|
141 | *
|
---|
142 | * @returns Pointer to the user data part.
|
---|
143 | * @param pvNew The new header + user data block. This must be
|
---|
144 | * at least @a cb + sizeof(RTMEMTRACKERHDR). If
|
---|
145 | * this is NULL, we assume the realloc() call
|
---|
146 | * failed.
|
---|
147 | * @param cbNewUser The user data size (bytes).
|
---|
148 | * @param pvOldUser Pointer to the old user data. This is only
|
---|
149 | * valid on failure of course and used to bail out
|
---|
150 | * in that case. Should not be NULL.
|
---|
151 | * @param pszTag The tag string.
|
---|
152 | * @param pvCaller The return address.
|
---|
153 | */
|
---|
154 | RTDECL(void *) RTMemTrackerHdrReallocDone(void *pvNew, size_t cbNewUser, void *pvOldUser, const char *pszTag, void *pvCaller);
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Do the accounting on free.
|
---|
159 | *
|
---|
160 | * @returns @a pv.
|
---|
161 | * @param pvUser Pointer to the user data.
|
---|
162 | * @param cbUser The size of the user data, 0 if not known.
|
---|
163 | * @param pszTag The tag string.
|
---|
164 | * @param pvCaller The return address.
|
---|
165 | * @param enmMethod The method that the user called.
|
---|
166 | */
|
---|
167 | RTDECL(void *) RTMemTrackerHdrFree(void *pvUser, size_t cbUser, const char *pszTag, void *pvCaller, RTMEMTRACKERMETHOD enmMethod);
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Dumps all the allocations and tag statistics to the log.
|
---|
172 | */
|
---|
173 | RTDECL(void) RTMemTrackerDumpAllToLog(void);
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Dumps all the allocations and tag statistics to the release log.
|
---|
177 | */
|
---|
178 | RTDECL(void) RTMemTrackerDumpAllToLogRel(void);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Dumps all the allocations and tag statistics to standard out.
|
---|
182 | */
|
---|
183 | RTDECL(void) RTMemTrackerDumpAllToStdOut(void);
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Dumps all the allocations and tag statistics to standard err.
|
---|
187 | */
|
---|
188 | RTDECL(void) RTMemTrackerDumpAllToStdErr(void);
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Dumps all the allocations and tag statistics to the specified filename.
|
---|
192 | */
|
---|
193 | RTDECL(void) RTMemTrackerDumpAllToFile(const char *pszFilename);
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Dumps all the tag statistics to the log.
|
---|
198 | *
|
---|
199 | * @param fVerbose Whether to print all the stats or just the ones
|
---|
200 | * relevant to hunting leaks.
|
---|
201 | */
|
---|
202 | RTDECL(void) RTMemTrackerDumpStatsToLog(bool fVerbose);
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Dumps all the tag statistics to the release log.
|
---|
206 | *
|
---|
207 | * @param fVerbose Whether to print all the stats or just the ones
|
---|
208 | * relevant to hunting leaks.
|
---|
209 | */
|
---|
210 | RTDECL(void) RTMemTrackerDumpStatsToLogRel(bool fVerbose);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Dumps all the tag statistics to standard out.
|
---|
214 | *
|
---|
215 | * @param fVerbose Whether to print all the stats or just the ones
|
---|
216 | * relevant to hunting leaks.
|
---|
217 | */
|
---|
218 | RTDECL(void) RTMemTrackerDumpStatsToStdOut(bool fVerbose);
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Dumps all the tag statistics to standard err.
|
---|
222 | *
|
---|
223 | * @param fVerbose Whether to print all the stats or just the ones
|
---|
224 | * relevant to hunting leaks.
|
---|
225 | */
|
---|
226 | RTDECL(void) RTMemTrackerDumpStatsToStdErr(bool fVerbose);
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Dumps all the tag statistics to the specified filename.
|
---|
230 | *
|
---|
231 | * @param fVerbose Whether to print all the stats or just the ones
|
---|
232 | * relevant to hunting leaks.
|
---|
233 | * @param pszFilename The name of the file to dump to.
|
---|
234 | */
|
---|
235 | RTDECL(void) RTMemTrackerDumpStatsToFile(bool fVerbose, const char *pszFilename);
|
---|
236 |
|
---|
237 |
|
---|
238 |
|
---|
239 | /** @} */
|
---|
240 |
|
---|
241 | RT_C_DECLS_END
|
---|
242 |
|
---|
243 | #endif
|
---|
244 |
|
---|