Blame WWW/Library/Implementation/LYLeaks.h

Packit f574b8
/*
Packit f574b8
 * $LynxId: LYLeaks.h,v 1.17 2018/03/30 00:35:10 tom Exp $
Packit f574b8
 */
Packit f574b8
#ifndef __LYLEAKS_H
Packit f574b8
/*
Packit f574b8
 *	Avoid include redundancy
Packit f574b8
 *	Include only if finding memory leaks.
Packit f574b8
 */
Packit f574b8
#define __LYLEAKS_H
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Copyright (c) 1994, University of Kansas, All Rights Reserved
Packit f574b8
 *
Packit f574b8
 *  Include File:	LYLeaks.h
Packit f574b8
 *  Purpose:		Header to convert requests for allocation to Lynx
Packit f574b8
 *			custom functions to track memory leaks.
Packit f574b8
 *  Remarks/Portability/Dependencies/Restrictions:
Packit f574b8
 *	For the stdlib.h allocation functions to be overriden by the
Packit f574b8
 *		Lynx memory tracking functions all modules allocating,
Packit f574b8
 *		freeing, or resizing memory must have LY_FIND_LEAKS
Packit f574b8
 *		defined before including this file.
Packit f574b8
 *	This header file should be included in every source file which
Packit f574b8
 *		does any memory manipulation through use of the
Packit f574b8
 *		stdlib.h memory functions.
Packit f574b8
 *	For proper reporting of memory leaks, the function LYLeaks
Packit f574b8
 *		should be registered for execution by atexit as the
Packit f574b8
 *		very first executable statement in main.
Packit f574b8
 *	This code is slow and should not be used except in debugging
