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);
739ar_try_convert_table(
VALUE hash)
741 if (!RHASH_AR_TABLE_P(hash))
return;
743 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
748 if (size < RHASH_AR_TABLE_MAX_SIZE) {
752 new_tab = st_init_table_with_size(&objhash, size * 2);
754 for (i = 0; i < RHASH_AR_TABLE_MAX_BOUND; i++) {
756 st_add_direct(new_tab, pair->key, pair->val);
758 ar_free_and_clear_table(hash);
759 RHASH_ST_TABLE_SET(hash, new_tab);
764ar_force_convert_table(
VALUE hash,
const char *file,
int line)
768 if (RHASH_ST_TABLE_P(hash)) {
769 return RHASH_ST_TABLE(hash);
772 if (RHASH_AR_TABLE(hash)) {
773 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
775#if defined(RHASH_CONVERT_TABLE_DEBUG) && RHASH_CONVERT_TABLE_DEBUG
776 rb_obj_info_dump(hash);
777 fprintf(stderr,
"force_convert: %s:%d\n", file, line);
778 RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
781 new_tab = st_init_table_with_size(&objhash, RHASH_AR_TABLE_SIZE(hash));
783 for (i = 0; i < bound; i++) {
784 if (ar_cleared_entry(hash, i))
continue;
787 st_add_direct(new_tab, pair->key, pair->val);
789 ar_free_and_clear_table(hash);
792 new_tab = st_init_table(&objhash);
794 RHASH_ST_TABLE_SET(hash, new_tab);
800hash_ar_table(
VALUE hash)
802 if (RHASH_TABLE_NULL_P(hash)) {
803 ar_alloc_table(hash);
805 return RHASH_AR_TABLE(hash);
809ar_compact_table(
VALUE hash)
811 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
812 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
821 for (i=0; i<bound; i++) {
822 if (ar_cleared_entry(hash, i)) {
824 for (; j<bound; j++) {
825 if (!ar_cleared_entry(hash, j)) {
827 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
828 ar_clear_entry(hash, j);
839 HASH_ASSERT(i<=bound);
841 RHASH_AR_TABLE_BOUND_SET(hash, size);
848ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
850 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
852 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
856 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
857 bin = ar_compact_table(hash);
860 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
862 ar_set_entry(hash, bin, key, val, hash_value);
863 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
864 RHASH_AR_TABLE_SIZE_INC(hash);
870ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
872 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
873 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
875 for (i = 0; i < bound; i++) {
876 if (ar_cleared_entry(hash, i))
continue;
879 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
890 VALUE key = pair->key;
891 VALUE val = pair->val;
892 retval = (*replace)(&key, &val, arg, TRUE);
901 ar_clear_entry(hash, i);
902 RHASH_AR_TABLE_SIZE_DEC(hash);
911ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
913 return ar_general_foreach(hash, func, replace, arg);
917 st_foreach_callback_func *func;
922apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
924 const struct functor *f = (
void *)d;
925 return f->func(k, v, f->arg);
929ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
931 const struct functor f = { func, arg };
932 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
936ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
939 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
940 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
941 enum st_retval retval;
946 for (i = 0; i < bound; i++) {
947 if (ar_cleared_entry(hash, i))
continue;
949 pair = RHASH_AR_TABLE_REF(hash, i);
951 hint = ar_hint(hash, i);
953 retval = (*func)(key, pair->val, arg, 0);
958 pair = RHASH_AR_TABLE_REF(hash, i);
959 if (pair->key == never)
break;
960 ret = ar_find_entry_hint(hash, hint, key);
961 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
962 retval = (*func)(0, 0, arg, 1);
972 if (!ar_cleared_entry(hash, i)) {
973 ar_clear_entry(hash, i);
974 RHASH_AR_TABLE_SIZE_DEC(hash);
985ar_update(
VALUE hash, st_data_t key,
986 st_update_callback_func *func, st_data_t arg)
988 int retval, existing;
989 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
990 st_data_t value = 0, old_key;
991 st_hash_t hash_value = ar_do_hash(key);
993 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
998 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
999 bin = ar_find_entry(hash, hash_value, key);
1000 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
1003 hash_ar_table(hash);
1013 retval = (*func)(&key, &value, arg, existing);
1019 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1025 if (old_key != key) {
1033 ar_clear_entry(hash, bin);
1034 RHASH_AR_TABLE_SIZE_DEC(hash);
1042ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1044 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1045 st_hash_t hash_value = ar_do_hash(key);
1047 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1052 hash_ar_table(hash);
1054 bin = ar_find_entry(hash, hash_value, key);
1055 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1056 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1059 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1060 bin = ar_compact_table(hash);
1061 hash_ar_table(hash);
1063 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1065 ar_set_entry(hash, bin, key, value, hash_value);
1066 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1067 RHASH_AR_TABLE_SIZE_INC(hash);
1071 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1077ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1079 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1083 st_hash_t hash_value = ar_do_hash(key);
1084 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1086 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1088 unsigned bin = ar_find_entry(hash, hash_value, key);
1090 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1094 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1095 if (value != NULL) {
1096 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1104ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1107 st_hash_t hash_value = ar_do_hash(*key);
1109 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1111 return st_delete(RHASH_ST_TABLE(hash), key, value);
1114 bin = ar_find_entry(hash, hash_value, *key);
1116 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1117 if (value != 0) *value = 0;
1125 ar_clear_entry(hash, bin);
1126 RHASH_AR_TABLE_SIZE_DEC(hash);
1132ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1134 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1135 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1137 for (i = 0; i < bound; i++) {
1138 if (!ar_cleared_entry(hash, i)) {
1140 if (value != 0) *value = pair->val;
1142 ar_clear_entry(hash, i);
1143 RHASH_AR_TABLE_SIZE_DEC(hash);
1148 if (value != NULL) *value = 0;
1153ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1155 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1156 st_data_t *keys_start = keys, *keys_end = keys + size;
1158 for (i = 0; i < bound; i++) {
1159 if (keys == keys_end) {
1163 if (!ar_cleared_entry(hash, i)) {
1164 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1169 return keys - keys_start;
1173ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1175 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1176 st_data_t *values_start = values, *values_end = values + size;
1178 for (i = 0; i < bound; i++) {
1179 if (values == values_end) {
1183 if (!ar_cleared_entry(hash, i)) {
1184 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1189 return values - values_start;
1195 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1197 if (old_tab != NULL) {
1198 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1199 if (new_tab == NULL) {
1201 if (new_tab != NULL) {
1202 RHASH_SET_TRANSIENT_FLAG(hash1);
1205 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1209 *new_tab = *old_tab;
1210 RHASH(hash1)->ar_hint.word = RHASH(hash2)->ar_hint.word;
1211 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1212 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1213 hash_ar_table_set(hash1, new_tab);
1215 rb_gc_writebarrier_remember(hash1);
1219 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1220 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1222 if (RHASH_TRANSIENT_P(hash1)) {
1223 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1225 else if (RHASH_AR_TABLE(hash1)) {
1226 ruby_xfree(RHASH_AR_TABLE(hash1));
1229 hash_ar_table_set(hash1, NULL);
1231 rb_gc_writebarrier_remember(hash1);
1239 if (RHASH_AR_TABLE(hash) != NULL) {
1240 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1241 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1244 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1245 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1249#if USE_TRANSIENT_HEAP
1251rb_hash_transient_heap_evacuate(
VALUE hash,
int promote)
1253 if (RHASH_TRANSIENT_P(hash)) {
1255 ar_table *old_tab = RHASH_AR_TABLE(hash);
1257 if (UNLIKELY(old_tab == NULL)) {
1260 HASH_ASSERT(old_tab != NULL);
1262 new_tab = rb_transient_heap_alloc(hash,
sizeof(
ar_table));
1263 if (new_tab == NULL) promote =
true;
1266 new_tab = ruby_xmalloc(
sizeof(
ar_table));
1267 RHASH_UNSET_TRANSIENT_FLAG(hash);
1269 *new_tab = *old_tab;
1270 hash_ar_table_set(hash, new_tab);
1276typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1280 st_foreach_func *func;
1285foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1290 if (error)
return ST_STOP;
1291 status = (*arg->func)(key, value, arg->arg);
1292 if (status == ST_CONTINUE) {
1304 arg.func = (st_foreach_func *)func;
1306 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1315 rb_foreach_func *func;
1320hash_iter_status_check(
int status)
1335hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1339 if (error)
return ST_STOP;
1341 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1344 return hash_iter_status_check(status);
1348hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1352 if (error)
return ST_STOP;
1354 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1355 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1357 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1361 return hash_iter_status_check(status);
1365iter_lev_in_ivar(
VALUE hash)
1375iter_lev_in_ivar_set(
VALUE hash,
int lev)
1377 rb_ivar_set_internal(hash, id_hash_iter_lev,
INT2FIX(lev));
1381iter_lev_in_flags(
VALUE hash)
1383 unsigned int u = (
unsigned int)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1388iter_lev_in_flags_set(
VALUE hash,
int lev)
1390 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1396 int lev = iter_lev_in_flags(hash);
1398 if (lev == RHASH_LEV_MAX) {
1399 return iter_lev_in_ivar(hash);
1407hash_iter_lev_inc(
VALUE hash)
1409 int lev = iter_lev_in_flags(hash);
1410 if (lev == RHASH_LEV_MAX) {
1411 lev = iter_lev_in_ivar(hash);
1412 iter_lev_in_ivar_set(hash, lev+1);
1416 iter_lev_in_flags_set(hash, lev);
1417 if (lev == RHASH_LEV_MAX) {
1418 iter_lev_in_ivar_set(hash, lev);
1424hash_iter_lev_dec(
VALUE hash)
1426 int lev = iter_lev_in_flags(hash);
1427 if (lev == RHASH_LEV_MAX) {
1428 lev = iter_lev_in_ivar(hash);
1429 HASH_ASSERT(lev > 0);
1430 iter_lev_in_ivar_set(hash, lev-1);
1433 HASH_ASSERT(lev > 0);
1434 iter_lev_in_flags_set(hash, lev - 1);
1439hash_foreach_ensure_rollback(
VALUE hash)
1441 hash_iter_lev_inc(hash);
1446hash_foreach_ensure(
VALUE hash)
1448 hash_iter_lev_dec(hash);
1453rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1455 if (RHASH_AR_TABLE_P(hash)) {
1456 return ar_foreach(hash, func, arg);
1459 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1464rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1466 if (RHASH_AR_TABLE_P(hash)) {
1467 return ar_foreach_with_replace(hash, func, replace, arg);
1470 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1475hash_foreach_call(
VALUE arg)
1479 if (RHASH_AR_TABLE_P(hash)) {
1480 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1481 (st_data_t)arg, (st_data_t)
Qundef);
1483 else if (RHASH_ST_TABLE_P(hash)) {
1484 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1485 (st_data_t)arg, (st_data_t)
Qundef);
1494rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1498 if (RHASH_TABLE_EMPTY_P(hash))
1501 arg.func = (rb_foreach_func *)func;
1504 hash_foreach_call((
VALUE)&arg);
1507 hash_iter_lev_inc(hash);
1508 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1513void rb_st_compact_table(
st_table *tab);
1516compact_after_delete(
VALUE hash)
1519 rb_st_compact_table(RHASH_ST_TABLE(hash));
1535hash_alloc(
VALUE klass)
1537 return hash_alloc_flags(klass, 0,
Qnil);
1541empty_hash_alloc(
VALUE klass)
1543 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1545 return hash_alloc(klass);
1557 if (rb_hash_compare_by_id_p(basis)) {
1558 return rb_hash_compare_by_id(hash);
1563MJIT_FUNC_EXPORTED
VALUE
1564rb_hash_new_with_size(st_index_t size)
1566 VALUE ret = rb_hash_new();
1570 else if (size <= RHASH_AR_TABLE_MAX_SIZE) {
1571 ar_alloc_table(ret);
1574 RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
1580rb_hash_new_capa(
long capa)
1582 return rb_hash_new_with_size((st_index_t)capa);
1589 if (RHASH_AR_TABLE_P(hash))
1591 else if (RHASH_ST_TABLE_P(hash))
1592 RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
1598hash_dup_with_compare_by_id(
VALUE hash)
1600 return hash_copy(copy_compare_by_id(rb_hash_new(), hash), hash);
1606 return hash_copy(hash_alloc_flags(klass, flags,
RHASH_IFNONE(hash)),
1615 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1621MJIT_FUNC_EXPORTED
VALUE
1622rb_hash_resurrect(
VALUE hash)
1629rb_hash_modify_check(
VALUE hash)
1634MJIT_FUNC_EXPORTED
struct st_table *
1635rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1637 return ar_force_convert_table(hash, file, line);
1641rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1644 return rb_hash_tbl_raw(hash, file, line);
1648rb_hash_modify(
VALUE hash)
1650 rb_hash_modify_check(hash);
1653NORETURN(
static void no_new_key(
void));
1665#define NOINSERT_UPDATE_CALLBACK(func) \
1667func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1669 if (!existing) no_new_key(); \
1670 return func(key, val, (struct update_arg *)arg, existing); \
1674func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1676 return func(key, val, (struct update_arg *)arg, existing); \
1681 st_update_callback_func *func;
1687typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1690rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1692 if (RHASH_AR_TABLE_P(hash)) {
1693 int result = ar_update(hash, key, func, arg);
1695 ar_try_convert_table(hash);
1702 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1706tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1709 st_data_t old_key = *key;
1710 st_data_t old_value = *val;
1711 VALUE hash = p->hash;
1712 int ret = (p->func)(key, val, arg, existing);
1717 if (!existing || *key != old_key || *val != old_value) {
1718 rb_hash_modify(hash);
1725 rb_hash_modify(hash);
1733tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1736 .arg = optional_arg,
1740 .value = (
VALUE)optional_arg,
1743 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1752#define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
1754#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
1755 tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
1758#define RHASH_UPDATE(hash, key, func, arg) \
1759 RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
1767 if (n != 2 && (n >= 0 || n < -3)) {
1768 if (n < 0) n = -n-1;
1809rb_hash_initialize(
int argc,
VALUE *argv,
VALUE hash)
1813 rb_hash_modify(hash);
1817 SET_PROC_DEFAULT(hash, ifnone);
1821 ifnone = argc == 0 ?
Qnil : argv[0];
1864rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1869 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1871 hash = hash_alloc(klass);
1872 hash_copy(hash, tmp);
1876 tmp = rb_check_array_type(argv[0]);
1880 hash = hash_alloc(klass);
1883 VALUE v = rb_check_array_type(e);
1888 rb_builtin_class_name(e), i);
1898 rb_hash_aset(hash, key, val);
1904 if (argc % 2 != 0) {
1908 hash = hash_alloc(klass);
1909 rb_hash_bulk_insert(argc, argv, hash);
1914MJIT_FUNC_EXPORTED
VALUE
1915rb_to_hash_type(
VALUE hash)
1917 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1919#define to_hash rb_to_hash_type
1924 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1941rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1943 return rb_check_hash_type(hash);
1962rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1965 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1984rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
1987 hash = rb_hash_dup(hash);
1988 RHASH(hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2000 if (RHASH_AR_TABLE_P(arg)) {
2001 ar_insert(arg, (st_data_t)key, (st_data_t)value);
2004 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
2022rb_hash_rehash(
VALUE hash)
2030 rb_hash_modify_check(hash);
2031 if (RHASH_AR_TABLE_P(hash)) {
2032 tmp = hash_alloc(0);
2033 ar_alloc_table(tmp);
2034 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2035 ar_free_and_clear_table(hash);
2037 ar_free_and_clear_table(tmp);
2039 else if (RHASH_ST_TABLE_P(hash)) {
2040 st_table *old_tab = RHASH_ST_TABLE(hash);
2041 tmp = hash_alloc(0);
2042 tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
2043 RHASH_ST_TABLE_SET(tmp, tbl);
2044 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2045 st_free_table(old_tab);
2046 RHASH_ST_TABLE_SET(hash, tbl);
2047 RHASH_ST_CLEAR(tmp);
2056 VALUE args[2] = {hash, key};
2061rb_hash_default_unredefined(
VALUE hash)
2065 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2068 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2077 if (LIKELY(rb_hash_default_unredefined(hash))) {
2079 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2080 if (UNDEF_P(key))
return Qnil;
2081 return call_default_proc(ifnone, hash, key);
2089hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2093 if (RHASH_AR_TABLE_P(hash)) {
2094 return ar_lookup(hash, key, pval);
2097 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2101MJIT_FUNC_EXPORTED
int
2102rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2104 return hash_stlike_lookup(hash, key, pval);
2126 if (hash_stlike_lookup(hash, key, &val)) {
2130 return rb_hash_default_value(hash, key);
2139 if (hash_stlike_lookup(hash, key, &val)) {
2150 return rb_hash_lookup2(hash, key,
Qnil);
2177rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2187 if (block_given && argc == 2) {
2188 rb_warn(
"block supersedes default value argument");
2191 if (hash_stlike_lookup(hash, key, &val)) {
2198 else if (argc == 1) {
2204 rb_key_err_raise(
rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2215 return rb_hash_fetch_m(1, &key, hash);
2239rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2245 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2246 if (argc == 0)
return Qnil;
2247 return call_default_proc(ifnone, hash, argv[0]);
2268 rb_hash_modify_check(hash);
2269 SET_DEFAULT(hash, ifnone);
2286rb_hash_default_proc(
VALUE hash)
2288 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2309rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2313 rb_hash_modify_check(hash);
2315 SET_DEFAULT(hash, proc);
2318 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2321 "wrong default_proc type %s (expected Proc)",
2325 SET_PROC_DEFAULT(hash, proc);
2362 rb_hash_foreach(hash, key_i, (
VALUE)args);
2368rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2370 if (RHASH_AR_TABLE_P(hash)) {
2371 return ar_delete(hash, pkey, pval);
2374 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2386 st_data_t ktmp = (st_data_t)key, val;
2388 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2404 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2406 if (!UNDEF_P(deleted_value)) {
2407 return deleted_value;
2446 rb_hash_modify_check(hash);
2447 val = rb_hash_delete_entry(hash, key);
2449 if (!UNDEF_P(val)) {
2450 compact_after_delete(hash);
2493rb_hash_shift(
VALUE hash)
2497 rb_hash_modify_check(hash);
2498 if (RHASH_AR_TABLE_P(hash)) {
2501 if (ar_shift(hash, &var.key, &var.val)) {
2502 return rb_assoc_new(var.key, var.val);
2506 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2507 if (!UNDEF_P(var.key)) {
2508 rb_hash_delete_entry(hash, var.key);
2509 return rb_assoc_new(var.key, var.val);
2513 if (RHASH_ST_TABLE_P(hash)) {
2516 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2517 return rb_assoc_new(var.key, var.val);
2521 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2522 if (!UNDEF_P(var.key)) {
2523 rb_hash_delete_entry(hash, var.key);
2524 return rb_assoc_new(var.key, var.val);
2535 rb_hash_modify(hash);
2544 return rb_hash_size(hash);
2568 rb_hash_modify_check(hash);
2569 if (!RHASH_TABLE_EMPTY_P(hash)) {
2570 rb_hash_foreach(hash, delete_if_i, hash);
2571 compact_after_delete(hash);
2595rb_hash_reject_bang(
VALUE hash)
2600 rb_hash_modify(hash);
2602 if (!n)
return Qnil;
2603 rb_hash_foreach(hash, delete_if_i, hash);
2627rb_hash_reject(
VALUE hash)
2632 result = hash_dup_with_compare_by_id(hash);
2634 rb_hash_foreach(result, delete_if_i, result);
2635 compact_after_delete(result);
2652rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2655 VALUE key, value, result;
2658 return copy_compare_by_id(rb_hash_new(), hash);
2660 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2662 for (i = 0; i < argc; i++) {
2664 value = rb_hash_lookup2(hash, key,
Qundef);
2665 if (!UNDEF_P(value))
2666 rb_hash_aset(result, key, value);
2684rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2689 result = hash_dup_with_compare_by_id(hash);
2691 for (i = 0; i < argc; i++) {
2693 rb_hash_delete(result, key);
2695 compact_after_delete(result);
2714rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2719 for (i=0; i<argc; i++) {
2720 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2746rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2751 for (i=0; i<argc; i++) {
2752 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2761 rb_hash_modify(hash);
2785rb_hash_select(
VALUE hash)
2790 result = hash_dup_with_compare_by_id(hash);
2792 rb_hash_foreach(result, keep_if_i, result);
2793 compact_after_delete(result);
2818rb_hash_select_bang(
VALUE hash)
2823 rb_hash_modify_check(hash);
2825 if (!n)
return Qnil;
2826 rb_hash_foreach(hash, keep_if_i, hash);
2849rb_hash_keep_if(
VALUE hash)
2852 rb_hash_modify_check(hash);
2853 if (!RHASH_TABLE_EMPTY_P(hash)) {
2854 rb_hash_foreach(hash, keep_if_i, hash);
2875 rb_hash_modify_check(hash);
2878 rb_hash_foreach(hash, clear_i, 0);
2880 else if (RHASH_AR_TABLE_P(hash)) {
2884 st_clear(RHASH_ST_TABLE(hash));
2885 compact_after_delete(hash);
2892hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2899rb_hash_key_str(
VALUE key)
2902 return rb_fstring(key);
2910hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2913 *key = rb_hash_key_str(*key);
2915 return hash_aset(key, val, arg, existing);
2918NOINSERT_UPDATE_CALLBACK(hash_aset)
2919NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2952 rb_hash_modify(hash);
2954 if (RHASH_TABLE_NULL_P(hash)) {
2955 if (iter_lev > 0) no_new_key();
2956 ar_alloc_table(hash);
2960 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
2963 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
2981 rb_hash_modify_check(hash);
2982 if (hash == hash2)
return hash;
2986 hash2 = to_hash(hash2);
2988 COPY_DEFAULT(hash, hash2);
2990 if (RHASH_AR_TABLE_P(hash)) {
2991 ar_free_and_clear_table(hash);
2994 st_free_table(RHASH_ST_TABLE(hash));
2995 RHASH_ST_CLEAR(hash);
2997 hash_copy(hash, hash2);
3000 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), 0));
3003 rb_gc_writebarrier_remember(hash);
3041rb_hash_empty_p(
VALUE hash)
3078rb_hash_each_value(
VALUE hash)
3081 rb_hash_foreach(hash, each_value_i, 0);
3116rb_hash_each_key(
VALUE hash)
3119 rb_hash_foreach(hash, each_key_i, 0);
3126 rb_yield(rb_assoc_new(key, value));
3169rb_hash_each_pair(
VALUE hash)
3172 if (rb_block_pair_yield_optimizable())
3173 rb_hash_foreach(hash, each_pair_i_fast, 0);
3175 rb_hash_foreach(hash, each_pair_i, 0);
3189 VALUE trans = p->trans, result = p->result;
3190 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3191 if (UNDEF_P(new_key)) {
3197 rb_hash_aset(result, new_key, value);
3205 rb_hash_aset(result, new_key, value);
3247rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3254 transarg.trans = to_hash(argv[0]);
3260 result = rb_hash_new();
3262 if (transarg.trans) {
3263 transarg.result = result;
3264 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3267 rb_hash_foreach(hash, transform_keys_i, result);
3287rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3290 int block_given = 0;
3294 trans = to_hash(argv[0]);
3300 rb_hash_modify_check(hash);
3301 if (!RHASH_TABLE_EMPTY_P(hash)) {
3303 VALUE new_keys = hash_alloc(0);
3305 rb_hash_foreach(hash, flatten_i, pairs);
3312 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3315 else if (block_given) {
3322 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3323 rb_hash_stlike_delete(hash, &key, NULL);
3325 rb_hash_aset(hash, new_key, val);
3326 rb_hash_aset(new_keys, new_key,
Qnil);
3328 rb_ary_clear(pairs);
3329 rb_hash_clear(new_keys);
3331 compact_after_delete(hash);
3336transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3342transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3346 rb_hash_modify(hash);
3372rb_hash_transform_values(
VALUE hash)
3377 result = hash_dup_with_compare_by_id(hash);
3378 SET_DEFAULT(result,
Qnil);
3381 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3382 compact_after_delete(result);
3404rb_hash_transform_values_bang(
VALUE hash)
3407 rb_hash_modify_check(hash);
3409 if (!RHASH_TABLE_EMPTY_P(hash)) {
3410 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3419 rb_ary_push(ary, rb_assoc_new(key, value));
3434rb_hash_to_a(
VALUE hash)
3439 rb_hash_foreach(hash, to_a_i, ary);
3454 rb_enc_copy(str, str2);
3465inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3471 rb_hash_foreach(hash, inspect_i, str);
3489rb_hash_inspect(
VALUE hash)
3503rb_hash_to_hash(
VALUE hash)
3513 pair = rb_check_array_type(arg);
3516 rb_builtin_class_name(arg));
3534rb_hash_to_h_block(
VALUE hash)
3537 rb_hash_foreach(hash, to_h_i, h);
3561rb_hash_to_h(
VALUE hash)
3564 return rb_hash_to_h_block(hash);
3568 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3576 rb_ary_push(ary, key);
3589MJIT_FUNC_EXPORTED
VALUE
3590rb_hash_keys(
VALUE hash)
3593 VALUE keys = rb_ary_new_capa(size);
3595 if (size == 0)
return keys;
3597 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3599 if (RHASH_AR_TABLE_P(hash)) {
3600 size = ar_keys(hash, ptr, size);
3603 st_table *table = RHASH_ST_TABLE(hash);
3604 size = st_keys(table, ptr, size);
3607 rb_gc_writebarrier_remember(keys);
3608 rb_ary_set_len(keys, size);
3611 rb_hash_foreach(hash, keys_i, keys);
3620 rb_ary_push(ary, value);
3634rb_hash_values(
VALUE hash)
3639 values = rb_ary_new_capa(size);
3640 if (size == 0)
return values;
3642 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3643 if (RHASH_AR_TABLE_P(hash)) {
3644 rb_gc_writebarrier_remember(values);
3646 size = ar_values(hash, ptr, size);
3649 else if (RHASH_ST_TABLE_P(hash)) {
3650 st_table *table = RHASH_ST_TABLE(hash);
3651 rb_gc_writebarrier_remember(values);
3653 size = st_values(table, ptr, size);
3656 rb_ary_set_len(values, size);
3660 rb_hash_foreach(hash, values_i, values);
3678MJIT_FUNC_EXPORTED
VALUE
3681 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3713 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3729 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3743recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3747 if (recur)
return Qtrue;
3749 data->result =
Qtrue;
3750 rb_hash_foreach(hash, eql_i, dt);
3752 return data->result;
3760 if (hash1 == hash2)
return Qtrue;
3766 if (
rb_eql(hash2, hash1)) {
3779 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3780 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3792 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3820 return hash_equal(hash1, hash2, FALSE);
3845 return hash_equal(hash1, hash2, TRUE);
3851 st_index_t *hval = (st_index_t *)arg;
3852 st_index_t hdata[2];
3854 hdata[0] = rb_hash(key);
3855 hdata[1] = rb_hash(val);
3856 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3875rb_hash_hash(
VALUE hash)
3881 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
3890 rb_hash_aset(hash, value, key);
3910rb_hash_invert(
VALUE hash)
3914 rb_hash_foreach(hash, rb_hash_invert_i, h);
3919rb_hash_update_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3925NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback)
3930 RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
3935rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3937 st_data_t newvalue = arg->arg;
3946NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3951 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
4006rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4011 rb_hash_modify(self);
4012 for (i = 0; i < argc; i++){
4013 VALUE hash = to_hash(argv[i]);
4015 rb_hash_foreach(hash, rb_hash_update_block_i, self);
4018 rb_hash_foreach(hash, rb_hash_update_i, self);
4031rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4034 VALUE newvalue = uf_arg->value;
4037 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4043NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4049 VALUE hash = arg->hash;
4052 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4059 rb_hash_modify(hash1);
4060 hash2 = to_hash(hash2);
4065 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4068 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4125rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4127 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4137lookup2_call(
VALUE arg)
4140 return rb_hash_lookup2(args[0], args[1],
Qundef);
4149reset_hash_type(
VALUE arg)
4152 HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
4153 RHASH_ST_TABLE(p->hash)->type = p->orighash;
4163 args[1] = rb_assoc_new(key, val);
4189 ar_force_convert_table(hash, __FILE__, __LINE__);
4190 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4191 table = RHASH_ST_TABLE(hash);
4192 orighash = table->type;
4194 if (orighash != &identhash) {
4199 assochash.compare = assoc_cmp;
4200 assochash.hash = orighash->hash;
4201 table->type = &assochash;
4204 ensure_arg.hash = hash;
4205 ensure_arg.orighash = orighash;
4207 if (!UNDEF_P(value))
return rb_assoc_new(key, value);
4212 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4222 args[1] = rb_assoc_new(key, val);
4248 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4259 rb_ary_cat(ary, pair, 2);
4296rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4305 if (level == 0)
return rb_hash_to_a(hash);
4308 rb_hash_foreach(hash, flatten_i, ary);
4313 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4315 else if (level < 0) {
4317 rb_funcallv(ary, id_flatten_bang, 0, 0);
4322 rb_hash_foreach(hash, flatten_i, ary);
4340 if (!
NIL_P(value)) {
4341 rb_hash_aset(hash, key, value);
4357rb_hash_compact(
VALUE hash)
4359 VALUE result = rb_hash_new();
4361 rb_hash_foreach(hash, set_if_not_nil, result);
4378rb_hash_compact_bang(
VALUE hash)
4381 rb_hash_modify_check(hash);
4384 rb_hash_foreach(hash, delete_if_nil, hash);
4391static st_table *rb_init_identtable_with_size(st_index_t size);
4422rb_hash_compare_by_id(
VALUE hash)
4427 if (rb_hash_compare_by_id_p(hash))
return hash;
4429 rb_hash_modify_check(hash);
4430 ar_force_convert_table(hash, __FILE__, __LINE__);
4431 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4433 tmp = hash_alloc(0);
4434 identtable = rb_init_identtable_with_size(
RHASH_SIZE(hash));
4435 RHASH_ST_TABLE_SET(tmp, identtable);
4436 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4437 st_free_table(RHASH_ST_TABLE(hash));
4438 RHASH_ST_TABLE_SET(hash, identtable);
4439 RHASH_ST_CLEAR(tmp);
4451MJIT_FUNC_EXPORTED
VALUE
4452rb_hash_compare_by_id_p(
VALUE hash)
4454 return RBOOL(RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->
type == &identhash);
4458rb_ident_hash_new(
void)
4460 VALUE hash = rb_hash_new();
4461 RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
4466rb_ident_hash_new_with_size(st_index_t size)
4468 VALUE hash = rb_hash_new();
4469 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(&identhash, size));
4474rb_init_identtable(
void)
4476 return st_init_table(&identhash);
4480rb_init_identtable_with_size(st_index_t size)
4482 return st_init_table_with_size(&identhash, size);
4548rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4557 rb_warn(
"given block not used");
4561 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4568 if (rb_block_pair_yield_optimizable())
4569 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4571 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4606rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4609 self = rb_hash_aref(self, *argv);
4610 if (!--argc)
return self;
4612 return rb_obj_dig(argc, argv, self,
Qnil);
4620 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4631 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4649 other = to_hash(other);
4651 return hash_le(hash, other);
4668 other = to_hash(other);
4670 return hash_le(hash, other);
4687 other = to_hash(other);
4689 return hash_le(other, hash);
4706 other = to_hash(other);
4708 return hash_le(other, hash);
4715 return rb_hash_aref(hash, *argv);
4731rb_hash_to_proc(
VALUE hash)
4733 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4737rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4743add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4746 if (existing)
return ST_STOP;
4765 if (RHASH_AR_TABLE_P(hash)) {
4766 hash_ar_table(hash);
4768 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4772 ar_try_convert_table(hash);
4774 tbl = RHASH_TBL_RAW(hash);
4775 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4780key_stringify(
VALUE key)
4783 rb_hash_key_str(key) : key;
4787ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4790 for (i = 0; i < argc; ) {
4791 st_data_t k = key_stringify(argv[i++]);
4792 st_data_t v = argv[i++];
4793 ar_insert(hash, k, v);
4802 HASH_ASSERT(argc % 2 == 0);
4804 st_index_t size = argc / 2;
4806 if (RHASH_TABLE_NULL_P(hash)) {
4807 if (size <= RHASH_AR_TABLE_MAX_SIZE) {
4808 hash_ar_table(hash);
4811 RHASH_TBL_RAW(hash);
4815 if (RHASH_AR_TABLE_P(hash) &&
4816 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4817 ar_bulk_insert(hash, argc, argv);
4820 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4825static char **origenviron;
4827#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4828#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4829static char **my_environ;
4831#define environ my_environ
4833#define getenv(n) rb_w32_ugetenv(n)
4834#elif defined(__APPLE__)
4836#define environ (*_NSGetEnviron())
4837#define GET_ENVIRON(e) (e)
4838#define FREE_ENVIRON(e)
4840extern char **environ;
4841#define GET_ENVIRON(e) (e)
4842#define FREE_ENVIRON(e)
4844#ifdef ENV_IGNORECASE
4845#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4846#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4848#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4849#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4852#define ENV_LOCK() RB_VM_LOCK_ENTER()
4853#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4859 return rb_utf8_encoding();
4861 return rb_locale_encoding();
4866env_enc_str_new(
const char *ptr,
long len,
rb_encoding *enc)
4875env_str_new(
const char *ptr,
long len)
4877 return env_enc_str_new(ptr, len, env_encoding());
4881env_str_new2(
const char *ptr)
4883 if (!ptr)
return Qnil;
4884 return env_str_new(ptr, strlen(ptr));
4888getenv_with_lock(
const char *name)
4893 const char *val = getenv(name);
4894 ret = env_str_new2(val);
4901has_env_with_lock(
const char *name)
4911 return val ? true :
false;
4914static const char TZ_ENV[] =
"TZ";
4931 return rb_str_fill_terminator(str, 1);
4934#define get_env_ptr(var, val) \
4935 (var = get_env_cstr(val, #var))
4937static inline const char *
4938env_name(
volatile VALUE *s)
4942 get_env_ptr(name, *s);
4946#define env_name(s) env_name(&(s))
4951reset_by_modified_env(
const char *nam)
4959 if (ENVMATCH(nam, TZ_ENV)) {
4960 ruby_reset_timezone();
4965env_delete(
VALUE name)
4967 const char *nam = env_name(name);
4968 reset_by_modified_env(nam);
4969 VALUE val = getenv_with_lock(nam);
4972 ruby_setenv(nam, 0);
5007 val = env_delete(name);
5027 const char *nam = env_name(name);
5028 VALUE env = getenv_with_lock(nam);
5068 if (block_given && argc == 2) {
5069 rb_warn(
"block supersedes default value argument");
5071 nam = env_name(key);
5072 env = getenv_with_lock(nam);
5075 if (block_given)
return rb_yield(key);
5077 rb_key_err_raise(
rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5084#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5087in_origenv(
const char *str)
5090 for (env = origenviron; *env; ++env) {
5091 if (*env == str)
return 1;
5097envix(
const char *nam)
5101 register int i, len = strlen(nam);
5104 env = GET_ENVIRON(environ);
5105 for (i = 0; env[i]; i++) {
5106 if (ENVNMATCH(env[i],nam,len) && env[i][len] ==
'=')
5109 FREE_ENVIRON(environ);
5116getenvsize(
const WCHAR* p)
5118 const WCHAR* porg = p;
5119 while (*p++) p += lstrlenW(p) + 1;
5120 return p - porg + 1;
5124getenvblocksize(
void)
5134check_envsize(
size_t n)
5136 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5140 WCHAR* p = GetEnvironmentStringsW();
5143 FreeEnvironmentStringsW(p);
5144 if (n >= getenvblocksize()) {
5152#if defined(_WIN32) || \
5153 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5155NORETURN(
static void invalid_envname(
const char *name));
5158invalid_envname(
const char *name)
5164check_envname(
const char *name)
5166 if (strchr(name,
'=')) {
5167 invalid_envname(name);
5174ruby_setenv(
const char *name,
const char *value)
5177# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5178# define HAVE__WPUTENV_S 1
5185 check_envname(name);
5186 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5189 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5190 if (check_envsize((
size_t)len + len2)) {
5193 wname =
ALLOCV_N(WCHAR, buf, len + len2);
5194 wvalue = wname + len;
5195 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5196 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5197#ifndef HAVE__WPUTENV_S
5198 wname[len-1] = L
'=';
5202 wname =
ALLOCV_N(WCHAR, buf, len + 1);
5203 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5204 wvalue = wname + len;
5206#ifndef HAVE__WPUTENV_S
5207 wname[len-1] = L
'=';
5213#ifndef HAVE__WPUTENV_S
5214 failed = _wputenv(wname);
5216 failed = _wputenv_s(wname, wvalue);
5224 if (!value || !*value) {
5226 if (!SetEnvironmentVariable(name, value) &&
5227 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5231 invalid_envname(name);
5233#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5238 ret = setenv(name, value, 1);
5255 ret = unsetenv(name);
5267 size_t len, mem_size;
5268 char **env_ptr, *str, *mem_ptr;
5270 check_envname(name);
5273 mem_size = len + strlen(value) + 2;
5274 mem_ptr = malloc(mem_size);
5275 if (mem_ptr == NULL)
5277 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5282 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5283 if (!strncmp(str, name, len) && str[len] ==
'=') {
5284 if (!in_origenv(str)) free(str);
5285 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5296 ret = putenv(mem_ptr);
5313 if (environ == origenviron) {
5318 for (max = i; environ[max]; max++) ;
5319 tmpenv =
ALLOC_N(
char*, max+2);
5320 for (j=0; j<max; j++)
5327 char **envp = origenviron;
5328 while (*envp && *envp != environ[i]) envp++;
5332 while (environ[i]) {
5333 environ[i] = environ[i+1];
5340 if (!value)
goto finish;
5345 len = strlen(name) + strlen(value) + 2;
5346 environ[i] =
ALLOC_N(
char, len);
5347 snprintf(environ[i],len,
"%s=%s",name,value);
5356ruby_unsetenv(
const char *name)
5358 ruby_setenv(name, 0);
5408 return env_aset(nm, val);
5424 get_env_ptr(name, nm);
5425 get_env_ptr(value, val);
5427 ruby_setenv(name, value);
5428 reset_by_modified_env(name);
5435 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5436 VALUE ary = rb_ary_new();
5440 char **env = GET_ENVIRON(environ);
5442 char *s = strchr(*env,
'=');
5444 const char *p = *env;
5447 rb_ary_push(ary, e);
5451 FREE_ENVIRON(environ);
5474 return env_keys(FALSE);
5485 env = GET_ENVIRON(environ);
5486 for (; *env ; ++env) {
5487 if (strchr(*env,
'=')) {
5491 FREE_ENVIRON(environ);
5516env_each_key(
VALUE ehash)
5522 keys = env_keys(FALSE);
5532 VALUE ary = rb_ary_new();
5536 char **env = GET_ENVIRON(environ);
5539 char *s = strchr(*env,
'=');
5541 rb_ary_push(ary, env_str_new2(s+1));
5545 FREE_ENVIRON(environ);
5567 return env_values();
5588env_each_value(
VALUE ehash)
5594 values = env_values();
5620env_each_pair(
VALUE ehash)
5626 VALUE ary = rb_ary_new();
5630 char **env = GET_ENVIRON(environ);
5633 char *s = strchr(*env,
'=');
5635 rb_ary_push(ary, env_str_new(*env, s-*env));
5636 rb_ary_push(ary, env_str_new2(s+1));
5640 FREE_ENVIRON(environ);
5644 if (rb_block_pair_yield_optimizable()) {
5681env_reject_bang(
VALUE ehash)
5688 keys = env_keys(FALSE);
5689 RBASIC_CLEAR_CLASS(keys);
5700 if (del == 0)
return Qnil;
5725env_delete_if(
VALUE ehash)
5728 env_reject_bang(ehash);
5755 result = rb_ary_new();
5756 for (i=0; i<argc; i++) {
5757 rb_ary_push(result, rb_f_getenv(
Qnil, argv[i]));
5784env_select(
VALUE ehash)
5791 result = rb_hash_new();
5792 keys = env_keys(FALSE);
5798 rb_hash_aset(result, key, val);
5845env_select_bang(
VALUE ehash)
5852 keys = env_keys(FALSE);
5853 RBASIC_CLEAR_CLASS(keys);
5864 if (del == 0)
return Qnil;
5887env_keep_if(
VALUE ehash)
5890 env_select_bang(ehash);
5910 VALUE key, value, result;
5913 return rb_hash_new();
5915 result = rb_hash_new_with_size(argc);
5917 for (i = 0; i < argc; i++) {
5919 value = rb_f_getenv(
Qnil, key);
5921 rb_hash_aset(result, key, value);
5933 keys = env_keys(TRUE);
5937 ruby_setenv(nam, 0);
5956 return rb_env_clear();
5988 char **env = GET_ENVIRON(environ);
5990 char *s = strchr(*env,
'=');
5992 if (env != environ) {
6004 FREE_ENVIRON(environ);
6025 VALUE ary = rb_ary_new();
6029 char **env = GET_ENVIRON(environ);
6031 char *s = strchr(*env,
'=');
6033 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
6034 env_str_new2(s+1)));
6038 FREE_ENVIRON(environ);
6060env_size_with_lock(
void)
6066 char **env = GET_ENVIRON(environ);
6068 FREE_ENVIRON(environ);
6088 return INT2FIX(env_size_with_lock());
6108 char **env = GET_ENVIRON(environ);
6112 FREE_ENVIRON(environ);
6116 return RBOOL(empty);
6147 const char *s = env_name(key);
6148 return RBOOL(has_env_with_lock(s));
6174 const char *s = env_name(key);
6175 VALUE e = getenv_with_lock(s);
6178 return rb_assoc_new(key, e);
6207 char **env = GET_ENVIRON(environ);
6209 char *s = strchr(*env,
'=');
6211 long len = strlen(s);
6219 FREE_ENVIRON(environ);
6250 char **env = GET_ENVIRON(environ);
6253 const char *p = *env;
6254 char *s = strchr(p,
'=');
6256 long len = strlen(s);
6258 result = rb_assoc_new(
rb_str_new(p, s-p-1), obj);
6264 FREE_ENVIRON(environ);
6295 char **env = GET_ENVIRON(environ);
6297 char *s = strchr(*env,
'=');
6299 long len = strlen(s);
6301 str = env_str_new(*env, s-*env-1);
6307 FREE_ENVIRON(environ);
6317 VALUE hash = rb_hash_new();
6321 char **env = GET_ENVIRON(environ);
6323 char *s = strchr(*env,
'=');
6325 rb_hash_aset(hash, env_str_new(*env, s-*env),
6330 FREE_ENVIRON(environ);
6346 return env_to_hash();
6361 return env_to_hash();
6385 VALUE hash = env_to_hash();
6387 hash = rb_hash_to_h_block(hash);
6405 VALUE key, hash = env_to_hash();
6407 for (i = 0; i < argc; i++) {
6409 rb_hash_delete(hash, key);
6433 return rb_hash_delete_if(env_to_hash());
6436NORETURN(
static VALUE env_freeze(
VALUE self));
6445env_freeze(
VALUE self)
6474 char **env = GET_ENVIRON(environ);
6476 const char *p = *env;
6477 char *s = strchr(p,
'=');
6479 key = env_str_new(p, s-p);
6481 result = rb_assoc_new(key, val);
6484 FREE_ENVIRON(environ);
6513 return rb_hash_invert(env_to_hash());
6520 const char *keyptr, *eptr;
6528 if (elen != keylen)
continue;
6529 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6530 rb_ary_delete_at(keys, i);
6541 keylist_delete(keys, key);
6569 keys = env_keys(TRUE);
6570 if (env == hash)
return env;
6571 hash = to_hash(hash);
6572 rb_hash_foreach(hash, env_replace_i, keys);
6592 if (!
NIL_P(oldval)) {
6641 env_update_block_i : env_update_i;
6642 for (
int i = 0; i < argc; ++i) {
6643 VALUE hash = argv[i];
6644 if (env == hash)
continue;
6645 hash = to_hash(hash);
6646 rb_hash_foreach(hash, func, 0);
6666 rb_get_freeze_opt(1, &opt);
6695 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7177 id_hash_iter_lev = rb_make_internal_id();
7439 origenviron = environ;
7509 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7511 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.