106 #elif defined RUBY_EXPORT
107 #include "internal.h"
108 #include "internal/bits.h"
109 #include "internal/hash.h"
110 #include "internal/sanitizers.h"
111 #include "internal/st.h"
122 #define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
123 #define EXPECT(expr, val) __builtin_expect(expr, val)
124 #define ATTRIBUTE_UNUSED __attribute__((unused))
126 #define PREFETCH(addr, write_p)
127 #define EXPECT(expr, val) (expr)
128 #define ATTRIBUTE_UNUSED
132 typedef st_index_t st_hash_t;
140 #define type_numhash st_hashtype_num
146 static int st_strcmp(st_data_t, st_data_t);
147 static st_index_t strhash(st_data_t);
153 static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
154 static st_index_t strcasehash(st_data_t);
156 st_locale_insensitive_strcasecmp_i,
163 #define ST_INIT_VAL 0xafafafafafafafaf
164 #define ST_INIT_VAL_BYTE 0xafa
171 #define malloc ruby_xmalloc
172 #define calloc ruby_xcalloc
173 #define realloc ruby_xrealloc
174 #define free ruby_xfree
177 #define EQUAL(tab,x,y) ((x) == (y) || (*(tab)->type->compare)((x),(y)) == 0)
178 #define PTR_EQUAL(tab, ptr, hash_val, key_) \
179 ((ptr)->hash == (hash_val) && EQUAL((tab), (key_), (ptr)->key))
183 #define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
185 unsigned int _old_rebuilds_num = (tab)->rebuilds_num; \
186 res = PTR_EQUAL(tab, ptr, hash_val, key); \
187 rebuilt_p = _old_rebuilds_num != (tab)->rebuilds_num; \
193 unsigned char entry_power;
197 unsigned char bin_power;
199 unsigned char size_ind;
202 st_index_t bins_words;
206 #if SIZEOF_ST_INDEX_T == 8
207 #define MAX_POWER2 62
225 {16, 17, 2, 0x10000},
226 {17, 18, 2, 0x20000},
227 {18, 19, 2, 0x40000},
228 {19, 20, 2, 0x80000},
229 {20, 21, 2, 0x100000},
230 {21, 22, 2, 0x200000},
231 {22, 23, 2, 0x400000},
232 {23, 24, 2, 0x800000},
233 {24, 25, 2, 0x1000000},
234 {25, 26, 2, 0x2000000},
235 {26, 27, 2, 0x4000000},
236 {27, 28, 2, 0x8000000},
237 {28, 29, 2, 0x10000000},
238 {29, 30, 2, 0x20000000},
239 {30, 31, 2, 0x40000000},
240 {31, 32, 2, 0x80000000},
241 {32, 33, 3, 0x200000000},
242 {33, 34, 3, 0x400000000},
243 {34, 35, 3, 0x800000000},
244 {35, 36, 3, 0x1000000000},
245 {36, 37, 3, 0x2000000000},
246 {37, 38, 3, 0x4000000000},
247 {38, 39, 3, 0x8000000000},
248 {39, 40, 3, 0x10000000000},
249 {40, 41, 3, 0x20000000000},
250 {41, 42, 3, 0x40000000000},
251 {42, 43, 3, 0x80000000000},
252 {43, 44, 3, 0x100000000000},
253 {44, 45, 3, 0x200000000000},
254 {45, 46, 3, 0x400000000000},
255 {46, 47, 3, 0x800000000000},
256 {47, 48, 3, 0x1000000000000},
257 {48, 49, 3, 0x2000000000000},
258 {49, 50, 3, 0x4000000000000},
259 {50, 51, 3, 0x8000000000000},
260 {51, 52, 3, 0x10000000000000},
261 {52, 53, 3, 0x20000000000000},
262 {53, 54, 3, 0x40000000000000},
263 {54, 55, 3, 0x80000000000000},
264 {55, 56, 3, 0x100000000000000},
265 {56, 57, 3, 0x200000000000000},
266 {57, 58, 3, 0x400000000000000},
267 {58, 59, 3, 0x800000000000000},
268 {59, 60, 3, 0x1000000000000000},
269 {60, 61, 3, 0x2000000000000000},
270 {61, 62, 3, 0x4000000000000000},
271 {62, 63, 3, 0x8000000000000000},
275 #define MAX_POWER2 30
294 {16, 17, 2, 0x20000},
295 {17, 18, 2, 0x40000},
296 {18, 19, 2, 0x80000},
297 {19, 20, 2, 0x100000},
298 {20, 21, 2, 0x200000},
299 {21, 22, 2, 0x400000},
300 {22, 23, 2, 0x800000},
301 {23, 24, 2, 0x1000000},
302 {24, 25, 2, 0x2000000},
303 {25, 26, 2, 0x4000000},
304 {26, 27, 2, 0x8000000},
305 {27, 28, 2, 0x10000000},
306 {28, 29, 2, 0x20000000},
307 {29, 30, 2, 0x40000000},
308 {30, 31, 2, 0x80000000},
314 #define RESERVED_HASH_VAL (~(st_hash_t) 0)
315 #define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
318 static inline st_hash_t
319 do_hash(st_data_t key,
st_table *tab)
321 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
325 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
329 #define MINIMAL_POWER2 2
331 #if MINIMAL_POWER2 < 2
332 #error "MINIMAL_POWER2 should be >= 2"
337 #define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
341 get_power2(st_index_t size)
343 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
345 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
356 static inline st_index_t
357 get_bin(st_index_t *bins,
int s, st_index_t n)
359 return (s == 0 ? ((
unsigned char *) bins)[n]
360 : s == 1 ? ((
unsigned short *) bins)[n]
361 : s == 2 ? ((
unsigned int *) bins)[n]
362 : ((st_index_t *) bins)[n]);
368 set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
370 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
371 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
372 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
373 else ((st_index_t *) bins)[n] = v;
380 #define DELETED_BIN 1
386 #define MARK_BIN_EMPTY(tab, i) (set_bin((tab)->bins, get_size_ind(tab), i, EMPTY_BIN))
390 #define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
391 #define UNDEFINED_BIN_IND (~(st_index_t) 0)
395 #define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
396 #define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
401 #define MARK_BIN_DELETED(tab, i) \
403 set_bin((tab)->bins, get_size_ind(tab), i, DELETED_BIN); \
408 #define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
409 #define DELETED_BIN_P(b) ((b) == DELETED_BIN)
410 #define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
414 #define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
415 #define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
416 #define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
420 #define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
421 #define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
424 static inline unsigned int
427 return tab->size_ind;
431 static inline st_index_t
434 return ((st_index_t) 1)<<tab->bin_power;
438 static inline st_index_t
441 return get_bins_num(tab) - 1;
446 static inline st_index_t
447 hash_bin(st_hash_t hash_value,
st_table *tab)
449 return hash_value & bins_mask(tab);
453 static inline st_index_t
454 get_allocated_entries(
const st_table *tab)
456 return ((st_index_t) 1)<<tab->entry_power;
460 static inline st_index_t
463 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
470 memset(tab->bins, 0, bins_size(tab));
477 tab->num_entries = 0;
478 tab->entries_start = tab->entries_bound = 0;
479 if (tab->bins != NULL)
480 initialize_bins(tab);
488 int all, total, num, str, strcase;
493 static int init_st = 0;
500 char fname[10+
sizeof(long)*3];
502 if (!collision.total)
return;
503 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
506 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
507 ((
double)collision.all / (collision.total)) * 100);
508 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
521 const char *e = getenv(
"ST_HASH_LOG");
522 if (!e || !*e) init_st = 1;
531 n = get_power2(size);
538 tab->entry_power = n;
539 tab->bin_power = features[n].bin_power;
540 tab->size_ind = features[n].size_ind;
541 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
544 tab->bins = (st_index_t *) malloc(bins_size(tab));
546 if (tab->bins == NULL) {
552 tab->entries = (
st_table_entry *) malloc(get_allocated_entries(tab)
555 if (tab->entries == NULL) {
561 tab->rebuilds_num = 0;
578 st_init_existing_table_with_size(tab,
type, size);
580 if (st_init_existing_table_with_size(tab,
type, size) == NULL) {
590 st_table_size(
const struct st_table *tbl)
592 return tbl->num_entries;
600 return st_init_table_with_size(
type, 0);
606 st_init_numtable(
void)
608 return st_init_table(&type_numhash);
613 st_init_numtable_with_size(st_index_t size)
615 return st_init_table_with_size(&type_numhash, size);
621 st_init_strtable(
void)
623 return st_init_table(&type_strhash);
628 st_init_strtable_with_size(st_index_t size)
630 return st_init_table_with_size(&type_strhash, size);
636 st_init_strcasetable(
void)
638 return st_init_table(&type_strcasehash);
644 st_init_strcasetable_with_size(st_index_t size)
646 return st_init_table_with_size(&type_strcasehash, size);
671 + (tab->bins == NULL ? 0 : bins_size(tab))
676 find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
679 find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
682 find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
685 find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
686 st_data_t key, st_index_t *bin_ind);
693 if (
type == &type_numhash) {
696 else if (
type == &type_strhash) {
699 else if (
type == &type_strcasehash) {
704 #define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
705 #define FOUND_BIN (collision_check ? collision.total++ : (void)0)
706 #define collision_check 0
715 #define REBUILD_THRESHOLD 4
717 #if REBUILD_THRESHOLD < 2
718 #error "REBUILD_THRESHOLD should be >= 2"
721 static void rebuild_table_with(
st_table *
const new_tab,
st_table *
const tab);
722 static void rebuild_move_table(
st_table *
const new_tab,
st_table *
const tab);
723 static void rebuild_cleanup(
st_table *
const tab);
732 if ((2 * tab->num_entries <= get_allocated_entries(tab)
733 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
734 || tab->num_entries < (1 << MINIMAL_POWER2)) {
736 tab->num_entries = 0;
737 if (tab->bins != NULL)
738 initialize_bins(tab);
739 rebuild_table_with(tab, tab);
746 new_tab = st_init_table_with_size(tab->type,
747 2 * tab->num_entries - 1);
748 rebuild_table_with(new_tab, tab);
749 rebuild_move_table(new_tab, tab);
751 rebuild_cleanup(tab);
758 unsigned int size_ind;
764 new_entries = new_tab->entries;
767 bins = new_tab->bins;
768 size_ind = get_size_ind(new_tab);
769 st_index_t bound = tab->entries_bound;
772 for (i = tab->entries_start; i < bound; i++) {
773 curr_entry_ptr = &entries[i];
774 PREFETCH(entries + i + 1, 0);
775 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
777 if (&new_entries[ni] != curr_entry_ptr)
778 new_entries[ni] = *curr_entry_ptr;
779 if (EXPECT(bins != NULL, 1)) {
780 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
781 curr_entry_ptr->key);
782 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
784 new_tab->num_entries++;
792 tab->entry_power = new_tab->entry_power;
793 tab->bin_power = new_tab->bin_power;
794 tab->size_ind = new_tab->size_ind;
796 tab->bins = new_tab->bins;
798 tab->entries = new_tab->entries;
803 rebuild_cleanup(
st_table *
const tab)
805 tab->entries_start = 0;
806 tab->entries_bound = tab->num_entries;
822 static inline st_index_t
823 secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perturb)
826 ind = (ind << 2) + ind + *perturb + 1;
827 return hash_bin(ind, tab);
834 static inline st_index_t
835 find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
841 bound = tab->entries_bound;
842 entries = tab->entries;
843 for (i = tab->entries_start; i < bound; i++) {
844 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
845 if (EXPECT(rebuilt_p, 0))
846 return REBUILT_TABLE_ENTRY_IND;
850 return UNDEFINED_ENTRY_IND;
862 find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
866 #ifdef QUADRATIC_PROBE
874 ind = hash_bin(hash_value, tab);
875 #ifdef QUADRATIC_PROBE
878 perturb = hash_value;
882 bin = get_bin(tab->bins, get_size_ind(tab), ind);
883 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
884 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
885 if (EXPECT(rebuilt_p, 0))
886 return REBUILT_TABLE_ENTRY_IND;
890 else if (EMPTY_BIN_P(bin))
891 return UNDEFINED_ENTRY_IND;
892 #ifdef QUADRATIC_PROBE
893 ind = hash_bin(ind + d, tab);
896 ind = secondary_hash(ind, tab, &perturb);
908 find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
912 #ifdef QUADRATIC_PROBE
920 ind = hash_bin(hash_value, tab);
921 #ifdef QUADRATIC_PROBE
924 perturb = hash_value;
928 bin = get_bin(tab->bins, get_size_ind(tab), ind);
929 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
930 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
931 if (EXPECT(rebuilt_p, 0))
932 return REBUILT_TABLE_BIN_IND;
936 else if (EMPTY_BIN_P(bin))
937 return UNDEFINED_BIN_IND;
938 #ifdef QUADRATIC_PROBE
939 ind = hash_bin(ind + d, tab);
942 ind = secondary_hash(ind, tab, &perturb);
953 find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
956 #ifdef QUADRATIC_PROBE
963 ind = hash_bin(hash_value, tab);
964 #ifdef QUADRATIC_PROBE
967 perturb = hash_value;
971 bin = get_bin(tab->bins, get_size_ind(tab), ind);
972 if (EMPTY_OR_DELETED_BIN_P(bin))
974 #ifdef QUADRATIC_PROBE
975 ind = hash_bin(ind + d, tab);
978 ind = secondary_hash(ind, tab, &perturb);
994 find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
995 st_data_t key, st_index_t *bin_ind)
999 st_hash_t curr_hash_value = *hash_value;
1000 #ifdef QUADRATIC_PROBE
1005 st_index_t entry_index;
1006 st_index_t first_deleted_bin_ind;
1009 ind = hash_bin(curr_hash_value, tab);
1010 #ifdef QUADRATIC_PROBE
1013 perturb = curr_hash_value;
1016 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1017 entries = tab->entries;
1019 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
1020 if (EMPTY_BIN_P(entry_index)) {
1022 entry_index = UNDEFINED_ENTRY_IND;
1023 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1025 ind = first_deleted_bin_ind;
1026 MARK_BIN_EMPTY(tab, ind);
1030 else if (! DELETED_BIN_P(entry_index)) {
1031 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1032 if (EXPECT(rebuilt_p, 0))
1033 return REBUILT_TABLE_ENTRY_IND;
1037 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1038 first_deleted_bin_ind = ind;
1039 #ifdef QUADRATIC_PROBE
1040 ind = hash_bin(ind + d, tab);
1043 ind = secondary_hash(ind, tab, &perturb);
1054 st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1057 st_hash_t hash = do_hash(key, tab);
1060 if (tab->bins == NULL) {
1061 bin = find_entry(tab, hash, key);
1062 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1064 if (bin == UNDEFINED_ENTRY_IND)
1068 bin = find_table_entry_ind(tab, hash, key);
1069 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1071 if (bin == UNDEFINED_ENTRY_IND)
1076 *value = tab->entries[bin].record;
1083 st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1086 st_hash_t hash = do_hash(key, tab);
1089 if (tab->bins == NULL) {
1090 bin = find_entry(tab, hash, key);
1091 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1093 if (bin == UNDEFINED_ENTRY_IND)
1097 bin = find_table_entry_ind(tab, hash, key);
1098 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1100 if (bin == UNDEFINED_ENTRY_IND)
1105 *result = tab->entries[bin].key;
1111 rebuild_table_if_necessary (
st_table *tab)
1113 st_index_t bound = tab->entries_bound;
1115 if (bound == get_allocated_entries(tab))
1123 st_insert(
st_table *tab, st_data_t key, st_data_t value)
1128 st_hash_t hash_value;
1132 hash_value = do_hash(key, tab);
1134 rebuild_table_if_necessary(tab);
1135 if (tab->bins == NULL) {
1136 bin = find_entry(tab, hash_value, key);
1137 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1139 new_p = bin == UNDEFINED_ENTRY_IND;
1142 bin_ind = UNDEFINED_BIN_IND;
1145 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1147 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1149 new_p = bin == UNDEFINED_ENTRY_IND;
1153 ind = tab->entries_bound++;
1154 entry = &tab->entries[ind];
1155 entry->hash = hash_value;
1157 entry->record = value;
1158 if (bin_ind != UNDEFINED_BIN_IND)
1159 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1162 tab->entries[bin].record = value;
1169 st_add_direct_with_hash(
st_table *tab,
1170 st_data_t key, st_data_t value, st_hash_t hash)
1176 rebuild_table_if_necessary(tab);
1177 ind = tab->entries_bound++;
1178 entry = &tab->entries[ind];
1181 entry->record = value;
1183 if (tab->bins != NULL) {
1184 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1185 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1190 rb_st_add_direct_with_hash(
st_table *tab,
1191 st_data_t key, st_data_t value, st_hash_t hash)
1193 st_add_direct_with_hash(tab, key, value, hash);
1199 st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1201 st_hash_t hash_value;
1203 hash_value = do_hash(key, tab);
1204 st_add_direct_with_hash(tab, key, value, hash_value);
1211 st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1212 st_data_t (*func)(st_data_t))
1217 st_hash_t hash_value;
1221 hash_value = do_hash(key, tab);
1223 rebuild_table_if_necessary (tab);
1224 if (tab->bins == NULL) {
1225 bin = find_entry(tab, hash_value, key);
1226 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1228 new_p = bin == UNDEFINED_ENTRY_IND;
1231 bin_ind = UNDEFINED_BIN_IND;
1234 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1236 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1238 new_p = bin == UNDEFINED_ENTRY_IND;
1243 ind = tab->entries_bound++;
1244 entry = &tab->entries[ind];
1245 entry->hash = hash_value;
1247 entry->record = value;
1248 if (bin_ind != UNDEFINED_BIN_IND)
1249 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1252 tab->entries[bin].record = value;
1260 *new_tab = *old_tab;
1261 if (old_tab->bins == NULL)
1262 new_tab->bins = NULL;
1264 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1266 if (new_tab->bins == NULL) {
1271 new_tab->entries = (
st_table_entry *) malloc(get_allocated_entries(old_tab)
1274 if (new_tab->entries == NULL) {
1279 get_allocated_entries(old_tab));
1280 if (old_tab->bins != NULL)
1281 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1294 if (new_tab == NULL)
1298 if (st_replace(new_tab, old_tab) == NULL) {
1299 st_free_table(new_tab);
1309 update_range_for_deleted(
st_table *tab, st_index_t n)
1313 if (tab->entries_start == n) {
1314 st_index_t start = n + 1;
1315 st_index_t bound = tab->entries_bound;
1317 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1318 tab->entries_start = start;
1327 st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1334 hash = do_hash(*key, tab);
1336 if (tab->bins == NULL) {
1337 bin = find_entry(tab, hash, *key);
1338 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1340 if (bin == UNDEFINED_ENTRY_IND) {
1341 if (value != 0) *value = 0;
1346 bin_ind = find_table_bin_ind(tab, hash, *key);
1347 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1349 if (bin_ind == UNDEFINED_BIN_IND) {
1350 if (value != 0) *value = 0;
1353 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1354 MARK_BIN_DELETED(tab, bin_ind);
1356 entry = &tab->entries[bin];
1358 if (value != 0) *value = entry->record;
1359 MARK_ENTRY_DELETED(entry);
1361 update_range_for_deleted(tab, bin);
1366 st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1368 return st_general_delete(tab, key, value);
1377 st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1378 st_data_t never ATTRIBUTE_UNUSED)
1380 return st_general_delete(tab, key, value);
1388 st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1390 st_index_t i, bound;
1395 entries = tab->entries;
1396 bound = tab->entries_bound;
1397 for (i = tab->entries_start; i < bound; i++) {
1398 curr_entry_ptr = &entries[i];
1399 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1400 st_hash_t entry_hash = curr_entry_ptr->hash;
1401 st_data_t entry_key = curr_entry_ptr->key;
1403 if (value != 0) *value = curr_entry_ptr->record;
1406 if (tab->bins == NULL) {
1407 bin = find_entry(tab, entry_hash, entry_key);
1408 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1409 entries = tab->entries;
1412 curr_entry_ptr = &entries[bin];
1415 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1416 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1417 entries = tab->entries;
1420 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1422 MARK_BIN_DELETED(tab, bin_ind);
1424 MARK_ENTRY_DELETED(curr_entry_ptr);
1426 update_range_for_deleted(tab, i);
1430 if (value != 0) *value = 0;
1436 st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1437 st_data_t never ATTRIBUTE_UNUSED)
1451 st_update(
st_table *tab, st_data_t key,
1452 st_update_callback_func *func, st_data_t arg)
1458 st_data_t value = 0, old_key;
1459 int retval, existing;
1460 st_hash_t hash = do_hash(key, tab);
1463 entries = tab->entries;
1464 if (tab->bins == NULL) {
1465 bin = find_entry(tab, hash, key);
1466 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1468 existing = bin != UNDEFINED_ENTRY_IND;
1469 entry = &entries[bin];
1470 bin_ind = UNDEFINED_BIN_IND;
1473 bin_ind = find_table_bin_ind(tab, hash, key);
1474 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1476 existing = bin_ind != UNDEFINED_BIN_IND;
1478 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1479 entry = &entries[bin];
1484 value = entry->record;
1487 retval = (*func)(&key, &value, arg, existing);
1491 st_add_direct_with_hash(tab, key, value, hash);
1494 if (old_key != key) {
1497 entry->record = value;
1501 if (bin_ind != UNDEFINED_BIN_IND)
1502 MARK_BIN_DELETED(tab, bin_ind);
1503 MARK_ENTRY_DELETED(entry);
1505 update_range_for_deleted(tab, bin);
1521 st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1527 enum st_retval retval;
1528 st_index_t i, rebuilds_num;
1531 int error_p, packed_p = tab->bins == NULL;
1533 entries = tab->entries;
1536 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1537 curr_entry_ptr = &entries[i];
1538 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1540 key = curr_entry_ptr->key;
1541 rebuilds_num = tab->rebuilds_num;
1542 hash = curr_entry_ptr->hash;
1543 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1545 if (retval == ST_REPLACE && replace) {
1547 value = curr_entry_ptr->record;
1548 retval = (*replace)(&key, &value, arg, TRUE);
1549 curr_entry_ptr->key = key;
1550 curr_entry_ptr->record = value;
1553 if (rebuilds_num != tab->rebuilds_num) {
1555 entries = tab->entries;
1556 packed_p = tab->bins == NULL;
1558 i = find_entry(tab, hash, key);
1559 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1561 error_p = i == UNDEFINED_ENTRY_IND;
1564 i = find_table_entry_ind(tab, hash, key);
1565 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1567 error_p = i == UNDEFINED_ENTRY_IND;
1570 if (error_p && check_p) {
1572 retval = (*func)(0, 0, arg, 1);
1575 curr_entry_ptr = &entries[i];
1588 st_data_t key = curr_entry_ptr->key;
1592 bin = find_entry(tab, hash, key);
1593 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1595 if (bin == UNDEFINED_ENTRY_IND)
1599 bin_ind = find_table_bin_ind(tab, hash, key);
1600 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1602 if (bin_ind == UNDEFINED_BIN_IND)
1604 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1605 MARK_BIN_DELETED(tab, bin_ind);
1607 curr_entry_ptr = &entries[bin];
1608 MARK_ENTRY_DELETED(curr_entry_ptr);
1610 update_range_for_deleted(tab, bin);
1619 st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1621 return st_general_foreach(tab, func, replace, arg, TRUE);
1625 st_foreach_callback_func *func;
1630 apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1632 const struct functor *f = (
void *)d;
1633 return f->func(k, v, f->arg);
1639 const struct functor f = { func, arg };
1640 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1646 st_data_t never ATTRIBUTE_UNUSED)
1648 return st_general_foreach(tab, func, 0, arg, TRUE);
1653 static inline st_index_t
1654 st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1656 st_index_t i, bound;
1657 st_data_t key, *keys_start, *keys_end;
1660 bound = tab->entries_bound;
1662 keys_end = keys + size;
1663 for (i = tab->entries_start; i < bound; i++) {
1664 if (keys == keys_end)
1666 curr_entry_ptr = &entries[i];
1667 key = curr_entry_ptr->key;
1668 if (! DELETED_ENTRY_P(curr_entry_ptr))
1672 return keys - keys_start;
1676 st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1678 return st_general_keys(tab, keys, size);
1683 st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1684 st_data_t never ATTRIBUTE_UNUSED)
1686 return st_general_keys(tab, keys, size);
1691 static inline st_index_t
1692 st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1694 st_index_t i, bound;
1695 st_data_t *values_start, *values_end;
1698 values_start = values;
1699 values_end = values + size;
1700 bound = tab->entries_bound;
1701 for (i = tab->entries_start; i < bound; i++) {
1702 if (values == values_end)
1704 curr_entry_ptr = &entries[i];
1705 if (! DELETED_ENTRY_P(curr_entry_ptr))
1706 *values++ = curr_entry_ptr->record;
1709 return values - values_start;
1713 st_values(
st_table *tab, st_data_t *values, st_index_t size)
1715 return st_general_values(tab, values, size);
1720 st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1721 st_data_t never ATTRIBUTE_UNUSED)
1723 return st_general_values(tab, values, size);
1726 #define FNV1_32A_INIT 0x811c9dc5
1731 #define FNV_32_PRIME 0x01000193
1734 #ifndef UNALIGNED_WORD_ACCESS
1735 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1736 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1737 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1738 defined(__mc68020__)
1739 # define UNALIGNED_WORD_ACCESS 1
1742 #ifndef UNALIGNED_WORD_ACCESS
1743 # define UNALIGNED_WORD_ACCESS 0
1749 #define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1750 #define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1752 #if ST_INDEX_BITS <= 32
1753 #define C1 (st_index_t)0xcc9e2d51
1754 #define C2 (st_index_t)0x1b873593
1756 #define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1757 #define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1759 NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1760 NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1761 NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *
ptr,
size_t len, st_index_t h));
1763 static inline st_index_t
1764 murmur_step(st_index_t h, st_index_t k)
1766 #if ST_INDEX_BITS <= 32
1782 static inline st_index_t
1783 murmur_finish(st_index_t h)
1785 #if ST_INDEX_BITS <= 32
1789 const st_index_t c1 = 0x85ebca6b;
1790 const st_index_t c2 = 0xc2b2ae35;
1796 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1797 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1799 #if ST_INDEX_BITS > 64
1816 st_hash(
const void *
ptr,
size_t len, st_index_t h)
1818 const char *data =
ptr;
1822 #define data_at(n) (st_index_t)((unsigned char)data[(n)])
1823 #define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1824 #if SIZEOF_ST_INDEX_T > 4
1825 #define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1826 #if SIZEOF_ST_INDEX_T > 8
1827 #define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1828 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1829 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1831 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1833 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1836 if (
len >=
sizeof(st_index_t)) {
1837 #if !UNALIGNED_WORD_ACCESS
1838 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1844 #ifdef WORDS_BIGENDIAN
1845 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1846 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1848 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1849 t |= data_at(n) << CHAR_BIT*(n)
1852 #undef UNALIGNED_ADD
1855 #ifdef WORDS_BIGENDIAN
1856 t >>= (CHAR_BIT * align) - CHAR_BIT;
1858 t <<= (CHAR_BIT * align);
1861 data +=
sizeof(st_index_t)-align;
1862 len -=
sizeof(st_index_t)-align;
1864 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1865 sr = CHAR_BIT * align;
1867 while (
len >=
sizeof(st_index_t)) {
1868 d = *(st_index_t *)data;
1869 #ifdef WORDS_BIGENDIAN
1870 t = (t << sr) | (d >> sl);
1872 t = (t >> sr) | (d << sl);
1874 h = murmur_step(h, t);
1876 data +=
sizeof(st_index_t);
1877 len -=
sizeof(st_index_t);
1880 pack =
len < (size_t)align ? (
int)
len : align;
1883 #ifdef WORDS_BIGENDIAN
1884 # define UNALIGNED_ADD(n) case (n) + 1: \
1885 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1887 # define UNALIGNED_ADD(n) case (n) + 1: \
1888 d |= data_at(n) << CHAR_BIT*(n)
1891 #undef UNALIGNED_ADD
1893 #ifdef WORDS_BIGENDIAN
1894 t = (t << sr) | (d >> sl);
1896 t = (t >> sr) | (d << sl);
1899 if (
len < (
size_t)align)
goto skip_tail;
1900 # define SKIP_TAIL 1
1901 h = murmur_step(h, t);
1907 #ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1908 #define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1910 #define aligned_data data
1914 h = murmur_step(h, *(st_index_t *)aligned_data);
1915 data +=
sizeof(st_index_t);
1916 len -=
sizeof(st_index_t);
1917 }
while (
len >=
sizeof(st_index_t));
1923 #if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1925 #if SIZEOF_ST_INDEX_T > 4
1926 case 7: t |= data_at(6) << 48;
1927 case 6: t |= data_at(5) << 40;
1928 case 5: t |= data_at(4) << 32;
1930 t |= (st_index_t)*(uint32_t*)aligned_data;
1932 # define SKIP_TAIL 1
1934 case 3: t |= data_at(2) << 16;
1935 case 2: t |= data_at(1) << 8;
1936 case 1: t |= data_at(0);
1938 #ifdef WORDS_BIGENDIAN
1939 # define UNALIGNED_ADD(n) case (n) + 1: \
1940 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1942 # define UNALIGNED_ADD(n) case (n) + 1: \
1943 t |= data_at(n) << CHAR_BIT*(n)
1946 #undef UNALIGNED_ADD
1951 h ^= t; h -= ROTL(t, 7);
1957 return murmur_finish(h);
1961 st_hash_uint32(st_index_t h, uint32_t i)
1963 return murmur_step(h, i);
1966 NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
1968 st_hash_uint(st_index_t h, st_index_t i)
1973 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1974 h = murmur_step(h, i >> 8*8);
1976 h = murmur_step(h, i);
1981 st_hash_end(st_index_t h)
1983 h = murmur_finish(h);
1987 #undef st_hash_start
1989 rb_st_hash_start(st_index_t h)
1995 strhash(st_data_t arg)
1997 register const char *
string = (
const char *)arg;
1998 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2009 if (c1 ==
'\0' || c2 ==
'\0') {
2010 if (c1 !=
'\0')
return 1;
2011 if (c2 !=
'\0')
return -1;
2014 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2015 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2031 for (i = 0; i < n; i++) {
2034 if (c1 ==
'\0' || c2 ==
'\0') {
2035 if (c1 !=
'\0')
return 1;
2036 if (c2 !=
'\0')
return -1;
2039 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2040 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2052 st_strcmp(st_data_t lhs, st_data_t rhs)
2054 const char *s1 = (
char *)lhs;
2055 const char *s2 = (
char *)rhs;
2056 return strcmp(s1, s2);
2060 st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2062 const char *s1 = (
char *)lhs;
2063 const char *s2 = (
char *)rhs;
2067 NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2069 strcasehash(st_data_t arg)
2071 register const char *
string = (
const char *)arg;
2072 register st_index_t hval = FNV1_32A_INIT;
2078 unsigned int c = (
unsigned char)*
string++;
2079 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2083 hval *= FNV_32_PRIME;
2089 st_numcmp(st_data_t x, st_data_t y)
2095 st_numhash(st_data_t n)
2097 enum {s1 = 11, s2 = 3};
2098 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2106 st_expand_table(
st_table *tab, st_index_t siz)
2111 if (siz <= get_allocated_entries(tab))
2114 tmp = st_init_table_with_size(tab->type, siz);
2115 n = get_allocated_entries(tab);
2120 tab->entry_power = tmp->entry_power;
2121 tab->bin_power = tmp->bin_power;
2122 tab->size_ind = tmp->size_ind;
2123 tab->entries = tmp->entries;
2125 tab->rebuilds_num++;
2134 int eq_p, rebuilt_p;
2141 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2142 p = &tab->entries[i];
2143 if (DELETED_ENTRY_P(p))
2145 for (j = i + 1; j < tab->entries_bound; j++) {
2146 q = &tab->entries[j];
2147 if (DELETED_ENTRY_P(q))
2149 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2150 if (EXPECT(rebuilt_p, 0))
2154 MARK_ENTRY_DELETED(q);
2156 update_range_for_deleted(tab, j);
2168 int eq_p, rebuilt_p;
2170 st_index_t
const n = bins_size(tab);
2171 unsigned int const size_ind = get_size_ind(tab);
2172 st_index_t *bins = realloc(tab->bins, n);
2174 initialize_bins(tab);
2175 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2178 #ifdef QUADRATIC_PROBE
2181 st_index_t perturb = p->hash;
2184 if (DELETED_ENTRY_P(p))
2187 ind = hash_bin(p->hash, tab);
2189 st_index_t bin = get_bin(bins, size_ind, ind);
2190 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2192 set_bin(bins, size_ind, ind, i + ENTRY_BASE);
2197 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2198 if (EXPECT(rebuilt_p, 0))
2202 q->record = p->record;
2203 MARK_ENTRY_DELETED(p);
2205 update_range_for_deleted(tab, bin);
2210 #ifdef QUADRATIC_PROBE
2211 ind = hash_bin(ind + d, tab);
2214 ind = secondary_hash(ind, tab, &perturb);
2232 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2233 rebuilt_p = st_rehash_linear(tab);
2235 rebuilt_p = st_rehash_indexed(tab);
2236 }
while (rebuilt_p);
2240 st_stringify(
VALUE key)
2243 rb_hash_key_str(key) : key;
2249 st_data_t k = st_stringify(key);
2251 e.hash = do_hash(k, tab);
2255 tab->entries[tab->entries_bound++] = e;
2266 for (i = 0; i < argc; ) {
2267 st_data_t k = st_stringify(argv[i++]);
2268 st_data_t v = argv[i++];
2269 st_insert(tab, k, v);
2281 for (i = 0; i < argc; ) {
2282 VALUE key = argv[i++];
2283 VALUE val = argv[i++];
2284 st_insert_single(tab, hash, key, val);
2294 rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2296 st_index_t n, size = argc / 2;
2297 st_table *tab = RHASH_ST_TABLE(hash);
2299 tab = RHASH_TBL_RAW(hash);
2300 n = tab->entries_bound + size;
2301 st_expand_table(tab, n);
2302 if (UNLIKELY(tab->num_entries))
2303 st_insert_generic(tab, argc, argv, hash);
2305 st_insert_single(tab, hash, argv[0], argv[1]);
2306 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2307 st_insert_linear(tab, argc, argv, hash);
2309 st_insert_generic(tab, argc, argv, hash);
2314 rb_st_nth_key(
st_table *tab, st_index_t index)
2316 if (LIKELY(tab->entries_start == 0 &&
2317 tab->num_entries == tab->entries_bound &&
2318 index < tab->num_entries)) {
2319 return tab->entries[index].key;
2329 st_index_t num = tab->num_entries;
2330 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2332 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2333 rebuild_table_with(new_tab, tab);
2334 rebuild_move_table(new_tab, tab);
2335 rebuild_cleanup(tab);
int st_locale_insensitive_strcasecmp(const char *s1, const char *s2)
Our own locale-insensitive version of strcasecmp(3).
int st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n)
Our own locale-insensitive version of strcnasecmp(3).
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
#define Qundef
Old name of RUBY_Qundef.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cString
String class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
int st_foreach_check(st_table *q, int_type *w, st_data_t e, st_data_t)
Iteration over the given table.
#define _(args)
This was a transition path from K&R to ANSI.
uintptr_t VALUE
Type that represents a Ruby object.