Packit f574b8
 *		circumstances (don't define LY_FIND_LEAKS).
Packit f574b8
 *	If you are using LY_FIND_LEAKS and don't want the LYLeak*
Packit f574b8
 *		memory functions to be used in a certain file,
Packit f574b8
 *		define NO_MEMORY_TRACKING before including this file.
Packit f574b8
 *	The only safe way to call the LYLeak* functions is to use
Packit f574b8
 *		the below macros because they depend on the static
Packit f574b8
 *		string created by __FILE__ to not be dynamic in
Packit f574b8
 *		nature (don't free it and assume will exist at all
Packit f574b8
 *		times during execution).
Packit f574b8
 *	If you are using LY_FIND_LEAKS and LY_FIND_LEAKS_EXTENDED and
Packit f574b8
 *		want only normal memory tracking (not extended for
Packit f574b8
 *		HTSprintf/HTSprintf0) to be used in a certain file,
Packit f574b8
 *		define NO_EXTENDED_MEMORY_TRACKING and don't define
Packit f574b8
 *		NO_MEMORY_TRACKING before including this file.
Packit f574b8
 *  Revision History:
Packit f574b8
 *	05-26-94	created for Lynx 2-3-1, Garrett Arch Blythe
Packit f574b8
 *	10-30-97	modified to handle StrAllocCopy() and
Packit f574b8
 *			StrAllocCat(). - KW & FM
Packit f574b8
 *	1999-10-17	modified to handle HTSprintf0 and HTSprintf(),
Packit f574b8
 *			and to provide mark_malloced, if
Packit f574b8
 *			LY_FIND_LEAKS_EXTENDED is defined. - kw
Packit f574b8
 *	2003-01-22	add sequence-id for counting mallocs/frees -TD
Packit f574b8
 *	2004-04-27	ANSIfy'd -TD
Packit f574b8
 *	2012-02-09	add bstring interfaces -TD
Packit f574b8
 */
Packit f574b8
Packit f574b8
/* Undefine this to get no improved HTSprintf0/HTSprintf tracking: */
Packit f574b8
#define LY_FIND_LEAKS_EXTENDED
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Required includes
Packit f574b8
 */
Packit f574b8
Packit f574b8
#ifndef HTUTILS_H
Packit f574b8
#include <HTUtils.h>
Packit f574b8
#endif
Packit f574b8
Packit f574b8
#ifdef __cplusplus
Packit f574b8
extern "C" {
Packit f574b8
#endif
Packit f574b8
/*
Packit f574b8
 *	Constant defines
Packit f574b8
 */
Packit f574b8
#define MAX_CONTENT_LENGTH 50
Packit f574b8
#ifdef VMS
Packit f574b8
#define LEAKAGE_SINK "sys$login:Lynx.leaks"
Packit f574b8
#else
Packit f574b8
#define LEAKAGE_SINK "Lynx.leaks"
Packit f574b8
#endif				/* VMS */
Packit f574b8
/*
Packit f574b8
 * Data structures
Packit f574b8
 */
Packit f574b8
    typedef struct SourceLocation_tag {
Packit f574b8
	/*
Packit f574b8
	 * The file name and line number of where an event took place.
Packit f574b8
	 */
Packit f574b8
	const char *cp_FileName;
Packit f574b8
	short ssi_LineNumber;
Packit f574b8
    } SourceLocation;
Packit f574b8
Packit f574b8
    typedef struct AllocationList_tag {
Packit f574b8
	/*
Packit f574b8
	 * A singly linked list.
Packit f574b8
	 */
Packit f574b8
	struct AllocationList_tag *ALp_Next;
Packit f574b8
Packit f574b8
	/*
Packit f574b8
	 * Count the number of mallocs.
Packit f574b8
	 */
Packit f574b8
	long st_Sequence;
Packit f574b8
Packit f574b8
	/*
Packit f574b8
	 * The memory pointer allocated.  If set to NULL, then an invalid request
Packit f574b8
	 * was made.  The invalid pointer also.
Packit f574b8
	 */
Packit f574b8
	void *vp_Alloced;
Packit f574b8
	void *vp_BadRequest;
Packit f574b8
Packit f574b8
	/*
Packit f574b8
	 * The size in bytes of the allocated memory.
Packit f574b8
	 */
Packit f574b8
	size_t st_Bytes;
Packit f574b8
Packit f574b8
	/*
Packit f574b8
	 * The source location of specific event (calloc, malloc, free).  realloc
Packit f574b8
	 * kept separate since will track last realloc on pointer.
Packit f574b8
	 */
Packit f574b8
	SourceLocation SL_memory;
Packit f574b8
	SourceLocation SL_realloc;
Packit f574b8
    } AllocationList;
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Global variable declarations
Packit f574b8
 */
Packit f574b8
    extern char LYLeaksPath[];
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Macros
Packit f574b8
 */
Packit f574b8
#if defined(LY_FIND_LEAKS) && !defined(NO_MEMORY_TRACKING)
Packit f574b8
/*
Packit f574b8
 * Only use these macros if we are to track memory allocations.  The reason for
Packit f574b8
 * using a macro instead of a define is that we want to track where the initial
Packit f574b8
 * allocation took place or where the last reallocation took place.  Track
Packit f574b8
 * where the allocation took place by the __FILE__ and __LINE__ defines which
Packit f574b8
 * are automatic to the compiler.
Packit f574b8
 */
Packit f574b8
#ifdef malloc
Packit f574b8
#undef malloc
Packit f574b8
#endif				/* malloc */
Packit f574b8
#define malloc(st_bytes) LYLeakMalloc(st_bytes, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef calloc
Packit f574b8
#undef calloc
Packit f574b8
#endif				/* calloc */
Packit f574b8
#define calloc(st_number, st_bytes) LYLeakCalloc(st_number, st_bytes, \
Packit f574b8
	__FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef realloc
Packit f574b8
#undef realloc
Packit f574b8
#endif				/* realloc */
Packit f574b8
#define realloc(vp_alloced, st_newbytes) LYLeakRealloc(vp_alloced, \
Packit f574b8
	st_newbytes, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef free
Packit f574b8
#undef free
Packit f574b8
#endif				/* free */
Packit f574b8
#define free(vp_alloced) LYLeakFree(vp_alloced, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef strdup
Packit f574b8
#undef strdup
Packit f574b8
#endif				/* free */
Packit f574b8
#define strdup(vp_alloced) LYLeakStrdup(vp_alloced, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
/*
Packit f574b8
 * Added the following two defines to track Lynx's frequent use of those
Packit f574b8
 * macros.  - KW 1997-10-12
Packit f574b8
 */
Packit f574b8
#ifdef StrAllocCopy
Packit f574b8
#undef StrAllocCopy
Packit f574b8
#endif				/* StrAllocCopy */
Packit f574b8
#define StrAllocCopy(dest, src) LYLeakSACopy(&(dest), src, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef StrAllocCat
Packit f574b8
#undef StrAllocCat
Packit f574b8
#endif				/* StrAllocCat */
Packit f574b8
#define StrAllocCat(dest, src)  LYLeakSACat(&(dest), src, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef BStrAlloc
Packit f574b8
#undef BStrAlloc
Packit f574b8
#endif
Packit f574b8
#define BStrAlloc(d,n)   LYLeakSABAlloc( &(d), n, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef BStrCopy
Packit f574b8
#undef BStrCopy
Packit f574b8
#endif
Packit f574b8
#define BStrCopy(d,s)  LYLeakSABCopy( &(d), BStrData(s), BStrLen(s), __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef BStrCopy0
Packit f574b8
#undef BStrCopy0
Packit f574b8
#endif
Packit f574b8
#define BStrCopy0(d,s)  LYLeakSABCopy0( &(d), s, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef BStrCat
Packit f574b8
#undef BStrCat
Packit f574b8
#endif
Packit f574b8
#define BStrCat(d,s)  LYLeakSABCat( &(d), BStrData(s), BStrLen(s), __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#ifdef BStrCat0
Packit f574b8
#undef BStrCat0
Packit f574b8
#endif
Packit f574b8
#define BStrCat0(d,s)  LYLeakSABCat0( &(d), s, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#define mark_malloced(a,size) LYLeak_mark_malloced(a,size, __FILE__, __LINE__)
Packit f574b8
Packit f574b8
#if defined(LY_FIND_LEAKS_EXTENDED) && !defined(NO_EXTENDED_MEMORY_TRACKING)
Packit f574b8
Packit f574b8
#ifdef HTSprintf0
Packit f574b8
#undef HTSprintf0
Packit f574b8
#endif				/* HTSprintf0 */
Packit f574b8
#define HTSprintf0 (Get_htsprintf0_fn(__FILE__,__LINE__))
Packit f574b8
Packit f574b8
#ifdef HTSprintf
Packit f574b8
#undef HTSprintf
Packit f574b8
#endif				/* HTSprintf */
Packit f574b8
#define HTSprintf (Get_htsprintf_fn(__FILE__,__LINE__))
Packit f574b8
Packit f574b8
#endif				/* LY_FIND_LEAKS_EXTENDED and not NO_EXTENDED_MEMORY_TRACKING */
Packit f574b8
Packit f574b8
#else				/* LY_FIND_LEAKS && !NO_MEMORY_TRACKING */
Packit f574b8
Packit f574b8
#define mark_malloced(a,size)	/* no-op */
Packit f574b8
#define LYLeakSequence() (-1)
Packit f574b8
Packit f574b8
#endif				/* LY_FIND_LEAKS && !NO_MEMORY_TRACKING */
Packit f574b8
Packit f574b8
#if defined(LY_FIND_LEAKS)
Packit f574b8
#define PUBLIC_IF_FIND_LEAKS	/* nothing */
Packit f574b8
#else
Packit f574b8
#define PUBLIC_IF_FIND_LEAKS static
Packit f574b8
#endif
Packit f574b8
Packit f574b8
/*
Packit f574b8
 * Function declarations.
Packit f574b8
 * See the appropriate source file for usage.
Packit f574b8
 */
Packit f574b8
#ifndef LYLeakSequence
Packit f574b8
    extern long LYLeakSequence(void);
Packit f574b8
#endif
Packit f574b8
    extern void LYLeaks(void);
Packit f574b8
Packit f574b8
#ifdef LY_FIND_LEAKS_EXTENDED
Packit f574b8
    extern AllocationList *LYLeak_mark_malloced(void *vp_alloced,
Packit f574b8
						size_t st_bytes,
Packit f574b8
						const char *cp_File,
Packit f574b8
						const short ssi_Line);
Packit f574b8
#endif				/* LY_FIND_LEAKS_EXTENDED */
Packit f574b8
    extern void *LYLeakMalloc(size_t st_bytes, const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern void *LYLeakCalloc(size_t st_number, size_t st_bytes, const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern void *LYLeakRealloc(void *vp_alloced,
Packit f574b8
			       size_t st_newbytes,
Packit f574b8
			       const char *cp_File,
Packit f574b8
			       const short ssi_Line);
Packit f574b8
    extern void LYLeakFree(void *vp_alloced,
Packit f574b8
			   const char *cp_File,
Packit f574b8
			   const short ssi_Line);
Packit f574b8
    extern char *LYLeakStrdup(const char *src,
Packit f574b8
			      const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern char *LYLeakSACopy(char **dest,
Packit f574b8
			      const char *src,
Packit f574b8
			      const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern char *LYLeakSACat(char **dest,
Packit f574b8
			     const char *src,
Packit f574b8
			     const char *cp_File,
Packit f574b8
			     const short ssi_Line);
Packit f574b8
    extern void LYLeakSABAlloc(bstring **dest,
Packit f574b8
			       int len,
Packit f574b8
			       const char *cp_File,
Packit f574b8
			       const short ssi_Line);
Packit f574b8
    extern void LYLeakSABCopy(bstring **dest,
Packit f574b8
			      const char *src,
Packit f574b8
			      int len,
Packit f574b8
			      const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern void LYLeakSABCopy0(bstring **dest,
Packit f574b8
			       const char *src,
Packit f574b8
			       const char *cp_File,
Packit f574b8
			       const short ssi_Line);
Packit f574b8
    extern void LYLeakSABCat(bstring **dest,
Packit f574b8
			     const char *src,
Packit f574b8
			     int len,
Packit f574b8
			     const char *cp_File,
Packit f574b8
			     const short ssi_Line);
Packit f574b8
    extern void LYLeakSABCat0(bstring **dest,
Packit f574b8
			      const char *src,
Packit f574b8
			      const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
    extern void LYLeakSABFree(bstring **ptr,
Packit f574b8
			      const char *cp_File,
Packit f574b8
			      const short ssi_Line);
Packit f574b8
Packit f574b8
#ifdef LY_FIND_LEAKS_EXTENDED
Packit f574b8
/*
Packit f574b8
 * Trick to get tracking of var arg functions without relying on var arg
Packit f574b8
 * preprocessor macros:
Packit f574b8
 */
Packit f574b8
    typedef char *HTSprintflike(char **, const char *,...);
Packit f574b8
    extern HTSprintflike *Get_htsprintf_fn(const char *cp_File,
Packit f574b8
					   const short ssi_Line);
Packit f574b8
    extern HTSprintflike *Get_htsprintf0_fn(const char *cp_File,
Packit f574b8
					    const short ssi_Line);
Packit f574b8
#endif				/* LY_FIND_LEAKS_EXTENDED */
Packit f574b8
Packit f574b8
#ifdef __cplusplus
Packit f574b8
}
Packit f574b8
#endif
Packit f574b8
#endif				/* __LYLEAKS_H */