| 1 |
/* |
| 2 |
* $LynxId: LYLeaks.h,v 1.19 2020/01/21 22:05:46 tom Exp $ |
| 3 |
*/ |
| 4 |
#ifndef __LYLEAKS_H |
| 5 |
/* |
| 6 |
* Avoid include redundancy |
| 7 |
* Include only if finding memory leaks. |
| 8 |
*/ |
| 9 |
#define __LYLEAKS_H |
| 10 |
|
| 11 |
/* |
| 12 |
* Copyright (c) 1994, University of Kansas, All Rights Reserved |
| 13 |
* |
| 14 |
* Include File: LYLeaks.h |
| 15 |
* Purpose: Header to convert requests for allocation to Lynx |
| 16 |
* custom functions to track memory leaks. |
| 17 |
* Remarks/Portability/Dependencies/Restrictions: |
| 18 |
* For the stdlib.h allocation functions to be overridden by the |
| 19 |
* Lynx memory tracking functions all modules allocating, |
| 20 |
* freeing, or resizing memory must have LY_FIND_LEAKS |
| 21 |
* defined before including this file. |
| 22 |
* This header file should be included in every source file which |
| 23 |
* does any memory manipulation through use of the |
| 24 |
* stdlib.h memory functions. |
| 25 |
* For proper reporting of memory leaks, the function LYLeaks |
| 26 |
* should be registered for execution by atexit as the |
| 27 |
* very first executable statement in main. |
| 28 |
* This code is slow and should not be used except in debugging |
| 29 |
* circumstances (don't define LY_FIND_LEAKS). |
| 30 |
* If you are using LY_FIND_LEAKS and don't want the LYLeak* |
| 31 |
* memory functions to be used in a certain file, |
| 32 |
* define NO_MEMORY_TRACKING before including this file. |
| 33 |
* The only safe way to call the LYLeak* functions is to use |
| 34 |
* the below macros because they depend on the static |
| 35 |
* string created by __FILE__ to not be dynamic in |
| 36 |
* nature (don't free it and assume will exist at all |
| 37 |
* times during execution). |
| 38 |
* If you are using LY_FIND_LEAKS and LY_FIND_LEAKS_EXTENDED and |
| 39 |
* want only normal memory tracking (not extended for |
| 40 |
* HTSprintf/HTSprintf0) to be used in a certain file, |
| 41 |
* define NO_EXTENDED_MEMORY_TRACKING and don't define |
| 42 |
* NO_MEMORY_TRACKING before including this file. |
| 43 |
* Revision History: |
| 44 |
* 05-26-94 created for Lynx 2-3-1, Garrett Arch Blythe |
| 45 |
* 10-30-97 modified to handle StrAllocCopy() and |
| 46 |
* StrAllocCat(). - KW & FM |
| 47 |
* 1999-10-17 modified to handle HTSprintf0 and HTSprintf(), |
| 48 |
* and to provide mark_malloced, if |
| 49 |
* LY_FIND_LEAKS_EXTENDED is defined. - kw |
| 50 |
* 2003-01-22 add sequence-id for counting mallocs/frees -TD |
| 51 |
* 2004-04-27 ANSIfy'd -TD |
| 52 |
* 2012-02-09 add bstring interfaces -TD |
| 53 |
*/ |
| 54 |
|
| 55 |
/* Undefine this to get no improved HTSprintf0/HTSprintf tracking: */ |
| 56 |
#define LY_FIND_LEAKS_EXTENDED |
| 57 |
|
| 58 |
/* |
| 59 |
* Required includes |
| 60 |
*/ |
| 61 |
|
| 62 |
#ifndef HTUTILS_H |
| 63 |
#include <HTUtils.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifdef __cplusplus |
| 67 |
extern "C" { |
| 68 |
#endif |
| 69 |
/* |
| 70 |
* Constant defines |
| 71 |
*/ |
| 72 |
#define MAX_CONTENT_LENGTH 50 |
| 73 |
#ifdef VMS |
| 74 |
#define LEAKAGE_SINK "sys$login:Lynx.leaks" |
| 75 |
#else |
| 76 |
#define LEAKAGE_SINK "Lynx.leaks" |
| 77 |
#endif /* VMS */ |
| 78 |
/* |
| 79 |
* Data structures |
| 80 |
*/ |
| 81 |
typedef struct SourceLocation_tag { |
| 82 |
/* |
| 83 |
* The file name and line number of where an event took place. |
| 84 |
*/ |
| 85 |
const char *cp_FileName; |
| 86 |
short ssi_LineNumber; |
| 87 |
} SourceLocation; |
| 88 |
|
| 89 |
typedef struct AllocationList_tag { |
| 90 |
/* |
| 91 |
* A singly linked list. |
| 92 |
*/ |
| 93 |
struct AllocationList_tag *ALp_Next; |
| 94 |
|
| 95 |
/* |
| 96 |
* Count the number of mallocs. |
| 97 |
*/ |
| 98 |
long st_Sequence; |
| 99 |
|
| 100 |
/* |
| 101 |
* The memory pointer allocated. If set to NULL, then an invalid request |
| 102 |
* was made. The invalid pointer also. |
| 103 |
*/ |
| 104 |
void *vp_Alloced; |
| 105 |
void *vp_BadRequest; |
| 106 |
|
| 107 |
/* |
| 108 |
* The size in bytes of the allocated memory. |
| 109 |
*/ |
| 110 |
size_t st_Bytes; |
| 111 |
|
| 112 |
/* |
| 113 |
* The source location of specific event (calloc, malloc, free). realloc |
| 114 |
* kept separate since will track last realloc on pointer. |
| 115 |
*/ |
| 116 |
SourceLocation SL_memory; |
| 117 |
SourceLocation SL_realloc; |
| 118 |
} AllocationList; |
| 119 |
|
| 120 |
/* |
| 121 |
* Global variable declarations |
| 122 |
*/ |
| 123 |
extern char LYLeaksPath[]; |
| 124 |
|
| 125 |
/* |
| 126 |
* Macros |
| 127 |
*/ |
| 128 |
#if defined(LY_FIND_LEAKS) && !defined(NO_MEMORY_TRACKING) |
| 129 |
/* |
| 130 |
* Only use these macros if we are to track memory allocations. The reason for |
| 131 |
* using a macro instead of a define is that we want to track where the initial |
| 132 |
* allocation took place or where the last reallocation took place. Track |
| 133 |
* where the allocation took place by the __FILE__ and __LINE__ defines which |
| 134 |
* are automatic to the compiler. |
| 135 |
*/ |
| 136 |
#ifdef malloc |
| 137 |
#undef malloc |
| 138 |
#endif /* malloc */ |
| 139 |
#define malloc(st_bytes) LYLeakMalloc(st_bytes, __FILE__, __LINE__) |
| 140 |
|
| 141 |
#ifdef calloc |
| 142 |
#undef calloc |
| 143 |
#endif /* calloc */ |
| 144 |
#define calloc(st_number, st_bytes) LYLeakCalloc(st_number, st_bytes, \ |
| 145 |
__FILE__, __LINE__) |
| 146 |
|
| 147 |
#ifdef realloc |
| 148 |
#undef realloc |
| 149 |
#endif /* realloc */ |
| 150 |
#define realloc(vp_alloced, st_newbytes) LYLeakRealloc(vp_alloced, \ |
| 151 |
st_newbytes, __FILE__, __LINE__) |
| 152 |
|
| 153 |
#ifdef free |
| 154 |
#undef free |
| 155 |
#endif /* free */ |
| 156 |
#define free(vp_alloced) LYLeakFree(vp_alloced, __FILE__, __LINE__) |
| 157 |
|
| 158 |
#ifdef strdup |
| 159 |
#undef strdup |
| 160 |
#endif /* free */ |
| 161 |
#define strdup(vp_alloced) LYLeakStrdup(vp_alloced, __FILE__, __LINE__) |
| 162 |
|
| 163 |
/* |
| 164 |
* Added the following two defines to track Lynx's frequent use of those |
| 165 |
* macros. - KW 1997-10-12 |
| 166 |
*/ |
| 167 |
#ifdef StrAllocCopy |
| 168 |
#undef StrAllocCopy |
| 169 |
#endif /* StrAllocCopy */ |
| 170 |
#define StrAllocCopy(dest, src) LYLeakSACopy(&(dest), src, __FILE__, __LINE__) |
| 171 |
|
| 172 |
#ifdef StrAllocCat |
| 173 |
#undef StrAllocCat |
| 174 |
#endif /* StrAllocCat */ |
| 175 |
#define StrAllocCat(dest, src) LYLeakSACat(&(dest), src, __FILE__, __LINE__) |
| 176 |
|
| 177 |
#ifdef BStrAlloc |
| 178 |
#undef BStrAlloc |
| 179 |
#endif |
| 180 |
#define BStrAlloc(d,n) LYLeakSABAlloc( &(d), n, __FILE__, __LINE__) |
| 181 |
|
| 182 |
#ifdef BStrCopy |
| 183 |
#undef BStrCopy |
| 184 |
#endif |
| 185 |
#define BStrCopy(d,s) LYLeakSABCopy( &(d), BStrData(s), BStrLen(s), __FILE__, __LINE__) |
| 186 |
|
| 187 |
#ifdef BStrCopy0 |
| 188 |
#undef BStrCopy0 |
| 189 |
#endif |
| 190 |
#define BStrCopy0(d,s) LYLeakSABCopy0( &(d), s, __FILE__, __LINE__) |
| 191 |
|
| 192 |
#ifdef BStrCat |
| 193 |
#undef BStrCat |
| 194 |
#endif |
| 195 |
#define BStrCat(d,s) LYLeakSABCat( &(d), BStrData(s), BStrLen(s), __FILE__, __LINE__) |
| 196 |
|
| 197 |
#ifdef BStrCat0 |
| 198 |
#undef BStrCat0 |
| 199 |
#endif |
| 200 |
#define BStrCat0(d,s) LYLeakSABCat0( &(d), s, __FILE__, __LINE__) |
| 201 |
|
| 202 |
#define mark_malloced(a,size) LYLeak_mark_malloced(a,size, __FILE__, __LINE__) |
| 203 |
|
| 204 |
#if defined(LY_FIND_LEAKS_EXTENDED) && !defined(NO_EXTENDED_MEMORY_TRACKING) |
| 205 |
|
| 206 |
#ifdef HTSprintf0 |
| 207 |
#undef HTSprintf0 |
| 208 |
#endif /* HTSprintf0 */ |
| 209 |
#define HTSprintf0 (Get_htsprintf0_fn(__FILE__,__LINE__)) |
| 210 |
|
| 211 |
#ifdef HTSprintf |
| 212 |
#undef HTSprintf |
| 213 |
#endif /* HTSprintf */ |
| 214 |
#define HTSprintf (Get_htsprintf_fn(__FILE__,__LINE__)) |
| 215 |
|
| 216 |
#endif /* LY_FIND_LEAKS_EXTENDED and not NO_EXTENDED_MEMORY_TRACKING */ |
| 217 |
|
| 218 |
#else /* LY_FIND_LEAKS && !NO_MEMORY_TRACKING */ |
| 219 |
|
| 220 |
#define mark_malloced(a,size) /* no-op */ |
| 221 |
#define LYLeakSequence() (-1) |
| 222 |
|
| 223 |
#endif /* LY_FIND_LEAKS && !NO_MEMORY_TRACKING */ |
| 224 |
|
| 225 |
#if defined(LY_FIND_LEAKS) |
| 226 |
#define PUBLIC_IF_FIND_LEAKS /* nothing */ |
| 227 |
#else |
| 228 |
#define PUBLIC_IF_FIND_LEAKS static |
| 229 |
#endif |
| 230 |
|
| 231 |
/* |
| 232 |
* Function declarations. |
| 233 |
* See the appropriate source file for usage. |
| 234 |
*/ |
| 235 |
#ifndef LYLeakSequence |
| 236 |
extern long LYLeakSequence(void); |
| 237 |
#endif |
| 238 |
extern void LYLeaks(void); |
| 239 |
|
| 240 |
#ifdef LY_FIND_LEAKS_EXTENDED |
| 241 |
extern AllocationList *LYLeak_mark_malloced(void *vp_alloced, |
| 242 |
size_t st_bytes, |
| 243 |
const char *cp_File, |
| 244 |
const short ssi_Line); |
| 245 |
#endif /* LY_FIND_LEAKS_EXTENDED */ |
| 246 |
extern void *LYLeakMalloc(size_t st_bytes, const char *cp_File, |
| 247 |
const short ssi_Line); |
| 248 |
extern void *LYLeakCalloc(size_t st_number, size_t st_bytes, const char *cp_File, |
| 249 |
const short ssi_Line); |
| 250 |
extern void *LYLeakRealloc(void *vp_alloced, |
| 251 |
size_t st_newbytes, |
| 252 |
const char *cp_File, |
| 253 |
const short ssi_Line); |
| 254 |
extern void LYLeakFree(void *vp_alloced, |
| 255 |
const char *cp_File, |
| 256 |
const short ssi_Line); |
| 257 |
extern char *LYLeakStrdup(const char *src, |
| 258 |
const char *cp_File, |
| 259 |
const short ssi_Line); |
| 260 |
extern char *LYLeakSACopy(char **dest, |
| 261 |
const char *src, |
| 262 |
const char *cp_File, |
| 263 |
const short ssi_Line); |
| 264 |
extern char *LYLeakSACat(char **dest, |
| 265 |
const char *src, |
| 266 |
const char *cp_File, |
| 267 |
const short ssi_Line); |
| 268 |
extern void LYLeakSABAlloc(bstring **dest, |
| 269 |
int len, |
| 270 |
const char *cp_File, |
| 271 |
const short ssi_Line); |
| 272 |
extern void LYLeakSABCopy(bstring **dest, |
| 273 |
const char *src, |
| 274 |
int len, |
| 275 |
const char *cp_File, |
| 276 |
const short ssi_Line); |
| 277 |
extern void LYLeakSABCopy0(bstring **dest, |
| 278 |
const char *src, |
| 279 |
const char *cp_File, |
| 280 |
const short ssi_Line); |
| 281 |
extern void LYLeakSABCat(bstring **dest, |
| 282 |
const char *src, |
| 283 |
int len, |
| 284 |
const char *cp_File, |
| 285 |
const short ssi_Line); |
| 286 |
extern void LYLeakSABCat0(bstring **dest, |
| 287 |
const char *src, |
| 288 |
const char *cp_File, |
| 289 |
const short ssi_Line); |
| 290 |
extern void LYLeakSABFree(bstring **ptr, |
| 291 |
const char *cp_File, |
| 292 |
const short ssi_Line); |
| 293 |
|
| 294 |
#ifdef LY_FIND_LEAKS_EXTENDED |
| 295 |
/* |
| 296 |
* Trick to get tracking of var arg functions without relying on var arg |
| 297 |
* preprocessor macros: |
| 298 |
*/ |
| 299 |
typedef char *HTSprintflike(char **, const char *, ...); |
| 300 |
extern HTSprintflike *Get_htsprintf_fn(const char *cp_File, |
| 301 |
const short ssi_Line); |
| 302 |
extern HTSprintflike *Get_htsprintf0_fn(const char *cp_File, |
| 303 |
const short ssi_Line); |
| 304 |
#endif /* LY_FIND_LEAKS_EXTENDED */ |
| 305 |
|
| 306 |
#ifdef __cplusplus |
| 307 |
} |
| 308 |
#endif |
| 309 |
#endif /* __LYLEAKS_H */ |