summaryrefslogtreecommitdiffstats
path: root/private/windows/diamond/chuck/asrt.c
blob: 1260635377901cbc5e7fc60d91c4e044468822c3 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/***    asrt.c - Assertion Manager
 *
 *  Author:
 *      Benjamin W. Slivka
 *
 *  History:
 *      10-Aug-1993 bens    Initial version
 *      11-Aug-1993 bens    Lift code from 1988 PSCHAR.EXE
 *      12-Aug-1993 bens    Improve documentation, move messages to asrt.msg
 *      14-Aug-1993 bens    Add assertion flags, query calls
 *
 *  Functions available in ASSERT build:
 *      AssertRegisterFunc - Register assertion failure call back function
 *      AsrtCheck          - Check that parameter is TRUE
 *      AsrtStruct         - Check that pointer points to specified structure
 *      AssertForce        - Force an assertion failure
 */

#include "types.h"
#include "asrt.h"

#ifdef ASSERT   // Must be after asrt.h!

#include "asrt.msg"


void doFailure(char *pszMsg, char *pszFile, int iLine);

STATIC PFNASSERTFAILURE  pfnafClient=NULL;  // Assertion call back function
STATIC ASSERTFLAGS       asfClient=asfNONE; // Assertion flags


/***    AssertRegisterFunc - Register assertion failure call back function
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
void AssertRegisterFunc(PFNASSERTFAILURE pfnaf)
{
    pfnafClient = pfnaf;    // Store for future use
}


/***	AssertGetFunc - Get current assertion failure call back function
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
PFNASSERTFAILURE AssertGetFunc(void)
{
    return pfnafClient;
}


/***    AssertSetFlags - Set special assertion control flags
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
void AssertSetFlags(ASSERTFLAGS asf)
{
    asfClient = asf;
}


/***	AssertGetFlags - Get special assertion control flags
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
ASSERTFLAGS  AssertGetFlags(void)
{
    return asfClient;
}


/***    AsrtCheck - Check assertion that argument is TRUE
 *
 *  Entry:
 *      f       - Boolean value to check
 *      pszFile - name of source file
 *      iLine   - source line number
 *
 *  Exit-Success:
 *      Returns; f was TRUE
 *
 *  Exit-Failure:
 *      Calls assertion failure callback function; f was false.
 */
void AsrtCheck(BOOL f, char *pszFile, int iLine)
{
    if (!f) {
        doFailure(pszASRTERR_FALSE,pszFile,iLine); // Inform client
        // Client returned, ignore error!
    }
}


/***    AsrtStruct - Check assertion that pointer is of correct type
 *
 *  Entry:
 *      pv      - Pointer to structure
 *      sig     - Expected signature
 *      pszFile - name of source file
 *      iLine   - source line number
 *
 *  Exit-Success:
 *      Returns; pv != NULL, and pv->sig == sig.
 *
 *  Exit-Failure:
 *      Calls assertion failure callback function; pv was bad.
 */
void AsrtStruct(void *pv, SIGNATURE sig, char *pszFile, int iLine)
{
    if (pv == NULL) {
        doFailure(pszASRTERR_NULL_POINTER,pszFile,iLine); // Inform client
        // Client returned, ignore error!
    }
    else if (*((PSIGNATURE)pv) != sig) {
        (*pfnafClient)(pszASRTERR_SIGNATURE_BAD,pszFile,iLine);// Inform client
        // Client returned, ignore error!
    }
}


/***    AssertForce - Force an assertion failure
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
void AssertForce(char *pszMsg, char *pszFile, int iLine)
{
    doFailure(pszMsg,pszFile,iLine);   // Inform client
    // Client returned, ignore error!
}


/***    AssertErrPath - Report an internal error path
 *
 *  NOTE: See asrt.h for entry/exit conditions.
 */
void AssertErrPath(char *pszMsg, char *pszFile, int iLine)
{
    //** Only assert if we are not skipping error path assertions
    if (!(asfClient & asfSKIP_ERROR_PATH_ASSERTS)) {
        doFailure(pszMsg,pszFile,iLine);   // Inform client
    }
    // Client returned, ignore error!
}


/***    doFailure - Call registered call back function
 *
 *  Entry:
 *      pszMsg  - Message to display
 *      pszFile - Name of source file
 *      iLine   - Source line number
 *
 *  Exit-Success:
 *      Returns; client wanted to ignore assertion.
 *
 *  Exit-Failure:
 *      Does not return.
 */
void doFailure(char *pszMsg, char *pszFile, int iLine)
{
    if (pfnafClient == NULL) {
        //** Call back not registered!
        //
        // We don't have any output mechanism of our own, since we
        // are platform-independent.  So, just spin in a loop and
        // hope the developer can break in with a debugger to see
        // what is wrong!

        for (;;)
            ;
    }
    else {  //** Call back registered
        (*pfnafClient)(pszMsg,pszFile,iLine);   // Inform client
    }
}

#endif // !ASSERT