/
nullpo.h
211 lines (178 loc) · 6.2 KB
/
nullpo.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
#ifndef _NULLPO_H_
#define _NULLPO_H_
#include "cbasetypes.h"
#define NLP_MARK __FILE__, __LINE__, __func__
// enabled by default on debug builds
#if defined(DEBUG) && !defined(NULLPO_CHECK)
#define NULLPO_CHECK
#endif
#if defined(NULLPO_CHECK)
/**
* Macros used to check for NULL pointer and output that information.
*/
/**
* Return 0 if pointer is not found.
* @param t: Pointer to check
* @return 0 if t is NULL
*/
#define nullpo_ret(t) \
if (nullpo_chk(NLP_MARK, (void *)(t))) {return(0);}
/**
* Return void if pointer is not found.
* @param t: Pointer to check
* @return void if t is NULL
*/
#define nullpo_retv(t) \
if (nullpo_chk(NLP_MARK, (void *)(t))) {return;}
/**
* Return the given value if pointer is not found.
* @param ret: Value to return
* @param t: Pointer to check
* @return ret value
*/
#define nullpo_retr(ret, t) \
if (nullpo_chk(NLP_MARK, (void *)(t))) {return(ret);}
/**
* Break out of the loop/switch if pointer is not found.
* @param t: Pointer to check
*/
#define nullpo_retb(t) \
if (nullpo_chk(NLP_MARK, (void *)(t))) {break;}
// Different C compilers uses different argument formats
#if __STDC_VERSION__ >= 199901L || defined(_MSC_VER)
/* C99 standard */
/**
* Return 0 and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return 0 if t is NULL
*/
#define nullpo_ret_f(t, fmt, ...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(0);}
/**
* Return void and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return void if t is NULL
*/
#define nullpo_retv_f(t, fmt, ...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return;}
/**
* Return the given value and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return ret value
*/
#define nullpo_retr_f(ret, t, fmt, ...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(ret);}
/**
* Break out of the loop/switch and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
*/
#define nullpo_retb_f(t, fmt, ...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {break;}
#elif __GNUC__ >= 2
/* For GCC */
/**
* Return 0 and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return 0 if t is NULL
*/
#define nullpo_ret_f(t, fmt, args...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(0);}
/**
* Return void and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return void if t is NULL
*/
#define nullpo_retv_f(t, fmt, args...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return;}
/**
* Return the given value and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
* @return ret value
*/
#define nullpo_retr_f(ret, t, fmt, args...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(ret);}
/**
* Break out of the loop/switch and display additional information if pointer is not found.
* @param t: Pointer to check
* @param fmt: Pass to vprintf, Format and arguments such as description
*/
#define nullpo_retb_f(t, fmt, args...) \
if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {break;}
#else
/* Otherwise... */
#endif
#else /* NULLPO_CHECK */
/* No Nullpo check */
// if((t)){;}
// Do nothing if Nullpo check is disabled
#define nullpo_ret(t) (void)(t)
#define nullpo_retv(t) (void)(t)
#define nullpo_retr(ret, t) (void)(t)
#define nullpo_retb(t) (void)(t)
// Different C compilers uses different argument formats
#if __STDC_VERSION__ >= 199901L || defined(_MSC_VER)
/* C99 standard */
#define nullpo_ret_f(t, fmt, ...) (void)(t)
#define nullpo_retv_f(t, fmt, ...) (void)(t)
#define nullpo_retr_f(ret, t, fmt, ...) (void)(t)
#define nullpo_retb_f(t, fmt, ...) (void)(t)
#elif __GNUC__ >= 2
/* For GCC */
#define nullpo_ret_f(t, fmt, args...) (void)(t)
#define nullpo_retv_f(t, fmt, args...) (void)(t)
#define nullpo_retr_f(ret, t, fmt, args...) (void)(t)
#define nullpo_retb_f(t, fmt, args...) (void)(t)
#else
/* Otherwise... orz */
#endif
#endif /* NULLPO_CHECK */
/**
* Check for NULL pointer and output information.
* @param file: __FILE__
* @param line: __LINE__
* @param func: __func__ (name of the function) [NLP_MARK]
* @param target: Target to check
* @return 0 on success or 1 on NULL
*/
int nullpo_chk(const char *file, int line, const char *func, const void *target);
/**
* Check for NULL pointer and output detailed information.
* @param file: __FILE__
* @param line: __LINE__
* @param func: __func__ (name of the function) [NLP_MARK]
* @param target: Target to check
* @param fmt: Passed to vprintf
* @return 0 on success or 1 on NULL
*/
int nullpo_chk_f(const char *file, int line, const char *func, const void *target,
const char *fmt, ...)
__attribute__((format(printf,5,6)));
/**
* Display information of the code that cause this function to trigger.
* @param file: __FILE__
* @param line: __LINE__
* @param func: __func__ (name of the function) [NLP_MARK]
* @param target: Target to check
*/
void nullpo_info(const char *file, int line, const char *func);
/**
* Check for NULL pointer and output detailed information.
* @param file: __FILE__
* @param line: __LINE__
* @param func: __func__ (name of the function) [NLP_MARK]
* @param target: Target to check
* @param fmt: Passed to vprintf
*/
void nullpo_info_f(const char *file, int line, const char *func,
const char *fmt, ...)
__attribute__((format(printf,4,5)));
#endif /* _NULLPO_H_ */