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