|
|
1.1 root 1: #ifndef __DEBUG_H
2: #define __DEBUG_H
3:
4: /*
5: * _DEBUG.H 5.20A June 8, 1995
6: *
7: * The Greenleaf Comm Library
8: *
9: * Copyright (C) 1985-1995 Greenleaf Software Inc. All Rights Reserved.
10: *
11: * NOTES
12: *
13: * This header file contains prototypes and miscellaneous definitions
14: * for two different subsytems that are used to help diagnose
15: * problems under CommLib.
16: *
17: * The first set of functions are those used to replace all of the
18: * heap memory functions. When the _DEBUG macro is turned on, normal
19: * function calls to malloc(), calloc(), and free() are replaced with
20: * special versions. The special versions track memory leaks and
21: * heap over- and under-writes, as well as the general problem of
22: * heap destruction. Several other memory allocation functions get
23: * replaced here as well.
24: *
25: * The second subsytem controlled here is the GF_ASSERT() macro. This
26: * macro is nearly identical to the assert() macro used in the C RTL,
27: * but it includes an extra argument that allows you to print out the
28: * reason for an assertion failure.
29: *
30: * MODIFICATIONS
31: *
32: * June 6, 1995 5.20A : It really wasn't very nice to modify malloc()
33: * et. al. when _DEBUG is turned on. This turns
34: * out to cause people some trouble. So now, I
35: * just debug my *own* function calls when _DEBUG
36: * is turned on. If a user wants to debug malloc()
37: * and friends in their own libraries, they need
38: * to define GF_ENABLE_HEAP_DEBUGGING. The old
39: * macro, GF_DISABLE_HEAP_DEBUGGING, is gone.
40: * Regardless of the new macro's setting, I am
41: * going to debug Greenleaf internal allocation if
42: * _DEBUG is turned on.
43: */
44:
45:
46: #ifdef __cplusplus
47: extern "C" {
48: #endif
49:
50: /*
51: * Heap checking will only get turned on if the _DEBUG macro is defined.
52: * This is normally done using an option in BUILD.INI. The function
53: * shown below lets you test at runtime for the presence of the heap
54: * debugging code.
55: */
56:
57: int GF_CONV _DebuggingHeap( void );
58:
59: #ifdef __cplusplus
60: }
61: #endif
62:
63: #if defined( _DEBUG )
64: /*
65: * The heapcheck code uses specific function calls in the Borland and
66: * Microsoft run time libraries. It may work properly with other compilers,
67: * but that hasn't been checked yet.
68: */
69: #if defined( GF_TURBO_CPP ) || defined( GF_BORLAND_CPP ) || defined( GF_MICROSOFT_C )
70: /*
71: * We are not going to enable heap debugging if any of the DOS extenders
72: * are turned on, or if Powercomm is enabled.
73: */
74: #if !defined( GF_X32 )
75: #if !defined( GF_X16 )
76: #if !defined( VGFD ) && !defined( VGFD_DLL )
77: /*
78: * All we have to do at this point is decide whether we are running Windows
79: * or not.
80: */
81: #if !defined( GF_WINDOWS )
82: #define GF_HEAP_DEBUGGER
83: #else /* Small data, everything goes, large model, MSC only */
84: #if !defined( GF_DLL ) && !defined( _WINDOWS_DLL )
85: #if ( _LDATA == 0 )
86: #define GF_HEAP_DEBUGGER
87: #elif defined( GF_MICROSOFT_C )
88: #define GF_HEAP_DEBUGGER
89: #endif
90: #endif /* !defined( GF_DLL ) && !defined( _WINDOWS_DLL ) */
91: #endif
92: #endif /* !defined( VGFD ) */
93: #endif /* !defined( GF_X32 ) */
94: #endif /* !defined( GF_X16 ) */
95: #endif
96: #endif
97:
98: #ifdef GF_HEAP_DEBUGGER
99:
100: /*
101: * This only applies to CommLib, but I guess it won't hurt anything to have
102: * these definitions elsewhere.
103: */
104: /*
105: * Need to pick up the definition for size_t.
106: */
107: #include <stdlib.h>
108:
109: #define AllocateGreenleafPortStructure( port, size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
110: #define FreeGreenleafPortStructure( port, p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
111: #define AllocateDriverStructure( size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
112: #define FreeDriverStructure( p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
113: #define AllocateGenericStructure( size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
114: #define FreeGenericStructure( p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
115: #define AllocateTXBuffer( port, size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
116: #define FreeTXBuffer( port, p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
117: #define AllocateRXBuffer( port, size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
118: #define FreeRXBuffer( port, p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
119: #define AllocateXferBuffer( size ) _DebugCalloc( 1, size, __FILE__, __LINE__ ) /* Tag: Debug Public */
120: #define FreeXferBuffer( p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug Public */
121:
122: #ifdef __cplusplus
123: extern "C" {
124: #endif
125:
126: typedef void ( * _FailureRoutine )( int terminate, char *fmt, ... );
127: void * GF_CONV _DebugMalloc( size_t size, char *file, long line );
128: void * GF_CONV _DebugCalloc( size_t nitems, size_t size, char *file, long line );
129: void GF_CONV _DebugFree( void *p, char *file, long line );
130: char * GF_CONV _DebugStrdup( const char *p, char *file, long line );
131: int GF_CONV _DebuggingHeap( void );
132:
133: #ifdef __cplusplus
134: }
135: #endif
136:
137: /*
138: * This stuff only gets turned on if a customer wants to
139: * debug his or her own use of malloc(), etc.
140: */
141:
142: #if defined( _DEBUG ) && defined( GF_ENABLE_HEAP_DEBUGGING )
143: /*
144: * I need to make sure any prototypes created by alloc.h or malloc.h are
145: * set up before I define the replacement macros.
146: */
147:
148: #if defined( GF_MICROSOFT_C )
149: #include <malloc.h>
150: #elif defined( GF_BORLAND_CPP ) || defined( GF_TURBO_CPP )
151: #include <alloc.h>
152: #endif
153: #include <string.h>
154:
155: #define malloc( i ) _DebugMalloc( i, __FILE__, __LINE__ ) /* Tag: Debug private */
156: #define calloc( i, j ) _DebugCalloc( i, j, __FILE__, __LINE__ ) /* Tag: Debug private */
157: #define free( p ) _DebugFree( p, __FILE__, __LINE__ ) /* Tag: Debug private */
158: #define strdup( p ) _DebugStrdup( p, __FILE__, __LINE__ ) /* Tag: Debug private */
159: #endif
160: #endif /* #if defined( _DEBUG ) && defined( GF_ENABLE_HEAP_DEBUGGING ) */
161:
162: /*
163: * _GFAssertFailure is the function called by AL_ASSERT() and
164: * AL_ASSERT_OBJECT() when their assertion fails.
165: */
166:
167: #ifdef __cplusplus
168: extern "C" {
169: #endif
170:
171: void GF_CDECL _GFAssertFailure( const char GF_DLL_FAR *condition,
172: const char GF_DLL_FAR *filename,
173: int line,
174: const char GF_DLL_FAR *message,
175: ... );
176: #ifdef __cplusplus
177: }
178: #endif
179:
180: /*
181: * By definition, assertions go away when you turn on NDEBUG. Our assertions
182: * aren't exactly the same as those in the RTL, but we will use the
183: * same philosophy for consistency.
184: */
185:
186: #if defined( NDEBUG )
187:
188: /*
189: * Microsoft C++ 7.0 with /W4 and /Ox and /DNDEBUG gives an error on
190: * my GF_ASSERT statements. Not only that, I can't turn off the
191: * error. This ugly hack seems to help.
192: */
193:
194: #if defined( GF_MICROSOFT_C )
195: #if ( _MSC_VER < 0x800 )
196: # define GF_ASSERT( condition, message ) { int p = 0; }
197: #endif
198: #endif
199:
200: #if !defined( GF_ASSERT )
201: # define GF_ASSERT( condition, message ) ((void) 0)
202: #endif
203:
204: #else /* #if defined( NDEBUG ) */
205:
206: /*
207: * In debug mode, GF_ASSERT() tests the condition, and generates
208: * an abort with an error message when the condition fails.
209: */
210: #define GF_ASSERT( condition, message ) /* Tag: Debug private */ \
211: ( ( condition ) ? \
212: (void) 0 : \
213: _GFAssertFailure( #condition, \
214: __FILE__, \
215: __LINE__, \
216: message ) )
217:
218: #endif /* #if defined( NDEBUG ) ... #else */
219:
220: #endif /* !defined( __DEBUG_H ) */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.