📄 xmem.c
字号:
/*****************************************************************************/
/*
XMEM.C - Extended C/C++ Dynamic Memory Control And Debug Library
Copyright (C) Juergen Mueller (J.M.) 1987-2008
All rights reserved.
You are expressly prohibited from selling this software in any form
or removing this notice.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE
IMPLIED WARRANTIES OF MERCHANTIBILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. THE ENTIRE RISK
AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM AND DOCUMENTATION
IS WITH YOU.
Permission to modify the code and to distribute modified code is granted,
provided the above notices are retained, and a notice that the code was
modified is included with the above copyright notice.
written by: Juergen Mueller, D-70806 Kornwestheim, GERMANY
FILE : XMEM.C
REVISION : 14-May-2008
21:25:57
*/
/*****************************************************************************/
#ifndef SXT_WRAPPED /**** SXT_WRAPPED ****/
/* special define, only used for my SXT applications :-) */
#if defined (__XMEM__)
#error ERROR: __XMEM__ defined outside!!! /* should never happen */
/* __XMEM__ is reserved for internal use */
#endif
/********************************************************/
#define __XMEM__ /**** indicate that we're in XMEM implementation ****/
/**** NEVER define this outside this file, NEVER !!! ****/
/********************************************************/
/*****************************************************************************/
#if XMEM /**** if XMEM package enabled ****/
/*****************************************************************************/
#ifdef __cplusplus
#pragma message("XMEM C++ code compilation")
#else
#pragma message("XMEM C code compilation")
#endif
#ifndef MAIN_MODULE /**** if not inside MAIN_MODULE ****/
/* do some local definitions */
/*****************************************************************************/
/*****************************************************************************/
/* Include Files */
/*****************************************************************************/
/*****************************************************************************/
#if _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#if defined(_MSC_VER) /*** Microsoft specific ****/
#include <malloc.h>
#if (_MSC_VER >= 1100)
#if _DEBUG
#include <crtdbg.h>
#endif
#endif
#endif /**** ****/
#include <sys/timeb.h>
#include <time.h>
#if __GNUC__ /**** GNU compilers ****/
#include <errno.h>
#if !defined(__linux__) && !defined(__CYGWIN__) /* not under LINUX or with CYGWIN */
#include <pc.h>
#define getch getchar /*getkey*/
#else /* LINUX */
#define getch getchar
#endif
#define max(a,b) (((a) > (b)) ? (a) : (b))
#else /**** all compilers except GNU C/C++ ****/
#include <conio.h>
#if (_MSC_VER && M_I86LM) || (_MSC_VER && M_I86SM) /* old MS VC++ 1.5, MS C 5.0, ... */
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#endif /**** ****/
#if XMEM_ALLOC_CALL
#if _WIN32 && defined(_MSC_VER) && (_MSC_VER >= 1300) /**** Microsoft specific ****/
#include <detours.h> /* Detours package */
#endif
#endif
#if XMEM_CALLSTACK
#include "StackWalker.h" /* StackWalker package */
#endif
/*****************************************************************************/
/*****************************************************************************/
/* define data types */
/*****************************************************************************/
/*****************************************************************************/
#define BOOLEAN unsigned char
#define UNS08 unsigned char
#define UNS16 unsigned int /* must be int due to GNU-C */
#define UNS32 unsigned long
#define SGN08 char
#define SGN16 int /* must be int due to GNU-C */
#define SGN32 long
#define FALSE 0
#define TRUE 1
#endif /**** MAIN_MODULE ****/
/*****************************************************************************/
/*****************************************************************************/
/* interface XMEM header file */
/*****************************************************************************/
/*****************************************************************************/
#include "xmem.h"
/*****************************************************************************/
/* #undef function names, in XMEM the real functions are used */
/*****************************************************************************/
#ifdef xmem_overwrite
#undef xmem_overwrite
#endif
#ifdef malloc
#undef malloc
#endif
#ifdef calloc
#undef calloc
#endif
#ifdef realloc
#undef realloc
#endif
#ifdef alloca
#undef alloca
#endif
#ifdef xmalloc
#undef xmalloc
#endif
#ifdef xcalloc
#undef xcalloc
#endif
#ifdef xrealloc
#undef xrealloc
#endif
#ifdef _expand
#undef _expand
#endif
#ifdef free
#undef free
#endif
#ifdef cfree
#undef cfree
#endif
#ifdef xfree
#undef xfree
#endif
#ifdef strcat
#undef strcat
#endif
#ifdef strncat
#undef strncat
#endif
#ifdef strchr
#undef strchr
#endif
#ifdef strcmp
#undef strcmp
#endif
#ifdef strcmpi
#undef strcmpi
#endif
#ifdef _strcmpi
#undef _strcmpi
#endif
#ifdef stricmp
#undef stricmp
#endif
#ifdef _stricmp
#undef _stricmp
#endif
#ifdef strcpy
#undef strcpy
#endif
#ifdef strdup
#undef strdup
#endif
#ifdef _strdup
#undef _strdup
#endif
#ifdef strlen
#undef strlen
#endif
#ifdef strnlen
#undef strnlen
#endif
#ifdef strlwr
#undef strlwr
#endif
#ifdef _strlwr
#undef _strlwr
#endif
#ifdef strncmp
#undef strncmp
#endif
#ifdef strnicmp
#undef strnicmp
#endif
#ifdef _strnicmp
#undef _strnicmp
#endif
#ifdef strncpy
#undef strncpy
#endif
#ifdef strrchr
#undef strrchr
#endif
#ifdef strrev
#undef strrev
#endif
#ifdef _strrev
#undef _strrev
#endif
#ifdef strset
#undef strset
#endif
#ifdef _strset
#undef _strset
#endif
#ifdef strnset
#undef strnset
#endif
#ifdef _strnset
#undef _strnset
#endif
#ifdef strstr
#undef strstr
#endif
#ifdef strtok
#undef strtok
#endif
#ifdef strupr
#undef strupr
#endif
#ifdef _strupr
#undef _strupr
#endif
#ifdef gets
#undef gets
#endif
#ifdef vsprintf
#undef vsprintf
#endif
#ifdef sprintf
#undef sprintf
#endif
#ifdef memchr
#undef memchr
#endif
#ifdef memcmp
#undef memcmp
#endif
#ifdef memcpy
#undef memcpy
#endif
#ifdef memicmp
#undef memicmp
#endif
#ifdef _memicmp
#undef _memicmp
#endif
#ifdef memmove
#undef memmove
#endif
#ifdef memset
#undef memset
#endif
#ifdef fgets
#undef fgets
#endif
#ifdef fread
#undef fread
#endif
/*****************************************************************************/
/* defines for new ISO functions */
/*****************************************************************************/
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#define getch _getch
#define strdup _strdup
#define strupr _strupr
#define strlwr _strlwr
#define strrev _strrev
#define strset _strset
#define strnset _strnset
#define stricmp _stricmp
#define strcmpi _strcmpi
#define strnicmp _strnicmp
#define memicmp _memicmp
#endif
/*****************************************************************************/
/* */
/*****************************************************************************/
#define MACROSTRING2(x) #x
#define MACROSTRING(x) MACROSTRING2(x)
/*****************************************************************************/
/* */
/*****************************************************************************/
#if (_MSC_VER && M_I86LM) /* MS VC 1.5 large model */
#define PTRFMT "%08lx" /* 4 byte / 32 bit pointer size */
#define PTRCAST long /* the default int value does not work */
#define XMEM_32BIT 1
#pragma message("XMEM 4 byte / 32 bit pointer -> XMEM_32BIT")
#elif (_MSC_VER && M_I86SM) /* MS VC 1.5 small model */
#define PTRFMT "%04x" /* 2 byte / 16 bit pointer size */
#define PTRCAST ptrdiff_t /* default MS VC 1.5: int */
#define XMEM_16BIT 1
#pragma message("XMEM 2 byte / 16 bit pointer -> XMEM_16BIT")
#elif (_MSC_VER && _WIN64) /* MS Visual Studio 64 bit */
#define PTRFMT "%016lx" /* 8 byte / 64 bit pointer size */
#define PTRCAST ptrdiff_t
#define XMEM_64BIT 1
#pragma message("XMEM 8 byte / 64 bit pointer -> XMEM_64BIT")
#elif (_MSC_VER && _WIN32) /* MS Visual Studio 32 bit */
#define PTRFMT "%08lx" /* 4 byte / 32 bit pointer size */
#define PTRCAST ptrdiff_t
#define XMEM_32BIT 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -