platform.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _PLATFORM_H_
  14. #define _PLATFORM_H_
  15. #define _TESTING // this should remain set for the near future
  16. // **** Architecture detection ****
  17. // Ref: http://sourceforge.net/p/predef/wiki/Architectures/
  18. // Following GNU, ARMCC, ICC and MSVS macros only
  19. // Identify all possible flags that mean ARM32, even when in Thumb mode
  20. // further separation could be tested via #ifdef __thumb__
  21. #if defined(__arm__) || defined(__thumb__) \
  22. || defined(_M_ARM) || defined(_M_ARMT) \
  23. || defined(__TARGET_ARCH_ARM) || defined(__TARGET_ARCH_THUMB)
  24. #define _ARCH_ARM32_
  25. #endif
  26. // ARM64 has only one which all compilers follow
  27. #ifdef __aarch64__
  28. #define _ARCH_ARM64_
  29. #endif
  30. // Identify all possible flags that mean ppc64el
  31. #if defined(__powerpc__) || defined(__powerpc64__) \
  32. || defined(__ppc__) || defined(__ppc64__) || defined(__ppc64el__)
  33. #define _ARCH_PPC64EL_
  34. #endif
  35. // Identify all possible flags that mean x86_64
  36. #if defined(__amd64__) || defined(__x86_64__) \
  37. || defined(_M_X64) || defined(_M_AMD64)
  38. #define _ARCH_X86_64_
  39. #else
  40. // Identify all possible flags that mean x86
  41. #if defined(__i386__) || defined(_M_IX86)
  42. #define _ARCH_X86_
  43. #endif
  44. #endif
  45. #define CACHE_LINE_SIZE 64
  46. // **** START OF X-PLATFORM SECTION ****
  47. #if defined(_ARCH_X86_64_) || defined(_ARCH_ARM64_) || defined(_ARCH_PPC64EL_) || __WORDSIZE==64
  48. #define __64BIT__
  49. #endif
  50. #ifdef _FILE_OFFSET_BITS
  51. //#error PLATFORM.H must be included first
  52. #endif
  53. #define _FILE_OFFSET_BITS 64
  54. #ifndef _WIN32
  55. #ifndef _LARGEFILE64_SOURCE
  56. #define _LARGEFILE64_SOURCE 1
  57. #endif
  58. #endif
  59. #if defined(__linux__)
  60. #ifndef _GNU_SOURCE
  61. #define _GNU_SOURCE
  62. #endif
  63. #include <endian.h>
  64. #endif
  65. #ifndef __LITTLE_ENDIAN
  66. #define __LITTLE_ENDIAN 1
  67. #endif
  68. #ifndef __BIG_ENDIAN
  69. #define __BIG_ENDIAN 2
  70. #endif
  71. #define __BYTE_ORDER __LITTLE_ENDIAN
  72. typedef unsigned size32_t;
  73. #if (defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__))
  74. typedef __SIZE_TYPE__ memsize_t;
  75. #else
  76. typedef size_t memsize_t;
  77. #endif
  78. typedef memsize_t rowsize_t;
  79. #define count_t __int64
  80. #define kcount_t size32_t
  81. #define CF I64F
  82. #define KCF "d"
  83. #ifdef _WIN32
  84. #define I64C(n) n##i64
  85. #define U64C(n) n##ui64
  86. #else
  87. #define I64C(n) n##LL
  88. #define U64C(n) n##ULL
  89. #endif
  90. // **** END OF X-PLATFORM SECTION ****
  91. #if defined(_WIN32)
  92. #define _CRT_SECURE_NO_WARNINGS
  93. #define NO_WARN_MBCS_MFC_DEPRECATION
  94. #if (_MSC_VER>=1300)
  95. #pragma warning(disable:4996)
  96. #endif
  97. // **** START OF WIN32 SPECIFIC SECTION ****
  98. #ifndef ALL_WINDOWS
  99. #define WIN32_LEAN_AND_MEAN
  100. #endif
  101. #ifdef _DEBUG
  102. #ifndef USING_MPATROL //using mpatrol memory leak tool
  103. #define _CRTDBG_MAP_ALLOC
  104. #endif
  105. #endif
  106. #define NOMINMAX
  107. #include <windows.h>
  108. #include <stdlib.h>
  109. #include <io.h>
  110. #include <fcntl.h>
  111. #include <malloc.h>
  112. #include <sys/stat.h>
  113. #include <winioctl.h>
  114. #include <direct.h>
  115. #if (_MSC_VER>=1300)
  116. #include <string>
  117. #include <fstream>
  118. #endif
  119. //#ifdef USING_SCM_WITH_STL //before #define of "debug" new
  120. #ifdef _MSC_VER
  121. #include <new>
  122. #include <memory>
  123. #define __attribute__(param) /* do nothing */
  124. #endif
  125. #define ThreadId DWORD
  126. #define MutexId HANDLE
  127. #define sleep(X) Sleep(X*1000)
  128. #define I64F "I64"
  129. #define PATHSEPCHAR '\\'
  130. #define PATHSEPSTR "\\"
  131. #define TEXT_TRANS "t"
  132. #define LLC(NUM) NUM
  133. #define ENVSEPCHAR ';'
  134. #define ENVSEPSTR ";"
  135. #define SEPARATE_LIB_DLL_FILES
  136. #define SharedObjectPrefix ""
  137. #define SharedObjectExtension ".dll"
  138. #define LibraryExtension ".lib"
  139. #define ProcessExtension ".exe"
  140. #define GetSharedProcedure(h,name) GetProcAddress(h,(char *)name)
  141. #define LoadSucceeded(h) ((memsize_t)h >= 32)
  142. #define GetSharedObjectError() GetLastError()
  143. #define GetSharedObjectErrorString() strerror(errno)
  144. #define strtok_r(a,b,c) j_strtok_r(a,b,c)
  145. #define __builtin_prefetch(addr) _mm_prefetch((const char *)(addr), _MM_HINT_T0)
  146. #define __thread __declspec(thread)
  147. typedef unsigned __int64 off64_t;
  148. typedef int socklen_t;
  149. typedef int ssize_t; // correct return to type for unix read/write/pread etc.
  150. #define fpos_ht fpos_t
  151. typedef long double LDouble;
  152. typedef unsigned long MaxCard;
  153. /* Floating point constants */
  154. #define MaxValidExp +308
  155. #define MinValidExp -330
  156. #define MaxRealMant 1.7
  157. #define MinRealMant 2.33
  158. #define MaxRealValue 1.7E+308
  159. #define S_IXUSR 0x40 // Execute by owner
  160. #define S_IWUSR 0x80 // Write by owner
  161. #define S_IRUSR 0x100 // Read by owner
  162. #define S_IXGRP 0x200 // Execute by group
  163. #define S_IWGRP 0x400 // Write by group
  164. #define S_IRGRP 0x800 // Read by group
  165. #define S_IXOTH 0x1000 // Execute by other
  166. #define S_IWOTH 0x2000 // Write by other
  167. #define S_IROTH 0x4000 // Read by other
  168. #define S_IRWXU (S_IXGRP|S_IWGRP|S_IRGRP)
  169. #define S_IRWXG (S_IXGRP|S_IWGRP|S_IRGRP)
  170. #define S_IRWXO (S_IXGRP|S_IWGRP|S_IRGRP)
  171. // MSVC 2008 in debug (perhaps other versions too) will throw exception if negative passed (see bug: #32013)
  172. #if (_MSC_VER>=1400) // >=vs2005
  173. #define strictmsvc_isalpha(c) isalpha(c)
  174. #define isalpha(c) isalpha((const unsigned char)(c))
  175. #define strictmsvc_isupper(c) isupper(c)
  176. #define isupper(c) isupper((const unsigned char)(c))
  177. #define strictmsvc_islower(c) islower(c)
  178. #define islower(c) islower((const unsigned char)(c))
  179. #define strictmsvc_isdigit(c) isdigit(c)
  180. #define isdigit(c) isdigit((const unsigned char)(c))
  181. #define strictmsvc_isxdigit(c) isxdigit(c)
  182. #define isxdigit(c) isxdigit((const unsigned char)(c))
  183. #define strictmsvc_isspace(c) isspace(c)
  184. #define isspace(c) isspace((const unsigned char)(c))
  185. #define strictmsvc_ispunct(c) ispunct(c)
  186. #define ispunct(c) ispunct((const unsigned char)(c))
  187. #define strictmsvc_isalnum(c) isalnum(c)
  188. #define isalnum(c) isalnum((const unsigned char)(c))
  189. #define strictmsvc_isprint(c) isprint(c)
  190. #define isprint(c) isprint((const unsigned char)(c))
  191. #define strictmsvc_isgraph(c) isgraph(c)
  192. #define isgraph(c) isgraph((const unsigned char)(c))
  193. #define strictmsvc_iscntrl(c) iscntrl(c)
  194. #define iscntrl(c) iscntrl((const unsigned char)(c))
  195. #define strictmsvc_tolower(c) tolower(c)
  196. #define tolower(c) tolower((const unsigned char)(c))
  197. #define strictmsvc_toupper(c) toupper(c)
  198. #define toupper(c) toupper((const unsigned char)(c))
  199. #endif // (_MSC_VER>=1400)
  200. #define likely(x) (x)
  201. #define unlikely(x) (x)
  202. inline int daemon(int, int) { return -1; }
  203. // **** END OF WIN32 SPECIFIC SECTION ****
  204. #else
  205. // **** START OF UNIX GENERAL SECTION ****
  206. #define likely(x) __builtin_expect(!!(x), 1)
  207. #define unlikely(x) __builtin_expect(!!(x), 0)
  208. #define _stdcall
  209. #define __stdcall
  210. #define _fastcall
  211. #define __fastcall
  212. #define _fastcall
  213. #define __cdecl
  214. #if defined(__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
  215. // **** START OF LINUX SPECIFIC SECTION ****
  216. #include <aio.h>
  217. #define __BYTE_ORDER __LITTLE_ENDIAN
  218. #define _atoi64 atoll
  219. #define _lseek lseek
  220. #define _llseek ::lseek
  221. #define _lseeki64 ::lseek
  222. #define _vsnprintf vsnprintf
  223. #define _snprintf snprintf
  224. #define strnicmp strncasecmp
  225. #define INFINITE 0xFFFFFFFF
  226. #ifdef _LARGEFILE64_SOURCE
  227. // needed by <sys/types.h>
  228. #ifndef __USE_LARGEFILE64
  229. #define __USE_LARGEFILE64
  230. #endif
  231. #ifndef __USE_FILE_OFFSET64
  232. #define __USE_FILE_OFFSET64
  233. #endif
  234. #endif
  235. // **** END OF LINUX SPECIFIC SECTION ****
  236. #else
  237. #undef __BYTE_ORDER
  238. #define __BYTE_ORDER __BIG_ENDIAN // e.g. Solaris
  239. #endif
  240. #if defined(__SVR4)
  241. typedef int socklen_t;
  242. #endif
  243. #include <stdlib.h>
  244. #include <sys/types.h>
  245. #include <unistd.h>
  246. #include <fcntl.h>
  247. #include <string.h>
  248. #include <ctype.h>
  249. #include <limits.h>
  250. #include <stdio.h>
  251. #include <stdarg.h>
  252. #include <errno.h>
  253. #include <sys/socket.h>
  254. #include <netinet/in.h>
  255. #include <arpa/inet.h>
  256. #include <netdb.h>
  257. #include <signal.h>
  258. #define PASCAL
  259. #define __declspec(dllexport)
  260. #define __int32 int
  261. #define __int16 short
  262. #define __int8 char
  263. #define __int64 long long
  264. #ifndef __TIMESTAMP__
  265. #define __TIMESTAMP__ "<__TIMESTAMP__ unsupported>"
  266. #endif
  267. #define ENVSEPCHAR ':'
  268. #define ENVSEPSTR ":"
  269. #define PATHSEPCHAR '/'
  270. #define PATHSEPSTR "/"
  271. #define TEXT_TRANS
  272. #define LLC(NUM) NUM ## LL
  273. #define wsprintf sprintf
  274. #define _lread _read
  275. #define _lclose _close
  276. #define _lwrite _write
  277. #define _lopen _open
  278. #define DeleteFile(name) (unlink(name)==0)
  279. #define wvsprintf vsprintf
  280. #define _close ::close
  281. #define _stat stat
  282. #define _fstat ::fstat
  283. #define _dup ::dup
  284. #define _chdir ::chdir
  285. #define _setmode(a,b)
  286. #define TRUE 1
  287. #define FALSE 0
  288. //#define false FALSE
  289. //#define true TRUE
  290. #define HFILE int
  291. //#define bool unsigned char
  292. #define BOOL bool
  293. #define UINT unsigned int
  294. #define DWORD unsigned long
  295. #define VOID void
  296. #define LPBYTE char *
  297. #define LPSTR char *
  298. #define LPTSTR char *
  299. #define LPVOID
  300. #define FAR
  301. #define WINAPI
  302. #define fpos_ht off_t
  303. #define handle_t void *
  304. #define HINSTANCE void *
  305. #define HANDLE int
  306. #define HMODULE void *
  307. #define _MAX_PATH PATH_MAX
  308. #define HFILE_ERROR -1
  309. #define OF_READWRITE O_RDWR, S_IRUSR | S_IWUSR
  310. #define OF_READ O_RDONLY, S_IRUSR | S_IWUSR
  311. #define _O_RDWR O_RDWR
  312. #define _O_RDONLY O_RDONLY
  313. #define _O_CREAT O_CREAT
  314. #define _O_TRUNC O_TRUNC
  315. #define _O_APPEND O_APPEND
  316. #define _O_BINARY 0
  317. #define _O_SEQUENTIAL 0
  318. #define _O_TEXT 0
  319. #define _O_RDONLY O_RDONLY
  320. #define _O_WRONLY O_WRONLY
  321. #define _O_RANDOM 0
  322. #define _S_IREAD S_IRUSR | S_IRGRP | S_IROTH
  323. #define _S_IWRITE S_IWUSR | S_IWGRP | S_IWOTH
  324. #define _S_IEXEC S_IXUSR | S_IXGRP | S_IXOTH
  325. #define _S_IFDIR S_IFDIR
  326. #define FILE_CURRENT SEEK_CUR
  327. #define FILE_END SEEK_END
  328. #define FILE_BEGIN SEEK_SET
  329. #define SOCKET int
  330. #define SOCKET_ERROR -1
  331. #define INVALID_SOCKET -1
  332. #define SOCKADDR_IN struct sockaddr_in
  333. #define SOCKADDR struct sockaddr
  334. #define PSOCKADDR SOCKADDR *
  335. #define closesocket close
  336. #define ioctlsocket ioctl
  337. #define WSAGetLastError() 0
  338. #define GetCurrentDirectory(size, buf) getcwd(buf, size)
  339. #define SetCurrentDirectory(path) chdir(path)
  340. #define CreateDirectory(path,X) (_mkdir(path)==0)
  341. #define GetTempPath(size, buff) strncpy(buff, P_tmpdir, size); // return value not valid
  342. #define GetLastError() errno
  343. #define memicmp j_memicmp
  344. #define I64F "ll"
  345. #ifndef stricmp
  346. #define stricmp strcasecmp
  347. #endif
  348. #ifndef __FreeBSD__
  349. #ifndef __APPLE__
  350. #include <malloc.h>
  351. #include <alloca.h>
  352. #endif
  353. #endif
  354. #include <dlfcn.h>
  355. #include <pthread.h>
  356. #include <sys/stat.h>
  357. #include <sys/time.h>
  358. #include <sys/errno.h>
  359. #include <sys/utsname.h>
  360. #if defined (__FreeBSD__) || defined (__APPLE__)
  361. #define MAP_ANONYMOUS MAP_ANON
  362. #endif
  363. #if defined(__FreeBSD__) || defined(__linux__) || defined(__CYGWIN__) || defined (__APPLE__)
  364. #include <sys/ioctl.h>
  365. #else
  366. #include <sys/filio.h>
  367. #include <stropts.h>
  368. #include <sys/asynch.h>
  369. #define _llseek ::llseek
  370. #define _lseeki64 ::llseek
  371. #endif
  372. #define _write ::write
  373. #define _read ::read
  374. #define _open ::open
  375. #define _mkdir(P1) mkdir(P1, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)
  376. #define GetCurrentThreadId pthread_self
  377. #define GetCurrentProcessId getpid
  378. #define _stdcall
  379. // #define inline
  380. #define SharedObjectPrefix "lib"
  381. #if defined (__APPLE__)
  382. #define SharedObjectExtension ".dylib"
  383. #define LibraryExtension ".dylib"
  384. #else
  385. #define SharedObjectExtension ".so"
  386. #define LibraryExtension ".so"
  387. #endif
  388. #define ProcessExtension ""
  389. #define GetSharedProcedure(h,name) dlsym(h,(char *)name)
  390. #define LoadSucceeded(h) (h != NULL)
  391. #define GetSharedObjectError() errno
  392. #define GetSharedObjectErrorString() dlerror()
  393. #define ThreadId pthread_t
  394. #define MutexId pthread_mutex_t
  395. // **** END OF UNIX SPECIFIC SECTION ****
  396. #endif
  397. #define FLOAT_SIG_DIGITS 7
  398. #define DOUBLE_SIG_DIGITS 16
  399. #define MAX_DECIMAL_LEADING 32 // Maximum number of leading digits in a decimal field
  400. #define MAX_DECIMAL_PRECISION 32 // Maximum digits in a decimal field
  401. #define MAX_DECIMAL_DIGITS (MAX_DECIMAL_LEADING+MAX_DECIMAL_PRECISION)
  402. #define strtok(a,b) j_strtok_deprecated(a,b) // will disappear at some point
  403. typedef unsigned __int64 hash64_t;
  404. typedef unsigned __int64 __uint64;
  405. typedef __uint64 offset_t;
  406. typedef unsigned char byte;
  407. typedef __int64 cycle_t;
  408. typedef unsigned __int64 timestamp_type;
  409. // BUILD_TAG not needed here anymore - defined in build_tag.h
  410. //#define BUILD_TAG "build_0000" // Will get substituted during pre-build
  411. #ifdef _WINDOWS
  412. #ifndef popen
  413. #define popen _popen
  414. #endif
  415. #ifndef pclose
  416. #define pclose _pclose
  417. #endif
  418. #endif
  419. //Use the following DECL_XXX to annotate items that are exported from dlls, or objects that are thrown:
  420. //DECL_EXPORT for items that are exported from the dll containing the current file being compiled
  421. //DECL_IMPORT for items that are imported from another dll
  422. //DECL_LOCAL is not generally required but could be used if there was an unexported class in a header file
  423. //DECL_EXCEPTION for any non exported/imported objects which are thrown (to avoid problems with RTTI)
  424. #ifdef _MSC_VER
  425. #define DECL_EXPORT __declspec(dllexport)
  426. #define DECL_IMPORT __declspec(dllimport)
  427. #define DECL_LOCAL
  428. #define DECL_EXCEPTION
  429. #elif __GNUC__ >= 4
  430. #define DECL_EXPORT __attribute__ ((visibility ("default")))
  431. #define DECL_IMPORT __attribute__ ((visibility ("default")))
  432. #define DECL_LOCAL __attribute__ ((visibility ("hidden")))
  433. #define DECL_EXCEPTION DECL_EXPORT
  434. #else
  435. #define DECL_EXPORT
  436. #define DECL_IMPORT
  437. #define DECL_LOCAL
  438. #define DECL_EXCEPTION
  439. #endif
  440. //Sanitize support is different on every compiler
  441. //use these macros before a function definition to disable a particular sanitize option for the body of that function
  442. #ifdef __clang__
  443. #define NO_SANITIZE(a) [[clang::no_sanitize(a)]]
  444. #elif __GNUC__ >= 8
  445. #define NO_SANITIZE(a) [[gnu::no_sanitize(a)]]
  446. #else
  447. #define NO_SANITIZE(a)
  448. #endif
  449. #endif