Debian Codegraph @

lib/grn.h

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2018-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #pragma once
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif /* HAVE_CONFIG_H */
25
26 #ifdef WIN32
27 # ifdef __GNUC__
28 # define __MINGW_MSVC_COMPAT_WARNINGS
29 # endif /* __GNUC__ */
30
31 # define GRN_MINIMUM_WINDOWS_VERSION _WIN32_WINNT_WIN8
32
33 # ifdef WINVER
34 # undef WINVER
35 # endif /* WINVER */
36 # define WINVER GRN_MINIMUM_WINDOWS_VERSION
37 # ifdef _WIN32_WINNT
38 # undef _WIN32_WINNT
39 # endif /* _WIN32_WINNT */
40 # define _WIN32_WINNT GRN_MINIMUM_WINDOWS_VERSION
41
42 # ifdef WIN32_LEAN_AND_MEAN
43 # undef WIN32_LEAN_AND_MEAN
44 # endif /* WIN32_LEAN_AND_MEAN */
45 #endif /* WIN32 */
46
47 #ifdef __cplusplus
48 # define __STDC_LIMIT_MACROS
49 #endif
50
51 #include <stdlib.h>
52 #include <stdint.h>
53
54 #include <sys/types.h>
55
56 #ifdef HAVE_SYS_PARAM_H
57 # include <sys/param.h>
58 #endif /* HAVE_SYS_PARAM_H */
59
60 #ifdef HAVE_SYS_MMAN_H
61 # include <sys/mman.h>
62 #endif /* HAVE_SYS_MMAN_H */
63
64 #ifdef HAVE_SYS_TIME_H
65 # include <sys/time.h>
66 #endif /* HAVE_SYS_TIME_H */
67
68 #ifdef HAVE_SYS_RESOURCE_H
69 # include <sys/resource.h>
70 #endif /* HAVE_SYS_RESOURCE_H */
71
72 #ifdef GRN_STATIC
73 # define GRN_API
74 # define GRN_VAR extern
75 #else
76 # ifdef WIN32
77 # define GRN_API __declspec(dllexport)
78 # ifdef GROONGA_MAIN
79 # define GRN_VAR __declspec(dllimport)
80 # else
81 # define GRN_VAR __declspec(dllexport) extern
82 # endif /* GROONGA_MAIN */
83 # else
84 # define GRN_API
85 # define GRN_VAR extern
86 # endif
87 #endif
88
89 #ifdef WIN32
90 # ifndef NOMINMAX
91 # define NOMINMAX
92 # endif
93 # include <basetsd.h>
94 # include <process.h>
95 # include <winsock2.h>
96 # include <ws2tcpip.h>
97 # include <windows.h>
98 # include <stddef.h>
99 # include <windef.h>
100 # include <float.h>
101 # include <time.h>
102 # include <sys/types.h>
103
104 # ifndef __GNUC__
105 # define PATH_MAX (MAX_PATH - 1)
106 # endif
107
108 # ifndef __GNUC__
109 typedef SSIZE_T ssize_t;
110 typedef int pid_t;
111 typedef int64_t off64_t;
112 # endif
113
114 # undef MSG_WAITALL
115 # define MSG_WAITALL 0 /* before Vista, not supported... */
116 # define SHUT_RDWR SD_BOTH
117
118 typedef SOCKET grn_sock;
119 # define grn_sock_close(sock) closesocket(sock)
120 # define GRN_INVALID_SOCKET INVALID_SOCKET
121
122 # define CALLBACK __stdcall
123
124 # ifndef __GNUC__
125 # include <intrin.h>
126 # pragma intrinsic(_BitScanReverse)
127 # pragma intrinsic(_InterlockedExchangeAdd)
128 # include <sys/timeb.h>
129 # include <errno.h>
130 # endif
131
132 #else /* WIN32 */
133
134 # define GROONGA_API
135
136 # ifdef HAVE_UNISTD_H
137 # ifndef _LARGEFILE64_SOURCE
138 # define _LARGEFILE64_SOURCE
139 # endif
140 # include <unistd.h>
141 # endif /* HAVE_UNISTD_H */
142
143 # ifdef BSD
144 /* BSD doesn't define off64_t. So we use off_t for off64_t. */
145 typedef off_t off64_t;
146 # endif
147
148 # ifndef PATH_MAX
149 # if defined(MAXPATHLEN)
150 # define PATH_MAX MAXPATHLEN
151 # else /* MAXPATHLEN */
152 # define PATH_MAX 1024
153 # endif /* MAXPATHLEN */
154 # endif /* PATH_MAX */
155 # ifndef INT_LEAST8_MAX
156 typedef char int_least8_t;
157 # endif /* INT_LEAST8_MAX */
158 # ifndef UINT_LEAST8_MAX
159 typedef unsigned char uint_least8_t;
160 # endif /* UINT_LEAST8_MAX */
161 typedef int grn_sock;
162 # define grn_sock_close(sock) close(sock)
163 # define GRN_INVALID_SOCKET -1
164 # define CALLBACK
165
166 #endif /* WIN32 */
167
168 #ifndef INT8_MAX
169 # define INT8_MAX (127)
170 #endif /* INT8_MAX */
171
172 #ifndef INT8_MIN
173 # define INT8_MIN (-128)
174 #endif /* INT8_MIN */
175
176 #ifndef INT16_MAX
177 # define INT16_MAX (32767)
178 #endif /* INT16_MAX */
179
180 #ifndef INT16_MIN
181 # define INT16_MIN (-32768)
182 #endif /* INT16_MIN */
183
184 #ifndef INT32_MAX
185 # define INT32_MAX (2147483647)
186 #endif /* INT32_MAX */
187
188 #ifndef INT32_MIN
189 # define INT32_MIN (-2147483648)
190 #endif /* INT32_MIN */
191
192 #ifndef UINT32_MAX
193 # define UINT32_MAX (4294967295)
194 #endif /* UINT32_MAX */
195
196 #ifndef INT64_MAX
197 # define INT64_MAX (9223372036854775807)
198 #endif /* INT64_MAX */
199
200 #ifndef INT64_MIN
201 # define INT64_MIN (-9223372036854775808)
202 #endif /* INT64_MIN */
203
204 #ifdef WIN32
205 # define grn_lseek(fd, offset, whence) _lseeki64(fd, offset, whence)
206 #else /* WIN32 */
207 # define grn_lseek(fd, offset, whence) lseek(fd, offset, whence)
208 #endif /* WIN32 */
209
210 #ifdef HAVE_PTHREAD_H
211 # include <pthread.h>
212 typedef pthread_t grn_thread;
213 typedef void *grn_thread_func_result;
214 # define GRN_THREAD_FUNC_RETURN_VALUE NULL
215 # define THREAD_CREATE(thread, func, arg) \
216 (pthread_create(&(thread), NULL, (func), (arg)))
217 # define THREAD_JOIN(thread) (pthread_join(thread, NULL))
218 typedef pthread_mutex_t grn_mutex;
219 # define MUTEX_INIT(m) pthread_mutex_init(&(m), NULL)
220 # define MUTEX_LOCK(m) pthread_mutex_lock(&(m))
221 # define MUTEX_LOCK_CHECK(m) (MUTEX_LOCK(m) == 0)
222 # define MUTEX_UNLOCK(m) pthread_mutex_unlock(&(m))
223 # define MUTEX_FIN(m) pthread_mutex_destroy(&(m))
224
225 typedef pthread_mutex_t grn_critical_section;
226 # define CRITICAL_SECTION_INIT(cs) pthread_mutex_init(&(cs), NULL)
227 # define CRITICAL_SECTION_ENTER(cs) pthread_mutex_lock(&(cs))
228 # define CRITICAL_SECTION_LEAVE(cs) pthread_mutex_unlock(&(cs))
229 # define CRITICAL_SECTION_FIN(cs) pthread_mutex_destroy(&(cs))
230
231 typedef pthread_cond_t grn_cond;
232 # define COND_INIT(c) pthread_cond_init(&(c), NULL)
233 # define COND_SIGNAL(c) pthread_cond_signal(&(c))
234 # define COND_WAIT(c, cs) pthread_cond_wait(&(c), &(cs))
235 # define COND_BROADCAST(c) pthread_cond_broadcast(&(c))
236 # define COND_FIN(c) pthread_cond_destroy(&(c))
237
238 typedef pthread_key_t grn_thread_key;
239 # define THREAD_KEY_CREATE(key, destr) pthread_key_create(key, destr)
240 # define THREAD_KEY_DELETE(key) pthread_key_delete(key)
241 # define THREAD_SETSPECIFIC(key, value) pthread_setspecific(key, value)
242 # define THREAD_GETSPECIFIC(key) pthread_getspecific(key)
243
244 # ifdef USE_UYIELD
245 extern int grn_uyield_count;
246 # define GRN_TEST_YIELD() \
247 do { \
248 if (((++grn_uyield_count) & (0x20 - 1)) == 0) { \
249 sched_yield(); \
250 if (grn_uyield_count > 0x1000) { \
251 grn_uyield_count = (uint32_t)time(NULL) % 0x1000; \
252 } \
253 } \
254 } while (0)
255
256 # undef assert
257 # define assert(assert_expr) \
258 do { \
259 if (!(assert_expr)) { \
260 fprintf(stderr, "assertion failed: %s\n", #assert_expr); \
261 abort(); \
262 } \
263 GRN_TEST_YIELD(); \
264 } while (0)
265
266 # define if \
267 (if_cond) if ((((++grn_uyield_count) & (0x100 - 1)) != 0 || \
268 (sched_yield() * 0) == 0) && \
269 (if_cond))
270 # define while(while_cond) \
271 while ((((++grn_uyield_count) & (0x100 - 1)) != 0 || \
272 (sched_yield() * 0) == 0) && \
273 (while_cond))
274
275 # if !defined(_POSIX_PRIORITY_SCHEDULING)
276 # define sched_yield() grn_nanosleep(1000000 * 20)
277 # endif
278 # else /* USE_UYIELD */
279 # define GRN_TEST_YIELD() \
280 do { \
281 } while (0)
282 # endif /* USE_UYIELD */
283
284 #else /* HAVE_PTHREAD_H */
285
286 /* todo */
287 typedef int grn_thread_key;
288 # define THREAD_KEY_CREATE(key, destr)
289 # define THREAD_KEY_DELETE(key)
290 # define THREAD_SETSPECIFIC(key)
291 # define THREAD_GETSPECIFIC(key, value)
292
293 # ifdef WIN32
294 typedef uintptr_t grn_thread;
295 typedef unsigned int grn_thread_func_result;
296 # define GRN_THREAD_FUNC_RETURN_VALUE 0
297 # define THREAD_CREATE(thread, func, arg) \
298 (((thread) = _beginthreadex(NULL, 0, (func), (arg), 0, NULL)) == \
299 (grn_thread)0)
300 # define THREAD_JOIN(thread) \
301 (WaitForSingleObject((HANDLE)(thread), INFINITE) == WAIT_FAILED)
302 typedef HANDLE grn_mutex;
303 # define MUTEX_INIT(m) ((m) = CreateMutex(0, FALSE, NULL))
304 # define MUTEX_LOCK(m) WaitForSingleObject((m), INFINITE)
305 # define MUTEX_LOCK_CHECK(m) (MUTEX_LOCK(m) == WAIT_OBJECT_0)
306 # define MUTEX_UNLOCK(m) ReleaseMutex((m))
307 # define MUTEX_FIN(m) CloseHandle((m))
308 typedef CRITICAL_SECTION grn_critical_section;
309 # define CRITICAL_SECTION_INIT(cs) InitializeCriticalSection(&(cs))
310 # define CRITICAL_SECTION_ENTER(cs) EnterCriticalSection(&(cs))
311 # define CRITICAL_SECTION_LEAVE(cs) LeaveCriticalSection(&(cs))
312 # define CRITICAL_SECTION_FIN(cs) DeleteCriticalSection(&(cs))
313
314 typedef CONDITION_VARIABLE grn_cond;
315 # define COND_INIT(c) InitializeConditionVariable(&(c))
316 # define COND_SIGNAL(c) WakeConditionVariable(&(c))
317 # define COND_BROADCAST(c) WakeAllConditionVariable(&(c))
318 # define COND_WAIT(c, cs) SleepConditionVariableCS(&(c), &(cs), INFINITE)
319 # define COND_FIN(c)
320
321 # else /* WIN32 */
322 /* todo */
323 typedef int grn_cond;
324 # define COND_INIT(c) ((c) = 0)
325 # define COND_SIGNAL(c)
326 # define COND_WAIT(c, m) \
327 do { \
328 MUTEX_UNLOCK(m); \
329 grn_nanosleep(1000000); \
330 MUTEX_LOCK(m); \
331 } while (0)
332 # define COND_FIN(c)
333 /* todo : must be enhanced! */
334
335 # endif /* WIN32 */
336
337 # define GRN_TEST_YIELD() \
338 do { \
339 } while (0)
340
341 #endif /* HAVE_PTHREAD_H */
342
343 #define MUTEX_LOCK_ENSURE(ctx_, mutex) \
344 do { \
345 grn_ctx *ctx__ = (ctx_); \
346 do { \
347 grn_ctx *ctx = ctx__; \
348 if (MUTEX_LOCK_CHECK(mutex)) { \
349 break; \
350 } \
351 if (ctx) { \
352 SERR("MUTEX_LOCK"); \
353 } \
354 grn_nanosleep(1000000); \
355 } while (true); \
356 } while (false)
357
358 /* format string for printf */
359 #ifdef WIN32
360 # define GRN_FMT_INT32D "I32d"
361 # define GRN_FMT_INT32U "I32u"
362 # define GRN_FMT_INT64D "I64d"
363 # define GRN_FMT_INT64U "I64u"
364 # define GRN_FMT_INT64X "I64x"
365 #elif defined(HAVE_INTTYPES_H) && !defined(__cplusplus)
366 # include <inttypes.h>
367 # define GRN_FMT_INT32D PRId32
368 # define GRN_FMT_INT32U PRIu32
369 # define GRN_FMT_INT64D PRId64
370 # define GRN_FMT_INT64U PRIu64
371 # define GRN_FMT_INT64X PRIx64
372 #elif defined(__cplusplus) && __cplusplus >= 201103L
373 # include <cinttypes>
374 # define GRN_FMT_INT32D PRId32
375 # define GRN_FMT_INT32U PRIu32
376 # define GRN_FMT_INT64D PRId64
377 # define GRN_FMT_INT64U PRIu64
378 # define GRN_FMT_INT64X PRIx64
379 #else /* WIN32 */
380 # define GRN_FMT_INT32D "d"
381 # define GRN_FMT_INT32U "u"
382 # ifdef __x86_64__
383 # define GRN_FMT_INT64D "ld"
384 # define GRN_FMT_INT64U "lu"
385 # define GRN_FMT_INT64X "lx"
386 # else /* __x86_64__ */
387 # define GRN_FMT_INT64D "lld"
388 # define GRN_FMT_INT64U "llu"
389 # define GRN_FMT_INT64X "llx"
390 # endif /* __x86_64__ */
391 #endif /* WIN32 */
392
393 #ifdef WIN32
394 # define GRN_FMT_LLD "I64d"
395 # define GRN_FMT_LLU "I64u"
396 # define GRN_FMT_SIZE "Iu"
397 # define GRN_FMT_SSIZE "Id"
398 # ifdef WIN64
399 # define GRN_FMT_SOCKET GRN_FMT_INT64U
400 # else /* WIN64 */
401 # define GRN_FMT_SOCKET GRN_FMT_INT32U
402 # endif /* WIN64 */
403 # define GRN_FMT_DWORD "lu"
404 # define GRN_FMT_OFF64_T GRN_FMT_LLD
405 #else /* WIN32 */
406 # define GRN_FMT_LLD "lld"
407 # define GRN_FMT_LLU "llu"
408 # define GRN_FMT_SIZE "zu"
409 # define GRN_FMT_SSIZE "zd"
410 # define GRN_FMT_SOCKET "d"
411 # define GRN_FMT_OFF64_T GRN_FMT_INT64D
412 #endif /* WIN32 */
413
414 #ifdef __GNUC__
415 # if (defined(__i386__) || defined(__x86_64__)) /* ATOMIC ADD */
416 /* clang-format off */
417 /*
418 * GRN_ATOMIC_ADD_EX() performs { r = *p; *p += i; } atomically.
419 */
420 # define GRN_ATOMIC_ADD_EX(p, i, r) \
421 __asm__ __volatile__ ("lock xaddl %0, %1" : "=r"(r), "+m"(*p) : "0"(i))
422 typedef uint32_t grn_bit_scan_rev_result;
423 /*
424 * GRN_BIT_SCAN_REV() finds the most significant 1 bit of `v'. Then, `r' is set
425 * to the index of the found bit. Note that `v' must not be 0.
426 */
427 # define GRN_BIT_SCAN_REV(v, r) \
428 __asm__ __volatile__ ("bsrl %1, %%eax; movl %%eax, %0" : "=r"(r) : "r"(v) : "%eax")
429 /*
430 * GRN_BIT_SCAN_REV0() is similar to GRN_BIT_SCAN_REV() but if `v' is 0, `r' is
431 * set to 0.
432 */
433 # define GRN_BIT_SCAN_REV0(v, r) \
434 __asm__ __volatile__ ("bsrl %1, %%eax; cmovzl %1, %%eax; movl %%eax, %0" : "=r"(r) : "r"(v) : "%eax", "cc")
435 # elif (defined(__PPC__) || defined(__ppc__)) /* ATOMIC ADD */
436 # define GRN_ATOMIC_ADD_EX(p,i,r) \
437 __asm__ __volatile__ ("\n1:\n\tlwarx %0, 0, %1\n\tadd %0, %0, %2\n\tstwcx. %0, 0, %1\n\tbne- 1b\n\tsub %0, %0, %2" : "=&r" (r) : "r" (p), "r" (i) : "cc", "memory")
438 /* todo */
439 typedef uint32_t grn_bit_scan_rev_result;
440 # define GRN_BIT_SCAN_REV(v,r) for (r = 31; r && !((1 << r) & v); r--)
441 # define GRN_BIT_SCAN_REV0(v,r) GRN_BIT_SCAN_REV(v,r)
442 # elif (defined(__sun) && defined(__SVR4)) /* ATOMIC ADD */
443 # include <atomic.h>
444 # define GRN_ATOMIC_ADD_EX(p,i,r) \
445 (r = atomic_add_32_nv(p, i) - i)
446 /* todo */
447 typedef uint32_t grn_bit_scan_rev_result;
448 # define GRN_BIT_SCAN_REV(v,r) for (r = 31; r && !((1 << r) & v); r--)
449 # define GRN_BIT_SCAN_REV0(v,r) GRN_BIT_SCAN_REV(v,r)
450 # elif defined(__ATOMIC_SEQ_CST) /* GCC atomic builtins */
451 # define GRN_ATOMIC_ADD_EX(p,i,r) \
452 (r = __atomic_fetch_add(p, i, __ATOMIC_SEQ_CST))
453 typedef uint32_t grn_bit_scan_rev_result;
454 # define GRN_BIT_SCAN_REV(v,r) for (r = 31; r && !((1 << r) & v); r--)
455 # define GRN_BIT_SCAN_REV0(v,r) GRN_BIT_SCAN_REV(v,r)
456 # else /* ATOMIC ADD */
457 /* todo */
458 typedef uint32_t grn_bit_scan_rev_result;
459 # define GRN_BIT_SCAN_REV(v,r) for (r = 31; r && !((1 << r) & v); r--)
460 # define GRN_BIT_SCAN_REV0(v,r) GRN_BIT_SCAN_REV(v,r)
461 # endif /* ATOMIC ADD */
462
463 # ifdef __i386__ /* ATOMIC 64BIT SET */
464 # define GRN_SET_64BIT(p,v) \
465 __asm__ __volatile__ ("\txchgl %%esi, %%ebx\n1:\n\tmovl (%0), %%eax\n\tmovl 4(%0), %%edx\n\tlock; cmpxchg8b (%0)\n\tjnz 1b\n\txchgl %%ebx, %%esi" : : "D"(p), "S"(*(((uint32_t *)&(v))+0)), "c"(*(((uint32_t *)&(v))+1)) : "ax", "dx", "memory")
466 # elif defined(__x86_64__) /* ATOMIC 64BIT SET */
467 # define GRN_SET_64BIT(p,v) \
468 (*(p) = (v))
469 # elif (defined(__sun) && defined(__SVR4)) /* ATOMIC 64BIT SET */
470 /* todo */
471 # define GRN_SET_64BIT(p,v) \
472 (void)atomic_swap_64(p, v)
473 # elif defined(__ATOMIC_SEQ_CST) /* GCC atomic builtins */
474 # define GRN_SET_64BIT(p,v) \
475 __atomic_store_n(p, v, __ATOMIC_SEQ_CST)
476 # else
477 # warning Need atomic 64bit operation support. The current implementation may break data.
478 # define GRN_SET_64BIT(p,v) \
479 (*(p) = (v))
480 # endif /* ATOMIC 64BIT SET */
481
482 #elif (defined(WIN32) || defined (_WIN64)) /* __GNUC__ */
483
484 # define GRN_ATOMIC_ADD_EX(p,i,r) \
485 ((r) = _InterlockedExchangeAdd((p), (i)))
486 # if defined(_WIN64) /* ATOMIC 64BIT SET */
487 # define GRN_SET_64BIT(p,v) \
488 (*(p) = (v))
489 # else /* ATOMIC 64BIT SET */
490 # define GRN_SET_64BIT(p,v) do {\
491 uint32_t v1, v2; \
492 uint64_t *p2= (p); \
493 v1 = *(((uint32_t *)&(v))+0);\
494 v2 = *(((uint32_t *)&(v))+1);\
495 __asm _set_loop: \
496 __asm mov esi, p2 \
497 __asm mov ebx, v1 \
498 __asm mov ecx, v2 \
499 __asm mov eax, dword ptr [esi] \
500 __asm mov edx, dword ptr [esi + 4] \
501 __asm lock cmpxchg8b qword ptr [esi] \
502 __asm jnz _set_loop \
503 } while (0)
504 /* TODO: use _InterlockedCompareExchange64 or inline asm */
505 # endif /* ATOMIC 64BIT SET */
506 /* clang-format on */
507
508 /* todo */
509 typedef unsigned long grn_bit_scan_rev_result;
510 # define GRN_BIT_SCAN_REV(v, r) \
511 do { \
512 if (!BitScanReverse(&r, v)) { \
513 r = 0; \
514 } \
515 } while (0)
516 # define GRN_BIT_SCAN_REV0(v, r) GRN_BIT_SCAN_REV(v, r)
517
518 #else /* __GNUC__ */
519
520 # if (defined(__sun) && defined(__SVR4)) /* ATOMIC ADD */
521 # define __FUNCTION__ ""
522 # include <atomic.h>
523 # define GRN_ATOMIC_ADD_EX(p, i, r) (r = atomic_add_32_nv(p, i) - i)
524 /* todo */
525 # define GRN_SET_64BIT(p, v) (void)atomic_swap_64(p, v)
526 # endif /* ATOMIC ADD */
527 /* todo */
528 typedef uint32_t grn_bit_scan_rev_result;
529 # define GRN_BIT_SCAN_REV(v, r) for (r = 31; r && !((1 << r) & v); r--)
530 # define GRN_BIT_SCAN_REV0(v, r) GRN_BIT_SCAN_REV(v, r)
531
532 #endif /* __GNUC__ */
533
534 typedef uint8_t byte;
535
536 #define GRN_ID_WIDTH 30
537
538 #ifdef __GNUC__
539 static inline int
540 grn_str_greater(const uint8_t *ap, uint32_t as, const uint8_t *bp, uint32_t bs)
541 {
542 for (;; ap++, bp++, as--, bs--) {
543 if (!as) {
544 return 0;
545 }
546 if (!bs) {
547 return 1;
548 }
549 if (*ap < *bp) {
550 return 0;
551 }
552 if (*ap > *bp) {
553 return 1;
554 }
555 }
556 }
557 #else /* __GNUC__ */
558 # define grn_str_greater(ap, as, bp, bs) \
559 (((as) > (bs)) ? (memcmp((ap), (bp), (bs)) >= 0) \
560 : (memcmp((ap), (bp), (as)) > 0))
561 #endif /* __GNUC__ */
562
563 #ifdef WORDS_BIGENDIAN
564 # define grn_hton(buf, key, size) \
565 do { \
566 uint32_t size_ = (uint32_t)size; \
567 uint8_t *buf_ = (uint8_t *)buf; \
568 uint8_t *key_ = (uint8_t *)key; \
569 while (size_--) { \
570 *buf_++ = *key_++; \
571 } \
572 } while (0)
573 # define grn_ntohi(buf, key, size) \
574 do { \
575 uint32_t size_ = (uint32_t)size; \
576 uint8_t *buf_ = (uint8_t *)buf; \
577 uint8_t *key_ = (uint8_t *)key; \
578 if (size_) { \
579 *buf_++ = 0x80 ^ *key_++; \
580 size_--; \
581 } \
582 while (size_) { \
583 *buf_++ = *key_++; \
584 size_--; \
585 } \
586 } while (0)
587 #else /* WORDS_BIGENDIAN */
588 # define grn_hton(buf, key, size) \
589 do { \
590 uint32_t size_ = (uint32_t)size; \
591 uint8_t *buf_ = (uint8_t *)buf; \
592 uint8_t *key_ = (uint8_t *)key + size; \
593 while (size_--) { \
594 *buf_++ = *(--key_); \
595 } \
596 } while (0)
597 # define grn_ntohi(buf, key, size) \
598 do { \
599 uint32_t size_ = (uint32_t)size; \
600 uint8_t *buf_ = (uint8_t *)buf; \
601 uint8_t *key_ = (uint8_t *)key + size; \
602 while (size_ > 1) { \
603 *buf_++ = *(--key_); \
604 size_--; \
605 } \
606 if (size_) { \
607 *buf_ = 0x80 ^ *(--key_); \
608 } \
609 } while (0)
610 #endif /* WORDS_BIGENDIAN */
611 #define grn_ntoh(buf, key, size) grn_hton(buf, key, size)
612
613 #ifndef __GNUC_PREREQ
614 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
615 # define __GNUC_PREREQ(maj, min) \
616 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
617 # else
618 # define __GNUC_PREREQ(maj, min) 0
619 # endif /* defined(__GNUC__) && defined(__GNUC_MINOR__) */
620 #endif /* __GNUC_PREREQ */
621
622 #ifdef _MSC_VER
623 # define grn_bswap_uint64(in, out) ((out) = _byteswap_uint64(in))
624 #else /* _MSC_VER */
625 # if defined(__GNUC__) && __GNUC_PREREQ(4, 3)
626 # define grn_bswap_uint64(in, out) ((out) = __builtin_bswap64(in))
627 # else /* defined(__GNUC__) && __GNUC_PREREQ(4, 3) */
628 # define grn_bswap_uint64(in, out) \
629 do { \
630 uint64_t temp_ = (in); \
631 (out) = (temp_ << 56) | ((temp_ & (0xFFULL << 8)) << 40) | \
632 ((temp_ & (0xFFULL << 16)) << 24) | \
633 ((temp_ & (0xFFULL << 24)) << 8) | \
634 ((temp_ & (0xFFULL << 32)) >> 8) | \
635 ((temp_ & (0xFFULL << 40)) >> 24) | \
636 ((temp_ & (0xFFULL << 48)) >> 40) | (temp_ >> 56); \
637 } while (0)
638 # endif /* __GNUC__ */
639 #endif /* _MSC_VER */
640
641 #ifdef WORDS_BIGENDIAN
642 # define grn_hton_uint64(in, out) ((out) = (in))
643 #else /* WORDS_BIGENDIAN */
644 # define grn_hton_uint64(in, out) grn_bswap_uint64(in, out)
645 #endif /* WORDS_BIGENDIAN */
646 #define grn_ntoh_uint64(in, out) grn_hton_uint64(in, out)
647
648 #define grn_gton(keybuf, key, size) \
649 do { \
650 const grn_geo_point *point_ = (const grn_geo_point *)key; \
651 uint64_t la_ = (uint32_t)point_->latitude; \
652 uint64_t lo_ = (uint32_t)point_->longitude; \
653 uint64_t result_; \
654 la_ = (la_ | (la_ << 16)) & 0x0000FFFF0000FFFFULL; \
655 la_ = (la_ | (la_ << 8)) & 0x00FF00FF00FF00FFULL; \
656 la_ = (la_ | (la_ << 4)) & 0x0F0F0F0F0F0F0F0FULL; \
657 la_ = (la_ | (la_ << 2)) & 0x3333333333333333ULL; \
658 la_ = (la_ | (la_ << 1)) & 0x5555555555555555ULL; \
659 lo_ = (lo_ | (lo_ << 16)) & 0x0000FFFF0000FFFFULL; \
660 lo_ = (lo_ | (lo_ << 8)) & 0x00FF00FF00FF00FFULL; \
661 lo_ = (lo_ | (lo_ << 4)) & 0x0F0F0F0F0F0F0F0FULL; \
662 lo_ = (lo_ | (lo_ << 2)) & 0x3333333333333333ULL; \
663 lo_ = (lo_ | (lo_ << 1)) & 0x5555555555555555ULL; \
664 result_ = (la_ << 1) | lo_; \
665 grn_hton_uint64(result_, result_); \
666 grn_memcpy(keybuf, &result_, sizeof(result_)); \
667 } while (0)
668
669 #define grn_ntog(keybuf, key, size) \
670 do { \
671 grn_geo_point *point_ = (grn_geo_point *)keybuf; \
672 uint64_t key_ = *(const uint64_t *)key; \
673 uint64_t la_, lo_; \
674 grn_ntoh_uint64(key_, key_); \
675 la_ = (key_ >> 1) & 0x5555555555555555ULL; \
676 lo_ = key_ & 0x5555555555555555ULL; \
677 la_ = (la_ | (la_ >> 1)) & 0x3333333333333333ULL; \
678 la_ = (la_ | (la_ >> 2)) & 0x0F0F0F0F0F0F0F0FULL; \
679 la_ = (la_ | (la_ >> 4)) & 0x00FF00FF00FF00FFULL; \
680 la_ = (la_ | (la_ >> 8)) & 0x0000FFFF0000FFFFULL; \
681 la_ = (la_ | (la_ >> 16)) & 0x00000000FFFFFFFFULL; \
682 lo_ = (lo_ | (lo_ >> 1)) & 0x3333333333333333ULL; \
683 lo_ = (lo_ | (lo_ >> 2)) & 0x0F0F0F0F0F0F0F0FULL; \
684 lo_ = (lo_ | (lo_ >> 4)) & 0x00FF00FF00FF00FFULL; \
685 lo_ = (lo_ | (lo_ >> 8)) & 0x0000FFFF0000FFFFULL; \
686 lo_ = (lo_ | (lo_ >> 16)) & 0x00000000FFFFFFFFULL; \
687 point_->latitude = (int32_t)la_; \
688 point_->longitude = (int32_t)lo_; \
689 } while (0)
690
691 #ifdef HAVE__STRTOUI64
692 # define strtoull(nptr, endptr, base) _strtoui64(nptr, endptr, base)
693 #endif /* HAVE__STRTOUI64 */
694
695 #ifdef USE_FUTEX
696 # include <linux/futex.h>
697 # include <sys/syscall.h>
698
699 # define GRN_FUTEX_WAIT(p) \
700 do { \
701 int err; \
702 struct timespec timeout = {1, 0}; \
703 while (1) { \
704 if (!(err = syscall(SYS_futex, p, FUTEX_WAIT, *p, &timeout))) { \
705 break; \
706 } \
707 if (err == ETIMEDOUT) { \
708 GRN_LOG(ctx, GRN_LOG_CRIT, "timeout in GRN_FUTEX_WAIT(%p)", p); \
709 break; \
710 } else if (err != EWOULDBLOCK) { \
711 GRN_LOG(ctx, GRN_LOG_CRIT, "error %d in GRN_FUTEX_WAIT(%p)", err); \
712 break; \
713 } \
714 } \
715 } while (0)
716
717 # define GRN_FUTEX_WAKE(p) syscall(SYS_futex, p, FUTEX_WAKE, 1)
718 #else /* USE_FUTEX */
719 # define GRN_FUTEX_WAIT(p) grn_nanosleep(1000000)
720 # define GRN_FUTEX_WAKE(p)
721 #endif /* USE_FUTEX */
722
723 #ifndef HOST_NAME_MAX
724 # ifdef _POSIX_HOST_NAME_MAX
725 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
726 # else /* POSIX_HOST_NAME_MAX */
727 # define HOST_NAME_MAX 128
728 # endif /* POSIX_HOST_NAME_MAX */
729 #endif /* HOST_NAME_MAX */
730
731 #define GRN_NEXT_ADDR(p) (((byte *)(p)) + sizeof(*(p)))
732
733 #ifdef GRN_WITH_ONIGMO
734 # define GRN_SUPPORT_REGEXP
735 #endif /* GRN_WITH_ONIGMO */
736
737 #ifdef __GNUC__
738 # define GRN_LIKELY(expr) (__builtin_expect(!!(expr), 1))
739 # define GRN_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
740 #else
741 # define GRN_LIKELY(expr) expr
742 # define GRN_UNLIKELY(expr) expr
743 #endif
744
745 #ifdef __cplusplus
746 extern "C" {
747 #endif
748
749 GRN_API void
750 grn_sleep(uint32_t seconds);
751 GRN_API void
752 grn_nanosleep(uint64_t nanoseconds);
753
754 #ifdef __cplusplus
755 }
756 #endif
757
758 #define GRN_ID_MAX_IN_BIT 30 /* log2(GRN_ID_MAX + 1) == 30 */
759
760 #include <groonga.h>