platform.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 x86_64
  31. #if defined(__amd64__) || defined(__x86_64__) \
  32. || defined(_M_X64) || defined(_M_AMD64)
  33. #define _ARCH_X86_64_
  34. #else
  35. // Identify all possible flags that mean x86
  36. #if defined(__i386__) || defined(_M_IX86)
  37. #define _ARCH_X86_
  38. #endif
  39. #endif
  40. // **** START OF X-PLATFORM SECTION ****
  41. #if defined(_ARCH_X86_64_) || defined(_ARCH_ARM64_) || __WORDSIZE==64
  42. #define __64BIT__
  43. #endif
  44. #ifdef _FILE_OFFSET_BITS
  45. //#error PLATFORM.H must be included first
  46. #endif
  47. #define _FILE_OFFSET_BITS 64
  48. #ifndef _LARGEFILE64_SOURCE
  49. #define _LARGEFILE64_SOURCE 1
  50. #endif
  51. #if defined(__linux__)
  52. #ifndef _GNU_SOURCE
  53. #define _GNU_SOURCE
  54. #endif
  55. #include <endian.h>
  56. #endif
  57. #ifndef __LITTLE_ENDIAN
  58. #define __LITTLE_ENDIAN 1
  59. #endif
  60. #ifndef __BIG_ENDIAN
  61. #define __BIG_ENDIAN 2
  62. #endif
  63. #define __BYTE_ORDER __LITTLE_ENDIAN
  64. typedef unsigned size32_t;
  65. #if (defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__))
  66. typedef __SIZE_TYPE__ memsize_t;
  67. #else
  68. typedef size_t memsize_t;
  69. #endif
  70. typedef memsize_t rowsize_t;
  71. #define count_t __int64
  72. #define kcount_t size32_t
  73. #define CF I64F
  74. #define KCF "d"
  75. #ifdef _WIN32
  76. #define I64C(n) n##i64
  77. #else
  78. #define I64C(n) n##LL
  79. #endif
  80. // **** END OF X-PLATFORM SECTION ****
  81. #if defined(_WIN32)
  82. #if (_MSC_VER>=1300)
  83. #pragma warning(disable:4996)
  84. #endif
  85. // **** START OF WIN32 SPECIFIC SECTION ****
  86. #ifndef ALL_WINDOWS
  87. #define WIN32_LEAN_AND_MEAN
  88. #endif
  89. #ifdef _DEBUG
  90. #ifndef USING_MPATROL //using mpatrol memory leak tool
  91. #define _CRTDBG_MAP_ALLOC
  92. #endif
  93. #endif
  94. #define NOMINMAX
  95. #include <windows.h>
  96. #include <stdlib.h>
  97. #include <io.h>
  98. #include <fcntl.h>
  99. #include <malloc.h>
  100. #include <sys/stat.h>
  101. #include <winioctl.h>
  102. #include <direct.h>
  103. #if (_MSC_VER>=1300)
  104. #include <string>
  105. #include <fstream>
  106. #endif
  107. //#ifdef USING_SCM_WITH_STL //before #define of "debug" new
  108. #ifdef _MSC_VER
  109. #include <new>
  110. #include <memory>
  111. #define __attribute__(param) /* do nothing */
  112. #endif
  113. #ifdef _DEBUG
  114. #ifndef USING_MPATROL //using mpatrol memory leak tool
  115. #ifndef _INC_CRTDBG
  116. #include <crtdbg.h>
  117. #undef new
  118. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  119. #endif
  120. #endif
  121. #endif
  122. #define ThreadId DWORD
  123. #define MutexId HANDLE
  124. #define sleep(X) Sleep(X*1000)
  125. #define I64F "I64"
  126. #define PATHSEPCHAR '\\'
  127. #define PATHSEPSTR "\\"
  128. #define TEXT_TRANS "t"
  129. #define LLC(NUM) NUM
  130. #define ENVSEPCHAR ';'
  131. #define ENVSEPSTR ";"
  132. #define SEPARATE_LIB_DLL_FILES
  133. #define SharedObjectPrefix ""
  134. #define SharedObjectExtension ".dll"
  135. #define LibraryExtension ".lib"
  136. #define ProcessExtension ".exe"
  137. #define GetSharedProcedure(h,name) GetProcAddress(h,(char *)name)
  138. #define LoadSucceeded(h) ((unsigned)h >= 32)
  139. #define GetSharedObjectError() GetLastError()
  140. #define strtok_r(a,b,c) j_strtok_r(a,b,c)
  141. #define __thread __declspec(thread)
  142. typedef unsigned __int64 off64_t;
  143. typedef int socklen_t;
  144. typedef int ssize_t; // correct return to type for unix read/write/pread etc.
  145. #define fpos_ht fpos_t
  146. typedef long double LDouble;
  147. typedef unsigned long MaxCard;
  148. /* Floating point constants */
  149. #define MaxValidExp +308
  150. #define MinValidExp -330
  151. #define MaxRealMant 1.7
  152. #define MinRealMant 2.33
  153. #define MaxRealValue 1.7E+308
  154. #define S_IXUSR 0x40 // Execute by owner
  155. #define S_IWUSR 0x80 // Write by owner
  156. #define S_IRUSR 0x100 // Read by owner
  157. #define S_IXGRP 0x200 // Execute by group
  158. #define S_IWGRP 0x400 // Write by group
  159. #define S_IRGRP 0x800 // Read by group
  160. #define S_IXOTH 0x1000 // Execute by other
  161. #define S_IWOTH 0x2000 // Write by other
  162. #define S_IROTH 0x4000 // Read by other
  163. #define S_IRWXU (S_IXGRP|S_IWGRP|S_IRGRP)
  164. #define S_IRWXG (S_IXGRP|S_IWGRP|S_IRGRP)
  165. #define S_IRWXO (S_IXGRP|S_IWGRP|S_IRGRP)
  166. // MSVC 2008 in debug (perhaps other versions too) will throw exception if negative passed (see bug: #32013)
  167. #if (_MSC_VER>=1400) // >=vs2005
  168. #define strictmsvc_isalpha(c) isalpha(c)
  169. #define isalpha(c) isalpha((const unsigned char)(c))
  170. #define strictmsvc_isupper(c) isupper(c)
  171. #define isupper(c) isupper((const unsigned char)(c))
  172. #define strictmsvc_islower(c) islower(c)
  173. #define islower(c) islower((const unsigned char)(c))
  174. #define strictmsvc_isdigit(c) isdigit(c)
  175. #define isdigit(c) isdigit((const unsigned char)(c))
  176. #define strictmsvc_isxdigit(c) isxdigit(c)
  177. #define isxdigit(c) isxdigit((const unsigned char)(c))
  178. #define strictmsvc_isspace(c) isspace(c)
  179. #define isspace(c) isspace((const unsigned char)(c))
  180. #define strictmsvc_ispunct(c) ispunct(c)
  181. #define ispunct(c) ispunct((const unsigned char)(c))
  182. #define strictmsvc_isalnum(c) isalnum(c)
  183. #define isalnum(c) isalnum((const unsigned char)(c))
  184. #define strictmsvc_isprint(c) isprint(c)
  185. #define isprint(c) isprint((const unsigned char)(c))
  186. #define strictmsvc_isgraph(c) isgraph(c)
  187. #define isgraph(c) isgraph((const unsigned char)(c))
  188. #define strictmsvc_iscntrl(c) iscntrl(c)
  189. #define iscntrl(c) iscntrl((const unsigned char)(c))
  190. #define strictmsvc_tolower(c) tolower(c)
  191. #define tolower(c) tolower((const unsigned char)(c))
  192. #define strictmsvc_toupper(c) toupper(c)
  193. #define toupper(c) toupper((const unsigned char)(c))
  194. #endif // (_MSC_VER>=1400)
  195. // **** END OF WIN32 SPECIFIC SECTION ****
  196. #else
  197. // **** START OF UNIX GENERAL SECTION ****
  198. #define _stdcall
  199. #define __stdcall
  200. #define _fastcall
  201. #define __fastcall
  202. #define _fastcall
  203. #define __cdecl
  204. #if defined(__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
  205. // **** START OF LINUX SPECIFIC SECTION ****
  206. #include <aio.h>
  207. #define __BYTE_ORDER __LITTLE_ENDIAN
  208. #define _atoi64 atoll
  209. #define _lseek lseek
  210. #define _llseek ::lseek
  211. #define _lseeki64 ::lseek
  212. #define _vsnprintf vsnprintf
  213. #define strnicmp strncasecmp
  214. #define INFINITE 0xFFFFFFFF
  215. #ifdef _LARGEFILE64_SOURCE
  216. // needed by <sys/types.h>
  217. #ifndef __USE_LARGEFILE64
  218. #define __USE_LARGEFILE64
  219. #endif
  220. #ifndef __USE_FILE_OFFSET64
  221. #define __USE_FILE_OFFSET64
  222. #endif
  223. #endif
  224. // **** END OF LINUX SPECIFIC SECTION ****
  225. #else
  226. #undef __BYTE_ORDER
  227. #define __BYTE_ORDER __BIG_ENDIAN // e.g. Solaris
  228. #endif
  229. #if defined(__SVR4)
  230. typedef int socklen_t;
  231. #endif
  232. #include <stdlib.h>
  233. #include <sys/types.h>
  234. #include <unistd.h>
  235. #include <fcntl.h>
  236. #include <string.h>
  237. #include <ctype.h>
  238. #include <limits.h>
  239. #include <unistd.h>
  240. #include <stdio.h>
  241. #include <stdarg.h>
  242. #include <errno.h>
  243. #include <sys/socket.h>
  244. #include <netinet/in.h>
  245. #include <arpa/inet.h>
  246. #include <netdb.h>
  247. #include <signal.h>
  248. #define PASCAL
  249. #define __declspec(dllexport)
  250. #define __int32 int
  251. #define __int16 short
  252. #define __int8 char
  253. #define __int64 long long
  254. #ifndef __TIMESTAMP__
  255. #define __TIMESTAMP__ "<__TIMESTAMP__ unsupported>"
  256. #endif
  257. #define ENVSEPCHAR ':'
  258. #define ENVSEPSTR ":"
  259. #define PATHSEPCHAR '/'
  260. #define PATHSEPSTR "/"
  261. #define TEXT_TRANS
  262. #define LLC(NUM) NUM ## LL
  263. #define wsprintf sprintf
  264. #define _lread _read
  265. #define _lclose _close
  266. #define _lwrite _write
  267. #define _lopen _open
  268. #define DeleteFile(name) (unlink(name)==0)
  269. #define wvsprintf vsprintf
  270. #define _close ::close
  271. #define _stat stat
  272. #define _fstat ::fstat
  273. #define _dup ::dup
  274. #define _chdir ::chdir
  275. #define _setmode(a,b)
  276. #define TRUE 1
  277. #define FALSE 0
  278. //#define false FALSE
  279. //#define true TRUE
  280. #define HFILE int
  281. //#define bool unsigned char
  282. #define BOOL bool
  283. #define UINT unsigned int
  284. #define CHAR char
  285. #define DWORD unsigned long
  286. #define VOID void
  287. #define LPBYTE char *
  288. #define LPSTR char *
  289. #define LPTSTR char *
  290. #define LPVOID
  291. #define FAR
  292. #define WINAPI
  293. #define fpos_ht off_t
  294. #define handle_t void *
  295. #define HINSTANCE void *
  296. #define HANDLE int
  297. #define HMODULE void *
  298. #define _MAX_PATH PATH_MAX
  299. #define HFILE_ERROR -1
  300. #define OF_READWRITE O_RDWR, S_IRUSR | S_IWUSR
  301. #define OF_READ O_RDONLY, S_IRUSR | S_IWUSR
  302. #define _O_RDWR O_RDWR
  303. #define _O_RDONLY O_RDONLY
  304. #define _O_CREAT O_CREAT
  305. #define _O_TRUNC O_TRUNC
  306. #define _O_APPEND O_APPEND
  307. #define _O_BINARY 0
  308. #define _O_SEQUENTIAL 0
  309. #define _O_TEXT 0
  310. #define _O_RDONLY O_RDONLY
  311. #define _O_WRONLY O_WRONLY
  312. #define _O_RANDOM 0
  313. #define _S_IREAD S_IRUSR | S_IRGRP | S_IROTH
  314. #define _S_IWRITE S_IWUSR | S_IWGRP | S_IWOTH
  315. #define _S_IEXEC S_IXUSR | S_IXGRP | S_IXOTH
  316. #define _S_IFDIR S_IFDIR
  317. #define FILE_CURRENT SEEK_CUR
  318. #define FILE_END SEEK_END
  319. #define FILE_BEGIN SEEK_SET
  320. #define SOCKET int
  321. #define SOCKET_ERROR -1
  322. #define INVALID_SOCKET -1
  323. #define SOCKADDR_IN struct sockaddr_in
  324. #define SOCKADDR struct sockaddr
  325. #define PSOCKADDR SOCKADDR *
  326. #define closesocket close
  327. #define ioctlsocket ioctl
  328. #define WSAGetLastError() 0
  329. #define GetCurrentDirectory(size, buf) getcwd(buf, size)
  330. #define SetCurrentDirectory(path) chdir(path)
  331. #define CreateDirectory(path,X) (_mkdir(path)==0)
  332. #define GetTempPath(size, buff) strncpy(buff, P_tmpdir, size); // return value not valid
  333. #define GetLastError() errno
  334. #define memicmp j_memicmp
  335. #define I64F "ll"
  336. #ifndef stricmp
  337. #define stricmp strcasecmp
  338. #endif
  339. #ifndef __FreeBSD__
  340. #ifndef __APPLE__
  341. #include <malloc.h>
  342. #include <alloca.h>
  343. #endif
  344. #endif
  345. #include <dlfcn.h>
  346. #include <pthread.h>
  347. #include <sys/stat.h>
  348. #include <sys/time.h>
  349. #include <sys/errno.h>
  350. #include <sys/utsname.h>
  351. #if defined (__FreeBSD__) || defined (__APPLE__)
  352. #define MAP_ANONYMOUS MAP_ANON
  353. #endif
  354. #if defined(__FreeBSD__) || defined(__linux__) || defined(__CYGWIN__) || defined (__APPLE__)
  355. #include <sys/ioctl.h>
  356. #else
  357. #include <sys/filio.h>
  358. #include <stropts.h>
  359. #include <sys/asynch.h>
  360. #define _llseek ::llseek
  361. #define _lseeki64 ::llseek
  362. #endif
  363. #define _write ::write
  364. #define _read ::read
  365. #define _open ::open
  366. #define _mkdir(P1) mkdir(P1, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)
  367. #define GetCurrentThreadId pthread_self
  368. #define GetCurrentProcessId getpid
  369. #define _stdcall
  370. // #define inline
  371. #define SharedObjectPrefix "lib"
  372. #if defined (__APPLE__)
  373. #define SharedObjectExtension ".dylib"
  374. #define LibraryExtension ".dylib"
  375. #else
  376. #define SharedObjectExtension ".so"
  377. #define LibraryExtension ".so"
  378. #endif
  379. #define ProcessExtension ""
  380. #define GetSharedProcedure(h,name) dlsym(h,(char *)name)
  381. #define LoadSucceeded(h) (h != NULL)
  382. #define GetSharedObjectError() errno
  383. #define ThreadId pthread_t
  384. #define MutexId pthread_mutex_t
  385. // **** END OF UNIX SPECIFIC SECTION ****
  386. #endif
  387. #define FLOAT_SIG_DIGITS 7
  388. #define DOUBLE_SIG_DIGITS 16
  389. #define MAX_DECIMAL_LEADING 32 // Maximum number of leading digits in a decimal field
  390. #define MAX_DECIMAL_PRECISION 32 // Maximum digits in a decimal field
  391. #define MAX_DECIMAL_DIGITS (MAX_DECIMAL_LEADING+MAX_DECIMAL_PRECISION)
  392. #define strtok(a,b) j_strtok_deprecated(a,b) // will disappear at some point
  393. typedef unsigned __int64 hash64_t;
  394. typedef unsigned __int64 __uint64;
  395. typedef __uint64 offset_t;
  396. typedef unsigned char byte;
  397. // BUILD_TAG not needed here anymore - defined in build_tag.h
  398. //#define BUILD_TAG "build_0000" // Will get substituted during pre-build
  399. #endif