MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
Debug.h
1/* miktex/Core/Debug.h: MiKTeX debug macros -*- C++ -*-
2
3 Copyright (C) 1996-2018 Christian Schenk
4
5 This file is part of the MiKTeX Core Library.
6
7 The MiKTeX Core Library is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2, or
10 (at your option) any later version.
11
12 The MiKTeX Core Library is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the MiKTeX Core Library; if not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#pragma once
23
24#if !defined(ADD61B45737D4F4ABCD5725E989A2EDA)
25#define ADD61B45737D4F4ABCD5725E989A2EDA
26
27#include <miktex/Core/config.h>
28
29#if defined(_MSC_VER)
30# include <crtdbg.h>
31#endif
32
33#if defined(__cplusplus)
34# include "Exceptions.h"
35#endif
36
37#include "Text.h"
38
39#if defined(__cplusplus)
40# include <string>
41#endif
42
43#if defined(__cplusplus)
44# include <cstdlib>
45#else
46# include <stdlib.h>
47#endif
48
49#define MIKTEX_DEBUG_BEGIN_NAMESPACE \
50 namespace MiKTeX { \
51 namespace Debug {
52
53#define MIKTEX_DEBUG_END_NAMESPACE \
54 } \
55 }
56
57#if defined(__cplusplus)
58MIKTEX_DEBUG_BEGIN_NAMESPACE;
59
60MIKTEXCOREEXPORT MIKTEXNORETURN void MIKTEXCEECALL FatalMiKTeXError(const std::string& message, const MiKTeX::Core::MiKTeXException::KVMAP& info, const MiKTeX::Core::SourceLocation& sourceLocation);
61MIKTEXCORECEEAPI(void*) Malloc(std::size_t size, const MiKTeX::Core::SourceLocation& sourceLocation);
62MIKTEXCORECEEAPI(void*) Realloc(void* ptr, std::size_t size, const MiKTeX::Core::SourceLocation& sourceLocation);
63MIKTEXCORECEEAPI(void*) Calloc(std::size_t num, std::size_t size, const MiKTeX::Core::SourceLocation& sourceLocation);
64
65#if defined(_MSC_VER)
66# pragma push_macro("StrDup")
67# undef StrDup
68#endif
69
70MIKTEXCORECEEAPI(char*) StrDup(const char* lpsz, const MiKTeX::Core::SourceLocation& sourceLocation);
71
72#if defined(_MSC_VER)
73# pragma pop_macro("StrDup")
74#endif
75
76MIKTEXCORECEEAPI(void) Free(void* ptr, const MiKTeX::Core::SourceLocation& sourceLocation);
77
78MIKTEX_DEBUG_END_NAMESPACE;
79#endif /* C++ */
80
81MIKTEX_BEGIN_EXTERN_C_BLOCK;
82
83MIKTEXCORECEEAPI(void*) miktex_core_malloc(size_t size, const char* lpszFileName, int line);
84MIKTEXCORECEEAPI(void*) miktex_core_realloc(void* ptr, size_t size, const char* lpszFileName, int line);
85MIKTEXCORECEEAPI(void*) miktex_core_calloc(size_t num, size_t size, const char* lpszFileName, int line);
86MIKTEXCORECEEAPI(char*) miktex_core_strdup(const char* lpsz, const char* lpszFileName, int line);
87MIKTEXCORECEEAPI(void) miktex_core_free(void* ptr, const char* lpszFileName, int line);
88MIKTEXCOREEXPORT void MIKTEXNORETURN MIKTEXCEECALL miktex_core_fatal_error(const char* lpszMiktexFunction, const char* lpszMessage, const char* lpszInfo, const char* lpszSourceFile, int lpszSourceLine);
89
90MIKTEX_END_EXTERN_C_BLOCK;
91
92#define MIKTEX_MALLOC(size) \
93 MiKTeX::Debug::Malloc((size), MIKTEX_SOURCE_LOCATION_DEBUG())
94
95#define MIKTEX_REALLOC(ptr, size) \
96 MiKTeX::Debug::Realloc((ptr), (size), MIKTEX_SOURCE_LOCATION_DEBUG())
97
98#define MIKTEX_CALLOC(num, size) \
99 MiKTeX::Debug::Calloc(num, size, MIKTEX_SOURCE_LOCATION_DEBUG())
100
101# define MIKTEX_STRDUP(str) \
102 MiKTeX::Debug::StrDup((str), MIKTEX_SOURCE_LOCATION_DEBUG())
103
104#define MIKTEX_FREE(ptr) \
105 MiKTeX::Debug::Free((ptr), MIKTEX_SOURCE_LOCATION_DEBUG())
106
107#if defined(MIKTEX_DEBUG)
108# define MIKTEX_ASSERT(expr) \
109 static_cast<void> \
110 ((expr) \
111 ? 0 \
112 : (MiKTeX::Debug::FatalMiKTeXError(MIKTEXTEXT("Assertion failed."), MiKTeX::Core::MiKTeXException::KVMAP("expr", #expr), MIKTEX_SOURCE_LOCATION()), \
113 0))
114#else
115# define MIKTEX_ASSERT(expr) (static_cast<void>(0))
116#endif
117
118#if defined(MIKTEX_DEBUG)
119# define MIKTEX_VERIFY(expr) \
120 (void) \
121 ((expr) \
122 ? 0 \
123 : (miktex_core_fatal_error \
124 (0, \
125 MIKTEXTEXT("Assertion failed."), \
126 #expr, \
127 __FILE__, \
128 __LINE__), \
129 0))
130#else
131# define MIKTEX_VERIFY(expr) ((void)(expr))
132#endif
133
134#if defined(_MSC_VER)
135# define MIKTEX_ASSUME(expr) __assume(expr)
136#else
137# define MIKTEX_ASSUME(expr)
138#endif
139
140#if defined(MIKTEX_DEBUG)
141# define MIKTEX_ASSERT_VALID_HEAP() MiKTeX::Debug::AssertValidHeap()
142#else
143# define MIKTEX_ASSERT_VALID_HEAP()
144#endif
145
146#if defined(MIKTEX_DEBUG)
147# define MIKTEX_ASSERT_VALID_HEAP_POINTER_OR_NIL(ptr) \
148 if (ptr != nullptr) \
149 { \
150 MiKTeX::Debug::AssertValidHeapPointer(ptr); \
151 }
152#else
153# define MIKTEX_ASSERT_VALID_HEAP_POINTER_OR_NIL(ptr)
154#endif
155
156#if defined(MIKTEX_DEBUG)
157# define MIKTEX_ASSERT_BUFFER(ptr, n) MiKTeX::Debug::AssertValidBuf(ptr, n)
158#else
159# define MIKTEX_ASSERT_BUFFER(ptr, n)
160#endif
161
162#if defined(MIKTEX_DEBUG)
163# define MIKTEX_ASSERT_BUFFER_OR_NIL(ptr, n) \
164 if (ptr != nullptr) \
165 { \
166 MiKTeX::Debug::AssertValidBuf(ptr, n); \
167 }
168#else
169# define MIKTEX_ASSERT_BUFFER_OR_NIL(ptr, n)
170#endif
171
172#if defined(MIKTEX_DEBUG)
173# define MIKTEX_ASSERT_CHAR_BUFFER(ptr, n) \
174 MiKTeX::Debug::AssertValidBuf(ptr, sizeof(ptr[0]) * (n))
175#else
176# define MIKTEX_ASSERT_CHAR_BUFFER(ptr, n)
177#endif
178
179#if defined(MIKTEX_DEBUG)
180# define MIKTEX_ASSERT_CHAR_BUFFER_OR_NIL(ptr, n) \
181 if (ptr != nullptr) \
182 { \
183 MiKTeX::Debug::AssertValidBuf(ptr, sizeof(ptr[0]) * (n)); \
184 }
185#else
186# define MIKTEX_ASSERT_CHAR_BUFFER_OR_NIL(ptr, n)
187#endif
188
189#if defined(MIKTEX_DEBUG)
190# define MIKTEX_ASSERT_STRING(ptr) MiKTeX::Debug::AssertValidString(ptr)
191#else
192# define MIKTEX_ASSERT_STRING(ptr)
193#endif
194
195#if defined(MIKTEX_DEBUG)
196# define MIKTEX_ASSERT_STRING_OR_NIL(ptr) \
197 if (ptr != nullptr) \
198 { \
199 MiKTeX::Debug::AssertValidString(ptr); \
200 }
201#else
202# define MIKTEX_ASSERT_STRING_OR_NIL(ptr)
203#endif
204
205# define MIKTEX_ASSERT_PATH_BUFFER(ptr) \
206 MIKTEX_ASSERT_CHAR_BUFFER (ptr, MiKTeX::Core::BufferSizes::MaxPath)
207
208# define MIKTEX_ASSERT_PATH_BUFFER_OR_NIL(ptr) \
209 MIKTEX_ASSERT_CHAR_BUFFER_OR_NIL (ptr, MiKTeX::Core::BufferSizes::MaxPath)
210
211# define MIKTEX_ASSERT_FNAME_BUFFER(ptr) \
212 MIKTEX_ASSERT_CHAR_BUFFER (ptr, MiKTeX::Core::BufferSizes::MaxPath)
213
214#if defined(__cplusplus)
215MIKTEX_DEBUG_BEGIN_NAMESPACE;
216
217inline void AssertValidBuf(void* ptr, std::size_t n)
218{
219 MIKTEX_ASSERT(ptr != nullptr);
220#if defined(MIKTEX_DEBUG)
221 // TODO: check write
222#endif
223}
224
225MIKTEX_DEBUG_END_NAMESPACE;
226#endif
227
228#if defined(__cplusplus)
229MIKTEX_DEBUG_BEGIN_NAMESPACE;
230
231inline void AssertValidString(const char* ptr, std::size_t n = 4096)
232{
233 MIKTEX_ASSERT(ptr != nullptr);
234#if defined(MIKTEX_DEBUG)
235 // TODO: check null terminated string
236#endif
237}
238
239inline void AssertValidString(const wchar_t* ptr, std::size_t n = 4096)
240{
241 MIKTEX_ASSERT(ptr != nullptr);
242#if defined(MIKTEX_DEBUG)
243 // TODO: check null terminated string
244#endif
245}
246
247MIKTEX_DEBUG_END_NAMESPACE;
248#endif
249
250#if defined(__cplusplus)
251MIKTEX_DEBUG_BEGIN_NAMESPACE;
252
253inline void AssertValidHeap()
254{
255#if defined(_MSC_VER)
256# if defined(_DEBUG)
257 MIKTEX_ASSERT(_CrtCheckMemory());
258# elif defined(MIKTEX_DEBUG)
259 int heapStatus;
260 MIKTEX_ASSERT((heapStatus = _heapchk()) == _HEAPOK || (heapStatus == _HEAPEMPTY));
261# endif
262#endif
263}
264
265MIKTEX_DEBUG_END_NAMESPACE;
266#endif
267
268#if defined(__cplusplus)
269MIKTEX_DEBUG_BEGIN_NAMESPACE;
270
271inline void AssertValidHeapPointer(const void* ptr)
272{
273#if defined(_MSC_VER)
274# if defined(_DEBUG)
275 MIKTEX_ASSERT(_CrtIsValidHeapPointer(ptr));
276# endif
277#endif
278}
279
280MIKTEX_DEBUG_END_NAMESPACE;
281#endif
282
283#endif
Definition Exceptions.h:38