14 #include "ruby/internal/config.h"
19 # ifdef HAVE_CRT_EXTERNS_H
20 # include <crt_externs.h>
22 # include "missing/crt_externs.h"
26 #include "debug_counter.h"
29 #include "internal/array.h"
30 #include "internal/bignum.h"
31 #include "internal/class.h"
32 #include "internal/cont.h"
33 #include "internal/error.h"
34 #include "internal/hash.h"
35 #include "internal/object.h"
36 #include "internal/proc.h"
37 #include "internal/symbol.h"
38 #include "internal/time.h"
39 #include "internal/vm.h"
43 #include "ruby_assert.h"
45 #include "transient_heap.h"
58 #define SET_DEFAULT(hash, ifnone) ( \
59 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
60 RHASH_SET_IFNONE(hash, ifnone))
62 #define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
64 #define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
67 copy_default(
struct RHash *hash,
const struct RHash *hash2)
69 hash->basic.
flags &= ~RHASH_PROC_DEFAULT;
70 hash->basic.
flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
95 static ID id_hash, id_default, id_flatten_bang;
96 static ID id_hash_iter_lev;
108 if (a == b)
return 0;
122 hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
124 if (recurse)
return INT2FIX(0);
128 static long rb_objid_hash(st_index_t index);
131 dbl_to_index(
double d)
133 union {
double d; st_index_t i;} u;
139 rb_dbl_long_hash(
double d)
142 if (d == 0.0) d = 0.0;
143 #if SIZEOF_INT == SIZEOF_VOIDP
146 return rb_objid_hash(dbl_to_index(d));
151 any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
163 hnum = RSYMBOL(a)->hashval;
170 hnum = rb_objid_hash((st_index_t)a);
176 hval = rb_big_hash(a);
183 hnum = other_func(a);
193 obj_any_hash(
VALUE obj)
195 VALUE hval = rb_check_funcall_basic_kw(obj, id_hash,
rb_mKernel, 0, 0, 0);
223 return any_hash(a, obj_any_hash);
229 return LONG2FIX(any_hash(obj, obj_any_hash));
238 static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
239 static const uint32_t prime2 = 0x830fcab9;
242 static inline uint64_t
243 mult_and_mix(uint64_t m1, uint64_t m2)
245 #if defined HAVE_UINT128_T
246 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
247 return (uint64_t) (r >> 64) ^ (uint64_t) r;
249 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
250 uint64_t lm1 = m1, lm2 = m2;
251 uint64_t v64_128 = hm1 * hm2;
252 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
253 uint64_t v1_32 = lm1 * lm2;
255 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
259 static inline uint64_t
260 key64_hash(uint64_t key, uint32_t seed)
262 return mult_and_mix(key + seed, prime1);
266 #define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
269 rb_objid_hash(st_index_t index)
271 return (
long)st_index_hash(index);
275 objid_hash(
VALUE obj)
279 object_id = rb_big_hash(object_id);
281 #if SIZEOF_LONG == SIZEOF_VOIDP
282 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
283 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
284 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
292 rb_obj_hash(
VALUE obj)
294 long hnum = any_hash(obj, objid_hash);
303 #define rb_ident_cmp st_numcmp
306 rb_ident_hash(st_data_t n)
319 return (st_index_t)st_index_hash((st_index_t)n);
322 #define identhash rb_hashtype_ident
328 typedef st_index_t st_hash_t;
340 #define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
342 #define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
343 #define RHASH_AR_CLEARED_HINT 0xff
356 rb_hash_ar_table_size(
void)
361 static inline st_hash_t
362 ar_do_hash(st_data_t key)
364 return (st_hash_t)rb_any_hash(key);
367 static inline ar_hint_t
368 ar_do_hash_hint(st_hash_t hash_value)
370 return (ar_hint_t)hash_value;
373 static inline ar_hint_t
374 ar_hint(
VALUE hash,
unsigned int index)
376 return RHASH(hash)->ar_hint.ary[index];
380 ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
382 RHASH(hash)->ar_hint.ary[index] = hint;
386 ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
388 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
392 ar_clear_entry(
VALUE hash,
unsigned int index)
396 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
400 ar_cleared_entry(
VALUE hash,
unsigned int index)
402 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
407 return pair->key ==
Qundef;
415 ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
420 ar_hint_set(hash, index, hash_value);
423 #define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
424 RHASH_AR_TABLE_SIZE_RAW(h))
426 #define RHASH_AR_TABLE_BOUND_RAW(h) \
427 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
428 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
430 #define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
431 RHASH_AR_TABLE_BOUND_RAW(h))
433 #define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
434 #define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
436 #define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
439 #define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
442 rb_hash_dump(
VALUE hash)
444 rb_obj_info_dump(hash);
446 if (RHASH_AR_TABLE_P(hash)) {
447 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
449 fprintf(stderr,
" size:%u bound:%u\n",
450 RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
452 for (i=0; i<bound; i++) {
455 if (!ar_cleared_entry(hash, i)) {
456 char b1[0x100], b2[0x100];
460 fprintf(stderr,
" %d key:%s val:%s hint:%02x\n", i,
461 rb_raw_obj_info(b1, 0x100, k),
462 rb_raw_obj_info(b2, 0x100, v),
467 fprintf(stderr,
" %d empty\n", i);
474 hash_verify_(
VALUE hash,
const char *file,
int line)
478 if (RHASH_AR_TABLE_P(hash)) {
479 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
481 for (i=0; i<bound; i++) {
483 if (!ar_cleared_entry(hash, i)) {
492 if (n != RHASH_AR_TABLE_SIZE(hash)) {
493 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
497 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
498 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
499 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
502 #if USE_TRANSIENT_HEAP
503 if (RHASH_TRANSIENT_P(hash)) {
504 volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key;
505 HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
506 HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
513 #define hash_verify(h) ((void)0)
517 RHASH_TABLE_NULL_P(
VALUE hash)
519 if (RHASH(hash)->as.ar == NULL) {
520 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
529 RHASH_TABLE_EMPTY_P(
VALUE hash)
535 rb_hash_ar_table_p(
VALUE hash)
538 HASH_ASSERT(RHASH(hash)->as.st != NULL);
547 rb_hash_ar_table(
VALUE hash)
549 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
550 return RHASH(hash)->as.ar;
554 rb_hash_st_table(
VALUE hash)
556 HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
557 return RHASH(hash)->as.st;
563 HASH_ASSERT(st != NULL);
565 RHASH(hash)->as.st = st;
571 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
572 HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
573 RHASH(hash)->as.ar = ar;
577 #define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
578 #define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
581 RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
583 HASH_ASSERT(RHASH_AR_TABLE_P(h));
584 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
586 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
587 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
591 RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
593 HASH_ASSERT(RHASH_AR_TABLE_P(h));
594 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
596 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
597 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
601 HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
603 HASH_ASSERT(RHASH_AR_TABLE_P(h));
605 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
610 #define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
613 RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
615 HASH_ASSERT(RHASH_AR_TABLE_P(h));
616 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
619 RHASH_AR_TABLE_SIZE_SET(h, new_size);
622 RHASH_AR_TABLE_SIZE_SET(h, 0);
623 RHASH_AR_TABLE_BOUND_SET(h, 0);
629 RHASH_AR_TABLE_CLEAR(
VALUE h)
631 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
632 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
634 hash_ar_table_set(h, NULL);
638 ar_alloc_table(
VALUE hash)
643 RHASH_SET_TRANSIENT_FLAG(hash);
646 RHASH_UNSET_TRANSIENT_FLAG(hash);
650 RHASH_AR_TABLE_SIZE_SET(hash, 0);
651 RHASH_AR_TABLE_BOUND_SET(hash, 0);
652 hash_ar_table_set(hash, tab);
657 NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
662 return rb_any_cmp(x, y) == 0;
666 ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
668 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
669 const ar_hint_t *hints = RHASH(hash)->ar_hint.ary;
673 for (i = 0; i < bound; i++) {
674 if (hints[i] == hint) {
676 if (ar_equal(key, pair->key)) {
677 RB_DEBUG_COUNTER_INC(artable_hint_hit);
683 static char fname[256];
686 if (pid != getpid()) {
687 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
688 if ((fp = fopen(fname,
"w")) == NULL)
rb_bug(
"fopen");
691 st_hash_t h1 = ar_do_hash(key);
692 st_hash_t h2 = ar_do_hash(pair->key);
694 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
696 " pair->key:%016lx %s\n",
697 h1 == h2, i, hints[i], hint,
698 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
700 RB_DEBUG_COUNTER_INC(artable_hint_miss);
704 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
705 return RHASH_AR_TABLE_MAX_BOUND;
709 ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
711 ar_hint_t hint = ar_do_hash_hint(hash_value);
712 return ar_find_entry_hint(hash, hint, key);
716 ar_free_and_clear_table(
VALUE hash)
718 ar_table *tab = RHASH_AR_TABLE(hash);
721 if (RHASH_TRANSIENT_P(hash)) {
722 RHASH_UNSET_TRANSIENT_FLAG(hash);
727 RHASH_AR_TABLE_CLEAR(hash);
729 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
730 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
731 HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
735 ar_try_convert_table(
VALUE hash)
737 if (!RHASH_AR_TABLE_P(hash))
return;
739 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
744 if (size < RHASH_AR_TABLE_MAX_SIZE) {
748 new_tab = st_init_table_with_size(&objhash, size * 2);
750 for (i = 0; i < RHASH_AR_TABLE_MAX_BOUND; i++) {
752 st_add_direct(new_tab, pair->key, pair->val);
754 ar_free_and_clear_table(hash);
755 RHASH_ST_TABLE_SET(hash, new_tab);
760 ar_force_convert_table(
VALUE hash,
const char *file,
int line)
764 if (RHASH_ST_TABLE_P(hash)) {
765 return RHASH_ST_TABLE(hash);
768 if (RHASH_AR_TABLE(hash)) {
769 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
771 #if defined(RHASH_CONVERT_TABLE_DEBUG) && RHASH_CONVERT_TABLE_DEBUG
772 rb_obj_info_dump(hash);
773 fprintf(stderr,
"force_convert: %s:%d\n", file, line);
774 RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
777 new_tab = st_init_table_with_size(&objhash, RHASH_AR_TABLE_SIZE(hash));
779 for (i = 0; i < bound; i++) {
780 if (ar_cleared_entry(hash, i))
continue;
783 st_add_direct(new_tab, pair->key, pair->val);
785 ar_free_and_clear_table(hash);
788 new_tab = st_init_table(&objhash);
790 RHASH_ST_TABLE_SET(hash, new_tab);
796 hash_ar_table(
VALUE hash)
798 if (RHASH_TABLE_NULL_P(hash)) {
799 ar_alloc_table(hash);
801 return RHASH_AR_TABLE(hash);
805 ar_compact_table(
VALUE hash)
807 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
808 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
817 for (i=0; i<bound; i++) {
818 if (ar_cleared_entry(hash, i)) {
820 for (; j<bound; j++) {
821 if (!ar_cleared_entry(hash, j)) {
823 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
824 ar_clear_entry(hash, j);
835 HASH_ASSERT(i<=bound);
837 RHASH_AR_TABLE_BOUND_SET(hash, size);
844 ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
846 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
848 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
852 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
853 bin = ar_compact_table(hash);
856 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
858 ar_set_entry(hash, bin, key, val, hash_value);
859 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
860 RHASH_AR_TABLE_SIZE_INC(hash);
866 ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
868 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
869 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
871 for (i = 0; i < bound; i++) {
872 if (ar_cleared_entry(hash, i))
continue;
875 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
886 VALUE key = pair->key;
887 VALUE val = pair->val;
888 retval = (*replace)(&key, &val, arg, TRUE);
897 ar_clear_entry(hash, i);
898 RHASH_AR_TABLE_SIZE_DEC(hash);
907 ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
909 return ar_general_foreach(hash, func, replace, arg);
913 st_foreach_callback_func *func;
918 apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
920 const struct functor *f = (
void *)d;
921 return f->func(k, v, f->arg);
925 ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
927 const struct functor f = { func, arg };
928 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
932 ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
935 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
936 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
937 enum st_retval retval;
942 for (i = 0; i < bound; i++) {
943 if (ar_cleared_entry(hash, i))
continue;
945 pair = RHASH_AR_TABLE_REF(hash, i);
947 hint = ar_hint(hash, i);
949 retval = (*func)(key, pair->val, arg, 0);
954 pair = RHASH_AR_TABLE_REF(hash, i);
955 if (pair->key == never)
break;
956 ret = ar_find_entry_hint(hash, hint, key);
957 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
958 retval = (*func)(0, 0, arg, 1);
968 if (!ar_cleared_entry(hash, i)) {
969 ar_clear_entry(hash, i);
970 RHASH_AR_TABLE_SIZE_DEC(hash);
981 ar_update(
VALUE hash, st_data_t key,
982 st_update_callback_func *func, st_data_t arg)
984 int retval, existing;
985 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
986 st_data_t value = 0, old_key;
987 st_hash_t hash_value = ar_do_hash(key);
989 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
994 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
995 bin = ar_find_entry(hash, hash_value, key);
996 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
1009 retval = (*func)(&key, &value, arg, existing);
1015 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1021 if (old_key != key) {
1029 ar_clear_entry(hash, bin);
1030 RHASH_AR_TABLE_SIZE_DEC(hash);
1038 ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1040 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1041 st_hash_t hash_value = ar_do_hash(key);
1043 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1048 hash_ar_table(hash);
1050 bin = ar_find_entry(hash, hash_value, key);
1051 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1052 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1055 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1056 bin = ar_compact_table(hash);
1057 hash_ar_table(hash);
1059 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1061 ar_set_entry(hash, bin, key, value, hash_value);
1062 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1063 RHASH_AR_TABLE_SIZE_INC(hash);
1067 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1073 ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1075 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1079 st_hash_t hash_value = ar_do_hash(key);
1080 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1082 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1084 unsigned bin = ar_find_entry(hash, hash_value, key);
1086 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1090 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1091 if (value != NULL) {
1092 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1100 ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1103 st_hash_t hash_value = ar_do_hash(*key);
1105 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1107 return st_delete(RHASH_ST_TABLE(hash), key, value);
1110 bin = ar_find_entry(hash, hash_value, *key);
1112 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1113 if (value != 0) *value = 0;
1121 ar_clear_entry(hash, bin);
1122 RHASH_AR_TABLE_SIZE_DEC(hash);
1128 ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1130 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1131 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1133 for (i = 0; i < bound; i++) {
1134 if (!ar_cleared_entry(hash, i)) {
1136 if (value != 0) *value = pair->val;
1138 ar_clear_entry(hash, i);
1139 RHASH_AR_TABLE_SIZE_DEC(hash);
1144 if (value != NULL) *value = 0;
1149 ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1151 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1152 st_data_t *keys_start = keys, *keys_end = keys + size;
1154 for (i = 0; i < bound; i++) {
1155 if (keys == keys_end) {
1159 if (!ar_cleared_entry(hash, i)) {
1160 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1165 return keys - keys_start;
1169 ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1171 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1172 st_data_t *values_start = values, *values_end = values + size;
1174 for (i = 0; i < bound; i++) {
1175 if (values == values_end) {
1179 if (!ar_cleared_entry(hash, i)) {
1180 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1185 return values - values_start;
1191 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1193 if (old_tab != NULL) {
1194 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1195 if (new_tab == NULL) {
1197 if (new_tab != NULL) {
1198 RHASH_SET_TRANSIENT_FLAG(hash1);
1201 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1205 *new_tab = *old_tab;
1206 RHASH(hash1)->ar_hint.word = RHASH(hash2)->ar_hint.word;
1207 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1208 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1209 hash_ar_table_set(hash1, new_tab);
1211 rb_gc_writebarrier_remember(hash1);
1215 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1216 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1218 if (RHASH_TRANSIENT_P(hash1)) {
1219 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1221 else if (RHASH_AR_TABLE(hash1)) {
1225 hash_ar_table_set(hash1, NULL);
1227 rb_gc_writebarrier_remember(hash1);
1233 ar_clear(
VALUE hash)
1235 if (RHASH_AR_TABLE(hash) != NULL) {
1236 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1237 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1240 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1241 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1245 #if USE_TRANSIENT_HEAP
1247 rb_hash_transient_heap_evacuate(
VALUE hash,
int promote)
1249 if (RHASH_TRANSIENT_P(hash)) {
1251 ar_table *old_tab = RHASH_AR_TABLE(hash);
1253 if (UNLIKELY(old_tab == NULL)) {
1256 HASH_ASSERT(old_tab != NULL);
1258 new_tab = rb_transient_heap_alloc(hash,
sizeof(
ar_table));
1259 if (new_tab == NULL) promote =
true;
1263 RHASH_UNSET_TRANSIENT_FLAG(hash);
1265 *new_tab = *old_tab;
1266 hash_ar_table_set(hash, new_tab);
1272 typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1276 st_foreach_func *func;
1281 foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1286 if (error)
return ST_STOP;
1287 status = (*arg->func)(key, value, arg->arg);
1288 if (status == ST_CONTINUE) {
1300 arg.func = (st_foreach_func *)func;
1311 rb_foreach_func *func;
1316 hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1321 if (error)
return ST_STOP;
1322 status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1337 hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1343 if (error)
return ST_STOP;
1344 tbl = RHASH_ST_TABLE(arg->hash);
1345 status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1346 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1361 iter_lev_in_ivar(
VALUE hash)
1368 void rb_ivar_set_internal(
VALUE obj,
ID id,
VALUE val);
1371 iter_lev_in_ivar_set(
VALUE hash,
int lev)
1373 rb_ivar_set_internal(hash, id_hash_iter_lev,
INT2FIX(lev));
1377 iter_lev_in_flags(
VALUE hash)
1379 unsigned int u = (
unsigned int)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1386 int lev = iter_lev_in_flags(hash);
1388 if (lev == RHASH_LEV_MAX) {
1389 return iter_lev_in_ivar(hash);
1397 hash_iter_lev_inc(
VALUE hash)
1399 int lev = iter_lev_in_flags(hash);
1400 if (lev == RHASH_LEV_MAX) {
1401 lev = iter_lev_in_ivar(hash);
1402 iter_lev_in_ivar_set(hash, lev+1);
1406 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1407 if (lev == RHASH_LEV_MAX) {
1408 iter_lev_in_ivar_set(hash, lev);
1414 hash_iter_lev_dec(
VALUE hash)
1416 int lev = iter_lev_in_flags(hash);
1417 if (lev == RHASH_LEV_MAX) {
1418 lev = iter_lev_in_ivar(hash);
1419 HASH_ASSERT(lev > 0);
1420 iter_lev_in_ivar_set(hash, lev-1);
1423 HASH_ASSERT(lev > 0);
1424 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((lev-1) << RHASH_LEV_SHIFT));
1429 hash_foreach_ensure_rollback(
VALUE hash)
1431 hash_iter_lev_inc(hash);
1436 hash_foreach_ensure(
VALUE hash)
1438 hash_iter_lev_dec(hash);
1443 rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1445 if (RHASH_AR_TABLE_P(hash)) {
1446 return ar_foreach(hash, func, arg);
1449 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1454 rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1456 if (RHASH_AR_TABLE_P(hash)) {
1457 return ar_foreach_with_replace(hash, func, replace, arg);
1460 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1465 hash_foreach_call(
VALUE arg)
1469 if (RHASH_AR_TABLE_P(hash)) {
1470 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1471 (st_data_t)arg, (st_data_t)
Qundef);
1473 else if (RHASH_ST_TABLE_P(hash)) {
1475 (st_data_t)arg, (st_data_t)
Qundef);
1488 if (RHASH_TABLE_EMPTY_P(hash))
1491 arg.func = (rb_foreach_func *)func;
1494 hash_foreach_call((
VALUE)&arg);
1497 hash_iter_lev_inc(hash);
1498 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1515 hash_alloc(
VALUE klass)
1517 return hash_alloc_flags(klass, 0,
Qnil);
1521 empty_hash_alloc(
VALUE klass)
1523 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1525 return hash_alloc(klass);
1537 if (rb_hash_compare_by_id_p(basis)) {
1538 return rb_hash_compare_by_id(hash);
1543 MJIT_FUNC_EXPORTED
VALUE
1544 rb_hash_new_with_size(st_index_t size)
1550 else if (size <= RHASH_AR_TABLE_MAX_SIZE) {
1551 ar_alloc_table(ret);
1554 RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
1563 if (RHASH_AR_TABLE_P(hash))
1565 else if (RHASH_ST_TABLE_P(hash))
1566 RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
1572 hash_dup_with_compare_by_id(
VALUE hash)
1574 return hash_copy(copy_compare_by_id(
rb_hash_new(), hash), hash);
1580 return hash_copy(hash_alloc_flags(klass, flags,
RHASH_IFNONE(hash)),
1589 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1595 MJIT_FUNC_EXPORTED
VALUE
1596 rb_hash_resurrect(
VALUE hash)
1603 rb_hash_modify_check(
VALUE hash)
1608 MJIT_FUNC_EXPORTED
struct st_table *
1609 rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1611 return ar_force_convert_table(hash, file, line);
1618 return rb_hash_tbl_raw(hash, file, line);
1622 rb_hash_modify(
VALUE hash)
1624 rb_hash_modify_check(hash);
1627 NORETURN(
static void no_new_key(
void));
1639 #define NOINSERT_UPDATE_CALLBACK(func) \
1641 func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1643 if (!existing) no_new_key(); \
1644 return func(key, val, (struct update_arg *)arg, existing); \
1648 func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1650 return func(key, val, (struct update_arg *)arg, existing); \
1655 st_update_callback_func *func;
1661 typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1664 rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1666 if (RHASH_AR_TABLE_P(hash)) {
1667 int result = ar_update(hash, key, func, arg);
1669 ar_try_convert_table(hash);
1676 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1680 tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1683 st_data_t old_key = *key;
1684 st_data_t old_value = *val;
1685 VALUE hash = p->hash;
1686 int ret = (p->func)(key, val, arg, existing);
1691 if (!existing || *key != old_key || *val != old_value) {
1692 rb_hash_modify(hash);
1699 rb_hash_modify(hash);
1707 tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1710 .arg = optional_arg,
1714 .value = (
VALUE)optional_arg,
1717 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1726 #define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
1728 #define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
1729 tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
1732 #define RHASH_UPDATE(hash, key, func, arg) \
1733 RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
1741 if (n != 2 && (n >= 0 || n < -3)) {
1742 if (n < 0) n = -n-1;
1783 rb_hash_initialize(
int argc,
VALUE *argv,
VALUE hash)
1787 rb_hash_modify(hash);
1791 SET_PROC_DEFAULT(hash, ifnone);
1795 ifnone = argc == 0 ?
Qnil : argv[0];
1838 rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1843 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1845 hash = hash_alloc(klass);
1846 hash_copy(hash, tmp);
1854 hash = hash_alloc(klass);
1862 rb_builtin_class_name(e), i);
1878 if (argc % 2 != 0) {
1882 hash = hash_alloc(klass);
1888 MJIT_FUNC_EXPORTED
VALUE
1889 rb_to_hash_type(
VALUE hash)
1891 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1893 #define to_hash rb_to_hash_type
1898 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1915 rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1936 rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1939 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1958 rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
1962 RHASH(hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1974 if (RHASH_AR_TABLE_P(arg)) {
1975 ar_insert(arg, (st_data_t)key, (st_data_t)value);
1978 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
1996 rb_hash_rehash(
VALUE hash)
2004 rb_hash_modify_check(hash);
2005 if (RHASH_AR_TABLE_P(hash)) {
2006 tmp = hash_alloc(0);
2007 ar_alloc_table(tmp);
2009 ar_free_and_clear_table(hash);
2011 ar_free_and_clear_table(tmp);
2013 else if (RHASH_ST_TABLE_P(hash)) {
2014 st_table *old_tab = RHASH_ST_TABLE(hash);
2015 tmp = hash_alloc(0);
2016 tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
2017 RHASH_ST_TABLE_SET(tmp, tbl);
2019 st_free_table(old_tab);
2020 RHASH_ST_TABLE_SET(hash, tbl);
2021 RHASH_ST_CLEAR(tmp);
2030 VALUE args[2] = {hash, key};
2039 if (!
FL_TEST(hash, RHASH_PROC_DEFAULT))
return ifnone;
2041 return call_default_proc(ifnone, hash, key);
2049 hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2053 if (RHASH_AR_TABLE_P(hash)) {
2054 return ar_lookup(hash, key, pval);
2057 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2061 MJIT_FUNC_EXPORTED
int
2062 rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2064 return hash_stlike_lookup(hash, key, pval);
2086 if (hash_stlike_lookup(hash, key, &val)) {
2090 return rb_hash_default_value(hash, key);
2099 if (hash_stlike_lookup(hash, key, &val)) {
2137 rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2147 if (block_given && argc == 2) {
2148 rb_warn(
"block supersedes default value argument");
2151 if (hash_stlike_lookup(hash, key, &val)) {
2158 else if (argc == 1) {
2164 rb_key_err_raise(
rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2175 return rb_hash_fetch_m(1, &key, hash);
2199 rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2205 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2206 if (argc == 0)
return Qnil;
2207 return call_default_proc(ifnone, hash, argv[0]);
2226 rb_hash_set_default(
VALUE hash,
VALUE ifnone)
2228 rb_hash_modify_check(hash);
2229 SET_DEFAULT(hash, ifnone);
2246 rb_hash_default_proc(
VALUE hash)
2248 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2269 rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2273 rb_hash_modify_check(hash);
2275 SET_DEFAULT(hash, proc);
2278 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2281 "wrong default_proc type %s (expected Proc)",
2285 SET_PROC_DEFAULT(hash, proc);
2328 rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2330 if (RHASH_AR_TABLE_P(hash)) {
2331 return ar_delete(hash, pkey, pval);
2334 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2346 st_data_t ktmp = (st_data_t)key, val;
2348 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2364 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2366 if (deleted_value !=
Qundef) {
2367 return deleted_value;
2406 rb_hash_modify_check(hash);
2407 val = rb_hash_delete_entry(hash, key);
2453 rb_hash_shift(
VALUE hash)
2457 rb_hash_modify_check(hash);
2458 if (RHASH_AR_TABLE_P(hash)) {
2461 if (ar_shift(hash, &var.key, &var.val)) {
2468 rb_hash_delete_entry(hash, var.key);
2473 if (RHASH_ST_TABLE_P(hash)) {
2476 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2483 rb_hash_delete_entry(hash, var.key);
2488 return rb_hash_default_value(hash,
Qnil);
2495 rb_hash_modify(hash);
2528 rb_hash_modify_check(hash);
2529 if (!RHASH_TABLE_EMPTY_P(hash)) {
2554 rb_hash_reject_bang(
VALUE hash)
2559 rb_hash_modify(hash);
2561 if (!n)
return Qnil;
2586 rb_hash_reject(
VALUE hash)
2591 result = hash_dup_with_compare_by_id(hash);
2610 rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2613 VALUE key, value, result;
2618 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2620 for (i = 0; i < argc; i++) {
2642 rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2647 result = hash_dup_with_compare_by_id(hash);
2649 for (i = 0; i < argc; i++) {
2671 rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2676 for (i=0; i<argc; i++) {
2703 rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2708 for (i=0; i<argc; i++) {
2718 rb_hash_modify(hash);
2742 rb_hash_select(
VALUE hash)
2747 result = hash_dup_with_compare_by_id(hash);
2774 rb_hash_select_bang(
VALUE hash)
2779 rb_hash_modify_check(hash);
2781 if (!n)
return Qnil;
2805 rb_hash_keep_if(
VALUE hash)
2808 rb_hash_modify_check(hash);
2809 if (!RHASH_TABLE_EMPTY_P(hash)) {
2831 rb_hash_modify_check(hash);
2836 else if (RHASH_AR_TABLE_P(hash)) {
2840 st_clear(RHASH_ST_TABLE(hash));
2847 hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2854 rb_hash_key_str(
VALUE key)
2857 return rb_fstring(key);
2865 hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2868 *key = rb_hash_key_str(*key);
2870 return hash_aset(key, val, arg, existing);
2873 NOINSERT_UPDATE_CALLBACK(hash_aset)
2874 NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2907 rb_hash_modify(hash);
2909 if (RHASH_TABLE_NULL_P(hash)) {
2910 if (iter_lev > 0) no_new_key();
2911 ar_alloc_table(hash);
2915 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
2918 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
2936 rb_hash_modify_check(hash);
2937 if (hash == hash2)
return hash;
2941 hash2 = to_hash(hash2);
2943 COPY_DEFAULT(hash, hash2);
2945 if (RHASH_AR_TABLE_P(hash)) {
2946 ar_free_and_clear_table(hash);
2949 st_free_table(RHASH_ST_TABLE(hash));
2950 RHASH_ST_CLEAR(hash);
2952 hash_copy(hash, hash2);
2955 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), 0));
2958 rb_gc_writebarrier_remember(hash);
2996 rb_hash_empty_p(
VALUE hash)
3033 rb_hash_each_value(
VALUE hash)
3071 rb_hash_each_key(
VALUE hash)
3124 rb_hash_each_pair(
VALUE hash)
3127 if (rb_block_pair_yield_optimizable())
3144 VALUE trans = p->trans, result = p->result;
3202 rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3209 transarg.trans = to_hash(argv[0]);
3217 if (transarg.trans) {
3218 transarg.result = result;
3242 rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3245 int block_given = 0;
3249 trans = to_hash(argv[0]);
3255 rb_hash_modify_check(hash);
3256 if (!RHASH_TABLE_EMPTY_P(hash)) {
3258 VALUE new_keys = hash_alloc(0);
3270 else if (block_given) {
3277 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3278 rb_hash_stlike_delete(hash, &key, NULL);
3290 transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3296 transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3300 rb_hash_modify(hash);
3326 rb_hash_transform_values(
VALUE hash)
3331 result = hash_dup_with_compare_by_id(hash);
3332 SET_DEFAULT(result,
Qnil);
3335 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3357 rb_hash_transform_values_bang(
VALUE hash)
3360 rb_hash_modify_check(hash);
3362 if (!RHASH_TABLE_EMPTY_P(hash)) {
3363 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3387 rb_hash_to_a(
VALUE hash)
3418 inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3442 rb_hash_inspect(
VALUE hash)
3456 rb_hash_to_hash(
VALUE hash)
3469 rb_builtin_class_name(arg));
3487 rb_hash_to_h_block(
VALUE hash)
3514 rb_hash_to_h(
VALUE hash)
3517 return rb_hash_to_h_block(hash);
3521 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3542 MJIT_FUNC_EXPORTED
VALUE
3543 rb_hash_keys(
VALUE hash)
3548 if (size == 0)
return keys;
3550 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3552 if (RHASH_AR_TABLE_P(hash)) {
3553 size = ar_keys(hash, ptr, size);
3556 st_table *table = RHASH_ST_TABLE(hash);
3557 size = st_keys(table, ptr, size);
3560 rb_gc_writebarrier_remember(keys);
3561 rb_ary_set_len(keys, size);
3587 rb_hash_values(
VALUE hash)
3593 if (size == 0)
return values;
3595 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3596 if (RHASH_AR_TABLE_P(hash)) {
3597 rb_gc_writebarrier_remember(values);
3599 size = ar_values(hash, ptr, size);
3602 else if (RHASH_ST_TABLE_P(hash)) {
3603 st_table *table = RHASH_ST_TABLE(hash);
3604 rb_gc_writebarrier_remember(values);
3606 size = st_values(table, ptr, size);
3609 rb_ary_set_len(values, size);
3631 MJIT_FUNC_EXPORTED
VALUE
3634 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3682 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3696 recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3700 if (recur)
return Qtrue;
3702 data->result =
Qtrue;
3705 return data->result;
3709 hash_equal(
VALUE hash1,
VALUE hash2,
int eql)
3713 if (hash1 == hash2)
return Qtrue;
3719 if (
rb_eql(hash2, hash1)) {
3732 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3733 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3745 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3773 return hash_equal(hash1, hash2, FALSE);
3798 return hash_equal(hash1, hash2, TRUE);
3804 st_index_t *hval = (st_index_t *)arg;
3805 st_index_t hdata[2];
3809 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3828 rb_hash_hash(
VALUE hash)
3863 rb_hash_invert(
VALUE hash)
3872 rb_hash_update_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3878 NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback)
3883 RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
3888 rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3890 st_data_t newvalue = arg->arg;
3899 NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3904 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
3959 rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
3964 rb_hash_modify(
self);
3965 for (i = 0; i < argc; i++){
3966 VALUE hash = to_hash(argv[i]);
3984 rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3987 VALUE newvalue = uf_arg->value;
3990 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
3996 NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4002 VALUE hash = arg->hash;
4005 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4012 rb_hash_modify(hash1);
4013 hash2 = to_hash(hash2);
4078 rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4080 return rb_hash_update(argc, argv, copy_compare_by_id(
rb_hash_dup(
self),
self));
4090 lookup2_call(
VALUE arg)
4102 reset_hash_type(
VALUE arg)
4105 HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
4106 RHASH_ST_TABLE(p->hash)->type = p->orighash;
4142 ar_force_convert_table(hash, __FILE__, __LINE__);
4143 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4144 table = RHASH_ST_TABLE(hash);
4145 orighash = table->type;
4147 if (orighash != &identhash) {
4152 assochash.compare = assoc_cmp;
4153 assochash.hash = orighash->hash;
4154 table->type = &assochash;
4157 ensure_arg.hash = hash;
4158 ensure_arg.orighash = orighash;
4249 rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4258 if (level == 0)
return rb_hash_to_a(hash);
4266 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4268 else if (level < 0) {
4293 if (!
NIL_P(value)) {
4310 rb_hash_compact(
VALUE hash)
4331 rb_hash_compact_bang(
VALUE hash)
4334 rb_hash_modify_check(hash);
4344 static st_table *rb_init_identtable_with_size(st_index_t size);
4375 rb_hash_compare_by_id(
VALUE hash)
4380 if (rb_hash_compare_by_id_p(hash))
return hash;
4382 rb_hash_modify_check(hash);
4383 ar_force_convert_table(hash, __FILE__, __LINE__);
4384 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4386 tmp = hash_alloc(0);
4387 identtable = rb_init_identtable_with_size(
RHASH_SIZE(hash));
4388 RHASH_ST_TABLE_SET(tmp, identtable);
4390 st_free_table(RHASH_ST_TABLE(hash));
4391 RHASH_ST_TABLE_SET(hash, identtable);
4392 RHASH_ST_CLEAR(tmp);
4404 MJIT_FUNC_EXPORTED
VALUE
4405 rb_hash_compare_by_id_p(
VALUE hash)
4407 return RBOOL(RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->
type == &identhash);
4411 rb_ident_hash_new(
void)
4414 RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
4419 rb_ident_hash_new_with_size(st_index_t size)
4422 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(&identhash, size));
4427 rb_init_identtable(
void)
4429 return st_init_table(&identhash);
4433 rb_init_identtable_with_size(st_index_t size)
4435 return st_init_table_with_size(&identhash, size);
4501 rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4510 rb_warn(
"given block not used");
4521 if (rb_block_pair_yield_optimizable())
4559 rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4563 if (!--argc)
return self;
4565 return rb_obj_dig(argc, argv,
self,
Qnil);
4602 other = to_hash(other);
4604 return hash_le(hash, other);
4621 other = to_hash(other);
4623 return hash_le(hash, other);
4640 other = to_hash(other);
4642 return hash_le(other, hash);
4659 other = to_hash(other);
4661 return hash_le(other, hash);
4684 rb_hash_to_proc(
VALUE hash)
4686 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4690 rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4696 add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4699 if (existing)
return ST_STOP;
4718 if (RHASH_AR_TABLE_P(hash)) {
4719 hash_ar_table(hash);
4721 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4725 ar_try_convert_table(hash);
4727 tbl = RHASH_TBL_RAW(hash);
4728 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4733 key_stringify(
VALUE key)
4736 rb_hash_key_str(key) : key;
4740 ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4743 for (i = 0; i < argc; ) {
4744 st_data_t k = key_stringify(argv[i++]);
4745 st_data_t v = argv[i++];
4746 ar_insert(hash, k, v);
4755 HASH_ASSERT(argc % 2 == 0);
4757 st_index_t size = argc / 2;
4759 if (RHASH_TABLE_NULL_P(hash)) {
4760 if (size <= RHASH_AR_TABLE_MAX_SIZE) {
4761 hash_ar_table(hash);
4764 RHASH_TBL_RAW(hash);
4768 if (RHASH_AR_TABLE_P(hash) &&
4769 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4770 ar_bulk_insert(hash, argc, argv);
4773 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4778 static char **origenviron;
4780 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4781 #define FREE_ENVIRON(e) rb_w32_free_environ(e)
4782 static char **my_environ;
4784 #define environ my_environ
4786 #define getenv(n) rb_w32_ugetenv(n)
4787 #elif defined(__APPLE__)
4789 #define environ (*_NSGetEnviron())
4790 #define GET_ENVIRON(e) (e)
4791 #define FREE_ENVIRON(e)
4793 extern char **environ;
4794 #define GET_ENVIRON(e) (e)
4795 #define FREE_ENVIRON(e)
4797 #ifdef ENV_IGNORECASE
4798 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4799 #define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4801 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4802 #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4805 #define ENV_LOCK() RB_VM_LOCK_ENTER()
4806 #define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4819 env_enc_str_new(
const char *ptr,
long len,
rb_encoding *enc)
4828 env_str_new(
const char *ptr,
long len)
4830 return env_enc_str_new(ptr, len, env_encoding());
4834 env_str_new2(
const char *ptr)
4836 if (!ptr)
return Qnil;
4837 return env_str_new(ptr, strlen(ptr));
4841 getenv_with_lock(
const char *name)
4846 const char *val = getenv(name);
4847 ret = env_str_new2(val);
4854 has_env_with_lock(
const char *name)
4864 return val ? true :
false;
4867 static const char TZ_ENV[] =
"TZ";
4884 return rb_str_fill_terminator(str, 1);
4887 #define get_env_ptr(var, val) \
4888 (var = get_env_cstr(val, #var))
4890 static inline const char *
4891 env_name(
volatile VALUE *s)
4895 get_env_ptr(name, *s);
4899 #define env_name(s) env_name(&(s))
4904 reset_by_modified_env(
const char *nam)
4912 if (ENVMATCH(nam, TZ_ENV)) {
4913 ruby_reset_timezone();
4918 env_delete(
VALUE name)
4920 const char *nam = env_name(name);
4921 reset_by_modified_env(nam);
4922 VALUE val = getenv_with_lock(nam);
4960 val = env_delete(name);
4980 const char *nam = env_name(name);
4981 VALUE env = getenv_with_lock(nam);
5021 if (block_given && argc == 2) {
5022 rb_warn(
"block supersedes default value argument");
5024 nam = env_name(key);
5025 env = getenv_with_lock(nam);
5028 if (block_given)
return rb_yield(key);
5030 rb_key_err_raise(
rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5040 rb_warn_deprecated_to_remove_at(3.2,
"rb_env_path_tainted", NULL);
5044 #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5047 in_origenv(
const char *str)
5050 for (env = origenviron; *env; ++env) {
5051 if (*env == str)
return 1;
5057 envix(
const char *nam)
5061 register int i, len = strlen(nam);
5064 env = GET_ENVIRON(environ);
5065 for (i = 0; env[i]; i++) {
5066 if (ENVNMATCH(env[i],nam,len) && env[i][len] ==
'=')
5069 FREE_ENVIRON(environ);
5076 getenvsize(
const WCHAR* p)
5078 const WCHAR* porg = p;
5079 while (*p++) p += lstrlenW(p) + 1;
5080 return p - porg + 1;
5084 getenvblocksize(
void)
5094 check_envsize(
size_t n)
5096 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5100 WCHAR* p = GetEnvironmentStringsW();
5103 FreeEnvironmentStringsW(p);
5104 if (n >= getenvblocksize()) {
5112 #if defined(_WIN32) || \
5113 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5115 NORETURN(
static void invalid_envname(
const char *name));
5118 invalid_envname(
const char *name)
5124 check_envname(
const char *name)
5126 if (strchr(name,
'=')) {
5127 invalid_envname(name);
5137 # if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5138 # define HAVE__WPUTENV_S 1
5145 check_envname(name);
5146 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5149 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5150 if (check_envsize((
size_t)len + len2)) {
5153 wname =
ALLOCV_N(WCHAR, buf, len + len2);
5154 wvalue = wname + len;
5155 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5156 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5157 #ifndef HAVE__WPUTENV_S
5158 wname[len-1] = L
'=';
5162 wname =
ALLOCV_N(WCHAR, buf, len + 1);
5163 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5164 wvalue = wname + len;
5166 #ifndef HAVE__WPUTENV_S
5167 wname[len-1] = L
'=';
5173 #ifndef HAVE__WPUTENV_S
5174 failed = _wputenv(wname);
5176 failed = _wputenv_s(wname, wvalue);
5184 if (!value || !*value) {
5186 if (!SetEnvironmentVariable(name, value) &&
5187 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5191 invalid_envname(name);
5193 #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5198 ret = setenv(name, value, 1);
5205 #ifdef VOID_UNSETENV
5215 ret = unsetenv(name);
5227 size_t len, mem_size;
5228 char **env_ptr, *str, *mem_ptr;
5230 check_envname(name);
5233 mem_size = len + strlen(value) + 2;
5234 mem_ptr = malloc(mem_size);
5235 if (mem_ptr == NULL)
5237 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5242 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5243 if (!strncmp(str, name, len) && str[len] ==
'=') {
5244 if (!in_origenv(str)) free(str);
5245 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5256 ret = putenv(mem_ptr);
5273 if (environ == origenviron) {
5278 for (max = i; environ[max]; max++) ;
5279 tmpenv =
ALLOC_N(
char*, max+2);
5280 for (j=0; j<max; j++)
5287 char **envp = origenviron;
5288 while (*envp && *envp != environ[i]) envp++;
5292 while (environ[i]) {
5293 environ[i] = environ[i+1];
5300 if (!value)
goto finish;
5305 len = strlen(name) + strlen(value) + 2;
5306 environ[i] =
ALLOC_N(
char, len);
5307 snprintf(environ[i],len,
"%s=%s",name,value);
5368 return env_aset(nm, val);
5384 get_env_ptr(name, nm);
5385 get_env_ptr(value, val);
5388 reset_by_modified_env(name);
5400 char **env = GET_ENVIRON(environ);
5402 char *s = strchr(*env,
'=');
5404 const char *p = *env;
5411 FREE_ENVIRON(environ);
5434 return env_keys(FALSE);
5445 env = GET_ENVIRON(environ);
5446 for (; *env ; ++env) {
5447 if (strchr(*env,
'=')) {
5451 FREE_ENVIRON(environ);
5476 env_each_key(
VALUE ehash)
5482 keys = env_keys(FALSE);
5496 char **env = GET_ENVIRON(environ);
5499 char *s = strchr(*env,
'=');
5505 FREE_ENVIRON(environ);
5527 return env_values();
5548 env_each_value(
VALUE ehash)
5554 values = env_values();
5580 env_each_pair(
VALUE ehash)
5590 char **env = GET_ENVIRON(environ);
5593 char *s = strchr(*env,
'=');
5600 FREE_ENVIRON(environ);
5604 if (rb_block_pair_yield_optimizable()) {
5641 env_reject_bang(
VALUE ehash)
5648 keys = env_keys(FALSE);
5649 RBASIC_CLEAR_CLASS(keys);
5660 if (del == 0)
return Qnil;
5685 env_delete_if(
VALUE ehash)
5688 env_reject_bang(ehash);
5716 for (i=0; i<argc; i++) {
5744 env_select(
VALUE ehash)
5752 keys = env_keys(FALSE);
5805 env_select_bang(
VALUE ehash)
5812 keys = env_keys(FALSE);
5813 RBASIC_CLEAR_CLASS(keys);
5824 if (del == 0)
return Qnil;
5847 env_keep_if(
VALUE ehash)
5850 env_select_bang(ehash);
5870 VALUE key, value, result;
5875 result = rb_hash_new_with_size(argc);
5877 for (i = 0; i < argc; i++) {
5879 value = rb_f_getenv(
Qnil, key);
5893 keys = env_keys(TRUE);
5948 char **env = GET_ENVIRON(environ);
5950 char *s = strchr(*env,
'=');
5952 if (env != environ) {
5964 FREE_ENVIRON(environ);
5989 char **env = GET_ENVIRON(environ);
5991 char *s = strchr(*env,
'=');
5994 env_str_new2(s+1)));
5998 FREE_ENVIRON(environ);
6020 env_size_with_lock(
void)
6026 char **env = GET_ENVIRON(environ);
6028 FREE_ENVIRON(environ);
6048 return INT2FIX(env_size_with_lock());
6068 char **env = GET_ENVIRON(environ);
6072 FREE_ENVIRON(environ);
6076 return RBOOL(empty);
6107 const char *s = env_name(key);
6108 return RBOOL(has_env_with_lock(s));
6134 const char *s = env_name(key);
6135 VALUE e = getenv_with_lock(s);
6167 char **env = GET_ENVIRON(environ);
6169 char *s = strchr(*env,
'=');
6171 long len = strlen(s);
6179 FREE_ENVIRON(environ);
6210 char **env = GET_ENVIRON(environ);
6213 const char *p = *env;
6214 char *s = strchr(p,
'=');
6216 long len = strlen(s);
6224 FREE_ENVIRON(environ);
6255 char **env = GET_ENVIRON(environ);
6257 char *s = strchr(*env,
'=');
6259 long len = strlen(s);
6261 str = env_str_new(*env, s-*env-1);
6267 FREE_ENVIRON(environ);
6281 char **env = GET_ENVIRON(environ);
6283 char *s = strchr(*env,
'=');
6290 FREE_ENVIRON(environ);
6304 rb_env_to_hash(
void)
6306 return env_to_hash();
6321 return env_to_hash();
6345 VALUE hash = env_to_hash();
6347 hash = rb_hash_to_h_block(hash);
6365 VALUE key, hash = env_to_hash();
6367 for (i = 0; i < argc; i++) {
6396 NORETURN(
static VALUE env_freeze(
VALUE self));
6405 env_freeze(
VALUE self)
6434 char **env = GET_ENVIRON(environ);
6436 const char *p = *env;
6437 char *s = strchr(p,
'=');
6439 key = env_str_new(p, s-p);
6444 FREE_ENVIRON(environ);
6473 return rb_hash_invert(env_to_hash());
6480 const char *keyptr, *eptr;
6488 if (elen != keylen)
continue;
6489 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6501 keylist_delete(keys, key);
6529 keys = env_keys(TRUE);
6530 if (env == hash)
return env;
6531 hash = to_hash(hash);
6552 if (!
NIL_P(oldval)) {
6598 if (env == hash)
return env;
6599 hash = to_hash(hash);
6601 env_update_block_i : env_update_i;
6620 VALUE opt, kwfreeze;
6622 kwfreeze = rb_get_freeze_opt(1, &opt);
6623 if (
RTEST(kwfreeze)) {
6629 rb_warn_deprecated(
"ENV.clone",
"ENV.to_h");
6655 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7138 id_hash_iter_lev = rb_make_internal_id();
7400 origenviron = environ;
7470 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7472 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
double rb_float_value(VALUE num)
Extracts its double value from an instance of rb_cFloat.
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implenentation detail of RB_FL_ANY().
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_DATA
Old name of RUBY_T_DATA.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define OBJ_WB_UNPROTECT
Old name of RB_OBJ_WB_UNPROTECT.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
void rb_sys_fail_str(VALUE mesg)
Identical to rb_sys_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_mKernel
Kernel module.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_mEnumerable
Enumerable module.
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
VALUE rb_cHash
Hash class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_cString
String class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
void rb_enc_copy(VALUE dst, VALUE src)
Destructively copies the encoding of the latter object to that of former one.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_tmp_new(long capa)
Allocates a "temporary" array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Exports an integer into a buffer.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
#define rb_check_frozen
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_size(VALUE hash)
Identical to RHASH_SIZE(), except it returns the size in Ruby's integer instead of C's.
void rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
Inserts a list of key-value pairs into a hash table at once.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_check_hash_type(VALUE obj)
Try converting an object to its hash representation using its to_hash method, if any.
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Identical to rb_hash_lookup(), except you can specify what to return on misshits.
VALUE rb_hash_freeze(VALUE obj)
Just another name of rb_obj_freeze.
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Type of callback functions to pass to rb_hash_update_by().
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
int rb_env_path_tainted(void)
VALUE rb_hash_delete(VALUE hash, VALUE key)
Deletes the passed key from the passed hash table, if any.
VALUE rb_hash_fetch(VALUE hash, VALUE key)
Identical to rb_hash_lookup(), except it yields the (implicitly) passed block instead of returning RU...
VALUE rb_hash_delete_if(VALUE hash)
Deletes each entry for which the block returns a truthy value.
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
Destructively merges two hash tables into one.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_env_clear(void)
Destructively removes every environment variables of the running process.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
VALUE rb_hash_dup(VALUE hash)
Duplicates a hash.
VALUE rb_hash(VALUE obj)
Calculates a message authentication code of the passed object.
VALUE rb_hash_clear(VALUE hash)
Swipes everything out of the passed hash table.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_obj_id(VALUE obj)
Finds or creates an integer primary key of the given object.
VALUE rb_proc_lambda_p(VALUE recv)
Queries if the given object is a lambda.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
int rb_proc_arity(VALUE recv)
Queries the number of mandatory arguments of the given Proc.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
VALUE rb_utf8_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "UTF-8" encoding.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
VALUE rb_str_buf_cat(VALUE, const char *, long)
Just another name of rb_str_cat.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_buf_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_exec_recursive_outer(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
Identical to rb_exec_recursive(), except it calls f for outermost recursion only.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well...
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
void ruby_setenv(const char *key, const char *val)
Sets an environment variable.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
void ruby_unsetenv(const char *key)
Deletes the passed environment variable, if any.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_PTR_USE_TRANSIENT(ary, ptr_name, expr)
Identical to RARRAY_PTR_USE, except the pointer can be a transient one.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RGENGC_WB_PROTECTED_HASH
This is a compile-time flag to enable/disable write barrier for struct RHash.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
This is the implementation detail of RHASH_SET_IFNONE.
#define RHASH_ITER_LEV(h)
#define RHASH_SIZE(h)
Queries the size of the hash.
struct st_table * rb_hash_tbl(VALUE hash, const char *file, int line)
This is the implementation detail of RHASH_TBL.
size_t rb_hash_size_num(VALUE hash)
This is the implementation detail of RHASH_SIZE.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define SafeStringValue(v)
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
VALUE flags
Per-object flags.
This is the struct that holds necessary info for a struct.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
void ruby_xfree(void *ptr)
Deallocates a storage instance.