-
Notifications
You must be signed in to change notification settings - Fork 7
/
global.hpp
326 lines (283 loc) · 9.8 KB
/
global.hpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* <!-- mksqlite: A MATLAB Interface to SQLite -->
*
* @file global.hpp
* @brief Global definitions.
* @details
* @authors Martin Kortmann <mail@kortmann.de>,
* Andreas Martin <andimartin@users.sourceforge.net>
* @version 2.14
* @date 2008-2024
* @copyright Distributed under BSD-2
* @pre
* @warning
* @bug
*/
#pragma once
#ifndef _GLOBAL_H
#define _GLOBAL_H /**< include guard (redundant) */
#endif
/* Common global definitions */
// Is noexcept supported?
#ifndef NOEXCEPT
#if defined(_MSC_VER) && defined(_NOEXCEPT)
// MSVC
#define NOEXCEPT _NOEXCEPT
#elif __cplusplus >= 199711L
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
#endif
#if defined( MATLAB_MEX_FILE ) /* MATLAB MEX file */
/* solve the 'error C2371: 'char16_t' : redefinition; different basic types' problem */
/* ref: http://www.mathworks.com/matlabcentral/newsreader/view_thread/281754 */
/* ref: http://connect.microsoft.com/VisualStudio/feedback/details/498952/vs2010-iostream-is-incompatible-with-matlab-matrix-h */
#ifdef _WIN32
#include <yvals.h>
#if (_MSC_VER >= 1600)
#define __STDC_UTF_16__
#endif
/* Following solution doesn't work with Matlab R2011b, MSVC2010, Win7 64bit
#ifdef _CHAR16T
#define CHAR16_T
#endif
*/
#endif
#include "mex.h"
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define STRICT
#define NOMINMAX
#include <windows.h>
#undef min
#undef max
#if( __GNUC__ )
#define _copysign copysign
#endif
#else // linux
#include <string.h>
#include <ctype.h>
#define _strcmpi strcasecmp
#define _strnicmp strncasecmp
#define _snprintf snprintf
#define _vsnprintf vsnprintf
#define _strdup strdup
#define _copysign copysign ///< alias (win/linux compatibility)
#endif
#include "config.h"
#include <cstddef>
#include <cmath>
#include <cassert>
#include <climits>
// Patch for Mac:
// Tested on Mac OSX 10.9.2, Malab R2014a, 64 bit (Stefan Balke)
#if defined(__APPLE__) || defined(TARGET_OS_X)
// todo: Which header is needed on mac?
//#include <tr1/cstdint>
#include <cstdint>
#else
#include <cstdint>
#endif
/**
* \def MEM_ALLOC
* \brief standard memory allocator
*
* \def MEM_REALLOC
* \brief standard memory deallocator
*
* \def MEM_FREE(ptr)
* \brief standard memory free function
*
* \def MAT_ARRAY_TYPE
* \brief standard type for internally handled MATLAB array types (not used yet)
*
* \def MAT_ALLOC(m,n,typeID)
* \brief standard allocator for MAT_ARRAY_TYPE (not used yet)
*
* \def MAT_FREE(ptr)
* \brief standard deallocator for MAT_ARRAY_TYPE (not used yet)
*
* Use ::utils_free_ptr() instead of MEM_FREE, when a NULL check
* must be performed on \p ptr and &ptr must be set to NULL after freeing.
*
* \def WALKHEAP
* When heap checking is on (\ref CONFIG_USE_HEAP_CHECK), reports
* the monitored heap allocation state (MATLAB command window).
*
* \def FREEHEAP
* When heap checking is on (\ref CONFIG_USE_HEAP_CHECK), frees
* heap space where allocation was monitored.
*
*/
// define standard memory handlers used by mksqlite
#undef MEM_ALLOC
#undef MEM_REALLOC
#undef MEM_FREE
#if 1
// mxCalloc() and mxFree() are extremely slow!
#define MEM_ALLOC( count, bytes ) ((void*)mxMalloc( (count) * (bytes) ))
#define MEM_FREE( ptr ) mxFree( (void*)ptr )
#define MEM_REALLOC( ptr, bytes ) ((void*)mxRealloc( (void*)ptr, bytes ))
#else
// Global memory allocator
#define MEM_ALLOC( count, bytes ) ( (void*)new char[(count) * (bytes)] )
// Global memory deallocator
#define MEM_FREE( ptr ) ( delete[] ptr )
// Global memory deallocator
#define MEM_REALLOC( ptr, size ) HC_ASSERT_ERROR
#endif
// definition how matrix arrays are managed
#if 1 && defined( MATLAB_MEX_FILE )
#define MAT_ARRAY_TYPE mxArray
#define MAT_ALLOC( m, n, typeID ) mxCreateNumericMatrix( m, n, typeID, mxREAL )
#define MAT_FREE( ptr ) mxDestroyArray( ptr )
#else
// \todo not supported yet
struct tagNumericArray;
#define MAT_ARRAY_TYPE tagNumericArray
#define MAT_ALLOC( m, n, typeID ) tagNumericArray::Create( m, n, typeID )
#define MAT_FREE( ptr ) tagNumericArray::FreeArray( ptr )
#endif
/**
* \def CONFIG_USE_HEAP_CHECK
* When defined, MEM_ALLOC, MEM_REALLOC and MEM_FREE actions were monitored
* and some simple boundary checks will be made.
*/
#if CONFIG_USE_HEAP_CHECK
// redefine memory (de-)allocators, if heap checking is on
// memory macros MEM_ALLOC, MEM_REALLOC and MEM_FREE were used by heap_check.hpp
#include "heap_check.hpp"
// Now redirect memory macros to heap checking functions
#undef MEM_ALLOC
#undef MEM_REALLOC
#undef MEM_FREE
#define MEM_ALLOC(n,s) (HeapCheck.New( (n) * (s), __FILE__, __FUNCTION__, /*notes*/ "", __LINE__ ))
#define MEM_REALLOC(p,s) (HeapCheck.Realloc( (void*)(p), (s), __FILE__, __FUNCTION__, /*notes*/ "", __LINE__ ))
#define MEM_FREE(p) (HeapCheck.Free( (void*)(p) ))
#define WALKHEAP (HeapCheck.Walk())
#define FREEHEAP (HeapCheck.Release())
#else
#define HC_COMP_ASSERT(exp)
#define HC_ASSERT(exp)
#define HC_ASSERT_ERROR
#define HC_NOTES(ptr,notes)
#define WALKHEAP
#define FREEHEAP
#endif // CONFIG_USE_HEAP_CHECK
/**
* \name IEEE representation functions
*
* @{
*/
#if defined( MATLAB_MEX_FILE )
#define DBL_ISFINITE mxIsFinite
#define DBL_ISINF mxIsInf
#define DBL_ISNAN mxIsNaN
#define DBL_INF mxGetInf()
#define DBL_NAN mxGetNaN()
#define DBL_EPS mxGetEps()
#else
#if defined( _WIN32 )
// MSVC2010
#define DBL_INF (_Inf._Double) /* <ymath.h> -?-/double */
#define DBL_NAN (_Nan._Double) /* <ymath.h> -?-/double */
// #define DBL_EPS DBL_EPS
#define DBL_ISFINITE(x) _finite(x) /* <float.h> -?-/double */
#define DBL_ISNAN(x) _isnan(x) /* <float.h> -?-/double */
#define DBL_ISINF(x) (!DBL_ISFINITE(x) && !DBL_ISNAN(x))
#else
// gcc
#define DBL_INF INFINITY /* <cmath> implementation defined */
#define DBL_NAN NAN /* <cmath> implementation defined */
// #define DBL_EPS DBL_EPS
#if 0
#define DBL_ISFINITE(x) (((x)-(x)) == 0.0)
#define DBL_ISNAN(x) ((x)!=(x))
#define DBL_ISINF(x) (!DBL_ISFINITE(x) && !DBL_ISNAN(x))
#else
#define DBL_ISFINITE(x) isfinite(x) /* <cmath> float/double */
#define DBL_ISNAN(x) isnan(x) /* <cmath> float/double */
#define DBL_ISINF(x) isinf(x) /* <cmath> float/double */
#endif
#endif
#endif
/** @} */
/**
* \name Versionstrings
*
* mksqlite version string is defined in config.h
* (\ref CONFIG_MKSQLITE_VERSION_STRING)
*
* @{
*/
#define SQLITE_VERSION_STRING SQLITE_VERSION
#define DEELX_VERSION_STRING "1.3"
/** @} */
/* early bind of serializing functions (earlier MATLAB versions only) */
#if defined( MATLAB_MEX_FILE )
#if defined( CONFIG_EARLY_BIND_SERIALIZE )
extern "C" mxArray* mxSerialize(const mxArray*); ///< Serialize a MATLAB array
extern "C" mxArray* mxDeserialize(const void*, size_t); ///< Deserialize a MATLAB array
#endif
extern mxArray *mxCreateSharedDataCopy(const mxArray *pr); ///< Create a "shadowed" MATLAB array
#define PRINTF mexPrintf ///< Global text output function
#endif
typedef unsigned char byte; ///< byte type
#if MKSQLITE_CONFIG_USE_LOGGING
extern "C"
{
#include "logging/src/log.h"
}
/// Logger file class. Ensure file is closed at exit
class LogFile
{
FILE *fp;
public:
LogFile(const char* name = NULL)
{
// todo: Current directory?
fp = fopen( ( name ? name : "mksqlite.log" ), "at");
}
~LogFile()
{
fclose(fp);
}
FILE* get_fp()
{
return fp;
}
};
#endif
#if defined( MAIN_MODULE )
/* common global states */
/**
* \name Compression settings for typed BLOBs
*
* @{
*/
int g_compression_level = MKSQLITE_CONFIG_COMPRESSION_LEVEL;
const char* g_compression_type = MKSQLITE_CONFIG_COMPRESSION_TYPE;
int g_compression_check = MKSQLITE_CONFIG_COMPRESSION_CHECK;
/** @} */
/// Flag: String representation (utf8 or ansi)
int g_convertUTF8 = MKSQLITE_CONFIG_CONVERT_UTF8;
/// global NaN representation
const double g_NaN = DBL_NAN;
/// MATALAB specific globals
#if defined( MATLAB_MEX_FILE )
/// Max. length for fieldnames in MATLAB
int g_namelengthmax = 63; // (mxMAXNAM-1)
/// Flag: return NULL as NaN
int g_NULLasNaN = MKSQLITE_CONFIG_NULL_AS_NAN;
/// Flag: Check for unique fieldnames
int g_check4uniquefields = MKSQLITE_CONFIG_CHECK_4_UNIQUE_FIELDS;
/// Flag: Allow streaming
int g_streaming = MKSQLITE_CONFIG_STREAMING;
/// Data organisation of returning query results
int g_result_type = MKSQLITE_CONFIG_RESULT_TYPE;
/// Wrap parameters
int g_param_wrapping = MKSQLITE_CONFIG_PARAM_WRAPPING;
#endif // defined( MATLAB_MEX_FILE )
#endif // defined( MAIN_MODULE )