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
332#define RHASH_IDENTHASH_P(hash) (RHASH_TYPE(hash) == &identhash)
333#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
335typedef st_index_t st_hash_t;
347#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
349#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
350#define RHASH_AR_CLEARED_HINT 0xff
363rb_hash_ar_table_size(
void)
368static inline st_hash_t
369ar_do_hash(st_data_t key)
371 return (st_hash_t)rb_any_hash(key);
374static inline ar_hint_t
375ar_do_hash_hint(st_hash_t hash_value)
377 return (ar_hint_t)hash_value;
380static inline ar_hint_t
381ar_hint(
VALUE hash,
unsigned int index)
383 return RHASH(hash)->ar_hint.ary[index];
387ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
389 RHASH(hash)->ar_hint.ary[index] = hint;
393ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
395 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
399ar_clear_entry(
VALUE hash,
unsigned int index)
403 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
407ar_cleared_entry(
VALUE hash,
unsigned int index)
409 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
414 return UNDEF_P(pair->key);
422ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
427 ar_hint_set(hash, index, hash_value);
430#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
431 RHASH_AR_TABLE_SIZE_RAW(h))
433#define RHASH_AR_TABLE_BOUND_RAW(h) \
434 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
435 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
437#define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
438 RHASH_AR_TABLE_BOUND_RAW(h))
440#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
441#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
443#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
446#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
449rb_hash_dump(
VALUE hash)
451 rb_obj_info_dump(hash);
453 if (RHASH_AR_TABLE_P(hash)) {
454 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
456 fprintf(stderr,
" size:%u bound:%u\n",
457 RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
459 for (i=0; i<bound; i++) {
462 if (!ar_cleared_entry(hash, i)) {
463 char b1[0x100], b2[0x100];
467 fprintf(stderr,
" %d key:%s val:%s hint:%02x\n", i,
468 rb_raw_obj_info(b1, 0x100, k),
469 rb_raw_obj_info(b2, 0x100, v),
474 fprintf(stderr,
" %d empty\n", i);
481hash_verify_(
VALUE hash,
const char *file,
int line)
485 if (RHASH_AR_TABLE_P(hash)) {
486 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
488 for (i=0; i<bound; i++) {
490 if (!ar_cleared_entry(hash, i)) {
494 HASH_ASSERT(!UNDEF_P(k));
495 HASH_ASSERT(!UNDEF_P(v));
499 if (n != RHASH_AR_TABLE_SIZE(hash)) {
500 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
504 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
505 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
506 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
509#if USE_TRANSIENT_HEAP
510 if (RHASH_TRANSIENT_P(hash)) {
511 volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key;
512 HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
513 HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
520#define hash_verify(h) ((void)0)
524RHASH_TABLE_NULL_P(
VALUE hash)
526 if (RHASH(hash)->as.ar == NULL) {
527 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
536RHASH_TABLE_EMPTY_P(
VALUE hash)
542rb_hash_ar_table_p(
VALUE hash)
545 HASH_ASSERT(RHASH(hash)->as.st != NULL);
554rb_hash_ar_table(
VALUE hash)
556 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
557 return RHASH(hash)->as.ar;
561rb_hash_st_table(
VALUE hash)
563 HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
564 return RHASH(hash)->as.st;
570 HASH_ASSERT(st != NULL);
572 RHASH(hash)->as.st = st;
578 HASH_ASSERT(RHASH_AR_TABLE_P(hash));
579 HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
580 RHASH(hash)->as.ar = ar;
584#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
585#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
588RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
590 HASH_ASSERT(RHASH_AR_TABLE_P(h));
591 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
593 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
594 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
598RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
600 HASH_ASSERT(RHASH_AR_TABLE_P(h));
601 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
603 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
604 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
608HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
610 HASH_ASSERT(RHASH_AR_TABLE_P(h));
612 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
617#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
620RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
622 HASH_ASSERT(RHASH_AR_TABLE_P(h));
623 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
626 RHASH_AR_TABLE_SIZE_SET(h, new_size);
629 RHASH_AR_TABLE_SIZE_SET(h, 0);
630 RHASH_AR_TABLE_BOUND_SET(h, 0);
636RHASH_AR_TABLE_CLEAR(
VALUE h)
638 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
639 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
641 hash_ar_table_set(h, NULL);
645ar_alloc_table(
VALUE hash)
650 RHASH_SET_TRANSIENT_FLAG(hash);
653 RHASH_UNSET_TRANSIENT_FLAG(hash);
657 RHASH_AR_TABLE_SIZE_SET(hash, 0);
658 RHASH_AR_TABLE_BOUND_SET(hash, 0);
659 hash_ar_table_set(hash, tab);
664NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
669 return rb_any_cmp(x, y) == 0;
673ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
675 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
676 const ar_hint_t *hints = RHASH(hash)->ar_hint.ary;
680 for (i = 0; i < bound; i++) {
681 if (hints[i] == hint) {
683 if (ar_equal(key, pair->key)) {
684 RB_DEBUG_COUNTER_INC(artable_hint_hit);
690 static char fname[256];
693 if (pid != getpid()) {
694 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
695 if ((fp = fopen(fname,
"w")) == NULL)
rb_bug(
"fopen");
698 st_hash_t h1 = ar_do_hash(key);
699 st_hash_t h2 = ar_do_hash(pair->key);
701 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
703 " pair->key:%016lx %s\n",
704 h1 == h2, i, hints[i], hint,
705 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
707 RB_DEBUG_COUNTER_INC(artable_hint_miss);
711 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
712 return RHASH_AR_TABLE_MAX_BOUND;
716ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
718 ar_hint_t hint = ar_do_hash_hint(hash_value);
719 return ar_find_entry_hint(hash, hint, key);
723ar_free_and_clear_table(
VALUE hash)
725 ar_table *tab = RHASH_AR_TABLE(hash);
728 if (RHASH_TRANSIENT_P(hash)) {
729 RHASH_UNSET_TRANSIENT_FLAG(hash);
732 ruby_xfree(RHASH_AR_TABLE(hash));
734 RHASH_AR_TABLE_CLEAR(hash);
736 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
737 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
738 HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
741void rb_st_add_direct_with_hash(
st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
743enum ar_each_key_type {
750ar_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)
752 for (
int i = 0; i < max; i++) {
756 case ar_each_key_copy:
757 dst_keys[i] = pair->key;
759 case ar_each_key_cmp:
760 if (dst_keys[i] != pair->key)
return 1;
762 case ar_each_key_insert:
763 if (UNDEF_P(pair->key))
continue;
764 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
775ar_force_convert_table(
VALUE hash,
const char *file,
int line)
779 if (RHASH_ST_TABLE_P(hash)) {
780 return RHASH_ST_TABLE(hash);
783 if (RHASH_AR_TABLE(hash)) {
784 ar_table *ar = RHASH_AR_TABLE(hash);
785 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
786 unsigned int bound, size;
790 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
791 bound = RHASH_AR_TABLE_BOUND(hash);
792 size = RHASH_AR_TABLE_SIZE(hash);
793 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
795 for (
unsigned int i = 0; i < bound; i++) {
797 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
801 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
802 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
803 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
808 new_tab = st_init_table_with_size(&objhash, size);
809 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
810 ar_free_and_clear_table(hash);
813 new_tab = st_init_table(&objhash);
815 RHASH_ST_TABLE_SET(hash, new_tab);
821hash_ar_table(
VALUE hash)
823 if (RHASH_TABLE_NULL_P(hash)) {
824 ar_alloc_table(hash);
826 return RHASH_AR_TABLE(hash);
830ar_compact_table(
VALUE hash)
832 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
833 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
842 for (i=0; i<bound; i++) {
843 if (ar_cleared_entry(hash, i)) {
845 for (; j<bound; j++) {
846 if (!ar_cleared_entry(hash, j)) {
848 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
849 ar_clear_entry(hash, j);
860 HASH_ASSERT(i<=bound);
862 RHASH_AR_TABLE_BOUND_SET(hash, size);
869ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
871 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
873 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
877 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
878 bin = ar_compact_table(hash);
881 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
883 ar_set_entry(hash, bin, key, val, hash_value);
884 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
885 RHASH_AR_TABLE_SIZE_INC(hash);
891ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
893 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
894 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
896 for (i = 0; i < bound; i++) {
897 if (ar_cleared_entry(hash, i))
continue;
900 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
911 VALUE key = pair->key;
912 VALUE val = pair->val;
913 retval = (*replace)(&key, &val, arg, TRUE);
922 ar_clear_entry(hash, i);
923 RHASH_AR_TABLE_SIZE_DEC(hash);
932ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
934 return ar_general_foreach(hash, func, replace, arg);
938 st_foreach_callback_func *func;
943apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
945 const struct functor *f = (
void *)d;
946 return f->func(k, v, f->arg);
950ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
952 const struct functor f = { func, arg };
953 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
957ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
960 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
961 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
962 enum st_retval retval;
967 for (i = 0; i < bound; i++) {
968 if (ar_cleared_entry(hash, i))
continue;
970 pair = RHASH_AR_TABLE_REF(hash, i);
972 hint = ar_hint(hash, i);
974 retval = (*func)(key, pair->val, arg, 0);
979 pair = RHASH_AR_TABLE_REF(hash, i);
980 if (pair->key == never)
break;
981 ret = ar_find_entry_hint(hash, hint, key);
982 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
983 retval = (*func)(0, 0, arg, 1);
993 if (!ar_cleared_entry(hash, i)) {
994 ar_clear_entry(hash, i);
995 RHASH_AR_TABLE_SIZE_DEC(hash);
1006ar_update(
VALUE hash, st_data_t key,
1007 st_update_callback_func *func, st_data_t arg)
1009 int retval, existing;
1010 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
1011 st_data_t value = 0, old_key;
1012 st_hash_t hash_value = ar_do_hash(key);
1014 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1019 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1020 bin = ar_find_entry(hash, hash_value, key);
1021 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
1024 hash_ar_table(hash);
1034 retval = (*func)(&key, &value, arg, existing);
1040 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1046 if (old_key != key) {
1054 ar_clear_entry(hash, bin);
1055 RHASH_AR_TABLE_SIZE_DEC(hash);
1063ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1065 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1066 st_hash_t hash_value = ar_do_hash(key);
1068 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1073 hash_ar_table(hash);
1075 bin = ar_find_entry(hash, hash_value, key);
1076 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1077 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1080 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1081 bin = ar_compact_table(hash);
1082 hash_ar_table(hash);
1084 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1086 ar_set_entry(hash, bin, key, value, hash_value);
1087 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1088 RHASH_AR_TABLE_SIZE_INC(hash);
1092 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1098ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1100 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1104 st_hash_t hash_value = ar_do_hash(key);
1105 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1107 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1109 unsigned bin = ar_find_entry(hash, hash_value, key);
1111 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1115 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1116 if (value != NULL) {
1117 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1125ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1128 st_hash_t hash_value = ar_do_hash(*key);
1130 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1132 return st_delete(RHASH_ST_TABLE(hash), key, value);
1135 bin = ar_find_entry(hash, hash_value, *key);
1137 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1138 if (value != 0) *value = 0;
1146 ar_clear_entry(hash, bin);
1147 RHASH_AR_TABLE_SIZE_DEC(hash);
1153ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1155 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1156 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1158 for (i = 0; i < bound; i++) {
1159 if (!ar_cleared_entry(hash, i)) {
1161 if (value != 0) *value = pair->val;
1163 ar_clear_entry(hash, i);
1164 RHASH_AR_TABLE_SIZE_DEC(hash);
1169 if (value != NULL) *value = 0;
1174ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1176 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1177 st_data_t *keys_start = keys, *keys_end = keys + size;
1179 for (i = 0; i < bound; i++) {
1180 if (keys == keys_end) {
1184 if (!ar_cleared_entry(hash, i)) {
1185 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1190 return keys - keys_start;
1194ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1196 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1197 st_data_t *values_start = values, *values_end = values + size;
1199 for (i = 0; i < bound; i++) {
1200 if (values == values_end) {
1204 if (!ar_cleared_entry(hash, i)) {
1205 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1210 return values - values_start;
1216 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1218 if (old_tab != NULL) {
1219 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1220 if (new_tab == NULL) {
1222 if (new_tab != NULL) {
1223 RHASH_SET_TRANSIENT_FLAG(hash1);
1226 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1230 *new_tab = *old_tab;
1231 RHASH(hash1)->ar_hint.word = RHASH(hash2)->ar_hint.word;
1232 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1233 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1234 hash_ar_table_set(hash1, new_tab);
1236 rb_gc_writebarrier_remember(hash1);
1240 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1241 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1243 if (RHASH_TRANSIENT_P(hash1)) {
1244 RHASH_UNSET_TRANSIENT_FLAG(hash1);
1246 else if (RHASH_AR_TABLE(hash1)) {
1247 ruby_xfree(RHASH_AR_TABLE(hash1));
1250 hash_ar_table_set(hash1, NULL);
1252 rb_gc_writebarrier_remember(hash1);
1260 if (RHASH_AR_TABLE(hash) != NULL) {
1261 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1262 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1265 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1266 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1270#if USE_TRANSIENT_HEAP
1272rb_hash_transient_heap_evacuate(
VALUE hash,
int promote)
1274 if (RHASH_TRANSIENT_P(hash)) {
1276 ar_table *old_tab = RHASH_AR_TABLE(hash);
1278 if (UNLIKELY(old_tab == NULL)) {
1281 HASH_ASSERT(old_tab != NULL);
1283 new_tab = rb_transient_heap_alloc(hash,
sizeof(
ar_table));
1284 if (new_tab == NULL) promote =
true;
1287 new_tab = ruby_xmalloc(
sizeof(
ar_table));
1288 RHASH_UNSET_TRANSIENT_FLAG(hash);
1290 *new_tab = *old_tab;
1291 hash_ar_table_set(hash, new_tab);
1297typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1301 st_foreach_func *func;
1306foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1311 if (error)
return ST_STOP;
1312 status = (*arg->func)(key, value, arg->arg);
1313 if (status == ST_CONTINUE) {
1325 arg.func = (st_foreach_func *)func;
1327 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1336 rb_foreach_func *func;
1341hash_iter_status_check(
int status)
1356hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1360 if (error)
return ST_STOP;
1362 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1365 return hash_iter_status_check(status);
1369hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1373 if (error)
return ST_STOP;
1375 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1376 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1378 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1382 return hash_iter_status_check(status);
1386iter_lev_in_ivar(
VALUE hash)
1396iter_lev_in_ivar_set(
VALUE hash,
int lev)
1398 rb_ivar_set_internal(hash, id_hash_iter_lev,
INT2FIX(lev));
1402iter_lev_in_flags(
VALUE hash)
1404 unsigned int u = (
unsigned int)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1409iter_lev_in_flags_set(
VALUE hash,
int lev)
1411 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1417 int lev = iter_lev_in_flags(hash);
1419 if (lev == RHASH_LEV_MAX) {
1420 return iter_lev_in_ivar(hash);
1428hash_iter_lev_inc(
VALUE hash)
1430 int lev = iter_lev_in_flags(hash);
1431 if (lev == RHASH_LEV_MAX) {
1432 lev = iter_lev_in_ivar(hash);
1433 iter_lev_in_ivar_set(hash, lev+1);
1437 iter_lev_in_flags_set(hash, lev);
1438 if (lev == RHASH_LEV_MAX) {
1439 iter_lev_in_ivar_set(hash, lev);
1445hash_iter_lev_dec(
VALUE hash)
1447 int lev = iter_lev_in_flags(hash);
1448 if (lev == RHASH_LEV_MAX) {
1449 lev = iter_lev_in_ivar(hash);
1450 HASH_ASSERT(lev > 0);
1451 iter_lev_in_ivar_set(hash, lev-1);
1454 HASH_ASSERT(lev > 0);
1455 iter_lev_in_flags_set(hash, lev - 1);
1460hash_foreach_ensure_rollback(
VALUE hash)
1462 hash_iter_lev_inc(hash);
1467hash_foreach_ensure(
VALUE hash)
1469 hash_iter_lev_dec(hash);
1474rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1476 if (RHASH_AR_TABLE_P(hash)) {
1477 return ar_foreach(hash, func, arg);
1480 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1485rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1487 if (RHASH_AR_TABLE_P(hash)) {
1488 return ar_foreach_with_replace(hash, func, replace, arg);
1491 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1496hash_foreach_call(
VALUE arg)
1500 if (RHASH_AR_TABLE_P(hash)) {
1501 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1502 (st_data_t)arg, (st_data_t)
Qundef);
1504 else if (RHASH_ST_TABLE_P(hash)) {
1505 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1506 (st_data_t)arg, (st_data_t)
Qundef);
1515rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1519 if (RHASH_TABLE_EMPTY_P(hash))
1522 arg.func = (rb_foreach_func *)func;
1525 hash_foreach_call((
VALUE)&arg);
1528 hash_iter_lev_inc(hash);
1529 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1534void rb_st_compact_table(
st_table *tab);
1537compact_after_delete(
VALUE hash)
1540 rb_st_compact_table(RHASH_ST_TABLE(hash));
1556hash_alloc(
VALUE klass)
1558 return hash_alloc_flags(klass, 0,
Qnil);
1562empty_hash_alloc(
VALUE klass)
1564 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1566 return hash_alloc(klass);
1578 if (rb_hash_compare_by_id_p(basis)) {
1579 return rb_hash_compare_by_id(hash);
1584MJIT_FUNC_EXPORTED
VALUE
1585rb_hash_new_with_size(st_index_t size)
1587 VALUE ret = rb_hash_new();
1591 else if (size <= RHASH_AR_TABLE_MAX_SIZE) {
1592 ar_alloc_table(ret);
1595 RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
1601rb_hash_new_capa(
long capa)
1603 return rb_hash_new_with_size((st_index_t)capa);
1610 if (RHASH_AR_TABLE_P(hash))
1612 else if (RHASH_ST_TABLE_P(hash))
1613 RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
1619hash_dup_with_compare_by_id(
VALUE hash)
1621 return hash_copy(copy_compare_by_id(rb_hash_new(), hash), hash);
1627 return hash_copy(hash_alloc_flags(klass, flags,
RHASH_IFNONE(hash)),
1636 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1642MJIT_FUNC_EXPORTED
VALUE
1643rb_hash_resurrect(
VALUE hash)
1650rb_hash_modify_check(
VALUE hash)
1655MJIT_FUNC_EXPORTED
struct st_table *
1656rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1658 return ar_force_convert_table(hash, file, line);
1662rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1665 return rb_hash_tbl_raw(hash, file, line);
1669rb_hash_modify(
VALUE hash)
1671 rb_hash_modify_check(hash);
1674NORETURN(
static void no_new_key(
void));
1686#define NOINSERT_UPDATE_CALLBACK(func) \
1688func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1690 if (!existing) no_new_key(); \
1691 return func(key, val, (struct update_arg *)arg, existing); \
1695func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1697 return func(key, val, (struct update_arg *)arg, existing); \
1702 st_update_callback_func *func;
1708typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1711rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1713 if (RHASH_AR_TABLE_P(hash)) {
1714 int result = ar_update(hash, key, func, arg);
1716 ar_force_convert_table(hash, __FILE__, __LINE__);
1723 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1727tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1730 st_data_t old_key = *key;
1731 st_data_t old_value = *val;
1732 VALUE hash = p->hash;
1733 int ret = (p->func)(key, val, arg, existing);
1738 if (!existing || *key != old_key || *val != old_value) {
1739 rb_hash_modify(hash);
1746 rb_hash_modify(hash);
1754tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1757 .arg = optional_arg,
1761 .value = (
VALUE)optional_arg,
1764 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1773#define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
1775#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
1776 tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
1779#define RHASH_UPDATE(hash, key, func, arg) \
1780 RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
1788 if (n != 2 && (n >= 0 || n < -3)) {
1789 if (n < 0) n = -n-1;
1830rb_hash_initialize(
int argc,
VALUE *argv,
VALUE hash)
1834 rb_hash_modify(hash);
1838 SET_PROC_DEFAULT(hash, ifnone);
1842 ifnone = argc == 0 ?
Qnil : argv[0];
1885rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1890 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1892 hash = hash_alloc(klass);
1893 hash_copy(hash, tmp);
1897 tmp = rb_check_array_type(argv[0]);
1901 hash = hash_alloc(klass);
1904 VALUE v = rb_check_array_type(e);
1909 rb_builtin_class_name(e), i);
1919 rb_hash_aset(hash, key, val);
1925 if (argc % 2 != 0) {
1929 hash = hash_alloc(klass);
1930 rb_hash_bulk_insert(argc, argv, hash);
1935MJIT_FUNC_EXPORTED
VALUE
1936rb_to_hash_type(
VALUE hash)
1938 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1940#define to_hash rb_to_hash_type
1945 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1962rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1964 return rb_check_hash_type(hash);
1983rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1986 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
2005rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
2008 hash = rb_hash_dup(hash);
2009 RHASH(hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2021 if (RHASH_AR_TABLE_P(arg)) {
2022 ar_insert(arg, (st_data_t)key, (st_data_t)value);
2025 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
2043rb_hash_rehash(
VALUE hash)
2051 rb_hash_modify_check(hash);
2052 if (RHASH_AR_TABLE_P(hash)) {
2053 tmp = hash_alloc(0);
2054 ar_alloc_table(tmp);
2055 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2056 ar_free_and_clear_table(hash);
2058 ar_free_and_clear_table(tmp);
2060 else if (RHASH_ST_TABLE_P(hash)) {
2061 st_table *old_tab = RHASH_ST_TABLE(hash);
2062 tmp = hash_alloc(0);
2063 tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
2064 RHASH_ST_TABLE_SET(tmp, tbl);
2065 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2066 st_free_table(old_tab);
2067 RHASH_ST_TABLE_SET(hash, tbl);
2068 RHASH_ST_CLEAR(tmp);
2077 VALUE args[2] = {hash, key};
2082rb_hash_default_unredefined(
VALUE hash)
2086 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2089 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2098 if (LIKELY(rb_hash_default_unredefined(hash))) {
2100 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2101 if (UNDEF_P(key))
return Qnil;
2102 return call_default_proc(ifnone, hash, key);
2110hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2114 if (RHASH_AR_TABLE_P(hash)) {
2115 return ar_lookup(hash, key, pval);
2118 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2122MJIT_FUNC_EXPORTED
int
2123rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2125 return hash_stlike_lookup(hash, key, pval);
2147 if (hash_stlike_lookup(hash, key, &val)) {
2151 return rb_hash_default_value(hash, key);
2160 if (hash_stlike_lookup(hash, key, &val)) {
2171 return rb_hash_lookup2(hash, key,
Qnil);
2198rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2208 if (block_given && argc == 2) {
2209 rb_warn(
"block supersedes default value argument");
2212 if (hash_stlike_lookup(hash, key, &val)) {
2219 else if (argc == 1) {
2225 rb_key_err_raise(
rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2236 return rb_hash_fetch_m(1, &key, hash);
2260rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2266 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2267 if (argc == 0)
return Qnil;
2268 return call_default_proc(ifnone, hash, argv[0]);
2289 rb_hash_modify_check(hash);
2290 SET_DEFAULT(hash, ifnone);
2307rb_hash_default_proc(
VALUE hash)
2309 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2330rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2334 rb_hash_modify_check(hash);
2336 SET_DEFAULT(hash, proc);
2339 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2342 "wrong default_proc type %s (expected Proc)",
2346 SET_PROC_DEFAULT(hash, proc);
2383 rb_hash_foreach(hash, key_i, (
VALUE)args);
2389rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2391 if (RHASH_AR_TABLE_P(hash)) {
2392 return ar_delete(hash, pkey, pval);
2395 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2407 st_data_t ktmp = (st_data_t)key, val;
2409 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2425 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2427 if (!UNDEF_P(deleted_value)) {
2428 return deleted_value;
2467 rb_hash_modify_check(hash);
2468 val = rb_hash_delete_entry(hash, key);
2470 if (!UNDEF_P(val)) {
2471 compact_after_delete(hash);
2514rb_hash_shift(
VALUE hash)
2518 rb_hash_modify_check(hash);
2519 if (RHASH_AR_TABLE_P(hash)) {
2522 if (ar_shift(hash, &var.key, &var.val)) {
2523 return rb_assoc_new(var.key, var.val);
2527 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2528 if (!UNDEF_P(var.key)) {
2529 rb_hash_delete_entry(hash, var.key);
2530 return rb_assoc_new(var.key, var.val);
2534 if (RHASH_ST_TABLE_P(hash)) {
2537 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2538 return rb_assoc_new(var.key, var.val);
2542 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2543 if (!UNDEF_P(var.key)) {
2544 rb_hash_delete_entry(hash, var.key);
2545 return rb_assoc_new(var.key, var.val);
2556 rb_hash_modify(hash);
2565 return rb_hash_size(hash);
2589 rb_hash_modify_check(hash);
2590 if (!RHASH_TABLE_EMPTY_P(hash)) {
2591 rb_hash_foreach(hash, delete_if_i, hash);
2592 compact_after_delete(hash);
2616rb_hash_reject_bang(
VALUE hash)
2621 rb_hash_modify(hash);
2623 if (!n)
return Qnil;
2624 rb_hash_foreach(hash, delete_if_i, hash);
2648rb_hash_reject(
VALUE hash)
2653 result = hash_dup_with_compare_by_id(hash);
2655 rb_hash_foreach(result, delete_if_i, result);
2656 compact_after_delete(result);
2673rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2676 VALUE key, value, result;
2679 return copy_compare_by_id(rb_hash_new(), hash);
2681 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2683 for (i = 0; i < argc; i++) {
2685 value = rb_hash_lookup2(hash, key,
Qundef);
2686 if (!UNDEF_P(value))
2687 rb_hash_aset(result, key, value);
2705rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2710 result = hash_dup_with_compare_by_id(hash);
2712 for (i = 0; i < argc; i++) {
2714 rb_hash_delete(result, key);
2716 compact_after_delete(result);
2735rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2740 for (i=0; i<argc; i++) {
2741 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2767rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2772 for (i=0; i<argc; i++) {
2773 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2782 rb_hash_modify(hash);
2806rb_hash_select(
VALUE hash)
2811 result = hash_dup_with_compare_by_id(hash);
2813 rb_hash_foreach(result, keep_if_i, result);
2814 compact_after_delete(result);
2839rb_hash_select_bang(
VALUE hash)
2844 rb_hash_modify_check(hash);
2846 if (!n)
return Qnil;
2847 rb_hash_foreach(hash, keep_if_i, hash);
2870rb_hash_keep_if(
VALUE hash)
2873 rb_hash_modify_check(hash);
2874 if (!RHASH_TABLE_EMPTY_P(hash)) {
2875 rb_hash_foreach(hash, keep_if_i, hash);
2896 rb_hash_modify_check(hash);
2899 rb_hash_foreach(hash, clear_i, 0);
2901 else if (RHASH_AR_TABLE_P(hash)) {
2905 st_clear(RHASH_ST_TABLE(hash));
2906 compact_after_delete(hash);
2913hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2920rb_hash_key_str(
VALUE key)
2923 return rb_fstring(key);
2931hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2934 *key = rb_hash_key_str(*key);
2936 return hash_aset(key, val, arg, existing);
2939NOINSERT_UPDATE_CALLBACK(hash_aset)
2940NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2973 rb_hash_modify(hash);
2975 if (RHASH_TABLE_NULL_P(hash)) {
2976 if (iter_lev > 0) no_new_key();
2977 ar_alloc_table(hash);
2980 if (!RHASH_STRING_KEY_P(hash, key)) {
2981 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
2984 RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
3002 rb_hash_modify_check(hash);
3003 if (hash == hash2)
return hash;
3007 hash2 = to_hash(hash2);
3009 COPY_DEFAULT(hash, hash2);
3011 if (RHASH_AR_TABLE_P(hash)) {
3012 ar_free_and_clear_table(hash);
3015 st_free_table(RHASH_ST_TABLE(hash));
3016 RHASH_ST_CLEAR(hash);
3018 hash_copy(hash, hash2);
3021 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), 0));
3024 rb_gc_writebarrier_remember(hash);
3062rb_hash_empty_p(
VALUE hash)
3099rb_hash_each_value(
VALUE hash)
3102 rb_hash_foreach(hash, each_value_i, 0);
3137rb_hash_each_key(
VALUE hash)
3140 rb_hash_foreach(hash, each_key_i, 0);
3147 rb_yield(rb_assoc_new(key, value));
3190rb_hash_each_pair(
VALUE hash)
3193 if (rb_block_pair_yield_optimizable())
3194 rb_hash_foreach(hash, each_pair_i_fast, 0);
3196 rb_hash_foreach(hash, each_pair_i, 0);
3210 VALUE trans = p->trans, result = p->result;
3211 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3212 if (UNDEF_P(new_key)) {
3218 rb_hash_aset(result, new_key, value);
3226 rb_hash_aset(result, new_key, value);
3268rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3275 transarg.trans = to_hash(argv[0]);
3281 result = rb_hash_new();
3283 if (transarg.trans) {
3284 transarg.result = result;
3285 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3288 rb_hash_foreach(hash, transform_keys_i, result);
3308rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3311 int block_given = 0;
3315 trans = to_hash(argv[0]);
3321 rb_hash_modify_check(hash);
3322 if (!RHASH_TABLE_EMPTY_P(hash)) {
3324 VALUE new_keys = hash_alloc(0);
3326 rb_hash_foreach(hash, flatten_i, pairs);
3333 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3336 else if (block_given) {
3343 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3344 rb_hash_stlike_delete(hash, &key, NULL);
3346 rb_hash_aset(hash, new_key, val);
3347 rb_hash_aset(new_keys, new_key,
Qnil);
3349 rb_ary_clear(pairs);
3350 rb_hash_clear(new_keys);
3352 compact_after_delete(hash);
3357transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3363transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3367 rb_hash_modify(hash);
3393rb_hash_transform_values(
VALUE hash)
3398 result = hash_dup_with_compare_by_id(hash);
3399 SET_DEFAULT(result,
Qnil);
3402 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3403 compact_after_delete(result);
3425rb_hash_transform_values_bang(
VALUE hash)
3428 rb_hash_modify_check(hash);
3430 if (!RHASH_TABLE_EMPTY_P(hash)) {
3431 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3440 rb_ary_push(ary, rb_assoc_new(key, value));
3455rb_hash_to_a(
VALUE hash)
3460 rb_hash_foreach(hash, to_a_i, ary);
3475 rb_enc_copy(str, str2);
3486inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3492 rb_hash_foreach(hash, inspect_i, str);
3510rb_hash_inspect(
VALUE hash)
3524rb_hash_to_hash(
VALUE hash)
3534 pair = rb_check_array_type(arg);
3537 rb_builtin_class_name(arg));
3555rb_hash_to_h_block(
VALUE hash)
3558 rb_hash_foreach(hash, to_h_i, h);
3582rb_hash_to_h(
VALUE hash)
3585 return rb_hash_to_h_block(hash);
3589 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3597 rb_ary_push(ary, key);
3610MJIT_FUNC_EXPORTED
VALUE
3611rb_hash_keys(
VALUE hash)
3614 VALUE keys = rb_ary_new_capa(size);
3616 if (size == 0)
return keys;
3618 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3620 if (RHASH_AR_TABLE_P(hash)) {
3621 size = ar_keys(hash, ptr, size);
3624 st_table *table = RHASH_ST_TABLE(hash);
3625 size = st_keys(table, ptr, size);
3628 rb_gc_writebarrier_remember(keys);
3629 rb_ary_set_len(keys, size);
3632 rb_hash_foreach(hash, keys_i, keys);
3641 rb_ary_push(ary, value);
3655rb_hash_values(
VALUE hash)
3660 values = rb_ary_new_capa(size);
3661 if (size == 0)
return values;
3663 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3664 if (RHASH_AR_TABLE_P(hash)) {
3665 rb_gc_writebarrier_remember(values);
3667 size = ar_values(hash, ptr, size);
3670 else if (RHASH_ST_TABLE_P(hash)) {
3671 st_table *table = RHASH_ST_TABLE(hash);
3672 rb_gc_writebarrier_remember(values);
3674 size = st_values(table, ptr, size);
3677 rb_ary_set_len(values, size);
3681 rb_hash_foreach(hash, values_i, values);
3699MJIT_FUNC_EXPORTED
VALUE
3702 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3734 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3750 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3764recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3768 if (recur)
return Qtrue;
3770 data->result =
Qtrue;
3771 rb_hash_foreach(hash, eql_i, dt);
3773 return data->result;
3781 if (hash1 == hash2)
return Qtrue;
3787 if (
rb_eql(hash2, hash1)) {
3800 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3801 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3813 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3841 return hash_equal(hash1, hash2, FALSE);
3866 return hash_equal(hash1, hash2, TRUE);
3872 st_index_t *hval = (st_index_t *)arg;
3873 st_index_t hdata[2];
3875 hdata[0] = rb_hash(key);
3876 hdata[1] = rb_hash(val);
3877 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3896rb_hash_hash(
VALUE hash)
3902 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
3911 rb_hash_aset(hash, value, key);
3931rb_hash_invert(
VALUE hash)
3935 rb_hash_foreach(hash, rb_hash_invert_i, h);
3942 rb_hash_aset(hash, key, value);
3947rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3949 st_data_t newvalue = arg->arg;
3954 else if (RHASH_STRING_KEY_P(arg->hash, *key) && !
RB_OBJ_FROZEN(*key)) {
3955 *key = rb_hash_key_str(*key);
3961NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3966 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
4021rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4026 rb_hash_modify(self);
4027 for (i = 0; i < argc; i++){
4028 VALUE hash = to_hash(argv[i]);
4030 rb_hash_foreach(hash, rb_hash_update_block_i, self);
4033 rb_hash_foreach(hash, rb_hash_update_i, self);
4046rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4049 VALUE newvalue = uf_arg->value;
4052 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4058NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4064 VALUE hash = arg->hash;
4067 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4074 rb_hash_modify(hash1);
4075 hash2 = to_hash(hash2);
4080 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4083 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4140rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4142 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4152lookup2_call(
VALUE arg)
4155 return rb_hash_lookup2(args[0], args[1],
Qundef);
4164reset_hash_type(
VALUE arg)
4167 HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
4168 RHASH_ST_TABLE(p->hash)->type = p->orighash;
4178 args[1] = rb_assoc_new(key, val);
4204 ar_force_convert_table(hash, __FILE__, __LINE__);
4205 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4206 table = RHASH_ST_TABLE(hash);
4207 orighash = table->type;
4209 if (!RHASH_IDENTHASH_P(hash)) {
4214 assochash.compare = assoc_cmp;
4215 assochash.hash = orighash->hash;
4216 table->type = &assochash;
4219 ensure_arg.hash = hash;
4220 ensure_arg.orighash = orighash;
4222 if (!UNDEF_P(value))
return rb_assoc_new(key, value);
4227 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4237 args[1] = rb_assoc_new(key, val);
4263 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4274 rb_ary_cat(ary, pair, 2);
4311rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4320 if (level == 0)
return rb_hash_to_a(hash);
4323 rb_hash_foreach(hash, flatten_i, ary);
4328 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4330 else if (level < 0) {
4332 rb_funcallv(ary, id_flatten_bang, 0, 0);
4337 rb_hash_foreach(hash, flatten_i, ary);
4355 if (!
NIL_P(value)) {
4356 rb_hash_aset(hash, key, value);
4372rb_hash_compact(
VALUE hash)
4374 VALUE result = rb_hash_new();
4376 rb_hash_foreach(hash, set_if_not_nil, result);
4393rb_hash_compact_bang(
VALUE hash)
4396 rb_hash_modify_check(hash);
4399 rb_hash_foreach(hash, delete_if_nil, hash);
4406static st_table *rb_init_identtable_with_size(st_index_t size);
4437rb_hash_compare_by_id(
VALUE hash)
4442 if (rb_hash_compare_by_id_p(hash))
return hash;
4444 rb_hash_modify_check(hash);
4445 ar_force_convert_table(hash, __FILE__, __LINE__);
4446 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4448 tmp = hash_alloc(0);
4449 identtable = rb_init_identtable_with_size(
RHASH_SIZE(hash));
4450 RHASH_ST_TABLE_SET(tmp, identtable);
4451 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4452 st_free_table(RHASH_ST_TABLE(hash));
4453 RHASH_ST_TABLE_SET(hash, identtable);
4454 RHASH_ST_CLEAR(tmp);
4466MJIT_FUNC_EXPORTED
VALUE
4467rb_hash_compare_by_id_p(
VALUE hash)
4469 return RBOOL(RHASH_IDENTHASH_P(hash));
4473rb_ident_hash_new(
void)
4475 VALUE hash = rb_hash_new();
4476 RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
4481rb_ident_hash_new_with_size(st_index_t size)
4483 VALUE hash = rb_hash_new();
4484 RHASH_ST_TABLE_SET(hash, st_init_table_with_size(&identhash, size));
4489rb_init_identtable(
void)
4491 return st_init_table(&identhash);
4495rb_init_identtable_with_size(st_index_t size)
4497 return st_init_table_with_size(&identhash, size);
4563rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4572 rb_warn(
"given block not used");
4576 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4583 if (rb_block_pair_yield_optimizable())
4584 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4586 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4621rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4624 self = rb_hash_aref(self, *argv);
4625 if (!--argc)
return self;
4627 return rb_obj_dig(argc, argv, self,
Qnil);
4635 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4646 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4664 other = to_hash(other);
4666 return hash_le(hash, other);
4683 other = to_hash(other);
4685 return hash_le(hash, other);
4702 other = to_hash(other);
4704 return hash_le(other, hash);
4721 other = to_hash(other);
4723 return hash_le(other, hash);
4730 return rb_hash_aref(hash, *argv);
4746rb_hash_to_proc(
VALUE hash)
4748 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4752rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4758add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4761 if (existing)
return ST_STOP;
4780 if (RHASH_AR_TABLE_P(hash)) {
4781 hash_ar_table(hash);
4783 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4787 ar_force_convert_table(hash, __FILE__, __LINE__);
4789 tbl = RHASH_TBL_RAW(hash);
4790 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4795key_stringify(
VALUE key)
4798 rb_hash_key_str(key) : key;
4802ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4805 for (i = 0; i < argc; ) {
4806 st_data_t k = key_stringify(argv[i++]);
4807 st_data_t v = argv[i++];
4808 ar_insert(hash, k, v);
4817 HASH_ASSERT(argc % 2 == 0);
4819 st_index_t size = argc / 2;
4821 if (RHASH_TABLE_NULL_P(hash)) {
4822 if (size <= RHASH_AR_TABLE_MAX_SIZE) {
4823 hash_ar_table(hash);
4826 RHASH_TBL_RAW(hash);
4830 if (RHASH_AR_TABLE_P(hash) &&
4831 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4832 ar_bulk_insert(hash, argc, argv);
4835 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4840static char **origenviron;
4842#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4843#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4844static char **my_environ;
4846#define environ my_environ
4848#define getenv(n) rb_w32_ugetenv(n)
4849#elif defined(__APPLE__)
4851#define environ (*_NSGetEnviron())
4852#define GET_ENVIRON(e) (e)
4853#define FREE_ENVIRON(e)
4855extern char **environ;
4856#define GET_ENVIRON(e) (e)
4857#define FREE_ENVIRON(e)
4859#ifdef ENV_IGNORECASE
4860#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4861#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4863#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4864#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4867#define ENV_LOCK() RB_VM_LOCK_ENTER()
4868#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4874 return rb_utf8_encoding();
4876 return rb_locale_encoding();
4881env_enc_str_new(
const char *ptr,
long len,
rb_encoding *enc)
4890env_str_new(
const char *ptr,
long len)
4892 return env_enc_str_new(ptr, len, env_encoding());
4896env_str_new2(
const char *ptr)
4898 if (!ptr)
return Qnil;
4899 return env_str_new(ptr, strlen(ptr));
4903getenv_with_lock(
const char *name)
4908 const char *val = getenv(name);
4909 ret = env_str_new2(val);
4916has_env_with_lock(
const char *name)
4926 return val ? true :
false;
4929static const char TZ_ENV[] =
"TZ";
4946 return rb_str_fill_terminator(str, 1);
4949#define get_env_ptr(var, val) \
4950 (var = get_env_cstr(val, #var))
4952static inline const char *
4953env_name(
volatile VALUE *s)
4957 get_env_ptr(name, *s);
4961#define env_name(s) env_name(&(s))
4966reset_by_modified_env(
const char *nam)
4974 if (ENVMATCH(nam, TZ_ENV)) {
4975 ruby_reset_timezone();
4980env_delete(
VALUE name)
4982 const char *nam = env_name(name);
4983 reset_by_modified_env(nam);
4984 VALUE val = getenv_with_lock(nam);
4987 ruby_setenv(nam, 0);
5022 val = env_delete(name);
5042 const char *nam = env_name(name);
5043 VALUE env = getenv_with_lock(nam);
5083 if (block_given && argc == 2) {
5084 rb_warn(
"block supersedes default value argument");
5086 nam = env_name(key);
5087 env = getenv_with_lock(nam);
5090 if (block_given)
return rb_yield(key);
5092 rb_key_err_raise(
rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5099#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5102in_origenv(
const char *str)
5105 for (env = origenviron; *env; ++env) {
5106 if (*env == str)
return 1;
5112envix(
const char *nam)
5116 register int i, len = strlen(nam);
5119 env = GET_ENVIRON(environ);
5120 for (i = 0; env[i]; i++) {
5121 if (ENVNMATCH(env[i],nam,len) && env[i][len] ==
'=')
5124 FREE_ENVIRON(environ);
5131getenvsize(
const WCHAR* p)
5133 const WCHAR* porg = p;
5134 while (*p++) p += lstrlenW(p) + 1;
5135 return p - porg + 1;
5139getenvblocksize(
void)
5149check_envsize(
size_t n)
5151 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5155 WCHAR* p = GetEnvironmentStringsW();
5158 FreeEnvironmentStringsW(p);
5159 if (n >= getenvblocksize()) {
5167#if defined(_WIN32) || \
5168 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5170NORETURN(
static void invalid_envname(
const char *name));
5173invalid_envname(
const char *name)
5179check_envname(
const char *name)
5181 if (strchr(name,
'=')) {
5182 invalid_envname(name);
5189ruby_setenv(
const char *name,
const char *value)
5192# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5193# define HAVE__WPUTENV_S 1
5200 check_envname(name);
5201 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5204 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5205 if (check_envsize((
size_t)len + len2)) {
5208 wname =
ALLOCV_N(WCHAR, buf, len + len2);
5209 wvalue = wname + len;
5210 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5211 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5212#ifndef HAVE__WPUTENV_S
5213 wname[len-1] = L
'=';
5217 wname =
ALLOCV_N(WCHAR, buf, len + 1);
5218 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5219 wvalue = wname + len;
5221#ifndef HAVE__WPUTENV_S
5222 wname[len-1] = L
'=';
5228#ifndef HAVE__WPUTENV_S
5229 failed = _wputenv(wname);
5231 failed = _wputenv_s(wname, wvalue);
5239 if (!value || !*value) {
5241 if (!SetEnvironmentVariable(name, value) &&
5242 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5246 invalid_envname(name);
5248#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5253 ret = setenv(name, value, 1);
5270 ret = unsetenv(name);
5282 size_t len, mem_size;
5283 char **env_ptr, *str, *mem_ptr;
5285 check_envname(name);
5288 mem_size = len + strlen(value) + 2;
5289 mem_ptr = malloc(mem_size);
5290 if (mem_ptr == NULL)
5292 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5297 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5298 if (!strncmp(str, name, len) && str[len] ==
'=') {
5299 if (!in_origenv(str)) free(str);
5300 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5311 ret = putenv(mem_ptr);
5328 if (environ == origenviron) {
5333 for (max = i; environ[max]; max++) ;
5334 tmpenv =
ALLOC_N(
char*, max+2);
5335 for (j=0; j<max; j++)
5342 char **envp = origenviron;
5343 while (*envp && *envp != environ[i]) envp++;
5347 while (environ[i]) {
5348 environ[i] = environ[i+1];
5355 if (!value)
goto finish;
5360 len = strlen(name) + strlen(value) + 2;
5361 environ[i] =
ALLOC_N(
char, len);
5362 snprintf(environ[i],len,
"%s=%s",name,value);
5371ruby_unsetenv(
const char *name)
5373 ruby_setenv(name, 0);
5423 return env_aset(nm, val);
5439 get_env_ptr(name, nm);
5440 get_env_ptr(value, val);
5442 ruby_setenv(name, value);
5443 reset_by_modified_env(name);
5450 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5451 VALUE ary = rb_ary_new();
5455 char **env = GET_ENVIRON(environ);
5457 char *s = strchr(*env,
'=');
5459 const char *p = *env;
5462 rb_ary_push(ary, e);
5466 FREE_ENVIRON(environ);
5489 return env_keys(FALSE);
5500 env = GET_ENVIRON(environ);
5501 for (; *env ; ++env) {
5502 if (strchr(*env,
'=')) {
5506 FREE_ENVIRON(environ);
5531env_each_key(
VALUE ehash)
5537 keys = env_keys(FALSE);
5547 VALUE ary = rb_ary_new();
5551 char **env = GET_ENVIRON(environ);
5554 char *s = strchr(*env,
'=');
5556 rb_ary_push(ary, env_str_new2(s+1));
5560 FREE_ENVIRON(environ);
5582 return env_values();
5603env_each_value(
VALUE ehash)
5609 values = env_values();
5635env_each_pair(
VALUE ehash)
5641 VALUE ary = rb_ary_new();
5645 char **env = GET_ENVIRON(environ);
5648 char *s = strchr(*env,
'=');
5650 rb_ary_push(ary, env_str_new(*env, s-*env));
5651 rb_ary_push(ary, env_str_new2(s+1));
5655 FREE_ENVIRON(environ);
5659 if (rb_block_pair_yield_optimizable()) {
5696env_reject_bang(
VALUE ehash)
5703 keys = env_keys(FALSE);
5704 RBASIC_CLEAR_CLASS(keys);
5715 if (del == 0)
return Qnil;
5740env_delete_if(
VALUE ehash)
5743 env_reject_bang(ehash);
5770 result = rb_ary_new();
5771 for (i=0; i<argc; i++) {
5772 rb_ary_push(result, rb_f_getenv(
Qnil, argv[i]));
5799env_select(
VALUE ehash)
5806 result = rb_hash_new();
5807 keys = env_keys(FALSE);
5813 rb_hash_aset(result, key, val);
5860env_select_bang(
VALUE ehash)
5867 keys = env_keys(FALSE);
5868 RBASIC_CLEAR_CLASS(keys);
5879 if (del == 0)
return Qnil;
5902env_keep_if(
VALUE ehash)
5905 env_select_bang(ehash);
5925 VALUE key, value, result;
5928 return rb_hash_new();
5930 result = rb_hash_new_with_size(argc);
5932 for (i = 0; i < argc; i++) {
5934 value = rb_f_getenv(
Qnil, key);
5936 rb_hash_aset(result, key, value);
5948 keys = env_keys(TRUE);
5952 ruby_setenv(nam, 0);
5971 return rb_env_clear();
6003 char **env = GET_ENVIRON(environ);
6005 char *s = strchr(*env,
'=');
6007 if (env != environ) {
6019 FREE_ENVIRON(environ);
6040 VALUE ary = rb_ary_new();
6044 char **env = GET_ENVIRON(environ);
6046 char *s = strchr(*env,
'=');
6048 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
6049 env_str_new2(s+1)));
6053 FREE_ENVIRON(environ);
6075env_size_with_lock(
void)
6081 char **env = GET_ENVIRON(environ);
6083 FREE_ENVIRON(environ);
6103 return INT2FIX(env_size_with_lock());
6123 char **env = GET_ENVIRON(environ);
6127 FREE_ENVIRON(environ);
6131 return RBOOL(empty);
6162 const char *s = env_name(key);
6163 return RBOOL(has_env_with_lock(s));
6189 const char *s = env_name(key);
6190 VALUE e = getenv_with_lock(s);
6193 return rb_assoc_new(key, e);
6222 char **env = GET_ENVIRON(environ);
6224 char *s = strchr(*env,
'=');
6226 long len = strlen(s);
6234 FREE_ENVIRON(environ);
6265 char **env = GET_ENVIRON(environ);
6268 const char *p = *env;
6269 char *s = strchr(p,
'=');
6271 long len = strlen(s);
6273 result = rb_assoc_new(
rb_str_new(p, s-p-1), obj);
6279 FREE_ENVIRON(environ);
6310 char **env = GET_ENVIRON(environ);
6312 char *s = strchr(*env,
'=');
6314 long len = strlen(s);
6316 str = env_str_new(*env, s-*env-1);
6322 FREE_ENVIRON(environ);
6332 VALUE hash = rb_hash_new();
6336 char **env = GET_ENVIRON(environ);
6338 char *s = strchr(*env,
'=');
6340 rb_hash_aset(hash, env_str_new(*env, s-*env),
6345 FREE_ENVIRON(environ);
6361 return env_to_hash();
6376 return env_to_hash();
6400 VALUE hash = env_to_hash();
6402 hash = rb_hash_to_h_block(hash);
6420 VALUE key, hash = env_to_hash();
6422 for (i = 0; i < argc; i++) {
6424 rb_hash_delete(hash, key);
6448 return rb_hash_delete_if(env_to_hash());
6451NORETURN(
static VALUE env_freeze(
VALUE self));
6460env_freeze(
VALUE self)
6489 char **env = GET_ENVIRON(environ);
6491 const char *p = *env;
6492 char *s = strchr(p,
'=');
6494 key = env_str_new(p, s-p);
6496 result = rb_assoc_new(key, val);
6499 FREE_ENVIRON(environ);
6528 return rb_hash_invert(env_to_hash());
6535 const char *keyptr, *eptr;
6543 if (elen != keylen)
continue;
6544 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6545 rb_ary_delete_at(keys, i);
6556 keylist_delete(keys, key);
6584 keys = env_keys(TRUE);
6585 if (env == hash)
return env;
6586 hash = to_hash(hash);
6587 rb_hash_foreach(hash, env_replace_i, keys);
6607 if (!
NIL_P(oldval)) {
6656 env_update_block_i : env_update_i;
6657 for (
int i = 0; i < argc; ++i) {
6658 VALUE hash = argv[i];
6659 if (env == hash)
continue;
6660 hash = to_hash(hash);
6661 rb_hash_foreach(hash, func, 0);
6681 rb_get_freeze_opt(1, &opt);
6710 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7192 id_hash_iter_lev = rb_make_internal_id();
7454 origenviron = environ;
7524 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7526 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.