/
std.c
144 lines (121 loc) · 3.05 KB
/
std.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*//////////////////////////////////////////*/
/* std.c */
#include <assert.h>
#include "std.h"
/* globals */
#if defined(_WINDOWS)
CRITICAL_SECTION g_crsMemCount;
#endif
ERRORREF g_LastError;
int HEAPCHECK;
/* static member intialization */
int CHeap__m_cblocks = 0;
#if !defined(_UNIX) && !defined(_STRICT_ANSI_C)
void AssertFSz(BOOLFLAG fAssertion, int nline, char* szFile, char* szMsg)
{
if (!fAssertion)
{
char szAssertBuf[MAXASSERTBUF];
char szTempBuf[MAXTEMPBUF];
_itoa(nline,szTempBuf,10);
strcpy(szAssertBuf,"Assertion Failure in line ");
szTempBuf[MAXTEMPBUF - 1]= '\0';
strcat(szAssertBuf,szTempBuf);
strcat(szAssertBuf," of file ");
szFile[_MAX_PATH -1] = '\0';
strcat(szAssertBuf,szFile);
if (szMsg)
{
szMsg[MAXASSERTBUF / 2] = '\0';
strcat(szAssertBuf,"\n");
strcat(szAssertBuf,szMsg);
}
strcat(szAssertBuf,"\n");
#if !defined(_CONSOLE)
strcat(szAssertBuf,"Press OK to continue, Cancel to Debug");
if (MessageBox(
NULL,
szAssertBuf,
"Assertion Failure",
MB_APPLMODAL | MB_ICONSTOP | MB_OKCANCEL) == IDCANCEL)
{
DebugBreak();
}
#else /* !defined(_CONSOLE) */
printf("%s\n",szAssertBuf);
DebugBreak();
#endif /* defined(_CONSOLE) */
}
}
#endif /* !defined(_UNIX) && !defined(_STRICT_ANSI_C) */
CLASS_CONSTRUCTOR(CHeap)
{
AssertStd(!CHeap__m_cblocks);
INITIALIZE_CRITICAL_SECTION(&g_crsMemCount);
return pthis;
}
CLASS_DESTRUCTOR(CHeap)
{
#if defined _DEBUG
printf("%d objects not freed\n",CHeap__m_cblocks);
#endif
AssertStdSz(!CHeap__m_cblocks,"Some memory objects have not been freed!");
return pthis;
}
void* CHeap__malloc(size_t cb)
{
#if defined(_DEBUG)
void* pv;
#if defined(_MEMDUMPALLOC)
FILE* fp;
#endif /* defined (_MEMDUMP) */
#endif /* defined(_DEBUG) */
AssertStd(cb);
#if defined(_DEBUG)
pv =
#else /* defined(_DEBUG) */
return
#endif /* defined(_DEBUG) */
malloc(cb);
#if defined(_MEMDUMPALLOC)
fp = fopen("mem.txt","a+");
fprintf(fp,"%p\n",pv);
fclose(fp);
#endif /* defined(_MEMDUMP) */
#if defined(_DEBUG)
AssertStd((BOOLFLAG)pv);
if (pv)
{
CHeap__m_cblocks++;
}
HEAPCHECK +=(int)pv;
return pv;
#endif /* defined(_DEBUG) */
}
void CHeap__free(void* pv)
{
#if defined(_DEBUG)
#if defined (_MEMDUMPFREE)
FILE* fp;
#endif /* defined (_MEMDUMP) */
#endif
free(pv);
#if defined(_DEBUG)
#if defined(_MEMDUMPFREE)
fp = fopen("mem.txt","a+");
fprintf(fp,"%p\n",pv);
fclose(fp);
#endif
if(pv)CHeap__m_cblocks--;
HEAPCHECK -=(int)pv;
#endif /* defined(_DEBUG) */
}
void* CHeap__realloc(void* pv, size_t cb)
{
AssertStd((BOOLFLAG)pv);
return realloc(pv,cb);
}
ERRORREF StdGetLastError()
{
return g_LastError;
}