1 /*
2  *   XFrisk - The classic board game for X
3  *   Copyright (C) 1993-1999 Elan Feingold (elan@aetherworks.com)
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *   $Id: debug.h,v 1.4 1999/11/13 21:58:31 morphy Exp $
20  */
21 
22 #ifndef _DEBUG
23 #define _DEBUG
24 
25 /* Turn assertions on if MEM_DEBUG is on */
26 #ifdef MEM_DEBUG
27 #ifndef ASSERTIONS
28 #define ASSERTIONS
29 #endif
30 #endif
31 
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include "types.h"
37 
38 /* This is the public header file and should be used by programmers
39  * in their files.  Any functions not declared in here, as well as
40  * any data structures in debug.c are OFF LIMITS!  Functions should
41  * be accesed by their MEM_ equivalents to ensure that they disappear
42  * when DEBUG is not defined.
43  */
44 
45 extern Flag  fBootStrapped;
46 extern FILE    *hDebugFile;
47 typedef void   *FakePtr;
48 
49 #ifdef ASSERTIONS
50 #define D_Assert(expr, szError)  (expr) ? (void)0 : \
51   (void)_D_AssertFailed(__FILE__, __LINE__, #expr, szError)
52 #define D_AssertWhere(expr, szError, iLine, szFile)  (expr) ? (void)0 : \
53   (void)_D_AssertFailed(szFile, iLine, #expr, szError)
54 #define D_PrintStr(str) { fprintf(hDebugFile, str); fflush(hDebugFile); }
55 #define D_PrintStrInt32(str, iInt32) { fprintf(hDebugFile, str, iInt32); \
56 					 fflush(hDebugFile); }
57 #define D_PrintStrLong(str, lLong) { fprintf(hDebugFile, str, lLong); \
58 				       fflush(hDebugFile); }
59 #else
60 #define D_PrintStr(szString)          ;
61 #define D_PrintStrInt(szString,   i)  ;
62 #define D_PrintStrLong(szString, l)   ;
63 #define D_Assert(expr, str)           ;
64 #define D_AssertWhere(expr, s, i, e)  ;
65 #endif
66 
67 #ifdef MEM_DEBUG
68 #define MEM_BootStrap(str)  if(fBootStrapped==FALSE) D_BootStrap(str)
69 #define MEM_Alloc(uSize) D_MemAlloc(uSize, __LINE__, __FILE__)
70 #define MEM_Free(vPtr) { D_MemFree((void *)vPtr, __LINE__, __FILE__); vPtr=NULL; }
71 #define MEM_CheckPointer(vPtr)  D_MemCheckPointer(vPtr, __LINE__, __FILE__, 1)
72 #define MEM_Shrink(vPtr, iNewSize)  D_MemShrink(vPtr, iNewSize)
73 #define MEM_Grow(vPtr, iNewSize)  D_MemGrow(vPtr, iNewSize)
74 #define MEM_DumpUsage()  D_MemDumpUsage()
75 #define MEM_CheckAllPointers()  D_MemCheckAllPointers(__LINE__, __FILE__)
76 #define MEM_DumpPointers() D_MemDumpPointers()
77 #define MEM_TheEnd()  D_TheEnd()
78 #else
79 #define MEM_Alloc(uSize)              malloc(uSize)
80 #define MEM_Free(vPtr)                free((void *)vPtr)
81 #define MEM_CheckPointer(vPtr)        ;
82 #define MEM_CheckAllPointers()        ;
83 #define MEM_Shrink(vPtr, iNewSize)    realloc(vPtr, iNewSize)
84 #define MEM_Grow(vPtr, iNewSize)      realloc(vPtr, iNewSize)
85 #define MEM_DumpUsage()               ;
86 #define MEM_BootStrap(str)            ;
87 #define MEM_TheEnd()                  ;
88 #endif
89 
90 /* Random assertion helpers */
91 void _D_AssertFailed(CString strFile, UInt32 uiLine, CString strAssertion,
92 		     CString strError);
93 
94 /* prototypes for debugging memory functions */
95 void   *D_MemAlloc(size_t uSize, UInt32 uiLine, CString strFile);
96 void    D_MemFree(FakePtr fakePtr, UInt32 uiLine, CString strFile);
97 void   *D_MemShrink(void *vPtr, size_t sizeNew);
98 void   *D_MemGrow(void *vPtr, size_t sizeNew);
99 void    D_MemCheck(void);
100 void    D_MemDumpUsage(void);
101 
102 /* Pointer Checker stuff */
103 void    D_MemCheckPointer(FakePtr fakePtr, UInt32 uiLine, CString strFile,
104 			  Flag fDisp);
105 void    D_MemCheckAllPointers(UInt32 uiLine, CString strFile);
106 void    D_MemDumpPointers(void);
107 size_t  D_MemGetBlockSize(FakePtr fakePtr);
108 
109 /* Prototypes for hashing functions used */
110 void    D_MemHashNewEntry(Pointer uiKey, size_t sizeBlock, UInt32 uiLine,
111 			  CString strFile);
112 void    D_MemInitHashTable(void);
113 Flag    D_MemKeyInTable(Pointer uiKey);
114 UInt32  D_MemHashFunc(Pointer uiNumber);
115 void    D_MemDeleteEntry(Pointer uiKey);
116 
117 /* Misc prototypes */
118 void    D_BootStrap(CString str);
119 void    D_TheEnd(void);
120 char   *StringCopy(CString str1, CString str2, UInt16 usLength);
121 
122 #endif
123