precompiled-raindrops 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.document +7 -0
  3. data/.gitattributes +4 -0
  4. data/.github/workflows/cibuildgem.yaml +90 -0
  5. data/.gitignore +16 -0
  6. data/.olddoc.yml +16 -0
  7. data/COPYING +165 -0
  8. data/GIT-VERSION-GEN +40 -0
  9. data/GNUmakefile +4 -0
  10. data/Gemfile +1 -0
  11. data/LICENSE +16 -0
  12. data/README +111 -0
  13. data/Rakefile +0 -0
  14. data/TODO +3 -0
  15. data/archive/.gitignore +3 -0
  16. data/archive/slrnpull.conf +4 -0
  17. data/examples/linux-listener-stats.rb +123 -0
  18. data/examples/middleware.ru +6 -0
  19. data/examples/watcher.ru +5 -0
  20. data/examples/watcher_demo.ru +14 -0
  21. data/examples/yahns.conf.rb +31 -0
  22. data/examples/zbatery.conf.rb +17 -0
  23. data/ext/raindrops/extconf.rb +164 -0
  24. data/ext/raindrops/khashl.h +444 -0
  25. data/ext/raindrops/linux_inet_diag.c +719 -0
  26. data/ext/raindrops/my_fileno.h +16 -0
  27. data/ext/raindrops/raindrops.c +487 -0
  28. data/ext/raindrops/raindrops_atomic.h +23 -0
  29. data/ext/raindrops/tcp_info.c +245 -0
  30. data/lib/raindrops/aggregate/last_data_recv.rb +95 -0
  31. data/lib/raindrops/aggregate/pmq.rb +246 -0
  32. data/lib/raindrops/aggregate.rb +9 -0
  33. data/lib/raindrops/last_data_recv.rb +103 -0
  34. data/lib/raindrops/linux.rb +78 -0
  35. data/lib/raindrops/middleware/proxy.rb +41 -0
  36. data/lib/raindrops/middleware.rb +154 -0
  37. data/lib/raindrops/struct.rb +63 -0
  38. data/lib/raindrops/watcher.rb +429 -0
  39. data/lib/raindrops.rb +79 -0
  40. data/pkg.mk +151 -0
  41. data/raindrops.gemspec +26 -0
  42. data/setup.rb +1587 -0
  43. data/test/ipv6_enabled.rb +10 -0
  44. data/test/rack_unicorn.rb +12 -0
  45. data/test/test_aggregate_pmq.rb +66 -0
  46. data/test/test_inet_diag_socket.rb +17 -0
  47. data/test/test_last_data_recv.rb +58 -0
  48. data/test/test_last_data_recv_unicorn.rb +70 -0
  49. data/test/test_linux.rb +282 -0
  50. data/test/test_linux_all_tcp_listen_stats.rb +67 -0
  51. data/test/test_linux_all_tcp_listen_stats_leak.rb +44 -0
  52. data/test/test_linux_ipv6.rb +167 -0
  53. data/test/test_linux_middleware.rb +65 -0
  54. data/test/test_linux_reuseport_tcp_listen_stats.rb +52 -0
  55. data/test/test_middleware.rb +129 -0
  56. data/test/test_middleware_unicorn.rb +38 -0
  57. data/test/test_middleware_unicorn_ipv6.rb +38 -0
  58. data/test/test_raindrops.rb +208 -0
  59. data/test/test_raindrops_gc.rb +39 -0
  60. data/test/test_struct.rb +55 -0
  61. data/test/test_tcp_info.rb +89 -0
  62. data/test/test_watcher.rb +187 -0
  63. metadata +194 -0
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: false
2
+ # Inlined rack app using yahns server (git clone git://yhbt.net/yahns.git)
3
+ # Usage: yahns -c /path/to/this/file.conf.rb
4
+ # There is no separate config.ru file for this example,
5
+ # but rack_app may also be a string pointing to the path of a
6
+ # config.ru file
7
+
8
+ require 'rack'
9
+ rack_app = Rack::Builder.new do
10
+ use Rack::Head
11
+ addr = %w(0.0.0.0:9418 0.0.0.0:443 [::]:443 0.0.0.0:80 [::]:80
12
+ 127.0.0.1:6081 127.0.0.1:280 0.0.0.0:119 [::]:119)
13
+ use Raindrops::Middleware, listeners: addr
14
+ run Raindrops::Watcher.new(listeners: addr)
15
+ end.to_app
16
+ # rack_app = '/path/to/config.ru' # a more standard config
17
+
18
+ app(:rack, rack_app) do
19
+ # I keep IPv4 and IPv6 on separate sockets to avoid ugly
20
+ # IPv4-mapped-IPv6 addresses:
21
+ listen 8080
22
+ listen '[::]:8080', ipv6only: true
23
+ client_max_body_size 0 # no POST or any uploads
24
+ client_timeout 5
25
+ output_buffering false # needed for /tail/ endpoint to avoid ENOSPC
26
+ queue { worker_threads 30 }
27
+ end
28
+
29
+ # logging is optional, but recommended for diagnosing problems
30
+ # stderr_path '/var/log/yahns/stderr-raindrops.log'
31
+ # stdout_path '/var/log/yahns/stdout-raindrops.log'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: false
2
+ # Used for running Raindrops::Watcher, which requires a multi-threaded
3
+ # Rack server capable of streaming a response. Threads must be used,
4
+ # so any multi-threaded Rack server may be used.
5
+ # zbatery was recommended in the past, but it is abandoned
6
+ # <http://zbatery.bogomip.org/>.
7
+ # yahns may work as an alternative (see yahns.conf.rb in this dir)
8
+ Rainbows! do
9
+ use :ThreadSpawn
10
+ end
11
+ log_dir = "/var/log/zbatery"
12
+ if File.writable?(log_dir) && File.directory?(log_dir)
13
+ stderr_path "#{log_dir}/raindrops-demo.stderr.log"
14
+ stdout_path "#{log_dir}/raindrops-demo.stdout.log"
15
+ listen "/tmp/.r"
16
+ pid "/tmp/.raindrops.pid"
17
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: false
2
+ require 'mkmf'
3
+ require 'shellwords'
4
+
5
+ $CFLAGS += ' -O0 ' # faster checks
6
+ dir_config('atomic_ops')
7
+ have_func('mmap', 'sys/mman.h') or abort 'mmap() not found'
8
+ have_func('munmap', 'sys/mman.h') or abort 'munmap() not found'
9
+ have_func('rb_io_descriptor')
10
+
11
+ $CPPFLAGS += " -D_GNU_SOURCE "
12
+ have_func('mremap', 'sys/mman.h')
13
+ headers = %w(sys/types.h netdb.h string.h sys/socket.h netinet/in.h)
14
+ if have_header('linux/tcp.h')
15
+ headers << 'linux/tcp.h'
16
+ else
17
+ %w(netinet/tcp.h netinet/tcp_fsm.h).each { |h|
18
+ have_header(h, headers) and headers << h
19
+ }
20
+ end
21
+
22
+ $CPPFLAGS += " -D_BSD_SOURCE "
23
+
24
+ if have_type("struct tcp_info", headers)
25
+ %w(
26
+ tcpi_state
27
+ tcpi_ca_state
28
+ tcpi_retransmits
29
+ tcpi_probes
30
+ tcpi_backoff
31
+ tcpi_options
32
+ tcpi_snd_wscale
33
+ tcpi_rcv_wscale
34
+ tcpi_rto
35
+ tcpi_ato
36
+ tcpi_snd_mss
37
+ tcpi_rcv_mss
38
+ tcpi_unacked
39
+ tcpi_sacked
40
+ tcpi_lost
41
+ tcpi_retrans
42
+ tcpi_fackets
43
+ tcpi_last_data_sent
44
+ tcpi_last_ack_sent
45
+ tcpi_last_data_recv
46
+ tcpi_last_ack_recv
47
+ tcpi_pmtu
48
+ tcpi_rcv_ssthresh
49
+ tcpi_rtt
50
+ tcpi_rttvar
51
+ tcpi_snd_ssthresh
52
+ tcpi_snd_cwnd
53
+ tcpi_advmss
54
+ tcpi_reordering
55
+ tcpi_rcv_rtt
56
+ tcpi_rcv_space
57
+ tcpi_total_retrans
58
+ tcpi_snd_wnd
59
+ tcpi_snd_bwnd
60
+ tcpi_snd_nxt
61
+ tcpi_rcv_nxt
62
+ tcpi_toe_tid
63
+ tcpi_snd_rexmitpack
64
+ tcpi_rcv_ooopack
65
+ tcpi_snd_zerowin
66
+ ).each do |field|
67
+ cfunc = "tcp_info_#{field}"
68
+ if have_struct_member('struct tcp_info', field, headers)
69
+ func_body = <<EOF
70
+ static VALUE #{cfunc}(VALUE self)
71
+ {
72
+ struct tcp_info *info = DATA_PTR(self);
73
+ return UINT2NUM((uint32_t)info->#{field});
74
+ }
75
+ EOF
76
+ func_body.delete!("\n")
77
+ $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
78
+ else
79
+ func_body = "static inline void #{cfunc}(void) {}"
80
+ $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
81
+ cfunc = 'rb_f_notimplement'.freeze
82
+ end
83
+ rbmethod = %Q("#{field.sub(/\Atcpi_/, ''.freeze)}")
84
+ $defs << "-DDEFINE_METHOD_tcp_info_#{field}=" \
85
+ "#{Shellwords.shellescape(
86
+ %Q[rb_define_method(cTCP_Info,#{rbmethod},#{cfunc},0)])}"
87
+ end
88
+ tcp_state_map = {
89
+ ESTABLISHED: %w(TCP_ESTABLISHED TCPS_ESTABLISHED),
90
+ SYN_SENT: %w(TCP_SYN_SENT TCPS_SYN_SENT),
91
+ SYN_RECV: %w(TCP_SYN_RECV TCPS_SYN_RECEIVED),
92
+ FIN_WAIT1: %w(TCP_FIN_WAIT1 TCPS_FIN_WAIT_1),
93
+ FIN_WAIT2: %w(TCP_FIN_WAIT2 TCPS_FIN_WAIT_2),
94
+ TIME_WAIT: %w(TCP_TIME_WAIT TCPS_TIME_WAIT),
95
+ CLOSE: %w(TCP_CLOSE TCPS_CLOSED),
96
+ CLOSE_WAIT: %w(TCP_CLOSE_WAIT TCPS_CLOSE_WAIT),
97
+ LAST_ACK: %w(TCP_LAST_ACK TCPS_LAST_ACK),
98
+ LISTEN: %w(TCP_LISTEN TCPS_LISTEN),
99
+ CLOSING: %w(TCP_CLOSING TCPS_CLOSING),
100
+ }
101
+ nstate = 0
102
+ tcp_state_map.each do |state, try|
103
+ try.each do |os_name|
104
+ have_const(os_name, headers) or next
105
+ tcp_state_map[state] = os_name
106
+ nstate += 1
107
+ end
108
+ end
109
+ if nstate == tcp_state_map.size
110
+ $defs << '-DRAINDROPS_TCP_STATES_ALL_KNOWN=1'
111
+ tcp_state_map.each do |state, name|
112
+ $defs << "-DRAINDROPS_TCP_#{state}=#{name}"
113
+ end
114
+ end
115
+ end
116
+
117
+ have_func("getpagesize", "unistd.h")
118
+ have_func('rb_thread_call_without_gvl')
119
+ have_func('rb_thread_blocking_region')
120
+ have_func('rb_thread_io_blocking_region')
121
+
122
+ checking_for "GCC 4+ atomic builtins" do
123
+ # we test CMPXCHG anyways even though we don't need it to filter out
124
+ # ancient i386-only targets without CMPXCHG
125
+ src = <<SRC
126
+ int main(int argc, char * const argv[]) {
127
+ unsigned long i = 0;
128
+ __sync_lock_test_and_set(&i, 0);
129
+ __sync_lock_test_and_set(&i, 1);
130
+ __sync_bool_compare_and_swap(&i, 0, 1);
131
+ __sync_add_and_fetch(&i, argc);
132
+ __sync_sub_and_fetch(&i, argc);
133
+ return 0;
134
+ }
135
+ SRC
136
+
137
+ if try_link(src)
138
+ $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
139
+ true
140
+ else
141
+ # some compilers still target 386 by default, but we need at least 486
142
+ # to run atomic builtins.
143
+ prev_cflags = $CFLAGS
144
+ $CFLAGS += " -march=i486 "
145
+ if try_link(src)
146
+ $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
147
+ true
148
+ else
149
+ $CFLAGS = prev_cflags
150
+ false
151
+ end
152
+ end
153
+ end or have_header('atomic_ops.h') or abort <<-SRC
154
+
155
+ libatomic_ops is required if GCC 4+ is not used.
156
+ See https://github.com/ivmai/libatomic_ops
157
+
158
+ Users of Debian-based distros may run:
159
+
160
+ apt-get install libatomic-ops-dev
161
+ SRC
162
+ create_header # generate extconf.h to avoid excessively long command-line
163
+ $CFLAGS.sub!(/ -O0 /, '')
164
+ create_makefile('raindrops_ext')
@@ -0,0 +1,444 @@
1
+ /* The MIT License
2
+
3
+ Copyright (c) 2019-2023 by Attractive Chaos <attractor@live.co.uk>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
24
+ */
25
+
26
+ #ifndef __AC_KHASHL_H
27
+ #define __AC_KHASHL_H
28
+
29
+ #define AC_VERSION_KHASHL_H "0.2"
30
+
31
+ #include <stdlib.h>
32
+ #include <string.h>
33
+ #include <limits.h>
34
+
35
+ /************************************
36
+ * Compiler specific configurations *
37
+ ************************************/
38
+
39
+ #if UINT_MAX == 0xffffffffu
40
+ typedef unsigned int khint32_t;
41
+ #elif ULONG_MAX == 0xffffffffu
42
+ typedef unsigned long khint32_t;
43
+ #endif
44
+
45
+ #if ULONG_MAX == ULLONG_MAX
46
+ typedef unsigned long khint64_t;
47
+ #else
48
+ typedef unsigned long long khint64_t;
49
+ #endif
50
+
51
+ #ifndef kh_inline
52
+ #ifdef _MSC_VER
53
+ #define kh_inline __inline
54
+ #else
55
+ #define kh_inline inline
56
+ #endif
57
+ #endif /* kh_inline */
58
+
59
+ #ifndef klib_unused
60
+ #if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
61
+ #define klib_unused __attribute__ ((__unused__))
62
+ #else
63
+ #define klib_unused
64
+ #endif
65
+ #endif /* klib_unused */
66
+
67
+ #define KH_LOCAL static kh_inline klib_unused
68
+
69
+ typedef khint32_t khint_t;
70
+
71
+ /******************
72
+ * malloc aliases *
73
+ ******************/
74
+
75
+ #ifndef kcalloc
76
+ #define kcalloc(N,Z) calloc(N,Z)
77
+ #endif
78
+ #ifndef kmalloc
79
+ #define kmalloc(Z) malloc(Z)
80
+ #endif
81
+ #ifndef krealloc
82
+ #define krealloc(P,Z) realloc(P,Z)
83
+ #endif
84
+ #ifndef kfree
85
+ #define kfree(P) free(P)
86
+ #endif
87
+
88
+ /****************************
89
+ * Simple private functions *
90
+ ****************************/
91
+
92
+ #define __kh_used(flag, i) (flag[i>>5] >> (i&0x1fU) & 1U)
93
+ #define __kh_set_used(flag, i) (flag[i>>5] |= 1U<<(i&0x1fU))
94
+ #define __kh_set_unused(flag, i) (flag[i>>5] &= ~(1U<<(i&0x1fU)))
95
+
96
+ #define __kh_fsize(m) ((m) < 32? 1 : (m)>>5)
97
+
98
+ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 2654435769U >> (32 - bits); }
99
+
100
+ /*******************
101
+ * Hash table base *
102
+ *******************/
103
+
104
+ #define __KHASHL_TYPE(HType, khkey_t) \
105
+ typedef struct HType { \
106
+ khint_t bits, count; \
107
+ khint32_t *used; \
108
+ khkey_t *keys; \
109
+ } HType;
110
+
111
+ #define __KHASHL_PROTOTYPES(HType, prefix, khkey_t) \
112
+ extern HType *prefix##_init(void); \
113
+ extern void prefix##_destroy(HType *h); \
114
+ extern void prefix##_clear(HType *h); \
115
+ extern khint_t prefix##_getp(const HType *h, const khkey_t *key); \
116
+ extern int prefix##_resize(HType *h, khint_t new_n_buckets); \
117
+ extern khint_t prefix##_putp(HType *h, const khkey_t *key, int *absent); \
118
+ extern void prefix##_del(HType *h, khint_t k);
119
+
120
+ #define __KHASHL_IMPL_BASIC(SCOPE, HType, prefix) \
121
+ SCOPE HType *prefix##_init(void) { \
122
+ return (HType*)kcalloc(1, sizeof(HType)); \
123
+ } \
124
+ SCOPE void prefix##_destroy(HType *h) { \
125
+ if (!h) return; \
126
+ kfree((void *)h->keys); kfree(h->used); \
127
+ kfree(h); \
128
+ } \
129
+ SCOPE void prefix##_clear(HType *h) { \
130
+ if (h && h->used) { \
131
+ khint_t n_buckets = (khint_t)1U << h->bits; \
132
+ memset(h->used, 0, __kh_fsize(n_buckets) * sizeof(khint32_t)); \
133
+ h->count = 0; \
134
+ } \
135
+ }
136
+
137
+ #define __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
138
+ SCOPE khint_t prefix##_getp_core(const HType *h, const khkey_t *key, khint_t hash) { \
139
+ khint_t i, last, n_buckets, mask; \
140
+ if (h->keys == 0) return 0; \
141
+ n_buckets = (khint_t)1U << h->bits; \
142
+ mask = n_buckets - 1U; \
143
+ i = last = __kh_h2b(hash, h->bits); \
144
+ while (__kh_used(h->used, i) && !__hash_eq(h->keys[i], *key)) { \
145
+ i = (i + 1U) & mask; \
146
+ if (i == last) return n_buckets; \
147
+ } \
148
+ return !__kh_used(h->used, i)? n_buckets : i; \
149
+ } \
150
+ SCOPE khint_t prefix##_getp(const HType *h, const khkey_t *key) { return prefix##_getp_core(h, key, __hash_fn(*key)); } \
151
+ SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { return prefix##_getp_core(h, &key, __hash_fn(key)); }
152
+
153
+ #define __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
154
+ SCOPE int prefix##_resize(HType *h, khint_t new_n_buckets) { \
155
+ khint32_t *new_used = 0; \
156
+ khint_t j = 0, x = new_n_buckets, n_buckets, new_bits, new_mask; \
157
+ while ((x >>= 1) != 0) ++j; \
158
+ if (new_n_buckets & (new_n_buckets - 1)) ++j; \
159
+ new_bits = j > 2? j : 2; \
160
+ new_n_buckets = (khint_t)1U << new_bits; \
161
+ if (h->count > (new_n_buckets>>1) + (new_n_buckets>>2)) return 0; /* requested size is too small */ \
162
+ new_used = (khint32_t*)kcalloc(__kh_fsize(new_n_buckets), \
163
+ sizeof(khint32_t)); \
164
+ if (!new_used) return -1; /* not enough memory */ \
165
+ n_buckets = h->keys? (khint_t)1U<<h->bits : 0U; \
166
+ if (n_buckets < new_n_buckets) { /* expand */ \
167
+ h->keys = ruby_xrealloc2(h->keys, new_n_buckets, \
168
+ sizeof(khkey_t)); \
169
+ } /* otherwise shrink */ \
170
+ new_mask = new_n_buckets - 1; \
171
+ for (j = 0; j != n_buckets; ++j) { \
172
+ khkey_t key; \
173
+ if (!__kh_used(h->used, j)) continue; \
174
+ key = h->keys[j]; \
175
+ __kh_set_unused(h->used, j); \
176
+ while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
177
+ khint_t i; \
178
+ i = __kh_h2b(__hash_fn(key), new_bits); \
179
+ while (__kh_used(new_used, i)) i = (i + 1) & new_mask; \
180
+ __kh_set_used(new_used, i); \
181
+ if (i < n_buckets && __kh_used(h->used, i)) { /* kick out the existing element */ \
182
+ { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
183
+ __kh_set_unused(h->used, i); /* mark it as deleted in the old hash table */ \
184
+ } else { /* write the element and jump out of the loop */ \
185
+ h->keys[i] = key; \
186
+ break; \
187
+ } \
188
+ } \
189
+ } \
190
+ if (n_buckets > new_n_buckets) /* shrink the hash table */ \
191
+ h->keys = ruby_xrealloc2(h->keys, new_n_buckets, \
192
+ sizeof(khkey_t)); \
193
+ kfree(h->used); /* free the working space */ \
194
+ h->used = new_used, h->bits = new_bits; \
195
+ return 0; \
196
+ }
197
+
198
+ #define __KHASHL_IMPL_PUT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
199
+ SCOPE khint_t prefix##_putp_core(HType *h, const khkey_t *key, khint_t hash, int *absent) { \
200
+ khint_t n_buckets, i, last, mask; \
201
+ n_buckets = h->keys? (khint_t)1U<<h->bits : 0U; \
202
+ *absent = -1; \
203
+ if (h->count >= (n_buckets>>1) + (n_buckets>>2)) { /* rehashing */ \
204
+ if (prefix##_resize(h, n_buckets + 1U) < 0) \
205
+ return n_buckets; \
206
+ n_buckets = (khint_t)1U<<h->bits; \
207
+ } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
208
+ mask = n_buckets - 1; \
209
+ i = last = __kh_h2b(hash, h->bits); \
210
+ while (__kh_used(h->used, i) && !__hash_eq(h->keys[i], *key)) { \
211
+ i = (i + 1U) & mask; \
212
+ if (i == last) break; \
213
+ } \
214
+ if (!__kh_used(h->used, i)) { /* not present at all */ \
215
+ h->keys[i] = *key; \
216
+ __kh_set_used(h->used, i); \
217
+ ++h->count; \
218
+ *absent = 1; \
219
+ } else *absent = 0; /* Don't touch h->keys[i] if present */ \
220
+ return i; \
221
+ } \
222
+ SCOPE khint_t prefix##_putp(HType *h, const khkey_t *key, int *absent) { return prefix##_putp_core(h, key, __hash_fn(*key), absent); } \
223
+ SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { return prefix##_putp_core(h, &key, __hash_fn(key), absent); }
224
+
225
+ #define __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn) \
226
+ SCOPE int prefix##_del(HType *h, khint_t i) { \
227
+ khint_t j = i, k, mask, n_buckets; \
228
+ if (h->keys == 0) return 0; \
229
+ n_buckets = (khint_t)1U<<h->bits; \
230
+ mask = n_buckets - 1U; \
231
+ while (1) { \
232
+ j = (j + 1U) & mask; \
233
+ if (j == i || !__kh_used(h->used, j)) break; /* j==i only when the table is completely full */ \
234
+ k = __kh_h2b(__hash_fn(h->keys[j]), h->bits); \
235
+ if ((j > i && (k <= i || k > j)) || (j < i && (k <= i && k > j))) \
236
+ h->keys[i] = h->keys[j], i = j; \
237
+ } \
238
+ __kh_set_unused(h->used, i); \
239
+ --h->count; \
240
+ return 1; \
241
+ }
242
+
243
+ #define KHASHL_DECLARE(HType, prefix, khkey_t) \
244
+ __KHASHL_TYPE(HType, khkey_t) \
245
+ __KHASHL_PROTOTYPES(HType, prefix, khkey_t)
246
+
247
+ #define KHASHL_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
248
+ __KHASHL_TYPE(HType, khkey_t) \
249
+ __KHASHL_IMPL_BASIC(SCOPE, HType, prefix) \
250
+ __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
251
+ __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
252
+ __KHASHL_IMPL_PUT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
253
+ __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn)
254
+
255
+ /***************************
256
+ * Ensemble of hash tables *
257
+ ***************************/
258
+
259
+ typedef struct {
260
+ khint_t sub, pos;
261
+ } kh_ensitr_t;
262
+
263
+ #define KHASHE_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
264
+ KHASHL_INIT(KH_LOCAL, HType##_sub, prefix##_sub, khkey_t, __hash_fn, __hash_eq) \
265
+ typedef struct HType { \
266
+ khint64_t count:54, bits:8; \
267
+ HType##_sub *sub; \
268
+ } HType; \
269
+ SCOPE HType *prefix##_init(int bits) { \
270
+ HType *g; \
271
+ g = (HType*)kcalloc(1, sizeof(*g)); \
272
+ g->bits = bits; \
273
+ g->sub = (HType##_sub*)kcalloc(1U<<bits, sizeof(*g->sub)); \
274
+ return g; \
275
+ } \
276
+ SCOPE void prefix##_destroy(HType *g) { \
277
+ int t; \
278
+ if (!g) return; \
279
+ for (t = 0; t < 1<<g->bits; ++t) { kfree((void*)g->sub[t].keys); kfree(g->sub[t].used); } \
280
+ kfree(g->sub); kfree(g); \
281
+ } \
282
+ SCOPE kh_ensitr_t prefix##_getp(const HType *g, const khkey_t *key) { \
283
+ khint_t hash, low, ret; \
284
+ kh_ensitr_t r; \
285
+ HType##_sub *h; \
286
+ hash = __hash_fn(*key); \
287
+ low = hash & ((1U<<g->bits) - 1); \
288
+ h = &g->sub[low]; \
289
+ ret = prefix##_sub_getp_core(h, key, hash); \
290
+ if (ret == 1U<<h->bits) r.sub = low, r.pos = (khint_t)-1; \
291
+ else r.sub = low, r.pos = ret; \
292
+ return r; \
293
+ } \
294
+ SCOPE kh_ensitr_t prefix##_get(const HType *g, const khkey_t key) { return prefix##_getp(g, &key); } \
295
+ SCOPE kh_ensitr_t prefix##_putp(HType *g, const khkey_t *key, int *absent) { \
296
+ khint_t hash, low, ret; \
297
+ kh_ensitr_t r; \
298
+ HType##_sub *h; \
299
+ hash = __hash_fn(*key); \
300
+ low = hash & ((1U<<g->bits) - 1); \
301
+ h = &g->sub[low]; \
302
+ ret = prefix##_sub_putp_core(h, key, hash, absent); \
303
+ if (*absent) ++g->count; \
304
+ if (ret == 1U<<h->bits) r.sub = low, r.pos = (khint_t)-1; \
305
+ else r.sub = low, r.pos = ret; \
306
+ return r; \
307
+ } \
308
+ SCOPE kh_ensitr_t prefix##_put(HType *g, const khkey_t key, int *absent) { return prefix##_putp(g, &key, absent); } \
309
+ SCOPE int prefix##_del(HType *g, kh_ensitr_t itr) { \
310
+ HType##_sub *h = &g->sub[itr.sub]; \
311
+ int ret; \
312
+ ret = prefix##_sub_del(h, itr.pos); \
313
+ if (ret) --g->count; \
314
+ return ret; \
315
+ }
316
+
317
+ /*****************************
318
+ * More convenient interface *
319
+ *****************************/
320
+
321
+ #define __kh_packed __attribute__ ((__packed__))
322
+ #define __kh_cached_hash(x) ((x).hash)
323
+
324
+ #define KHASHL_SET_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
325
+ typedef struct { khkey_t key; } __kh_packed HType##_s_bucket_t; \
326
+ static kh_inline khint_t prefix##_s_hash(HType##_s_bucket_t x) { return __hash_fn(x.key); } \
327
+ static kh_inline int prefix##_s_eq(HType##_s_bucket_t x, HType##_s_bucket_t y) { return __hash_eq(x.key, y.key); } \
328
+ KHASHL_INIT(KH_LOCAL, HType, prefix##_s, HType##_s_bucket_t, prefix##_s_hash, prefix##_s_eq) \
329
+ SCOPE HType *prefix##_init(void) { return prefix##_s_init(); } \
330
+ SCOPE void prefix##_destroy(HType *h) { prefix##_s_destroy(h); } \
331
+ SCOPE void prefix##_resize(HType *h, khint_t new_n_buckets) { prefix##_s_resize(h, new_n_buckets); } \
332
+ SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_s_bucket_t t; t.key = key; return prefix##_s_getp(h, &t); } \
333
+ SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_s_del(h, k); } \
334
+ SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_s_bucket_t t; t.key = key; return prefix##_s_putp(h, &t, absent); }
335
+
336
+ #define KHASHL_MAP_INIT(SCOPE, HType, prefix, khkey_t, kh_val_t, __hash_fn, __hash_eq) \
337
+ typedef struct { khkey_t key; kh_val_t val; } __kh_packed HType##_m_bucket_t; \
338
+ static kh_inline khint_t prefix##_m_hash(HType##_m_bucket_t x) { return __hash_fn(x.key); } \
339
+ static kh_inline int prefix##_m_eq(HType##_m_bucket_t x, HType##_m_bucket_t y) { return __hash_eq(x.key, y.key); } \
340
+ KHASHL_INIT(KH_LOCAL, HType, prefix##_m, HType##_m_bucket_t, prefix##_m_hash, prefix##_m_eq) \
341
+ SCOPE HType *prefix##_init(void) { return prefix##_m_init(); } \
342
+ SCOPE void prefix##_destroy(HType *h) { prefix##_m_destroy(h); } \
343
+ SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_m_bucket_t t; t.key = key; return prefix##_m_getp(h, &t); } \
344
+ SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_m_del(h, k); } \
345
+ SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_m_bucket_t t; t.key = key; return prefix##_m_putp(h, &t, absent); }
346
+
347
+ #define KHASHL_CSET_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
348
+ typedef struct { khkey_t key; khint_t hash; } __kh_packed HType##_cs_bucket_t; \
349
+ static kh_inline int prefix##_cs_eq(HType##_cs_bucket_t x, HType##_cs_bucket_t y) { return x.hash == y.hash && __hash_eq(x.key, y.key); } \
350
+ KHASHL_INIT(KH_LOCAL, HType, prefix##_cs, HType##_cs_bucket_t, __kh_cached_hash, prefix##_cs_eq) \
351
+ SCOPE HType *prefix##_init(void) { return prefix##_cs_init(); } \
352
+ SCOPE void prefix##_destroy(HType *h) { prefix##_cs_destroy(h); } \
353
+ SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_cs_bucket_t t; t.key = key; t.hash = __hash_fn(key); return prefix##_cs_getp(h, &t); } \
354
+ SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_cs_del(h, k); } \
355
+ SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_cs_bucket_t t; t.key = key, t.hash = __hash_fn(key); return prefix##_cs_putp(h, &t, absent); }
356
+
357
+ #define KHASHL_CMAP_INIT(SCOPE, HType, prefix, khkey_t, kh_val_t, __hash_fn, __hash_eq) \
358
+ typedef struct { khkey_t key; kh_val_t val; khint_t hash; } __kh_packed HType##_cm_bucket_t; \
359
+ static kh_inline int prefix##_cm_eq(HType##_cm_bucket_t x, HType##_cm_bucket_t y) { return x.hash == y.hash && __hash_eq(x.key, y.key); } \
360
+ KHASHL_INIT(KH_LOCAL, HType, prefix##_cm, HType##_cm_bucket_t, __kh_cached_hash, prefix##_cm_eq) \
361
+ SCOPE HType *prefix##_init(void) { return prefix##_cm_init(); } \
362
+ SCOPE void prefix##_destroy(HType *h) { prefix##_cm_destroy(h); } \
363
+ SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_cm_bucket_t t; t.key = key; t.hash = __hash_fn(key); return prefix##_cm_getp(h, &t); } \
364
+ SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_cm_del(h, k); } \
365
+ SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_cm_bucket_t t; t.key = key, t.hash = __hash_fn(key); return prefix##_cm_putp(h, &t, absent); }
366
+
367
+ #define KHASHE_MAP_INIT(SCOPE, HType, prefix, khkey_t, kh_val_t, __hash_fn, __hash_eq) \
368
+ typedef struct { khkey_t key; kh_val_t val; } __kh_packed HType##_m_bucket_t; \
369
+ static kh_inline khint_t prefix##_m_hash(HType##_m_bucket_t x) { return __hash_fn(x.key); } \
370
+ static kh_inline int prefix##_m_eq(HType##_m_bucket_t x, HType##_m_bucket_t y) { return __hash_eq(x.key, y.key); } \
371
+ KHASHE_INIT(KH_LOCAL, HType, prefix##_m, HType##_m_bucket_t, prefix##_m_hash, prefix##_m_eq) \
372
+ SCOPE HType *prefix##_init(int bits) { return prefix##_m_init(bits); } \
373
+ SCOPE void prefix##_destroy(HType *h) { prefix##_m_destroy(h); } \
374
+ SCOPE kh_ensitr_t prefix##_get(const HType *h, khkey_t key) { HType##_m_bucket_t t; t.key = key; return prefix##_m_getp(h, &t); } \
375
+ SCOPE int prefix##_del(HType *h, kh_ensitr_t k) { return prefix##_m_del(h, k); } \
376
+ SCOPE kh_ensitr_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_m_bucket_t t; t.key = key; return prefix##_m_putp(h, &t, absent); }
377
+
378
+ /**************************
379
+ * Public macro functions *
380
+ **************************/
381
+
382
+ #define kh_bucket(h, x) ((h)->keys[x])
383
+ #define kh_size(h) ((h)->count)
384
+ #define kh_capacity(h) ((h)->keys? 1U<<(h)->bits : 0U)
385
+ #define kh_end(h) kh_capacity(h)
386
+
387
+ #define kh_key(h, x) ((h)->keys[x].key)
388
+ #define kh_val(h, x) ((h)->keys[x].val)
389
+ #define kh_exist(h, x) __kh_used((h)->used, (x))
390
+
391
+ #define kh_ens_key(g, x) kh_key(&(g)->sub[(x).sub], (x).pos)
392
+ #define kh_ens_val(g, x) kh_val(&(g)->sub[(x).sub], (x).pos)
393
+ #define kh_ens_exist(g, x) kh_exist(&(g)->sub[(x).sub], (x).pos)
394
+ #define kh_ens_is_end(x) ((x).pos == (khint_t)-1)
395
+ #define kh_ens_size(g) ((g)->count)
396
+
397
+ /**************************************
398
+ * Common hash and equality functions *
399
+ **************************************/
400
+
401
+ #define kh_eq_generic(a, b) ((a) == (b))
402
+ #define kh_eq_str(a, b) (strcmp((a), (b)) == 0)
403
+ #define kh_hash_dummy(x) ((khint_t)(x))
404
+
405
+ static kh_inline khint_t kh_hash_uint32(khint_t key) {
406
+ key += ~(key << 15);
407
+ key ^= (key >> 10);
408
+ key += (key << 3);
409
+ key ^= (key >> 6);
410
+ key += ~(key << 11);
411
+ key ^= (key >> 16);
412
+ return key;
413
+ }
414
+
415
+ static kh_inline khint_t kh_hash_uint64(khint64_t key) {
416
+ key = ~key + (key << 21);
417
+ key = key ^ key >> 24;
418
+ key = (key + (key << 3)) + (key << 8);
419
+ key = key ^ key >> 14;
420
+ key = (key + (key << 2)) + (key << 4);
421
+ key = key ^ key >> 28;
422
+ key = key + (key << 31);
423
+ return (khint_t)key;
424
+ }
425
+
426
+ #define KH_FNV_SEED 11
427
+
428
+ static kh_inline khint_t kh_hash_str(const char *s) { /* FNV1a */
429
+ khint_t h = KH_FNV_SEED ^ 2166136261U;
430
+ const unsigned char *t = (const unsigned char*)s;
431
+ for (; *t; ++t)
432
+ h ^= *t, h *= 16777619;
433
+ return h;
434
+ }
435
+
436
+ static kh_inline khint_t kh_hash_bytes(int len, const unsigned char *s) {
437
+ khint_t h = KH_FNV_SEED ^ 2166136261U;
438
+ int i;
439
+ for (i = 0; i < len; ++i)
440
+ h ^= s[i], h *= 16777619;
441
+ return h;
442
+ }
443
+
444
+ #endif /* __AC_KHASHL_H */