PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/assets/TokyoCabinet/myconf.h

http://github.com/isaact/TSDocDB
C Header | 549 lines | 373 code | 125 blank | 51 comment | 45 complexity | 1243cab3030491c7a614f711fb7bd8cc MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*************************************************************************************************
  2. * System-dependent configurations of Tokyo Cabinet
  3. * Copyright (C) 2006-2010 FAL Labs
  4. * This file is part of Tokyo Cabinet.
  5. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
  6. * the GNU Lesser General Public License as published by the Free Software Foundation; either
  7. * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
  8. * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  10. * License for more details.
  11. * You should have received a copy of the GNU Lesser General Public License along with Tokyo
  12. * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  13. * Boston, MA 02111-1307 USA.
  14. *************************************************************************************************/
  15. #ifndef _MYCONF_H // duplication check
  16. #define _MYCONF_H
  17. /*************************************************************************************************
  18. * system discrimination
  19. *************************************************************************************************/
  20. #if defined(__linux__)
  21. #define _SYS_LINUX_
  22. #define TCSYSNAME "Linux"
  23. #elif defined(__FreeBSD__)
  24. #define _SYS_FREEBSD_
  25. #define TCSYSNAME "FreeBSD"
  26. #elif defined(__NetBSD__)
  27. #define _SYS_NETBSD_
  28. #define TCSYSNAME "NetBSD"
  29. #elif defined(__OpenBSD__)
  30. #define _SYS_OPENBSD_
  31. #define TCSYSNAME "OpenBSD"
  32. #elif defined(__sun__) || defined(__sun)
  33. #define _SYS_SUNOS_
  34. #define TCSYSNAME "SunOS"
  35. #elif defined(__hpux)
  36. #define _SYS_HPUX_
  37. #define TCSYSNAME "HP-UX"
  38. #elif defined(__osf)
  39. #define _SYS_TRU64_
  40. #define TCSYSNAME "Tru64"
  41. #elif defined(_AIX)
  42. #define _SYS_AIX_
  43. #define TCSYSNAME "AIX"
  44. #elif defined(__APPLE__) && defined(__MACH__)
  45. #define _SYS_MACOSX_
  46. #define TCSYSNAME "Mac OS X"
  47. #elif defined(_MSC_VER)
  48. #define _SYS_MSVC_
  49. #define TCSYSNAME "Windows (VC++)"
  50. #elif defined(_WIN32)
  51. #define _SYS_MINGW_
  52. #define TCSYSNAME "Windows (MinGW)"
  53. #elif defined(__CYGWIN__)
  54. #define _SYS_CYGWIN_
  55. #define TCSYSNAME "Windows (Cygwin)"
  56. #else
  57. #define _SYS_GENERIC_
  58. #define TCSYSNAME "Generic"
  59. #endif
  60. /*************************************************************************************************
  61. * common settings
  62. *************************************************************************************************/
  63. #if defined(NDEBUG)
  64. #define TCDODEBUG(TC_expr) \
  65. do { \
  66. } while(false)
  67. #else
  68. #define TCDODEBUG(TC_expr) \
  69. do { \
  70. TC_expr; \
  71. } while(false)
  72. #endif
  73. #define TCSWAB16(TC_num) \
  74. ( \
  75. ((TC_num & 0x00ffU) << 8) | \
  76. ((TC_num & 0xff00U) >> 8) \
  77. )
  78. #define TCSWAB32(TC_num) \
  79. ( \
  80. ((TC_num & 0x000000ffUL) << 24) | \
  81. ((TC_num & 0x0000ff00UL) << 8) | \
  82. ((TC_num & 0x00ff0000UL) >> 8) | \
  83. ((TC_num & 0xff000000UL) >> 24) \
  84. )
  85. #define TCSWAB64(TC_num) \
  86. ( \
  87. ((TC_num & 0x00000000000000ffULL) << 56) | \
  88. ((TC_num & 0x000000000000ff00ULL) << 40) | \
  89. ((TC_num & 0x0000000000ff0000ULL) << 24) | \
  90. ((TC_num & 0x00000000ff000000ULL) << 8) | \
  91. ((TC_num & 0x000000ff00000000ULL) >> 8) | \
  92. ((TC_num & 0x0000ff0000000000ULL) >> 24) | \
  93. ((TC_num & 0x00ff000000000000ULL) >> 40) | \
  94. ((TC_num & 0xff00000000000000ULL) >> 56) \
  95. )
  96. #if defined(_MYBIGEND) || defined(_MYSWAB)
  97. #define TCBIGEND 1
  98. #define TCHTOIS(TC_num) TCSWAB16(TC_num)
  99. #define TCHTOIL(TC_num) TCSWAB32(TC_num)
  100. #define TCHTOILL(TC_num) TCSWAB64(TC_num)
  101. #define TCITOHS(TC_num) TCSWAB16(TC_num)
  102. #define TCITOHL(TC_num) TCSWAB32(TC_num)
  103. #define TCITOHLL(TC_num) TCSWAB64(TC_num)
  104. #else
  105. #define TCBIGEND 0
  106. #define TCHTOIS(TC_num) (TC_num)
  107. #define TCHTOIL(TC_num) (TC_num)
  108. #define TCHTOILL(TC_num) (TC_num)
  109. #define TCITOHS(TC_num) (TC_num)
  110. #define TCITOHL(TC_num) (TC_num)
  111. #define TCITOHLL(TC_num) (TC_num)
  112. #endif
  113. #if defined(_MYNOUBC)
  114. #define TCUBCACHE 0
  115. #elif defined(_SYS_LINUX_) || defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || \
  116. defined(_SYS_MACOSX_) || defined(_SYS_SUNOS_)
  117. #define TCUBCACHE 1
  118. #else
  119. #define TCUBCACHE 0
  120. #endif
  121. #if defined(_MYNOZLIB)
  122. #define TCUSEZLIB 0
  123. #else
  124. #define TCUSEZLIB 1
  125. #endif
  126. #if defined(_MYNOBZIP)
  127. #define TCUSEBZIP 0
  128. #else
  129. #define TCUSEBZIP 1
  130. #endif
  131. #if defined(_MYEXLZMA)
  132. #define TCUSEEXLZMA 1
  133. #else
  134. #define TCUSEEXLZMA 0
  135. #endif
  136. #if defined(_MYEXLZO)
  137. #define TCUSEEXLZO 1
  138. #else
  139. #define TCUSEEXLZO 0
  140. #endif
  141. #if defined(_MYNOPTHREAD)
  142. #define TCUSEPTHREAD 0
  143. #else
  144. #define TCUSEPTHREAD 1
  145. #endif
  146. #if defined(_MYMICROYIELD)
  147. #define TCMICROYIELD 1
  148. #else
  149. #define TCMICROYIELD 0
  150. #endif
  151. #define MYMALLOC malloc
  152. #define MYCALLOC calloc
  153. #define MYREALLOC realloc
  154. #define MYFREE free
  155. /*************************************************************************************************
  156. * general headers
  157. *************************************************************************************************/
  158. #include <assert.h>
  159. #include <ctype.h>
  160. #include <errno.h>
  161. #include <float.h>
  162. #include <limits.h>
  163. #include <locale.h>
  164. #include <math.h>
  165. #include <setjmp.h>
  166. #include <stdarg.h>
  167. #include <stddef.h>
  168. #include <stdio.h>
  169. #include <stdlib.h>
  170. #include <signal.h>
  171. #include <string.h>
  172. #include <time.h>
  173. #include <inttypes.h>
  174. #include <stdbool.h>
  175. #include <stdint.h>
  176. #include <unistd.h>
  177. #include <sys/param.h>
  178. #include <sys/types.h>
  179. #include <sys/stat.h>
  180. #include <sys/mman.h>
  181. #include <sys/time.h>
  182. #include <sys/times.h>
  183. #include <sys/wait.h>
  184. #include <sys/resource.h>
  185. #include <fcntl.h>
  186. #include <dirent.h>
  187. #include <regex.h>
  188. #include <glob.h>
  189. #if TCUSEPTHREAD
  190. #include <pthread.h>
  191. #if defined(_POSIX_PRIORITY_SCHEDULING)
  192. #include <sched.h>
  193. #endif
  194. #endif
  195. /*************************************************************************************************
  196. * miscellaneous hacks
  197. *************************************************************************************************/
  198. #if defined(__GNUC__)
  199. #define _alignof(TC_a) ((size_t)__alignof__(TC_a))
  200. #else
  201. #define _alignof(TC_a) sizeof(TC_a)
  202. #endif
  203. #define _issigned(TC_a) ((TC_a)-1 < 1 ? true : false)
  204. #define _maxof(TC_a) \
  205. ((TC_a)(sizeof(TC_a) == sizeof(int64_t) ? _issigned(TC_a) ? INT64_MAX : UINT64_MAX : \
  206. sizeof(TC_a) == sizeof(int32_t) ? _issigned(TC_a) ? INT32_MAX : UINT32_MAX : \
  207. sizeof(TC_a) == sizeof(int16_t) ? _issigned(TC_a) ? INT16_MAX : UINT16_MAX : \
  208. _issigned(TC_a) ? INT8_MAX : UINT8_MAX))
  209. #if defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || defined(_SYS_OPENBSD_)
  210. #define nan(TC_a) strtod("nan", NULL)
  211. #define nanl(TC_a) ((long double)strtod("nan", NULL))
  212. #endif
  213. #if ! defined(PATH_MAX)
  214. #if defined(MAXPATHLEN)
  215. #define PATH_MAX MAXPATHLEN
  216. #else
  217. #define PATH_MAX 4096
  218. #endif
  219. #endif
  220. #if ! defined(NAME_MAX)
  221. #define NAME_MAX 255
  222. #endif
  223. extern int _tc_dummy_cnt;
  224. int _tc_dummyfunc(void);
  225. int _tc_dummyfuncv(int a, ...);
  226. /*************************************************************************************************
  227. * notation of filesystems
  228. *************************************************************************************************/
  229. #define MYPATHCHR '/'
  230. #define MYPATHSTR "/"
  231. #define MYEXTCHR '.'
  232. #define MYEXTSTR "."
  233. #define MYCDIRSTR "."
  234. #define MYPDIRSTR ".."
  235. /*************************************************************************************************
  236. * for ZLIB
  237. *************************************************************************************************/
  238. enum {
  239. _TCZMZLIB,
  240. _TCZMRAW,
  241. _TCZMGZIP
  242. };
  243. extern char *(*_tc_deflate)(const char *, int, int *, int);
  244. extern char *(*_tc_inflate)(const char *, int, int *, int);
  245. extern unsigned int (*_tc_getcrc)(const char *, int);
  246. /*************************************************************************************************
  247. * for BZIP2
  248. *************************************************************************************************/
  249. extern char *(*_tc_bzcompress)(const char *, int, int *);
  250. extern char *(*_tc_bzdecompress)(const char *, int, int *);
  251. /*************************************************************************************************
  252. * for test of custom codec functions
  253. *************************************************************************************************/
  254. void *_tc_recencode(const void *ptr, int size, int *sp, void *op);
  255. void *_tc_recdecode(const void *ptr, int size, int *sp, void *op);
  256. /*************************************************************************************************
  257. * for POSIX thread disability
  258. *************************************************************************************************/
  259. #if ! TCUSEPTHREAD
  260. #define pthread_t intptr_t
  261. #define pthread_once_t intptr_t
  262. #undef PTHREAD_ONCE_INIT
  263. #define PTHREAD_ONCE_INIT 0
  264. #define pthread_once(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
  265. #define pthread_mutexattr_t intptr_t
  266. #undef PTHREAD_MUTEX_RECURSIVE
  267. #define PTHREAD_MUTEX_RECURSIVE 0
  268. #define pthread_mutexattr_init(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  269. #define pthread_mutexattr_destroy(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  270. #define pthread_mutexattr_settype(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
  271. #define pthread_mutex_t intptr_t
  272. #undef PTHREAD_MUTEX_INITIALIZER
  273. #define PTHREAD_MUTEX_INITIALIZER 0
  274. #define pthread_mutex_init(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
  275. #define pthread_mutex_destroy(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  276. #define pthread_mutex_lock(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  277. #define pthread_mutex_unlock(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  278. #define pthread_rwlock_t intptr_t
  279. #undef PTHREAD_RWLOCK_INITIALIZER
  280. #define PTHREAD_RWLOCK_INITIALIZER 0
  281. #define pthread_rwlock_init(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
  282. #define pthread_rwlock_destroy(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  283. #define pthread_rwlock_rdlock(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  284. #define pthread_rwlock_wrlock(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  285. #define pthread_rwlock_unlock(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  286. #define pthread_key_t intptr_t
  287. #define pthread_key_create(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
  288. #define pthread_key_delete(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  289. #define pthread_setspecific(TC_a, TC_b) _tc_dummyfuncv((intptr_t)(TC_a))
  290. #define pthread_getspecific(TC_a) _tc_dummyfuncv((intptr_t)(TC_a))
  291. #define pthread_create(TC_th, TC_attr, TC_func, TC_arg) \
  292. (*(TC_th) = 0, (TC_func)(TC_arg), 0)
  293. #define pthread_join(TC_th, TC_rv) (*(TC_rv) = NULL, 0)
  294. #define pthread_detach(TC_th) 0
  295. #define sched_yield() _tc_dummyfunc()
  296. #endif
  297. #if TCUSEPTHREAD && TCMICROYIELD
  298. #define TCTESTYIELD() \
  299. do { \
  300. if(((++_tc_dummy_cnt) & (0x20 - 1)) == 0){ \
  301. sched_yield(); \
  302. if(_tc_dummy_cnt > 0x1000) _tc_dummy_cnt = (uint32_t)time(NULL) % 0x1000; \
  303. } \
  304. } while(false)
  305. #undef assert
  306. #define assert(TC_expr) \
  307. do { \
  308. if(!(TC_expr)){ \
  309. fprintf(stderr, "assertion failed: %s\n", #TC_expr); \
  310. abort(); \
  311. } \
  312. TCTESTYIELD(); \
  313. } while(false)
  314. #define if(TC_cond) \
  315. if((((++_tc_dummy_cnt) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
  316. #define while(TC_cond) \
  317. while((((++_tc_dummy_cnt) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
  318. #else
  319. #define TCTESTYIELD() \
  320. do { \
  321. } while(false)
  322. #endif
  323. #if !defined(_POSIX_PRIORITY_SCHEDULING) && TCUSEPTHREAD
  324. #define sched_yield() usleep(1000 * 20)
  325. #endif
  326. /*************************************************************************************************
  327. * utilities for implementation
  328. *************************************************************************************************/
  329. #define TCNUMBUFSIZ 32 // size of a buffer for a number
  330. /* set a buffer for a variable length number */
  331. #define TCSETVNUMBUF(TC_len, TC_buf, TC_num) \
  332. do { \
  333. int _TC_num = (TC_num); \
  334. if(_TC_num == 0){ \
  335. ((signed char *)(TC_buf))[0] = 0; \
  336. (TC_len) = 1; \
  337. } else { \
  338. (TC_len) = 0; \
  339. while(_TC_num > 0){ \
  340. int _TC_rem = _TC_num & 0x7f; \
  341. _TC_num >>= 7; \
  342. if(_TC_num > 0){ \
  343. ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
  344. } else { \
  345. ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
  346. } \
  347. (TC_len)++; \
  348. } \
  349. } \
  350. } while(false)
  351. /* set a buffer for a variable length number of 64-bit */
  352. #define TCSETVNUMBUF64(TC_len, TC_buf, TC_num) \
  353. do { \
  354. long long int _TC_num = (TC_num); \
  355. if(_TC_num == 0){ \
  356. ((signed char *)(TC_buf))[0] = 0; \
  357. (TC_len) = 1; \
  358. } else { \
  359. (TC_len) = 0; \
  360. while(_TC_num > 0){ \
  361. int _TC_rem = _TC_num & 0x7f; \
  362. _TC_num >>= 7; \
  363. if(_TC_num > 0){ \
  364. ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
  365. } else { \
  366. ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
  367. } \
  368. (TC_len)++; \
  369. } \
  370. } \
  371. } while(false)
  372. /* read a variable length buffer */
  373. #define TCREADVNUMBUF(TC_buf, TC_num, TC_step) \
  374. do { \
  375. TC_num = 0; \
  376. int _TC_base = 1; \
  377. int _TC_i = 0; \
  378. while(true){ \
  379. if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
  380. TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
  381. break; \
  382. } \
  383. TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
  384. _TC_base <<= 7; \
  385. _TC_i++; \
  386. } \
  387. (TC_step) = _TC_i + 1; \
  388. } while(false)
  389. /* read a variable length buffer */
  390. #define TCREADVNUMBUF64(TC_buf, TC_num, TC_step) \
  391. do { \
  392. TC_num = 0; \
  393. long long int _TC_base = 1; \
  394. int _TC_i = 0; \
  395. while(true){ \
  396. if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
  397. TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
  398. break; \
  399. } \
  400. TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
  401. _TC_base <<= 7; \
  402. _TC_i++; \
  403. } \
  404. (TC_step) = _TC_i + 1; \
  405. } while(false)
  406. /* calculate the size of a buffer for a variable length number */
  407. #define TCCALCVNUMSIZE(TC_num) \
  408. ((TC_num) < 0x80 ? 1 : (TC_num) < 0x4000 ? 2 : (TC_num) < 0x200000 ? 3 : \
  409. (TC_num) < 0x10000000 ? 4 : 5)
  410. /* compare keys of two records by lexical order */
  411. #define TCCMPLEXICAL(TC_rv, TC_aptr, TC_asiz, TC_bptr, TC_bsiz) \
  412. do { \
  413. (TC_rv) = 0; \
  414. int _TC_min = (TC_asiz) < (TC_bsiz) ? (TC_asiz) : (TC_bsiz); \
  415. for(int _TC_i = 0; _TC_i < _TC_min; _TC_i++){ \
  416. if(((unsigned char *)(TC_aptr))[_TC_i] != ((unsigned char *)(TC_bptr))[_TC_i]){ \
  417. (TC_rv) = ((unsigned char *)(TC_aptr))[_TC_i] - ((unsigned char *)(TC_bptr))[_TC_i]; \
  418. break; \
  419. } \
  420. } \
  421. if((TC_rv) == 0) (TC_rv) = (TC_asiz) - (TC_bsiz); \
  422. } while(false)
  423. #endif // duplication check
  424. // END OF FILE