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/basic_operators.h"
32#include "internal/class.h"
33#include "internal/cont.h"
34#include "internal/error.h"
35#include "internal/hash.h"
36#include "internal/object.h"
37#include "internal/proc.h"
38#include "internal/symbol.h"
39#include "internal/thread.h"
40#include "internal/time.h"
41#include "internal/vm.h"
45#include "ruby_assert.h"
47#include "transient_heap.h"
60#define SET_DEFAULT(hash, ifnone) ( \
61 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
62 RHASH_SET_IFNONE(hash, ifnone))
64#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
66#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
69copy_default(
struct RHash *hash,
const struct RHash *hash2)
71 hash->basic.
flags &= ~RHASH_PROC_DEFAULT;
72 hash->basic.
flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
97static ID id_hash, id_flatten_bang;
98static ID id_hash_iter_lev;
100#define id_default idDefault
112 if (a == b)
return 0;
117 if (UNDEF_P(a) || UNDEF_P(b))
return -1;
126hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
128 if (recurse)
return INT2FIX(0);
129 return rb_funcallv(obj, id_hash, 0, 0);
132static long rb_objid_hash(st_index_t index);
135dbl_to_index(
double d)
137 union {
double d; st_index_t i;} u;
143rb_dbl_long_hash(
double d)
146 if (d == 0.0) d = 0.0;
147#if SIZEOF_INT == SIZEOF_VOIDP
150 return rb_objid_hash(dbl_to_index(d));
155any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
167 hnum = RSYMBOL(a)->hashval;
174 hnum = rb_objid_hash((st_index_t)a);
180 hval = rb_big_hash(a);
184 hnum = rb_dbl_long_hash(rb_float_value(a));
187 hnum = other_func(a);
197obj_any_hash(
VALUE obj)
199 VALUE hval = rb_check_funcall_basic_kw(obj, id_hash,
rb_mKernel, 0, 0, 0);
202 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
209 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
227 return any_hash(a, obj_any_hash);
233 return LONG2FIX(any_hash(obj, obj_any_hash));
242static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
243static const uint32_t prime2 = 0x830fcab9;
246static inline uint64_t
247mult_and_mix(uint64_t m1, uint64_t m2)
249#if defined HAVE_UINT128_T
250 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
251 return (uint64_t) (r >> 64) ^ (uint64_t) r;
253 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
254 uint64_t lm1 = m1, lm2 = m2;
255 uint64_t v64_128 = hm1 * hm2;
256 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
257 uint64_t v1_32 = lm1 * lm2;
259 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
263static inline uint64_t
264key64_hash(uint64_t key, uint32_t seed)
266 return mult_and_mix(key + seed, prime1);
270#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
273rb_objid_hash(st_index_t index)
275 return (
long)st_index_hash(index);
281 VALUE object_id = rb_obj_id(obj);
283 object_id = rb_big_hash(object_id);
285#if SIZEOF_LONG == SIZEOF_VOIDP
286 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
287#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
288 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
296rb_obj_hash(
VALUE obj)
298 long hnum = any_hash(obj, objid_hash);
307#define rb_ident_cmp st_numcmp
310rb_ident_hash(st_data_t n)
319 n ^= dbl_to_index(rb_float_value(n));
323 return (st_index_t)st_index_hash((st_index_t)n);
326#define identhash rb_hashtype_ident
332typedef st_index_t st_hash_t;
344#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
346#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
347#define RHASH_AR_CLEARED_HINT 0xff
360rb_hash_ar_table_size(
void)
365static inline st_hash_t
366ar_do_hash(st_data_t key)
368 return (st_hash_t)rb_any_hash(key);
371static inline ar_hint_t
372ar_do_hash_hint(st_hash_t hash_value)
374 return (ar_hint_t)hash_value;
377static inline ar_hint_t
378ar_hint(
VALUE hash,
unsigned int index)
380 return RHASH(hash)->ar_hint.ary[index];
384ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
386 RHASH(hash)->ar_hint.ary[index] = hint;
390ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
392 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
396ar_clear_entry(
VALUE hash,
unsigned int index)
400 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
404ar_cleared_entry(
VALUE hash,
unsigned int index)
406 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
411 return UNDEF_P(pair->key);
419ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
424 ar_hint_set(hash, index, hash_value);
427#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
428 RHASH_AR_TABLE_SIZE_RAW(h))
430#define RHASH_AR_TABLE_BOUND_RAW(h) \
431 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
432 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
434#define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
435 RHASH_AR_TABLE_BOUND_RAW(h))
437#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
438#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
440#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
443#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
446rb_hash_dump(
VALUE hash)
448 rb_obj_info_dump(hash);
450 if (RHASH_AR_TABLE_P(hash)) {
451 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
453 fprintf(stderr,
" size:%u bound:%u\n",
454 RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
456 for (i=0; i<bound; i++) {
459 if (!ar_cleared_entry(hash, i)) {
460 char b1[0x100], b2[0x100];
464 fprintf(stderr,
" %d key:%s val:%s hint:%02x\n", i,
465 rb_raw_obj_info(b1, 0x100, k),
466 rb_raw_obj_info(b2, 0x100, v),
471 fprintf(stderr,
" %d empty\n", i);
478hash_verify_(
VALUE hash,
const char *file,
int line)
482 if (RHASH_AR_TABLE_P(hash)) {
483 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
485 for (i=0; i<bound; i++) {
487 if (!ar_cleared_entry(hash, i)) {
491 HASH_ASSERT(!UNDEF_P(k));
492 HASH_ASSERT(!UNDEF_P(v));
496 if (n != RHASH_AR_TABLE_SIZE(hash)) {
497 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
501 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
502 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
503 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
506#if USE_TRANSIENT_HEAP
507 if (RHASH_TRANSIENT_P(hash)) {
508 volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key;
509 HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
510 HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
517#define hash_verify(h) ((void)0)
521RHASH_TABLE_NULL_P(
VALUE hash)
523 if (RHASH(hash)->as.ar == NULL) {
524 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
533RHASH_TABLE_EMPTY_P(
VALUE hash)
539rb_hash_ar_table_p(
VALUE hash)
542 HASH_ASSERT(RHASH(hash)->as.st != NULL);
551rb_hash_ar_table(
VALUE hash)
553 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
554 return RHASH(hash)->as.ar;
558rb_hash_st_table(
VALUE hash)
560 HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
561 return RHASH(hash)->as.st;
567 HASH_ASSERT(st != NULL);
569 RHASH(hash)->as.st = st;
575 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
576 HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
577 RHASH(hash)->as.ar = ar;
581#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
582#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
585RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
587 HASH_ASSERT(RHASH_AR_TABLE_P(h));
588 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
590 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
591 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
595RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
597 HASH_ASSERT(RHASH_AR_TABLE_P(h));
598 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
600 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
601 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
605HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
607 HASH_ASSERT(RHASH_AR_TABLE_P(h));
609 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
614#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
617RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
619 HASH_ASSERT(RHASH_AR_TABLE_P(h));
620 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
623 RHASH_AR_TABLE_SIZE_SET(h, new_size);
626 RHASH_AR_TABLE_SIZE_SET(h, 0);
627 RHASH_AR_TABLE_BOUND_SET(h, 0);
633RHASH_AR_TABLE_CLEAR(
VALUE h)
635 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
636 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
638 hash_ar_table_set(h, NULL);
642ar_alloc_table(
VALUE hash)
647 RHASH_SET_TRANSIENT_FLAG(hash);
650 RHASH_UNSET_TRANSIENT_FLAG(hash);
654 RHASH_AR_TABLE_SIZE_SET(hash, 0);
655 RHASH_AR_TABLE_BOUND_SET(hash, 0);
656 hash_ar_table_set(hash, tab);
661NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
666 return rb_any_cmp(x, y) == 0;
670ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
672 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
673 const ar_hint_t *hints = RHASH(hash)->ar_hint.ary;
677 for (i = 0; i < bound; i++) {
678 if (hints[i] == hint) {
680 if (ar_equal(key, pair->key)) {
681 RB_DEBUG_COUNTER_INC(artable_hint_hit);
687 static char fname[256];
690 if (pid != getpid()) {
691 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
692 if ((fp = fopen(fname,
"w")) == NULL)
rb_bug(
"fopen");
695 st_hash_t h1 = ar_do_hash(key);
696 st_hash_t h2 = ar_do_hash(pair->key);
698 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
700 " pair->key:%016lx %s\n",
701 h1 == h2, i, hints[i], hint,
702 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
704 RB_DEBUG_COUNTER_INC(artable_hint_miss);
708 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
709 return RHASH_AR_TABLE_MAX_BOUND;
713ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
715 ar_hint_t hint = ar_do_hash_hint(hash_value);
716 return ar_find_entry_hint(hash, hint, key);
720ar_free_and_clear_table(
VALUE hash)
722 ar_table *tab = RHASH_AR_TABLE(hash);
725 if (RHASH_TRANSIENT_P(hash)) {
726 RHASH_UNSET_TRANSIENT_FLAG(hash);
729 ruby_xfree(RHASH_AR_TABLE(hash));
731 RHASH_AR_TABLE_CLEAR(hash);
733 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
734 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
735 HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
738void rb_st_add_direct_with_hash(
st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
740enum ar_each_key_type {
747ar_each_key(
ar_table *ar,
int max,
enum ar_each_key_type
type, st_data_t *dst_keys,
st_table *new_tab, st_hash_t *hashes)
749 for (
int i = 0; i < max; i++) {
753 case ar_each_key_copy:
754 dst_keys[i] = pair->key;
756 case ar_each_key_cmp:
757 if (dst_keys[i] != pair->key)
return 1;
759 case ar_each_key_insert:
760 if (UNDEF_P(pair->key))
continue;
761 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
772ar_force_convert_table(
VALUE hash,
const char *file,
int line)
776 if (RHASH_ST_TABLE_P(hash)) {
777 return RHASH_ST_TABLE(hash);
780 if (RHASH_AR_TABLE(hash)) {
781 ar_table *ar = RHASH_AR_TABLE(hash);
782 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
783 unsigned int bound, size;
787 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
788 bound = RHASH_AR_TABLE_BOUND(hash);
789 size = RHASH_AR_TABLE_SIZE(hash);
790 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
792 for (
unsigned int i = 0; i < bound; i++) {
794 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
798 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
799 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
800 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
805 new_tab = st_init_table_with_size(&objhash, size);
806 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
807 ar_free_and_clear_table(hash);
810 new_tab = st_init_table(&objhash);
812 RHASH_ST_TABLE_SET(hash, new_tab);
818hash_ar_table(
VALUE hash)
820 if (RHASH_TABLE_NULL_P(hash)) {
821 ar_alloc_table(hash);
823 return RHASH_AR_TABLE(hash);
827ar_compact_table(
VALUE hash)
829 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
830 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
839 for (i=0; i<bound; i++) {
840 if (ar_cleared_entry(hash, i)) {
842 for (; j<bound; j++) {
843 if (!ar_cleared_entry(hash, j)) {
845 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
846 ar_clear_entry(hash, j);
857 HASH_ASSERT(i<=bound);
859 RHASH_AR_TABLE_BOUND_SET(hash, size);
866ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
868 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
870 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
874 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
875 bin = ar_compact_table(hash);
878 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
880 ar_set_entry(hash, bin, key, val, hash_value);
881 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
882 RHASH_AR_TABLE_SIZE_INC(hash);
888ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
890 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
891 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
893 for (i = 0; i < bound; i++) {
894 if (ar_cleared_entry(hash, i))
continue;
897 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
908 VALUE key = pair->key;
909 VALUE val = pair->val;
910 retval = (*replace)(&key, &val, arg, TRUE);
919 ar_clear_entry(hash, i);
920 RHASH_AR_TABLE_SIZE_DEC(hash);
929ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
931 return ar_general_foreach(hash, func, replace, arg);
935 st_foreach_callback_func *func;
940apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
942 const struct functor *f = (
void *)d;
943 return f->func(k, v, f->arg);
947ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
949 const struct functor f = { func, arg };
950 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
954ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
957 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
958 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
959 enum st_retval retval;
964 for (i = 0; i < bound; i++) {
965 if (ar_cleared_entry(hash, i))
continue;
967 pair = RHASH_AR_TABLE_REF(hash, i);
969 hint = ar_hint(hash, i);
971 retval = (*func)(key, pair->val, arg, 0);
976 pair = RHASH_AR_TABLE_REF(hash, i);
977 if (pair->key == never)
break;
978 ret = ar_find_entry_hint(hash, hint, key);
979 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
980 retval = (*func)(0, 0, arg, 1);
990 if (!ar_cleared_entry(hash, i)) {
991 ar_clear_entry(hash, i);
992 RHASH_AR_TABLE_SIZE_DEC(hash);
1003ar_update(
VALUE hash, st_data_t key,
1004 st_update_callback_func *func, st_data_t arg)
1006 int retval, existing;
1007 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
1008 st_data_t value = 0, old_key;
1009 st_hash_t hash_value = ar_do_hash(key);
1011 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1016 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1017 bin = ar_find_entry(hash, hash_value, key);
1018 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
1021 hash_ar_table(hash);
1031 retval = (*func)(&key, &value, arg, existing);
1037 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1043 if (old_key != key) {
1051 ar_clear_entry(hash, bin);
1052 RHASH_AR_TABLE_SIZE_DEC(hash);
1060ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1062 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1063 st_hash_t hash_value = ar_do_hash(key);
1065 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1070 hash_ar_table(hash);
1072 bin = ar_find_entry(hash, hash_value, key);
1073 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1074 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1077 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1078 bin = ar_compact_table(hash);
1079 hash_ar_table(hash);
1081 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1083 ar_set_entry(hash, bin, key, value, hash_value);
1084 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1085 RHASH_AR_TABLE_SIZE_INC(hash);
1089 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1095ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1097 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1101 st_hash_t hash_value = ar_do_hash(key);
1102 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1104 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1106 unsigned bin = ar_find_entry(hash, hash_value, key);
1108 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1112 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1113 if (value != NULL) {
1114 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1122ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1125 st_hash_t hash_value = ar_do_hash(*key);
1127 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1129 return st_delete(RHASH_ST_TABLE(hash), key, value);
1132 bin = ar_find_entry(hash, hash_value, *key);
1134 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1135 if (value != 0) *value = 0;
1143 ar_clear_entry(hash, bin);
1144 RHASH_AR_TABLE_SIZE_DEC(hash);
1150ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1152 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1153 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1155 for (i = 0; i < bound; i++) {
1156 if (!ar_cleared_entry(hash, i)) {
1158 if (value != 0) *value = pair->val;
1160 ar_clear_entry(hash, i);
1161 RHASH_AR_TABLE_SIZE_DEC(hash);
1166 if (value != NULL) *value = 0;
1171ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1173 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1174 st_data_t *keys_start = keys, *keys_end = keys + size;
1176 for (i = 0; i < bound; i++) {
1177 if (keys == keys_end) {
1181 if (!ar_cleared_entry(hash, i)) {
1182 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1187 return keys - keys_start;
1191ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1193 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1194 st_data_t *values_start = values, *values_end = values + size;
1196 for (i = 0; i < bound; i++) {
1197 if (values == values_end) {
1201 if (!ar_cleared_entry(hash, i)) {
1202 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1207 return values - values_start;
1213 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1215 if (old_tab != NULL) {
1216 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1217 if (new_tab == NULL) {
1219 if (new_tab != NULL) {
1220 RHASH_SET_TRANSIENT_FLAG(hash1);
1223 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1227 *new_tab = *old_tab;
1228 RHASH(hash1)->ar_hint.word = RHASH(hash2)->ar_hint.word;
1229 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1230 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1231 hash_ar_table_set(hash1, new_tab);
1233 rb_gc_writebarrier_remember(hash1);
1237 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1238 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1240 if (RHASH_TRANSIENT_P(hash1)) {
1241 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1243 else if (RHASH_AR_TABLE(hash1)) {
1244 ruby_xfree(RHASH_AR_TABLE(hash1));
1247 hash_ar_table_set(hash1, NULL);
1249 rb_gc_writebarrier_remember(hash1);
1257 if (RHASH_AR_TABLE(hash) != NULL) {
1258 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1259 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1262 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1263 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1267#if USE_TRANSIENT_HEAP
1269rb_hash_transient_heap_evacuate(
VALUE hash,
int promote)
1271 if (RHASH_TRANSIENT_P(hash)) {
1273 ar_table *old_tab = RHASH_AR_TABLE(hash);
1275 if (UNLIKELY(old_tab == NULL)) {
1278 HASH_ASSERT(old_tab != NULL);
1280 new_tab = rb_transient_heap_alloc(hash,
sizeof(
ar_table));
1281 if (new_tab == NULL) promote =
true;
1284 new_tab = ruby_xmalloc(
sizeof(
ar_table));
1285 RHASH_UNSET_TRANSIENT_FLAG(hash);
1287 *new_tab = *old_tab;
1288 hash_ar_table_set(hash, new_tab);
1294typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1298 st_foreach_func *func;
1303foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1308 if (error)
return ST_STOP;
1309 status = (*arg->func)(key, value, arg->arg);
1310 if (status == ST_CONTINUE) {
1322 arg.func = (st_foreach_func *)func;
1324 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1333 rb_foreach_func *func;
1338hash_iter_status_check(
int status)
1353hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1357 if (error)
return ST_STOP;
1359 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1362 return hash_iter_status_check(status);
1366hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1370 if (error)
return ST_STOP;
1372 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1373 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1375 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1379 return hash_iter_status_check(status);
1383iter_lev_in_ivar(
VALUE hash)
1393iter_lev_in_ivar_set(
VALUE hash,
int lev)
1395 rb_ivar_set_internal(hash, id_hash_iter_lev,
INT2FIX(lev));
1399iter_lev_in_flags(
VALUE hash)
1401 unsigned int u = (
unsigned int)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1406iter_lev_in_flags_set(
VALUE hash,
int lev)
1408 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1414 int lev = iter_lev_in_flags(hash);
1416 if (lev == RHASH_LEV_MAX) {
1417 return iter_lev_in_ivar(hash);
1425hash_iter_lev_inc(
VALUE hash)
1427 int lev = iter_lev_in_flags(hash);
1428 if (lev == RHASH_LEV_MAX) {
1429 lev = iter_lev_in_ivar(hash);
1430 iter_lev_in_ivar_set(hash, lev+1);
1434 iter_lev_in_flags_set(hash, lev);
1435 if (lev == RHASH_LEV_MAX) {
1436 iter_lev_in_ivar_set(hash, lev);
1442hash_iter_lev_dec(
VALUE hash)
1444 int lev = iter_lev_in_flags(hash);
1445 if (lev == RHASH_LEV_MAX) {
1446 lev = iter_lev_in_ivar(hash);
1447 HASH_ASSERT(lev > 0);
1448 iter_lev_in_ivar_set(hash, lev-1);
1451 HASH_ASSERT(lev > 0);
1452 iter_lev_in_flags_set(hash, lev - 1);
1457hash_foreach_ensure_rollback(
VALUE hash)
1459 hash_iter_lev_inc(hash);
1464hash_foreach_ensure(
VALUE hash)
1466 hash_iter_lev_dec(hash);
1471rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1473 if (RHASH_AR_TABLE_P(hash)) {
1474 return ar_foreach(hash, func, arg);
1477 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1482rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1484 if (RHASH_AR_TABLE_P(hash)) {
1485 return ar_foreach_with_replace(hash, func, replace, arg);
1488 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1493hash_foreach_call(
VALUE arg)
1497 if (RHASH_AR_TABLE_P(hash)) {
1498 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1499 (st_data_t)arg, (st_data_t)
Qundef);
1501 else if (RHASH_ST_TABLE_P(hash)) {
1502 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1503 (st_data_t)arg, (st_data_t)
Qundef);
1512rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1516 if (RHASH_TABLE_EMPTY_P(hash))
1519 arg.func = (rb_foreach_func *)func;
1522 hash_foreach_call((
VALUE)&arg);
1525 hash_iter_lev_inc(hash);
1526 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1531void rb_st_compact_table(
st_table *tab);
1534compact_after_delete(
VALUE hash)
1537 rb_st_compact_table(RHASH_ST_TABLE(hash));
1553hash_alloc(
VALUE klass)
1555 return hash_alloc_flags(klass, 0,
Qnil);
1559empty_hash_alloc(
VALUE klass)
1561 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1563 return hash_alloc(klass);
1575 if (rb_hash_compare_by_id_p(basis)) {
1576 return rb_hash_compare_by_id(hash);
1581MJIT_FUNC_EXPORTED
VALUE
1582rb_hash_new_with_size(st_index_t size)
1584 VALUE ret = rb_hash_new();
1588 else if (size <= RHASH_AR_TABLE_MAX_SIZE) {
1589 ar_alloc_table(ret);
1592 RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
1598rb_hash_new_capa(
long capa)
1600 return rb_hash_new_with_size((st_index_t)capa);
1607 if (RHASH_AR_TABLE_P(hash))
1609 else if (RHASH_ST_TABLE_P(hash))
1610 RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
1616hash_dup_with_compare_by_id(
VALUE hash)
1618 return hash_copy(copy_compare_by_id(rb_hash_new(), hash), hash);
1624 return hash_copy(hash_alloc_flags(klass, flags,
RHASH_IFNONE(hash)),
1633 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1639MJIT_FUNC_EXPORTED
VALUE
1640rb_hash_resurrect(
VALUE hash)
1647rb_hash_modify_check(
VALUE hash)
1652MJIT_FUNC_EXPORTED
struct st_table *
1653rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1655 return ar_force_convert_table(hash, file, line);
1659rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1662 return rb_hash_tbl_raw(hash, file, line);
1666rb_hash_modify(
VALUE hash)
1668 rb_hash_modify_check(hash);
1671NORETURN(
static void no_new_key(
void));
1683#define NOINSERT_UPDATE_CALLBACK(func) \
1685func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1687 if (!existing) no_new_key(); \
1688 return func(key, val, (struct update_arg *)arg, existing); \
1692func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1694 return func(key, val, (struct update_arg *)arg, existing); \
1699 st_update_callback_func *func;
1705typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1708rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1710 if (RHASH_AR_TABLE_P(hash)) {
1711 int result = ar_update(hash, key, func, arg);
1713 ar_force_convert_table(hash, __FILE__, __LINE__);
1720 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1724tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1727 st_data_t old_key = *key;
1728 st_data_t old_value = *val;
1729 VALUE hash = p->hash;
1730 int ret = (p->func)(key, val, arg, existing);
1735 if (!existing || *key != old_key || *val != old_value) {
1736 rb_hash_modify(hash);
1743 rb_hash_modify(hash);
1751tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1754 .arg = optional_arg,
1758 .value = (
VALUE)optional_arg,
1761 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1770#define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
1772#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
1773 tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
1776#define RHASH_UPDATE(hash, key, func, arg) \
1777 RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
1785 if (n != 2 && (n >= 0 || n < -3)) {
1786 if (n < 0) n = -n-1;
1827rb_hash_initialize(
int argc,
VALUE *argv,
VALUE hash)
1831 rb_hash_modify(hash);
1835 SET_PROC_DEFAULT(hash, ifnone);
1839 ifnone = argc == 0 ?
Qnil : argv[0];
1882rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1887 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1889 hash = hash_alloc(klass);
1890 hash_copy(hash, tmp);
1894 tmp = rb_check_array_type(argv[0]);
1898 hash = hash_alloc(klass);
1901 VALUE v = rb_check_array_type(e);
1906 rb_builtin_class_name(e), i);
1916 rb_hash_aset(hash, key, val);
1922 if (argc % 2 != 0) {
1926 hash = hash_alloc(klass);
1927 rb_hash_bulk_insert(argc, argv, hash);
1932MJIT_FUNC_EXPORTED
VALUE
1933rb_to_hash_type(
VALUE hash)
1935 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1937#define to_hash rb_to_hash_type
1942 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1959rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1961 return rb_check_hash_type(hash);
1980rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1983 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
2002rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
2005 hash = rb_hash_dup(hash);
2006 RHASH(hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2018 if (RHASH_AR_TABLE_P(arg)) {
2019 ar_insert(arg, (st_data_t)key, (st_data_t)value);
2022 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
2040rb_hash_rehash(
VALUE hash)
2048 rb_hash_modify_check(hash);
2049 if (RHASH_AR_TABLE_P(hash)) {
2050 tmp = hash_alloc(0);
2051 ar_alloc_table(tmp);
2052 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2053 ar_free_and_clear_table(hash);
2055 ar_free_and_clear_table(tmp);
2057 else if (RHASH_ST_TABLE_P(hash)) {
2058 st_table *old_tab = RHASH_ST_TABLE(hash);
2059 tmp = hash_alloc(0);
2060 tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
2061 RHASH_ST_TABLE_SET(tmp, tbl);
2062 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2063 st_free_table(old_tab);
2064 RHASH_ST_TABLE_SET(hash, tbl);
2065 RHASH_ST_CLEAR(tmp);
2074 VALUE args[2] = {hash, key};
2079rb_hash_default_unredefined(
VALUE hash)
2083 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2086 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2095 if (LIKELY(rb_hash_default_unredefined(hash))) {
2097 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2098 if (UNDEF_P(key))
return Qnil;
2099 return call_default_proc(ifnone, hash, key);
2107hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2111 if (RHASH_AR_TABLE_P(hash)) {
2112 return ar_lookup(hash, key, pval);
2115 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2119MJIT_FUNC_EXPORTED
int
2120rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2122 return hash_stlike_lookup(hash, key, pval);
2144 if (hash_stlike_lookup(hash, key, &val)) {
2148 return rb_hash_default_value(hash, key);
2157 if (hash_stlike_lookup(hash, key, &val)) {
2168 return rb_hash_lookup2(hash, key,
Qnil);
2195rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2205 if (block_given && argc == 2) {
2206 rb_warn(
"block supersedes default value argument");
2209 if (hash_stlike_lookup(hash, key, &val)) {
2216 else if (argc == 1) {
2222 rb_key_err_raise(
rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2233 return rb_hash_fetch_m(1, &key, hash);
2257rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2263 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2264 if (argc == 0)
return Qnil;
2265 return call_default_proc(ifnone, hash, argv[0]);
2286 rb_hash_modify_check(hash);
2287 SET_DEFAULT(hash, ifnone);
2304rb_hash_default_proc(
VALUE hash)
2306 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2327rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2331 rb_hash_modify_check(hash);
2333 SET_DEFAULT(hash, proc);
2336 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2339 "wrong default_proc type %s (expected Proc)",
2343 SET_PROC_DEFAULT(hash, proc);
2380 rb_hash_foreach(hash, key_i, (
VALUE)args);
2386rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2388 if (RHASH_AR_TABLE_P(hash)) {
2389 return ar_delete(hash, pkey, pval);
2392 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2404 st_data_t ktmp = (st_data_t)key, val;
2406 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2422 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2424 if (!UNDEF_P(deleted_value)) {
2425 return deleted_value;
2464 rb_hash_modify_check(hash);
2465 val = rb_hash_delete_entry(hash, key);
2467 if (!UNDEF_P(val)) {
2468 compact_after_delete(hash);
2511rb_hash_shift(
VALUE hash)
2515 rb_hash_modify_check(hash);
2516 if (RHASH_AR_TABLE_P(hash)) {
2519 if (ar_shift(hash, &var.key, &var.val)) {
2520 return rb_assoc_new(var.key, var.val);
2524 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2525 if (!UNDEF_P(var.key)) {
2526 rb_hash_delete_entry(hash, var.key);
2527 return rb_assoc_new(var.key, var.val);
2531 if (RHASH_ST_TABLE_P(hash)) {
2534 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2535 return rb_assoc_new(var.key, var.val);
2539 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2540 if (!UNDEF_P(var.key)) {
2541 rb_hash_delete_entry(hash, var.key);
2542 return rb_assoc_new(var.key, var.val);
2553 rb_hash_modify(hash);
2562 return rb_hash_size(hash);
2586 rb_hash_modify_check(hash);
2587 if (!RHASH_TABLE_EMPTY_P(hash)) {
2588 rb_hash_foreach(hash, delete_if_i, hash);
2589 compact_after_delete(hash);
2613rb_hash_reject_bang(
VALUE hash)
2618 rb_hash_modify(hash);
2620 if (!n)
return Qnil;
2621 rb_hash_foreach(hash, delete_if_i, hash);
2645rb_hash_reject(
VALUE hash)
2650 result = hash_dup_with_compare_by_id(hash);
2652 rb_hash_foreach(result, delete_if_i, result);
2653 compact_after_delete(result);
2670rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2673 VALUE key, value, result;
2676 return copy_compare_by_id(rb_hash_new(), hash);
2678 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2680 for (i = 0; i < argc; i++) {
2682 value = rb_hash_lookup2(hash, key,
Qundef);
2683 if (!UNDEF_P(value))
2684 rb_hash_aset(result, key, value);
2702rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2707 result = hash_dup_with_compare_by_id(hash);
2709 for (i = 0; i < argc; i++) {
2711 rb_hash_delete(result, key);
2713 compact_after_delete(result);
2732rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2737 for (i=0; i<argc; i++) {
2738 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2764rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2769 for (i=0; i<argc; i++) {
2770 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2779 rb_hash_modify(hash);
2803rb_hash_select(
VALUE hash)
2808 result = hash_dup_with_compare_by_id(hash);
2810 rb_hash_foreach(result, keep_if_i, result);
2811 compact_after_delete(result);
2836rb_hash_select_bang(
VALUE hash)
2841 rb_hash_modify_check(hash);
2843 if (!n)
return Qnil;
2844 rb_hash_foreach(hash, keep_if_i, hash);
2867rb_hash_keep_if(
VALUE hash)
2870 rb_hash_modify_check(hash);
2871 if (!RHASH_TABLE_EMPTY_P(hash)) {
2872 rb_hash_foreach(hash, keep_if_i, hash);
2893 rb_hash_modify_check(hash);
2896 rb_hash_foreach(hash, clear_i, 0);
2898 else if (RHASH_AR_TABLE_P(hash)) {
2902 st_clear(RHASH_ST_TABLE(hash));
2903 compact_after_delete(hash);
2910hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2917rb_hash_key_str(
VALUE key)
2920 return rb_fstring(key);
2928hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2931 *key = rb_hash_key_str(*key);
2933 return hash_aset(key, val, arg, existing);
2936NOINSERT_UPDATE_CALLBACK(hash_aset)
2937NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2970 rb_hash_modify(hash);
2972 if (RHASH_TABLE_NULL_P(hash)) {
2973 if (iter_lev > 0) no_new_key();
2974 ar_alloc_table(hash);
2978 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
2981 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
2999 rb_hash_modify_check(hash);
3000 if (hash == hash2)
return hash;
3004 hash2 = to_hash(hash2);
3006 COPY_DEFAULT(hash, hash2);
3008 if (RHASH_AR_TABLE_P(hash)) {
3009 ar_free_and_clear_table(hash);
3012 st_free_table(RHASH_ST_TABLE(hash));
3013 RHASH_ST_CLEAR(hash);
3015 hash_copy(hash, hash2);
3018 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), 0));
3021 rb_gc_writebarrier_remember(hash);
3059rb_hash_empty_p(
VALUE hash)
3096rb_hash_each_value(
VALUE hash)
3099 rb_hash_foreach(hash, each_value_i, 0);
3134rb_hash_each_key(
VALUE hash)
3137 rb_hash_foreach(hash, each_key_i, 0);
3144 rb_yield(rb_assoc_new(key, value));
3187rb_hash_each_pair(
VALUE hash)
3190 if (rb_block_pair_yield_optimizable())
3191 rb_hash_foreach(hash, each_pair_i_fast, 0);
3193 rb_hash_foreach(hash, each_pair_i, 0);
3207 VALUE trans = p->trans, result = p->result;
3208 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3209 if (UNDEF_P(new_key)) {
3215 rb_hash_aset(result, new_key, value);
3223 rb_hash_aset(result, new_key, value);
3265rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3272 transarg.trans = to_hash(argv[0]);
3278 result = rb_hash_new();
3280 if (transarg.trans) {
3281 transarg.result = result;
3282 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3285 rb_hash_foreach(hash, transform_keys_i, result);
3305rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3308 int block_given = 0;
3312 trans = to_hash(argv[0]);
3318 rb_hash_modify_check(hash);
3319 if (!RHASH_TABLE_EMPTY_P(hash)) {
3321 VALUE new_keys = hash_alloc(0);
3323 rb_hash_foreach(hash, flatten_i, pairs);
3330 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3333 else if (block_given) {
3340 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3341 rb_hash_stlike_delete(hash, &key, NULL);
3343 rb_hash_aset(hash, new_key, val);
3344 rb_hash_aset(new_keys, new_key,
Qnil);
3346 rb_ary_clear(pairs);
3347 rb_hash_clear(new_keys);
3349 compact_after_delete(hash);
3354transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3360transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3364 rb_hash_modify(hash);
3390rb_hash_transform_values(
VALUE hash)
3395 result = hash_dup_with_compare_by_id(hash);
3396 SET_DEFAULT(result,
Qnil);
3399 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3400 compact_after_delete(result);
3422rb_hash_transform_values_bang(
VALUE hash)
3425 rb_hash_modify_check(hash);
3427 if (!RHASH_TABLE_EMPTY_P(hash)) {
3428 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3437 rb_ary_push(ary, rb_assoc_new(key, value));
3452rb_hash_to_a(
VALUE hash)
3457 rb_hash_foreach(hash, to_a_i, ary);
3472 rb_enc_copy(str, str2);
3483inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3489 rb_hash_foreach(hash, inspect_i, str);
3507rb_hash_inspect(
VALUE hash)
3521rb_hash_to_hash(
VALUE hash)
3531 pair = rb_check_array_type(arg);
3534 rb_builtin_class_name(arg));
3552rb_hash_to_h_block(
VALUE hash)
3555 rb_hash_foreach(hash, to_h_i, h);
3579rb_hash_to_h(
VALUE hash)
3582 return rb_hash_to_h_block(hash);
3586 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3594 rb_ary_push(ary, key);
3607MJIT_FUNC_EXPORTED
VALUE
3608rb_hash_keys(
VALUE hash)
3611 VALUE keys = rb_ary_new_capa(size);
3613 if (size == 0)
return keys;
3615 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3617 if (RHASH_AR_TABLE_P(hash)) {
3618 size = ar_keys(hash, ptr, size);
3621 st_table *table = RHASH_ST_TABLE(hash);
3622 size = st_keys(table, ptr, size);
3625 rb_gc_writebarrier_remember(keys);
3626 rb_ary_set_len(keys, size);
3629 rb_hash_foreach(hash, keys_i, keys);
3638 rb_ary_push(ary, value);
3652rb_hash_values(
VALUE hash)
3657 values = rb_ary_new_capa(size);
3658 if (size == 0)
return values;
3660 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3661 if (RHASH_AR_TABLE_P(hash)) {
3662 rb_gc_writebarrier_remember(values);
3664 size = ar_values(hash, ptr, size);
3667 else if (RHASH_ST_TABLE_P(hash)) {
3668 st_table *table = RHASH_ST_TABLE(hash);
3669 rb_gc_writebarrier_remember(values);
3671 size = st_values(table, ptr, size);
3674 rb_ary_set_len(values, size);
3678 rb_hash_foreach(hash, values_i, values);
3696MJIT_FUNC_EXPORTED
VALUE
3699 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3731 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3747 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3761recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3765 if (recur)
return Qtrue;
3767 data->result =
Qtrue;
3768 rb_hash_foreach(hash, eql_i, dt);
3770 return data->result;
3778 if (hash1 == hash2)
return Qtrue;
3784 if (
rb_eql(hash2, hash1)) {
3797 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3798 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3810 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3838 return hash_equal(hash1, hash2, FALSE);
3863 return hash_equal(hash1, hash2, TRUE);
3869 st_index_t *hval = (st_index_t *)arg;
3870 st_index_t hdata[2];
3872 hdata[0] = rb_hash(key);
3873 hdata[1] = rb_hash(val);
3874 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3893rb_hash_hash(
VALUE hash)
3899 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
3908 rb_hash_aset(hash, value, key);
3928rb_hash_invert(
VALUE hash)
3932 rb_hash_foreach(hash, rb_hash_invert_i, h);
3937rb_hash_update_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3943NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback)
3948 RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
3953rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3955 st_data_t newvalue = arg->arg;
3964NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3969 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
4024rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4029 rb_hash_modify(self);
4030 for (i = 0; i < argc; i++){
4031 VALUE hash = to_hash(argv[i]);
4033 rb_hash_foreach(hash, rb_hash_update_block_i, self);
4036 rb_hash_foreach(hash, rb_hash_update_i, self);
4049rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4052 VALUE newvalue = uf_arg->value;
4055 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4061NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4067 VALUE hash = arg->hash;
4070 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4077 rb_hash_modify(hash1);
4078 hash2 = to_hash(hash2);
4083 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4086 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4143rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4145 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4155lookup2_call(
VALUE arg)
4158 return rb_hash_lookup2(args[0], args[1],
Qundef);
4167reset_hash_type(
VALUE arg)
4170 HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
4171 RHASH_ST_TABLE(p->hash)->type = p->orighash;
4181 args[1] = rb_assoc_new(key, val);
4207 ar_force_convert_table(hash, __FILE__, __LINE__);
4208 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4209 table = RHASH_ST_TABLE(hash);
4210 orighash = table->type;
4212 if (orighash != &identhash) {
4217 assochash.compare = assoc_cmp;
4218 assochash.hash = orighash->hash;
4219 table->type = &assochash;
4222 ensure_arg.hash = hash;
4223 ensure_arg.orighash = orighash;
4225 if (!UNDEF_P(value))
return rb_assoc_new(key, value);
4230 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4240 args[1] = rb_assoc_new(key, val);
4266 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4277 rb_ary_cat(ary, pair, 2);
4314rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4323 if (level == 0)
return rb_hash_to_a(hash);
4326 rb_hash_foreach(hash, flatten_i, ary);
4331 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4333 else if (level < 0) {
4335 rb_funcallv(ary, id_flatten_bang, 0, 0);
4340 rb_hash_foreach(hash, flatten_i, ary);
4358 if (!
NIL_P(value)) {
4359 rb_hash_aset(hash, key, value);
4375rb_hash_compact(
VALUE hash)
4377 VALUE result = rb_hash_new();
4379 rb_hash_foreach(hash, set_if_not_nil, result);
4396rb_hash_compact_bang(
VALUE hash)
4399 rb_hash_modify_check(hash);
4402 rb_hash_foreach(hash, delete_if_nil, hash);
4409static st_table *rb_init_identtable_with_size(st_index_t size);
4440rb_hash_compare_by_id(
VALUE hash)
4445 if (rb_hash_compare_by_id_p(hash))
return hash;
4447 rb_hash_modify_check(hash);
4448 ar_force_convert_table(hash, __FILE__, __LINE__);
4449 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4451 tmp = hash_alloc(0);
4452 identtable = rb_init_identtable_with_size(
RHASH_SIZE(hash));
4453 RHASH_ST_TABLE_SET(tmp, identtable);
4454 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4455 st_free_table(RHASH_ST_TABLE(hash));
4456 RHASH_ST_TABLE_SET(hash, identtable);
4457 RHASH_ST_CLEAR(tmp);
4469MJIT_FUNC_EXPORTED
VALUE
4470rb_hash_compare_by_id_p(
VALUE hash)
4472 return RBOOL(RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->
type == &identhash);
4476rb_ident_hash_new(
void)
4478 VALUE hash = rb_hash_new();
4479 RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
4484rb_ident_hash_new_with_size(st_index_t size)
4486 VALUE hash = rb_hash_new();
4487 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(&identhash, size));
4492rb_init_identtable(
void)
4494 return st_init_table(&identhash);
4498rb_init_identtable_with_size(st_index_t size)
4500 return st_init_table_with_size(&identhash, size);
4566rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4575 rb_warn(
"given block not used");
4579 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4586 if (rb_block_pair_yield_optimizable())
4587 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4589 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4624rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4627 self = rb_hash_aref(self, *argv);
4628 if (!--argc)
return self;
4630 return rb_obj_dig(argc, argv, self,
Qnil);
4638 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4649 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4667 other = to_hash(other);
4669 return hash_le(hash, other);
4686 other = to_hash(other);
4688 return hash_le(hash, other);
4705 other = to_hash(other);
4707 return hash_le(other, hash);
4724 other = to_hash(other);
4726 return hash_le(other, hash);
4733 return rb_hash_aref(hash, *argv);
4749rb_hash_to_proc(
VALUE hash)
4751 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4755rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4761add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4764 if (existing)
return ST_STOP;
4783 if (RHASH_AR_TABLE_P(hash)) {
4784 hash_ar_table(hash);
4786 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4790 ar_force_convert_table(hash, __FILE__, __LINE__);
4792 tbl = RHASH_TBL_RAW(hash);
4793 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4798key_stringify(
VALUE key)
4801 rb_hash_key_str(key) : key;
4805ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4808 for (i = 0; i < argc; ) {
4809 st_data_t k = key_stringify(argv[i++]);
4810 st_data_t v = argv[i++];
4811 ar_insert(hash, k, v);
4820 HASH_ASSERT(argc % 2 == 0);
4822 st_index_t size = argc / 2;
4824 if (RHASH_TABLE_NULL_P(hash)) {
4825 if (size <= RHASH_AR_TABLE_MAX_SIZE) {
4826 hash_ar_table(hash);
4829 RHASH_TBL_RAW(hash);
4833 if (RHASH_AR_TABLE_P(hash) &&
4834 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4835 ar_bulk_insert(hash, argc, argv);
4838 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4843static char **origenviron;
4845#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4846#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4847static char **my_environ;
4849#define environ my_environ
4851#define getenv(n) rb_w32_ugetenv(n)
4852#elif defined(__APPLE__)
4854#define environ (*_NSGetEnviron())
4855#define GET_ENVIRON(e) (e)
4856#define FREE_ENVIRON(e)
4858extern char **environ;
4859#define GET_ENVIRON(e) (e)
4860#define FREE_ENVIRON(e)
4862#ifdef ENV_IGNORECASE
4863#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4864#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4866#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4867#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4870#define ENV_LOCK() RB_VM_LOCK_ENTER()
4871#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4877 return rb_utf8_encoding();
4879 return rb_locale_encoding();
4884env_enc_str_new(
const char *ptr,
long len,
rb_encoding *enc)
4893env_str_new(
const char *ptr,
long len)
4895 return env_enc_str_new(ptr, len, env_encoding());
4899env_str_new2(
const char *ptr)
4901 if (!ptr)
return Qnil;
4902 return env_str_new(ptr, strlen(ptr));
4906getenv_with_lock(
const char *name)
4911 const char *val = getenv(name);
4912 ret = env_str_new2(val);
4919has_env_with_lock(
const char *name)
4929 return val ? true :
false;
4932static const char TZ_ENV[] =
"TZ";
4949 return rb_str_fill_terminator(str, 1);
4952#define get_env_ptr(var, val) \
4953 (var = get_env_cstr(val, #var))
4955static inline const char *
4956env_name(
volatile VALUE *s)
4960 get_env_ptr(name, *s);
4964#define env_name(s) env_name(&(s))
4969reset_by_modified_env(
const char *nam)
4977 if (ENVMATCH(nam, TZ_ENV)) {
4978 ruby_reset_timezone();
4983env_delete(
VALUE name)
4985 const char *nam = env_name(name);
4986 reset_by_modified_env(nam);
4987 VALUE val = getenv_with_lock(nam);
4990 ruby_setenv(nam, 0);
5025 val = env_delete(name);
5045 const char *nam = env_name(name);
5046 VALUE env = getenv_with_lock(nam);
5086 if (block_given && argc == 2) {
5087 rb_warn(
"block supersedes default value argument");
5089 nam = env_name(key);
5090 env = getenv_with_lock(nam);
5093 if (block_given)
return rb_yield(key);
5095 rb_key_err_raise(
rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5102#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5105in_origenv(
const char *str)
5108 for (env = origenviron; *env; ++env) {
5109 if (*env == str)
return 1;
5115envix(
const char *nam)
5119 register int i, len = strlen(nam);
5122 env = GET_ENVIRON(environ);
5123 for (i = 0; env[i]; i++) {
5124 if (ENVNMATCH(env[i],nam,len) && env[i][len] ==
'=')
5127 FREE_ENVIRON(environ);
5134getenvsize(
const WCHAR* p)
5136 const WCHAR* porg = p;
5137 while (*p++) p += lstrlenW(p) + 1;
5138 return p - porg + 1;
5142getenvblocksize(
void)
5152check_envsize(
size_t n)
5154 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5158 WCHAR* p = GetEnvironmentStringsW();
5161 FreeEnvironmentStringsW(p);
5162 if (n >= getenvblocksize()) {
5170#if defined(_WIN32) || \
5171 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5173NORETURN(
static void invalid_envname(
const char *name));
5176invalid_envname(
const char *name)
5182check_envname(
const char *name)
5184 if (strchr(name,
'=')) {
5185 invalid_envname(name);
5192ruby_setenv(
const char *name,
const char *value)
5195# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5196# define HAVE__WPUTENV_S 1
5203 check_envname(name);
5204 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5207 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5208 if (check_envsize((
size_t)len + len2)) {
5211 wname =
ALLOCV_N(WCHAR, buf, len + len2);
5212 wvalue = wname + len;
5213 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5214 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5215#ifndef HAVE__WPUTENV_S
5216 wname[len-1] = L
'=';
5220 wname =
ALLOCV_N(WCHAR, buf, len + 1);
5221 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5222 wvalue = wname + len;
5224#ifndef HAVE__WPUTENV_S
5225 wname[len-1] = L
'=';
5231#ifndef HAVE__WPUTENV_S
5232 failed = _wputenv(wname);
5234 failed = _wputenv_s(wname, wvalue);
5242 if (!value || !*value) {
5244 if (!SetEnvironmentVariable(name, value) &&
5245 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5249 invalid_envname(name);
5251#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5256 ret = setenv(name, value, 1);
5273 ret = unsetenv(name);
5285 size_t len, mem_size;
5286 char **env_ptr, *str, *mem_ptr;
5288 check_envname(name);
5291 mem_size = len + strlen(value) + 2;
5292 mem_ptr = malloc(mem_size);
5293 if (mem_ptr == NULL)
5295 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5300 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5301 if (!strncmp(str, name, len) && str[len] ==
'=') {
5302 if (!in_origenv(str)) free(str);
5303 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5314 ret = putenv(mem_ptr);
5331 if (environ == origenviron) {
5336 for (max = i; environ[max]; max++) ;
5337 tmpenv =
ALLOC_N(
char*, max+2);
5338 for (j=0; j<max; j++)
5345 char **envp = origenviron;
5346 while (*envp && *envp != environ[i]) envp++;
5350 while (environ[i]) {
5351 environ[i] = environ[i+1];
5358 if (!value)
goto finish;
5363 len = strlen(name) + strlen(value) + 2;
5364 environ[i] =
ALLOC_N(
char, len);
5365 snprintf(environ[i],len,
"%s=%s",name,value);
5374ruby_unsetenv(
const char *name)
5376 ruby_setenv(name, 0);
5426 return env_aset(nm, val);
5442 get_env_ptr(name, nm);
5443 get_env_ptr(value, val);
5445 ruby_setenv(name, value);
5446 reset_by_modified_env(name);
5453 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5454 VALUE ary = rb_ary_new();
5458 char **env = GET_ENVIRON(environ);
5460 char *s = strchr(*env,
'=');
5462 const char *p = *env;
5465 rb_ary_push(ary, e);
5469 FREE_ENVIRON(environ);
5492 return env_keys(FALSE);
5503 env = GET_ENVIRON(environ);
5504 for (; *env ; ++env) {
5505 if (strchr(*env,
'=')) {
5509 FREE_ENVIRON(environ);
5534env_each_key(
VALUE ehash)
5540 keys = env_keys(FALSE);
5550 VALUE ary = rb_ary_new();
5554 char **env = GET_ENVIRON(environ);
5557 char *s = strchr(*env,
'=');
5559 rb_ary_push(ary, env_str_new2(s+1));
5563 FREE_ENVIRON(environ);
5585 return env_values();
5606env_each_value(
VALUE ehash)
5612 values = env_values();
5638env_each_pair(
VALUE ehash)
5644 VALUE ary = rb_ary_new();
5648 char **env = GET_ENVIRON(environ);
5651 char *s = strchr(*env,
'=');
5653 rb_ary_push(ary, env_str_new(*env, s-*env));
5654 rb_ary_push(ary, env_str_new2(s+1));
5658 FREE_ENVIRON(environ);
5662 if (rb_block_pair_yield_optimizable()) {
5699env_reject_bang(
VALUE ehash)
5706 keys = env_keys(FALSE);
5707 RBASIC_CLEAR_CLASS(keys);
5718 if (del == 0)
return Qnil;
5743env_delete_if(
VALUE ehash)
5746 env_reject_bang(ehash);
5773 result = rb_ary_new();
5774 for (i=0; i<argc; i++) {
5775 rb_ary_push(result, rb_f_getenv(
Qnil, argv[i]));
5802env_select(
VALUE ehash)
5809 result = rb_hash_new();
5810 keys = env_keys(FALSE);
5816 rb_hash_aset(result, key, val);
5863env_select_bang(
VALUE ehash)
5870 keys = env_keys(FALSE);
5871 RBASIC_CLEAR_CLASS(keys);
5882 if (del == 0)
return Qnil;
5905env_keep_if(
VALUE ehash)
5908 env_select_bang(ehash);
5928 VALUE key, value, result;
5931 return rb_hash_new();
5933 result = rb_hash_new_with_size(argc);
5935 for (i = 0; i < argc; i++) {
5937 value = rb_f_getenv(
Qnil, key);
5939 rb_hash_aset(result, key, value);
5951 keys = env_keys(TRUE);
5955 ruby_setenv(nam, 0);
5974 return rb_env_clear();
6006 char **env = GET_ENVIRON(environ);
6008 char *s = strchr(*env,
'=');
6010 if (env != environ) {
6022 FREE_ENVIRON(environ);
6043 VALUE ary = rb_ary_new();
6047 char **env = GET_ENVIRON(environ);
6049 char *s = strchr(*env,
'=');
6051 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
6052 env_str_new2(s+1)));
6056 FREE_ENVIRON(environ);
6078env_size_with_lock(
void)
6084 char **env = GET_ENVIRON(environ);
6086 FREE_ENVIRON(environ);
6106 return INT2FIX(env_size_with_lock());
6126 char **env = GET_ENVIRON(environ);
6130 FREE_ENVIRON(environ);
6134 return RBOOL(empty);
6165 const char *s = env_name(key);
6166 return RBOOL(has_env_with_lock(s));
6192 const char *s = env_name(key);
6193 VALUE e = getenv_with_lock(s);
6196 return rb_assoc_new(key, e);
6225 char **env = GET_ENVIRON(environ);
6227 char *s = strchr(*env,
'=');
6229 long len = strlen(s);
6237 FREE_ENVIRON(environ);
6268 char **env = GET_ENVIRON(environ);
6271 const char *p = *env;
6272 char *s = strchr(p,
'=');
6274 long len = strlen(s);
6276 result = rb_assoc_new(
rb_str_new(p, s-p-1), obj);
6282 FREE_ENVIRON(environ);
6313 char **env = GET_ENVIRON(environ);
6315 char *s = strchr(*env,
'=');
6317 long len = strlen(s);
6319 str = env_str_new(*env, s-*env-1);
6325 FREE_ENVIRON(environ);
6335 VALUE hash = rb_hash_new();
6339 char **env = GET_ENVIRON(environ);
6341 char *s = strchr(*env,
'=');
6343 rb_hash_aset(hash, env_str_new(*env, s-*env),
6348 FREE_ENVIRON(environ);
6364 return env_to_hash();
6379 return env_to_hash();
6403 VALUE hash = env_to_hash();
6405 hash = rb_hash_to_h_block(hash);
6423 VALUE key, hash = env_to_hash();
6425 for (i = 0; i < argc; i++) {
6427 rb_hash_delete(hash, key);
6451 return rb_hash_delete_if(env_to_hash());
6454NORETURN(
static VALUE env_freeze(
VALUE self));
6463env_freeze(
VALUE self)
6492 char **env = GET_ENVIRON(environ);
6494 const char *p = *env;
6495 char *s = strchr(p,
'=');
6497 key = env_str_new(p, s-p);
6499 result = rb_assoc_new(key, val);
6502 FREE_ENVIRON(environ);
6531 return rb_hash_invert(env_to_hash());
6538 const char *keyptr, *eptr;
6546 if (elen != keylen)
continue;
6547 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6548 rb_ary_delete_at(keys, i);
6559 keylist_delete(keys, key);
6587 keys = env_keys(TRUE);
6588 if (env == hash)
return env;
6589 hash = to_hash(hash);
6590 rb_hash_foreach(hash, env_replace_i, keys);
6610 if (!
NIL_P(oldval)) {
6659 env_update_block_i : env_update_i;
6660 for (
int i = 0; i < argc; ++i) {
6661 VALUE hash = argv[i];
6662 if (env == hash)
continue;
6663 hash = to_hash(hash);
6664 rb_hash_foreach(hash, func, 0);
6684 rb_get_freeze_opt(1, &opt);
6713 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7195 id_hash_iter_lev = rb_make_internal_id();
7457 origenviron = environ;
7527 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7529 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
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.
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 rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#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 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.
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.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
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.
#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_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.
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,...
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.
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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
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_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_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
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_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.
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.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
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.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
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.
#define RHASH_ITER_LEV(h)
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define SafeStringValue(v)
#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.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer 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.