1 // #define MEMORY_SHRINK
2 
3 #if !defined(flagUSEMALLOC) && !defined(flagSO) && !defined(flagHEAPOVERRIDE)
4 #define UPP_HEAP
5 #endif
6 
7 #define __BREAK__        (*(volatile int *)0 = 0) // kill(getpid(), SIGTRAP)
8 
9 #ifdef COMPILER_MSC
10 	#pragma warning(disable : 4800)
11 	#pragma warning(disable : 4129)
12 	#pragma warning(disable : 4290)
13 	#pragma warning(disable : 4068)
14 	#pragma warning(disable : 4005)
15 	#pragma warning(disable : 4675)
16 	#pragma warning(disable : 4996)
17 	#pragma warning(disable : 4180)
18 
19 	#pragma setlocale("C")
20 #endif
21 
22 bool    IsPanicMode();
23 
24 void    Panic(const char *msg);
25 
26 void    AssertFailed(const char *file, int line, const char *cond);
27 
28 void    InstallPanicMessageBox(void (*mb)(const char *title, const char *text));
29 void    PanicMessageBox(const char *title, const char *text);
30 
31 #define VERIFY(x)        ((x) ? (void)0 : ::Upp::AssertFailed(__FILE__, __LINE__, #x))
32 
33 #ifdef _DEBUG
34 
35 #define ASSERT_(x, msg)  ((x) ? (void)0 : ::Upp	::AssertFailed(__FILE__, __LINE__, msg))
36 #define ASSERT(x)        ASSERT_(x, #x)
37 
38 #else
39 
LOGNOP__()40 inline  void LOGNOP__() {}
41 
42 #define LOG_NOP          Upp::LOGNOP__()
43 
44 #define ASSERT_(x, msg)  LOG_NOP
45 #define ASSERT(x)        LOG_NOP
46 
47 #endif
48 
49 #define _cm_  ,
50 
51 #define __countof(a)          int(sizeof(a) / sizeof(a[0]) )
52 
53 #define ASSTRING_(x)  #x
54 #define ASSTRING(x)   ASSTRING_(x)
55 
56 #define COMBINE__(a, b)            a##b
57 #define COMBINE(a, b)              COMBINE__(a, b)
58 
59 #define COMBINE3__(a, b, c)        a##b##c
60 #define COMBINE3(a, b, c)          COMBINE3__(a, b, c)
61 
62 #define COMBINE4__(a, b, c, d)     a##b##c##d
63 #define COMBINE4(a, b, c, d)       COMBINE4__(a, b, c, d)
64 
65 #define COMBINE5__(a, b, c, d, e)  a##b##c##d##e
66 #define COMBINE5(a, b, c, d, e)    COMBINE5__(a, b, c, d, e)
67 
68 #define MK__s__(x)       s__s##x
69 #define MK__s_(x)        MK__s__(x)
70 
71 #ifdef  BLITZ_INDEX__
72 #define MK__s            MK__s_(COMBINE3(BLITZ_INDEX__, _, __LINE__))
73 #else
74 #define MK__s            MK__s_(__LINE__)
75 #endif
76 
77 #ifdef flagCHECKINIT
78 void InitBlockBegin__(const char *fn, int line);
79 void InitBlockEnd__(const char *fn, int line);
80 #else
InitBlockBegin__(const char *,int)81 inline void InitBlockBegin__(const char *, int) {}
InitBlockEnd__(const char *,int)82 inline void InitBlockEnd__(const char *, int) {}
83 #endif
84 
85 struct Callinit {
CallinitCallinit86 	Callinit(void (*fn)(), const char *cpp, int line) { InitBlockBegin__(cpp, line); fn(); InitBlockEnd__(cpp, line); }
CallinitCallinit87 	Callinit(void (*fn)())                            { fn(); }
88 };
89 
90 struct Callexit {
CallexitCallexit91 	Callexit(void (*fn)()) { atexit(fn); }
92 };
93 
94 
95 #define INITBLOCK \
96 static void COMBINE(MK__s, _fn)(); static UPP::Callinit MK__s(COMBINE(MK__s, _fn), __FILE__, __LINE__); \
97 static void COMBINE(MK__s, _fn)()
98 
99 #define INITBLOCK_(x) \
100 static void COMBINE(x, _fn)(); static UPP::Callinit x(COMBINE(x, _fn), __FILE__, __LINE__); \
101 static void COMBINE(x, _fn)()
102 
103 
104 #define EXITBLOCK \
105 static void COMBINE(MK__s, _fn)(); static UPP::Callexit MK__s(COMBINE(MK__s, _fn)); \
106 static void COMBINE(MK__s, _fn)()
107 
108 #define EXITBLOCK_(x) \
109 static void COMBINE(x, _fn)(); static UPP::Callexit x(COMBINE(x, _fn)); \
110 static void COMBINE(x, _fn)()
111 
112 #define INITIALIZE(x) \
113 void x##__initializer(); \
114 static struct x##__initialize_struct { x##__initialize_struct() { x##__initializer(); } } x##__initialize_dummy;
115 
116 #define INITIALIZER(x) \
117 void x##__initializer_fn(); \
118 void x##__initializer() \
119 { \
120 	ONCELOCK { \
121 		x##__initializer_fn(); \
122 	} \
123 } \
124 void x##__initializer_fn()
125 
126 
127 
128 #ifdef DEPRECATED
129 #define INITCODE(x) \
130 static void COMBINE(MK__s, _fn)() { x } static UPP::Callinit MK__s(COMBINE(MK__s, _fn), __FILE__, __LINE__);
131 
132 #define EXITCODE(x) \
133 static void COMBINE(MK__s, _fn)() { x } static UPP::Callexit MK__s(COMBINE(MK__s, _fn));
134 #endif
135 
136 typedef unsigned char      byte;
137 typedef signed char        int8;
138 typedef unsigned char      uint8;
139 
140 typedef short unsigned     word;
141 typedef short int          int16;
142 typedef short unsigned     uint16;
143 
144 #ifdef PLATFORM_WIN32
145 typedef unsigned long      dword;
146 typedef long               int32;
147 typedef unsigned long      uint32;
148 typedef WCHAR              wchar;
149 #else
150 typedef unsigned int       dword;
151 typedef int                int32;
152 typedef unsigned int       uint32;
153 typedef word               wchar;
154 #endif
155 
156 
157 #ifdef COMPILER_MSC
158 typedef __int64            int64;
159 typedef unsigned __int64   uint64;
160 #else
161 typedef long long int      int64;
162 typedef long long unsigned uint64;
163 #endif
164 
165 typedef uint64             qword;
166 
167 struct m128 {
168 	int64 i64[2];
169 
Zerom128170 	static m128 Zero()             { m128 a; a.i64[0] = a.i64[1] = 0; return a; }
171 };
172 
IsNaN(double d)173 inline bool IsNaN(double d)        { return std::isnan(d); }
IsInf(double d)174 inline bool IsInf(double d)        { return std::isinf(d); }
IsFin(double d)175 inline bool IsFin(double d)        { return !IsNaN(d) && !IsInf(d); }
176 
177 #ifdef COMPILER_MSC
178 	#define I64(c) ((int64)COMBINE(c, i64))
179 #else
180 	#define I64(c) ((int64)COMBINE(c, LL))
181 #endif
182 
183 #ifndef INT64_MIN
184 #define INT64_MIN          I64(-0x8000000000000000)
185 #endif
186 #ifndef INT64_MAX
187 #define INT64_MAX          I64(+0x7FFFFFFFFFFFFFFF)
188 #endif
189 
190 #if !defined(PLATFORM_WIN32)
191 
192 #define HIBYTE(a)        (byte)((a) >> 8)
193 #define LOBYTE(a)        byte(a)
194 #define HIWORD(a)        (word)((a) >> 16)
195 #define LOWORD(a)        word(a)
196 
197 #define MAKEWORD(l, h)   ((word)  (((byte) (l)) | ((word) ((byte) (h))) << 8))
198 #define MAKELONG(l, h)   ((dword) (((word) (l)) | ((dword) ((word) (h))) << 16))
199 
200 #endif
201 
202 #define MAKEQWORD(l, h)  ((qword) (((dword) (l)) | ((qword) ((dword) (h))) << 32))
203 #define HIDWORD(a)       (dword)(((uint64)a) >> 32)
204 #define LODWORD(a)       dword(a)
205 
206 #define OFFSETOF(clss, mbr) ((int)(uintptr_t)&(((clss *)1)->mbr) - 1)
207 
clone(const T & x)208 template <typename T> T clone(const T& x) { return x; }
209 
210 #define pick_
211 #define rval_default(T) T(T&&) = default; T& operator=(T&&) = default;
212 
213 template <typename T>
214 auto pick(T&& x) noexcept -> decltype(std::move(x)) { return std::move(x); }
215 
216 template<class T> class Function;
217 
218 #ifdef DEPRECATED
219 #define rval_ &&
220 #define init_
221 #endif
222 
223 #ifdef COMPILER_MSC
224 #define force_inline __forceinline
225 #define never_inline __declspec(noinline)
226 #elif defined(COMPILER_GCC)
227 #define force_inline __attribute__((always_inline)) inline
228 #define never_inline __attribute__((noinline))
229 #else
230 #define force_inline inline
231 #define never_inline
232 #endif
233 
234 #define BINARY(i, f) \
235 extern "C" byte *i; \
236 extern "C" int COMBINE(i, _length);
237 
238 #define BINARY_ARRAY(i, x, f) \
239 extern "C" byte *i[]; \
240 extern "C" int COMBINE(i, _length)[]; \
241 extern "C" int COMBINE(i, _count);
242 
243 #define BINARY_MASK(i, m) \
244 extern "C" byte *i[]; \
245 extern "C" int COMBINE(i, _length)[]; \
246 extern "C" int COMBINE(i, _count); \
247 extern "C" char *COMBINE(i, _files)[];
248 
249 class NoCopy {
250 private:
251 	NoCopy(const NoCopy&);
252 	void operator=(const NoCopy&);
253 public:
NoCopy()254 	NoCopy() {}
255 };
256 
257 const int    INT_NULL           =    INT_MIN;
258 const int64  INT64_NULL         =    INT64_MIN;
259 
260 const double DOUBLE_NULL        =    -1.0E+308;
261 const double DOUBLE_NULL_LIM    =    -1.0E+307;
262 
263 class Nuller {
264 public:
265 	operator int() const                { return INT_NULL; }
int64()266 	operator int64() const              { return INT64_NULL; }
267 	operator double() const             { return DOUBLE_NULL; }
268 	operator bool() const               { return false; }
269 
Nuller()270 	Nuller() {}
271 };
272 
273 extern const Nuller Null;
274 
SetNull(T & x)275 template <class T> void SetNull(T& x) { x = Null; }
276 
IsNull(const T & x)277 template <class T> bool IsNull(const T& x)       { return x.IsNullInstance(); }
278 
IsNull(const int & i)279 template<> inline bool  IsNull(const int& i)     { return i == INT_NULL; }
IsNull(const int64 & i)280 template<> inline bool  IsNull(const int64& i)   { return i == INT64_NULL; }
IsNull(const double & r)281 template<> inline bool  IsNull(const double& r)  { return r < DOUBLE_NULL_LIM; }
IsNull(const bool & r)282 template<> inline bool  IsNull(const bool& r  )  { return false; }
283 
284 #include "Heap.h"
285 
286 #ifdef CPU_X86
287 bool CpuMMX();
288 bool CpuSSE();
289 bool CpuSSE2();
290 bool CpuSSE3();
291 bool CpuHypervisor();
292 bool CpuAVX();
293 #endif
294 
295 int  CPU_Cores();
296 
297 void GetSystemMemoryStatus(uint64& total, uint64& available);
298 
299 template <class T>
Swap(T & a,T & b)300 inline void Swap(T& a, T& b) { T tmp = pick(a); a = pick(b); b = pick(tmp); }
301 
302 //Quick fix....
303 #ifdef PLATFORM_WINCE
304 const char *FromSysChrSet(const wchar *s);
305 const wchar *ToSysChrSet(const char *s);
306 #else
FromSysChrSet(const char * s)307 inline const char *FromSysChrSet(const char *s) { return s; }
ToSysChrSet(const char * s)308 inline const char *ToSysChrSet(const char *s) { return s; }
309 #endif
310 
311 #ifdef _DEBUG
312 void __LOGF__(const char *format, ...);
313 #define LOGF             UPP::__LOGF__
314 #else
315 inline void __LOGF__(const char *format, ...);
316 #endif
317 
318 template <class T>
IGNORE_RESULT(const T &)319 void IGNORE_RESULT(const T&) {}
320 
321 // Backward compatibility
322 
323 #define GLOBAL_VP(type, name, param) \
324 name() \
325 { \
326 	static type x param; \
327 	return x; \
328 }
329 
330 #define GLOBAL_VARP(type, name, param) \
331 type& GLOBAL_VP(type, name, param)
332 
333 #define GLOBAL_V(type, name)   GLOBAL_VP(type, name, init_)
334 #define GLOBAL_VAR(type, name) type& GLOBAL_V(type, name)
335 
336 #define GLOBAL_VP_INIT(type, name, param) \
337 name() \
338 { \
339 	static type x param; \
340 	return x; \
341 } \
342 INITBLOCK { name(); }
343 
344 #define GLOBAL_VARP_INIT(type, name, param) \
345 type& GLOBAL_VP_INIT(type, name, param)
346 
347 #define GLOBAL_V_INIT(type, name)   GLOBAL_VP_INIT(type, name, init_)
348 #define GLOBAL_VAR_INIT(type, name) type& GLOBAL_V_INIT(type, name)
349 
350 #if __GNUC__ > 6
351 	#define NOUBSAN __attribute__((no_sanitize_undefined))
352 #elif __clang_major__ > 6
353 	#define NOUBSAN __attribute__((no_sanitize("undefined")))
354 #else
355 	#define NOUBSAN
356 #endif
357 
358 // DEPRECATED:
359 
360 // these are pre-c++ 11 tools to achieve something like variadic templates
361 #define __Expand1(x) x(1)
362 #define __Expand2(x)  __Expand1(x) x(2)
363 #define __Expand3(x)  __Expand2(x) x(3)
364 #define __Expand4(x)  __Expand3(x) x(4)
365 #define __Expand5(x)  __Expand4(x) x(5)
366 #define __Expand6(x)  __Expand5(x) x(6)
367 #define __Expand7(x)  __Expand6(x) x(7)
368 #define __Expand8(x)  __Expand7(x) x(8)
369 #define __Expand9(x)  __Expand8(x) x(9)
370 #define __Expand10(x) __Expand9(x) x(10)
371 #define __Expand11(x) __Expand10(x) x(11)
372 #define __Expand12(x) __Expand11(x) x(12)
373 #define __Expand13(x) __Expand12(x) x(13)
374 #define __Expand14(x) __Expand13(x) x(14)
375 #define __Expand15(x) __Expand14(x) x(15)
376 #define __Expand16(x) __Expand15(x) x(16)
377 #define __Expand17(x) __Expand16(x) x(17)
378 #define __Expand18(x) __Expand17(x) x(18)
379 #define __Expand19(x) __Expand18(x) x(19)
380 #define __Expand20(x) __Expand19(x) x(20)
381 #define __Expand21(x) __Expand20(x) x(21)
382 #define __Expand22(x) __Expand21(x) x(22)
383 #define __Expand23(x) __Expand22(x) x(23)
384 #define __Expand24(x) __Expand23(x) x(24)
385 #define __Expand25(x) __Expand24(x) x(25)
386 #define __Expand26(x) __Expand25(x) x(26)
387 #define __Expand27(x) __Expand26(x) x(27)
388 #define __Expand28(x) __Expand27(x) x(28)
389 #define __Expand29(x) __Expand28(x) x(29)
390 #define __Expand30(x) __Expand29(x) x(30)
391 #define __Expand31(x) __Expand30(x) x(31)
392 #define __Expand32(x) __Expand31(x) x(32)
393 #define __Expand33(x) __Expand32(x) x(33)
394 #define __Expand34(x) __Expand33(x) x(34)
395 #define __Expand35(x) __Expand34(x) x(35)
396 #define __Expand36(x) __Expand35(x) x(36)
397 #define __Expand37(x) __Expand36(x) x(37)
398 #define __Expand38(x) __Expand37(x) x(38)
399 #define __Expand39(x) __Expand38(x) x(39)
400 #define __Expand40(x) __Expand39(x) x(40)
401 
402 #define __Expand(x)   __Expand40(x)
403 
404 #define __List1(x) x(1)
405 #define __List2(x)  __List1(x), x(2)
406 #define __List3(x)  __List2(x), x(3)
407 #define __List4(x)  __List3(x), x(4)
408 #define __List5(x)  __List4(x), x(5)
409 #define __List6(x)  __List5(x), x(6)
410 #define __List7(x)  __List6(x), x(7)
411 #define __List8(x)  __List7(x), x(8)
412 #define __List9(x)  __List8(x), x(9)
413 #define __List10(x) __List9(x), x(10)
414 #define __List11(x) __List10(x), x(11)
415 #define __List12(x) __List11(x), x(12)
416 #define __List13(x) __List12(x), x(13)
417 #define __List14(x) __List13(x), x(14)
418 #define __List15(x) __List14(x), x(15)
419 #define __List16(x) __List15(x), x(16)
420 #define __List17(x) __List16(x), x(17)
421 #define __List18(x) __List17(x), x(18)
422 #define __List19(x) __List18(x), x(19)
423 #define __List20(x) __List19(x), x(20)
424 #define __List21(x) __List20(x), x(21)
425 #define __List22(x) __List21(x), x(22)
426 #define __List23(x) __List22(x), x(23)
427 #define __List24(x) __List23(x), x(24)
428 #define __List25(x) __List24(x), x(25)
429 #define __List26(x) __List25(x), x(26)
430 #define __List27(x) __List26(x), x(27)
431 #define __List28(x) __List27(x), x(28)
432 #define __List29(x) __List28(x), x(29)
433 #define __List30(x) __List29(x), x(30)
434 #define __List31(x) __List30(x), x(31)
435 #define __List32(x) __List31(x), x(32)
436 #define __List33(x) __List32(x), x(33)
437 #define __List34(x) __List33(x), x(34)
438 #define __List35(x) __List34(x), x(35)
439 #define __List36(x) __List35(x), x(36)
440 #define __List37(x) __List36(x), x(37)
441 #define __List38(x) __List37(x), x(38)
442 #define __List39(x) __List38(x), x(39)
443 #define __List40(x) __List39(x), x(40)
444 
445 #define E__p(I)       p##I
446