1 /*************************************************************************************************
2  * System-dependent configurations of Tokyo Cabinet
3  *                                                               Copyright (C) 2006-2012 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 
16 
17 #ifndef _MYCONF_H                        // duplication check
18 #define _MYCONF_H
19 
20 
21 
22 /*************************************************************************************************
23  * system discrimination
24  *************************************************************************************************/
25 
26 
27 #if defined(__linux__)
28 
29 #define _SYS_LINUX_
30 #define TCSYSNAME   "Linux"
31 
32 #elif defined(__FreeBSD__)
33 
34 #define _SYS_FREEBSD_
35 #define TCSYSNAME   "FreeBSD"
36 
37 #elif defined(__NetBSD__)
38 
39 #define _SYS_NETBSD_
40 #define TCSYSNAME   "NetBSD"
41 
42 #elif defined(__OpenBSD__)
43 
44 #define _SYS_OPENBSD_
45 #define TCSYSNAME   "OpenBSD"
46 
47 #elif defined(__sun__) || defined(__sun)
48 
49 #define _SYS_SUNOS_
50 #define TCSYSNAME   "SunOS"
51 
52 #elif defined(__hpux)
53 
54 #define _SYS_HPUX_
55 #define TCSYSNAME   "HP-UX"
56 
57 #elif defined(__osf)
58 
59 #define _SYS_TRU64_
60 #define TCSYSNAME   "Tru64"
61 
62 #elif defined(_AIX)
63 
64 #define _SYS_AIX_
65 #define TCSYSNAME   "AIX"
66 
67 #elif defined(__APPLE__) && defined(__MACH__)
68 
69 #define _SYS_MACOSX_
70 #define TCSYSNAME   "Mac OS X"
71 
72 #elif defined(_MSC_VER)
73 
74 #define _SYS_MSVC_
75 #define TCSYSNAME   "Windows (VC++)"
76 
77 #elif defined(_WIN32)
78 
79 #define _SYS_MINGW_
80 #define TCSYSNAME   "Windows (MinGW)"
81 
82 #elif defined(__CYGWIN__)
83 
84 #define _SYS_CYGWIN_
85 #define TCSYSNAME   "Windows (Cygwin)"
86 
87 #else
88 
89 #define _SYS_GENERIC_
90 #define TCSYSNAME   "Generic"
91 
92 #endif
93 
94 
95 
96 /*************************************************************************************************
97  * common settings
98  *************************************************************************************************/
99 
100 
101 #if defined(NDEBUG)
102 #define TCDODEBUG(TC_expr) \
103   do { \
104   } while(false)
105 #else
106 #define TCDODEBUG(TC_expr) \
107   do { \
108     TC_expr; \
109   } while(false)
110 #endif
111 
112 #define TCSWAB16(TC_num) \
113   ( \
114    ((TC_num & 0x00ffU) << 8) | \
115    ((TC_num & 0xff00U) >> 8) \
116   )
117 
118 #define TCSWAB32(TC_num) \
119   ( \
120    ((TC_num & 0x000000ffUL) << 24) | \
121    ((TC_num & 0x0000ff00UL) << 8) | \
122    ((TC_num & 0x00ff0000UL) >> 8) | \
123    ((TC_num & 0xff000000UL) >> 24) \
124   )
125 
126 #define TCSWAB64(TC_num) \
127   ( \
128    ((TC_num & 0x00000000000000ffULL) << 56) | \
129    ((TC_num & 0x000000000000ff00ULL) << 40) | \
130    ((TC_num & 0x0000000000ff0000ULL) << 24) | \
131    ((TC_num & 0x00000000ff000000ULL) << 8) | \
132    ((TC_num & 0x000000ff00000000ULL) >> 8) | \
133    ((TC_num & 0x0000ff0000000000ULL) >> 24) | \
134    ((TC_num & 0x00ff000000000000ULL) >> 40) | \
135    ((TC_num & 0xff00000000000000ULL) >> 56) \
136   )
137 
138 #if defined(_MYBIGEND) || defined(_MYSWAB)
139 #define TCBIGEND       1
140 #define TCHTOIS(TC_num)   TCSWAB16(TC_num)
141 #define TCHTOIL(TC_num)   TCSWAB32(TC_num)
142 #define TCHTOILL(TC_num)  TCSWAB64(TC_num)
143 #define TCITOHS(TC_num)   TCSWAB16(TC_num)
144 #define TCITOHL(TC_num)   TCSWAB32(TC_num)
145 #define TCITOHLL(TC_num)  TCSWAB64(TC_num)
146 #else
147 #define TCBIGEND       0
148 #define TCHTOIS(TC_num)   (TC_num)
149 #define TCHTOIL(TC_num)   (TC_num)
150 #define TCHTOILL(TC_num)  (TC_num)
151 #define TCITOHS(TC_num)   (TC_num)
152 #define TCITOHL(TC_num)   (TC_num)
153 #define TCITOHLL(TC_num)  (TC_num)
154 #endif
155 
156 #if defined(_MYNOUBC)
157 #define TCUBCACHE      0
158 #elif defined(_SYS_LINUX_) || defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || \
159   defined(_SYS_MACOSX_) || defined(_SYS_SUNOS_)
160 #define TCUBCACHE      1
161 #else
162 #define TCUBCACHE      0
163 #endif
164 
165 #if defined(_MYNOZLIB)
166 #define TCUSEZLIB      0
167 #else
168 #define TCUSEZLIB      1
169 #endif
170 
171 #if defined(_MYNOBZIP)
172 #define TCUSEBZIP      0
173 #else
174 #define TCUSEBZIP      1
175 #endif
176 
177 #if defined(_MYEXLZMA)
178 #define TCUSEEXLZMA    1
179 #else
180 #define TCUSEEXLZMA    0
181 #endif
182 
183 #if defined(_MYEXLZO)
184 #define TCUSEEXLZO     1
185 #else
186 #define TCUSEEXLZO     0
187 #endif
188 
189 #if defined(_MYNOPTHREAD)
190 #define TCUSEPTHREAD   0
191 #else
192 #define TCUSEPTHREAD   1
193 #endif
194 
195 #if defined(_MYMICROYIELD)
196 #define TCMICROYIELD   1
197 #else
198 #define TCMICROYIELD   0
199 #endif
200 
201 #define MYMALLOC       malloc
202 #define MYCALLOC       calloc
203 #define MYREALLOC      realloc
204 #define MYFREE         free
205 
206 
207 
208 /*************************************************************************************************
209  * general headers
210  *************************************************************************************************/
211 
212 
213 #include <assert.h>
214 #include <ctype.h>
215 #include <errno.h>
216 #include <float.h>
217 #include <limits.h>
218 #include <locale.h>
219 #include <math.h>
220 #include <setjmp.h>
221 #include <stdarg.h>
222 #include <stddef.h>
223 #include <stdio.h>
224 #include <stdlib.h>
225 #include <signal.h>
226 #include <string.h>
227 #include <time.h>
228 
229 #include <inttypes.h>
230 #include <stdbool.h>
231 #include <stdint.h>
232 
233 #include <unistd.h>
234 #include <sys/param.h>
235 #include <sys/types.h>
236 #include <sys/stat.h>
237 #include <sys/mman.h>
238 #include <sys/time.h>
239 #include <sys/times.h>
240 #include <sys/wait.h>
241 #include <sys/resource.h>
242 #include <fcntl.h>
243 #include <dirent.h>
244 #include <regex.h>
245 #include <glob.h>
246 
247 #if TCUSEPTHREAD
248 #include <pthread.h>
249 #if defined(_POSIX_PRIORITY_SCHEDULING)
250 #include <sched.h>
251 #endif
252 #endif
253 
254 
255 
256 /*************************************************************************************************
257  * miscellaneous hacks
258  *************************************************************************************************/
259 
260 
261 #if defined(__GNUC__)
262 #define _alignof(TC_a) ((size_t)__alignof__(TC_a))
263 #else
264 #define _alignof(TC_a) sizeof(TC_a)
265 #endif
266 #define _issigned(TC_a)  ((TC_a)-1 < 1 ? true : false)
267 #define _maxof(TC_a) \
268  ((TC_a)(sizeof(TC_a) == sizeof(int64_t) ? _issigned(TC_a) ? INT64_MAX : UINT64_MAX : \
269    sizeof(TC_a) == sizeof(int32_t) ? _issigned(TC_a) ? INT32_MAX : UINT32_MAX : \
270    sizeof(TC_a) == sizeof(int16_t) ? _issigned(TC_a) ? INT16_MAX : UINT16_MAX : \
271    _issigned(TC_a) ? INT8_MAX : UINT8_MAX))
272 
273 #if defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || defined(_SYS_OPENBSD_)
274 #define nan(TC_a)      strtod("nan", NULL)
275 #define nanl(TC_a)     ((long double)strtod("nan", NULL))
276 #endif
277 
278 #if ! defined(PATH_MAX)
279 #if defined(MAXPATHLEN)
280 #define PATH_MAX       MAXPATHLEN
281 #else
282 #define PATH_MAX       4096
283 #endif
284 #endif
285 #if ! defined(NAME_MAX)
286 #define NAME_MAX       255
287 #endif
288 
289 extern int _tc_dummy_cnt;
290 
291 int _tc_dummyfunc(void);
292 
293 int _tc_dummyfuncv(int a, ...);
294 
295 
296 
297 /*************************************************************************************************
298  * notation of filesystems
299  *************************************************************************************************/
300 
301 
302 #define MYPATHCHR       '/'
303 #define MYPATHSTR       "/"
304 #define MYEXTCHR        '.'
305 #define MYEXTSTR        "."
306 #define MYCDIRSTR       "."
307 #define MYPDIRSTR       ".."
308 
309 
310 
311 /*************************************************************************************************
312  * for ZLIB
313  *************************************************************************************************/
314 
315 
316 enum {
317   _TCZMZLIB,
318   _TCZMRAW,
319   _TCZMGZIP
320 };
321 
322 
323 extern char *(*_tc_deflate)(const char *, int, int *, int);
324 
325 extern char *(*_tc_inflate)(const char *, int, int *, int);
326 
327 extern unsigned int (*_tc_getcrc)(const char *, int);
328 
329 
330 
331 /*************************************************************************************************
332  * for BZIP2
333  *************************************************************************************************/
334 
335 
336 extern char *(*_tc_bzcompress)(const char *, int, int *);
337 
338 extern char *(*_tc_bzdecompress)(const char *, int, int *);
339 
340 
341 
342 /*************************************************************************************************
343  * for test of custom codec functions
344  *************************************************************************************************/
345 
346 
347 void *_tc_recencode(const void *ptr, int size, int *sp, void *op);
348 
349 void *_tc_recdecode(const void *ptr, int size, int *sp, void *op);
350 
351 
352 
353 /*************************************************************************************************
354  * for POSIX thread disability
355  *************************************************************************************************/
356 
357 
358 #if ! TCUSEPTHREAD
359 
360 #define pthread_t                        intptr_t
361 
362 #define pthread_once_t                   intptr_t
363 #undef PTHREAD_ONCE_INIT
364 #define PTHREAD_ONCE_INIT                0
365 #define pthread_once(TC_a, TC_b)         _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
366 
367 #define pthread_mutexattr_t              intptr_t
368 #undef PTHREAD_MUTEX_RECURSIVE
369 #define PTHREAD_MUTEX_RECURSIVE          0
370 #define pthread_mutexattr_init(TC_a)     _tc_dummyfuncv((intptr_t)(TC_a))
371 #define pthread_mutexattr_destroy(TC_a)  _tc_dummyfuncv((intptr_t)(TC_a))
372 #define pthread_mutexattr_settype(TC_a, TC_b)  _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
373 
374 #define pthread_mutex_t                  intptr_t
375 #undef PTHREAD_MUTEX_INITIALIZER
376 #define PTHREAD_MUTEX_INITIALIZER        0
377 #define pthread_mutex_init(TC_a, TC_b)   _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
378 #define pthread_mutex_destroy(TC_a)      _tc_dummyfuncv((intptr_t)(TC_a))
379 #define pthread_mutex_lock(TC_a)         _tc_dummyfuncv((intptr_t)(TC_a))
380 #define pthread_mutex_unlock(TC_a)       _tc_dummyfuncv((intptr_t)(TC_a))
381 
382 #define pthread_rwlock_t                 intptr_t
383 #undef PTHREAD_RWLOCK_INITIALIZER
384 #define PTHREAD_RWLOCK_INITIALIZER       0
385 #define pthread_rwlock_init(TC_a, TC_b)  _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
386 #define pthread_rwlock_destroy(TC_a)     _tc_dummyfuncv((intptr_t)(TC_a))
387 #define pthread_rwlock_rdlock(TC_a)      _tc_dummyfuncv((intptr_t)(TC_a))
388 #define pthread_rwlock_wrlock(TC_a)      _tc_dummyfuncv((intptr_t)(TC_a))
389 #define pthread_rwlock_unlock(TC_a)      _tc_dummyfuncv((intptr_t)(TC_a))
390 
391 #define pthread_key_t                    intptr_t
392 #define pthread_key_create(TC_a, TC_b)   _tc_dummyfuncv((intptr_t)(TC_a), (TC_b))
393 #define pthread_key_delete(TC_a)         _tc_dummyfuncv((intptr_t)(TC_a))
394 #define pthread_setspecific(TC_a, TC_b)  _tc_dummyfuncv((intptr_t)(TC_a))
395 #define pthread_getspecific(TC_a)        _tc_dummyfuncv((intptr_t)(TC_a))
396 
397 #define pthread_create(TC_th, TC_attr, TC_func, TC_arg) \
398   (*(TC_th) = 0, (TC_func)(TC_arg), 0)
399 #define pthread_join(TC_th, TC_rv)       (*(TC_rv) = NULL, 0)
400 #define pthread_detach(TC_th)            0
401 #define sched_yield()                    _tc_dummyfunc()
402 
403 #endif
404 
405 #if TCUSEPTHREAD && TCMICROYIELD
406 #define TCTESTYIELD() \
407   do { \
408     if(((++_tc_dummy_cnt) & (0x20 - 1)) == 0){ \
409       sched_yield(); \
410       if(_tc_dummy_cnt > 0x1000) _tc_dummy_cnt = (uint32_t)time(NULL) % 0x1000; \
411     } \
412   } while(false)
413 #undef assert
414 #define assert(TC_expr) \
415   do { \
416     if(!(TC_expr)){ \
417       fprintf(stderr, "assertion failed: %s\n", #TC_expr); \
418       abort(); \
419     } \
420     TCTESTYIELD(); \
421   } while(false)
422 #define if(TC_cond) \
423   if((((++_tc_dummy_cnt) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
424 #define while(TC_cond) \
425   while((((++_tc_dummy_cnt) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
426 #else
427 #define TCTESTYIELD() \
428   do { \
429   } while(false)
430 #endif
431 
432 #if !defined(_POSIX_PRIORITY_SCHEDULING) && TCUSEPTHREAD
433 #define sched_yield()                    usleep(1000 * 20)
434 #endif
435 
436 
437 
438 /*************************************************************************************************
439  * utilities for implementation
440  *************************************************************************************************/
441 
442 
443 #define TCNUMBUFSIZ    32                // size of a buffer for a number
444 
445 /* set a buffer for a variable length number */
446 #define TCSETVNUMBUF(TC_len, TC_buf, TC_num) \
447   do { \
448     int _TC_num = (TC_num); \
449     if(_TC_num == 0){ \
450       ((signed char *)(TC_buf))[0] = 0; \
451       (TC_len) = 1; \
452     } else { \
453       (TC_len) = 0; \
454       while(_TC_num > 0){ \
455         int _TC_rem = _TC_num & 0x7f; \
456         _TC_num >>= 7; \
457         if(_TC_num > 0){ \
458           ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
459         } else { \
460           ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
461         } \
462         (TC_len)++; \
463       } \
464     } \
465   } while(false)
466 
467 /* set a buffer for a variable length number of 64-bit */
468 #define TCSETVNUMBUF64(TC_len, TC_buf, TC_num) \
469   do { \
470     long long int _TC_num = (TC_num); \
471     if(_TC_num == 0){ \
472       ((signed char *)(TC_buf))[0] = 0; \
473       (TC_len) = 1; \
474     } else { \
475       (TC_len) = 0; \
476       while(_TC_num > 0){ \
477         int _TC_rem = _TC_num & 0x7f; \
478         _TC_num >>= 7; \
479         if(_TC_num > 0){ \
480           ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
481         } else { \
482           ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
483         } \
484         (TC_len)++; \
485       } \
486     } \
487   } while(false)
488 
489 /* read a variable length buffer */
490 #define TCREADVNUMBUF(TC_buf, TC_num, TC_step) \
491   do { \
492     TC_num = 0; \
493     int _TC_base = 1; \
494     int _TC_i = 0; \
495     while(true){ \
496       if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
497         TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
498         break; \
499       } \
500       TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
501       _TC_base <<= 7; \
502       _TC_i++; \
503     } \
504     (TC_step) = _TC_i + 1; \
505   } while(false)
506 
507 /* read a variable length buffer */
508 #define TCREADVNUMBUF64(TC_buf, TC_num, TC_step) \
509   do { \
510     TC_num = 0; \
511     long long int _TC_base = 1; \
512     int _TC_i = 0; \
513     while(true){ \
514       if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
515         TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
516         break; \
517       } \
518       TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
519       _TC_base <<= 7; \
520       _TC_i++; \
521     } \
522     (TC_step) = _TC_i + 1; \
523   } while(false)
524 
525 /* calculate the size of a buffer for a variable length number */
526 #define TCCALCVNUMSIZE(TC_num) \
527   ((TC_num) < 0x80 ? 1 : (TC_num) < 0x4000 ? 2 : (TC_num) < 0x200000 ? 3 : \
528    (TC_num) < 0x10000000 ? 4 : 5)
529 
530 /* compare keys of two records by lexical order */
531 #define TCCMPLEXICAL(TC_rv, TC_aptr, TC_asiz, TC_bptr, TC_bsiz) \
532   do { \
533     (TC_rv) = 0; \
534     int _TC_min = (TC_asiz) < (TC_bsiz) ? (TC_asiz) : (TC_bsiz); \
535     for(int _TC_i = 0; _TC_i < _TC_min; _TC_i++){ \
536       if(((unsigned char *)(TC_aptr))[_TC_i] != ((unsigned char *)(TC_bptr))[_TC_i]){ \
537         (TC_rv) = ((unsigned char *)(TC_aptr))[_TC_i] - ((unsigned char *)(TC_bptr))[_TC_i]; \
538         break; \
539       } \
540     } \
541     if((TC_rv) == 0) (TC_rv) = (TC_asiz) - (TC_bsiz); \
542   } while(false)
543 
544 
545 
546 #endif                                   // duplication check
547 
548 
549 // END OF FILE
550