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/st.h"
39#include "internal/symbol.h"
40#include "internal/thread.h"
41#include "internal/time.h"
42#include "internal/vm.h"
46#include "ruby_assert.h"
75#include "internal/gc.h"
78#define SET_DEFAULT(hash, ifnone) ( \
79 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
80 RHASH_SET_IFNONE(hash, ifnone))
82#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
84#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
87copy_default(
struct RHash *hash,
const struct RHash *hash2)
89 hash->basic.flags &= ~RHASH_PROC_DEFAULT;
90 hash->basic.flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
114VALUE rb_cHash_empty_frozen;
117static ID id_hash, id_flatten_bang;
118static ID id_hash_iter_lev;
120#define id_default idDefault
132 if (a == b)
return 0;
137 if (UNDEF_P(a) || UNDEF_P(b))
return -1;
146hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
148 if (recurse)
return INT2FIX(0);
149 return rb_funcallv(obj, id_hash, 0, 0);
152static long rb_objid_hash(st_index_t index);
155dbl_to_index(
double d)
157 union {
double d; st_index_t i;} u;
163rb_dbl_long_hash(
double d)
166 if (d == 0.0) d = 0.0;
167#if SIZEOF_INT == SIZEOF_VOIDP
170 return rb_objid_hash(dbl_to_index(d));
175any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
187 hnum = RSYMBOL(a)->hashval;
194 hnum = rb_objid_hash((st_index_t)a);
200 hval = rb_big_hash(a);
204 hnum = rb_dbl_long_hash(rb_float_value(a));
207 hnum = other_func(a);
217VALUE rb_vm_call0(rb_execution_context_t *ec,
VALUE recv,
ID id,
int argc,
const VALUE *argv,
const rb_callable_method_entry_t *cme,
int kw_splat);
220obj_any_hash(
VALUE obj)
225 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, id_hash);
226 if (cme && METHOD_ENTRY_BASIC(cme)) {
228 if (cme->def->type == VM_METHOD_TYPE_CFUNC && cme->def->body.cfunc.func == (rb_cfunc_t)rb_obj_hash) {
229 hval = rb_obj_hash(obj);
232 hval = rb_vm_call0(GET_EC(), obj, id_hash, 0, 0, cme, 0);
238 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
245 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
263 return any_hash(a, obj_any_hash);
269 return LONG2FIX(any_hash(obj, obj_any_hash));
278static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
279static const uint32_t prime2 = 0x830fcab9;
282static inline uint64_t
283mult_and_mix(uint64_t m1, uint64_t m2)
285#if defined HAVE_UINT128_T
286 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
287 return (uint64_t) (r >> 64) ^ (uint64_t) r;
289 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
290 uint64_t lm1 = m1, lm2 = m2;
291 uint64_t v64_128 = hm1 * hm2;
292 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
293 uint64_t v1_32 = lm1 * lm2;
295 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
299static inline uint64_t
300key64_hash(uint64_t key, uint32_t seed)
302 return mult_and_mix(key + seed, prime1);
306#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
309rb_objid_hash(st_index_t index)
311 return (
long)st_index_hash(index);
317 VALUE object_id = rb_obj_id(obj);
319 object_id = rb_big_hash(object_id);
321#if SIZEOF_LONG == SIZEOF_VOIDP
322 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
323#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
324 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
332rb_obj_hash(
VALUE obj)
334 long hnum = any_hash(obj, objid_hash);
338static const struct st_hash_type objhash = {
343#define rb_ident_cmp st_numcmp
346rb_ident_hash(st_data_t n)
355 n ^= dbl_to_index(rb_float_value(n));
359 return (st_index_t)st_index_hash((st_index_t)n);
362#define identhash rb_hashtype_ident
363const struct st_hash_type rb_hashtype_ident = {
368#define RHASH_IDENTHASH_P(hash) (RHASH_TYPE(hash) == &identhash)
369#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
371typedef st_index_t st_hash_t;
381#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
383#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
384#define RHASH_AR_CLEARED_HINT 0xff
386static inline st_hash_t
387ar_do_hash(st_data_t key)
389 return (st_hash_t)rb_any_hash(key);
392static inline ar_hint_t
393ar_do_hash_hint(st_hash_t hash_value)
395 return (ar_hint_t)hash_value;
398static inline ar_hint_t
399ar_hint(
VALUE hash,
unsigned int index)
401 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
405ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
407 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
411ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
413 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
417ar_clear_entry(
VALUE hash,
unsigned int index)
419 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
421 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
425ar_cleared_entry(
VALUE hash,
unsigned int index)
427 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
431 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
432 return UNDEF_P(pair->key);
440ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
442 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
445 ar_hint_set(hash, index, hash_value);
448#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
449 RHASH_AR_TABLE_SIZE_RAW(h))
451#define RHASH_AR_TABLE_BOUND_RAW(h) \
452 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
453 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
455#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
456#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
458#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
460static inline unsigned int
461RHASH_AR_TABLE_BOUND(
VALUE h)
463 HASH_ASSERT(RHASH_AR_TABLE_P(h));
464 const unsigned int bound = RHASH_AR_TABLE_BOUND_RAW(h);
465 HASH_ASSERT(bound <= RHASH_AR_TABLE_MAX_SIZE);
470#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
473rb_hash_dump(
VALUE hash)
475 rb_obj_info_dump(hash);
477 if (RHASH_AR_TABLE_P(hash)) {
478 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
480 fprintf(stderr,
" size:%u bound:%u\n",
481 RHASH_AR_TABLE_SIZE(hash), bound);
483 for (i=0; i<bound; i++) {
486 if (!ar_cleared_entry(hash, i)) {
487 char b1[0x100], b2[0x100];
488 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
491 fprintf(stderr,
" %d key:%s val:%s hint:%02x\n", i,
492 rb_raw_obj_info(b1, 0x100, k),
493 rb_raw_obj_info(b2, 0x100, v),
497 fprintf(stderr,
" %d empty\n", i);
504hash_verify_(
VALUE hash,
const char *file,
int line)
508 if (RHASH_AR_TABLE_P(hash)) {
509 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
511 for (i=0; i<bound; i++) {
513 if (!ar_cleared_entry(hash, i)) {
514 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
517 HASH_ASSERT(!UNDEF_P(k));
518 HASH_ASSERT(!UNDEF_P(v));
522 if (n != RHASH_AR_TABLE_SIZE(hash)) {
523 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
527 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
528 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
529 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
536#define hash_verify(h) ((void)0)
540RHASH_TABLE_EMPTY_P(
VALUE hash)
545#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
546#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
549hash_st_table_init(
VALUE hash,
const struct st_hash_type *
type, st_index_t size)
551 st_init_existing_table_with_size(RHASH_ST_TABLE(hash),
type, size);
552 RHASH_SET_ST_FLAG(hash);
556rb_hash_st_table_set(
VALUE hash, st_table *st)
558 HASH_ASSERT(st != NULL);
559 RHASH_SET_ST_FLAG(hash);
561 *RHASH_ST_TABLE(hash) = *st;
565RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
567 HASH_ASSERT(RHASH_AR_TABLE_P(h));
568 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
570 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
571 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
575RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
577 HASH_ASSERT(RHASH_AR_TABLE_P(h));
578 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
580 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
581 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
585HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
587 HASH_ASSERT(RHASH_AR_TABLE_P(h));
589 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
594#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
597RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
599 HASH_ASSERT(RHASH_AR_TABLE_P(h));
600 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
603 RHASH_AR_TABLE_SIZE_SET(h, new_size);
606 RHASH_AR_TABLE_SIZE_SET(h, 0);
607 RHASH_AR_TABLE_BOUND_SET(h, 0);
613RHASH_AR_TABLE_CLEAR(
VALUE h)
615 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
616 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
618 memset(RHASH_AR_TABLE(h), 0,
sizeof(ar_table));
621NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
626 return rb_any_cmp(x, y) == 0;
630ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
632 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
633 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
637 for (i = 0; i < bound; i++) {
638 if (hints[i] == hint) {
639 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
640 if (ar_equal(key, pair->key)) {
641 RB_DEBUG_COUNTER_INC(artable_hint_hit);
647 static char fname[256];
650 if (pid != getpid()) {
651 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
652 if ((fp = fopen(fname,
"w")) == NULL) rb_bug(
"fopen");
655 st_hash_t h1 = ar_do_hash(key);
656 st_hash_t h2 = ar_do_hash(pair->key);
658 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
660 " pair->key:%016lx %s\n",
661 h1 == h2, i, hints[i], hint,
662 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
664 RB_DEBUG_COUNTER_INC(artable_hint_miss);
668 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
669 return RHASH_AR_TABLE_MAX_BOUND;
673ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
675 ar_hint_t hint = ar_do_hash_hint(hash_value);
676 return ar_find_entry_hint(hash, hint, key);
680hash_ar_free_and_clear_table(
VALUE hash)
682 RHASH_AR_TABLE_CLEAR(hash);
684 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
685 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
688void rb_st_add_direct_with_hash(st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
690enum ar_each_key_type {
697ar_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)
699 for (
int i = 0; i < max; i++) {
700 ar_table_pair *pair = &ar->pairs[i];
703 case ar_each_key_copy:
704 dst_keys[i] = pair->key;
706 case ar_each_key_cmp:
707 if (dst_keys[i] != pair->key)
return 1;
709 case ar_each_key_insert:
710 if (UNDEF_P(pair->key))
continue;
711 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
720ar_force_convert_table(
VALUE hash,
const char *file,
int line)
722 if (RHASH_ST_TABLE_P(hash)) {
723 return RHASH_ST_TABLE(hash);
726 ar_table *ar = RHASH_AR_TABLE(hash);
727 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
728 unsigned int bound, size;
732 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
733 bound = RHASH_AR_TABLE_BOUND(hash);
734 size = RHASH_AR_TABLE_SIZE(hash);
735 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
737 for (
unsigned int i = 0; i < bound; i++) {
739 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
743 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
744 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
745 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
750 st_table *new_tab = &tab;
751 st_init_existing_table_with_size(new_tab, &objhash, size);
752 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
753 hash_ar_free_and_clear_table(hash);
754 RHASH_ST_TABLE_SET(hash, new_tab);
755 return RHASH_ST_TABLE(hash);
760ar_compact_table(
VALUE hash)
762 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
763 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
770 ar_table_pair *pairs = RHASH_AR_TABLE(hash)->pairs;
772 for (i=0; i<bound; i++) {
773 if (ar_cleared_entry(hash, i)) {
775 for (; j<bound; j++) {
776 if (!ar_cleared_entry(hash, j)) {
778 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
779 ar_clear_entry(hash, j);
790 HASH_ASSERT(i<=bound);
792 RHASH_AR_TABLE_BOUND_SET(hash, size);
799ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
801 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
803 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
807 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
808 bin = ar_compact_table(hash);
810 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
812 ar_set_entry(hash, bin, key, val, hash_value);
813 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
814 RHASH_AR_TABLE_SIZE_INC(hash);
820ensure_ar_table(
VALUE hash)
822 if (!RHASH_AR_TABLE_P(hash)) {
823 rb_raise(
rb_eRuntimeError,
"hash representation was changed during iteration");
828ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
830 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
831 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
833 for (i = 0; i < bound; i++) {
834 if (ar_cleared_entry(hash, i))
continue;
836 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
837 st_data_t key = (st_data_t)pair->key;
838 st_data_t val = (st_data_t)pair->val;
839 enum st_retval retval = (*func)(key, val, arg, 0);
840 ensure_ar_table(hash);
851 (*replace)(&key, &val, arg, TRUE);
854 pair = RHASH_AR_TABLE_REF(hash, i);
855 pair->key = (
VALUE)key;
856 pair->val = (
VALUE)val;
860 ar_clear_entry(hash, i);
861 RHASH_AR_TABLE_SIZE_DEC(hash);
870ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
872 return ar_general_foreach(hash, func, replace, arg);
876 st_foreach_callback_func *func;
881apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
883 const struct functor *f = (
void *)d;
884 return f->func(k, v, f->arg);
888ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
890 const struct functor f = { func, arg };
891 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
895ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
898 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
899 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
900 enum st_retval retval;
905 for (i = 0; i < bound; i++) {
906 if (ar_cleared_entry(hash, i))
continue;
908 pair = RHASH_AR_TABLE_REF(hash, i);
910 hint = ar_hint(hash, i);
912 retval = (*func)(key, pair->val, arg, 0);
913 ensure_ar_table(hash);
918 pair = RHASH_AR_TABLE_REF(hash, i);
919 if (pair->key == never)
break;
920 ret = ar_find_entry_hint(hash, hint, key);
921 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
922 (*func)(0, 0, arg, 1);
932 if (!ar_cleared_entry(hash, i)) {
933 ar_clear_entry(hash, i);
934 RHASH_AR_TABLE_SIZE_DEC(hash);
945ar_update(
VALUE hash, st_data_t key,
946 st_update_callback_func *func, st_data_t arg)
948 int retval, existing;
949 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
950 st_data_t value = 0, old_key;
951 st_hash_t hash_value = ar_do_hash(key);
953 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
958 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
959 bin = ar_find_entry(hash, hash_value, key);
960 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
967 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
972 retval = (*func)(&key, &value, arg, existing);
974 ensure_ar_table(hash);
979 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
984 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
985 if (old_key != key) {
993 ar_clear_entry(hash, bin);
994 RHASH_AR_TABLE_SIZE_DEC(hash);
1002ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1004 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1005 st_hash_t hash_value = ar_do_hash(key);
1007 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1012 bin = ar_find_entry(hash, hash_value, key);
1013 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1014 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1017 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1018 bin = ar_compact_table(hash);
1020 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1022 ar_set_entry(hash, bin, key, value, hash_value);
1023 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1024 RHASH_AR_TABLE_SIZE_INC(hash);
1028 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1034ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1036 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1040 st_hash_t hash_value = ar_do_hash(key);
1041 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1043 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1045 unsigned bin = ar_find_entry(hash, hash_value, key);
1047 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1051 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1052 if (value != NULL) {
1053 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1061ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1064 st_hash_t hash_value = ar_do_hash(*key);
1066 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1068 return st_delete(RHASH_ST_TABLE(hash), key, value);
1071 bin = ar_find_entry(hash, hash_value, *key);
1073 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1074 if (value != 0) *value = 0;
1079 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
1082 ar_clear_entry(hash, bin);
1083 RHASH_AR_TABLE_SIZE_DEC(hash);
1089ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1091 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1092 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1094 for (i = 0; i < bound; i++) {
1095 if (!ar_cleared_entry(hash, i)) {
1096 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1097 if (value != 0) *value = pair->val;
1099 ar_clear_entry(hash, i);
1100 RHASH_AR_TABLE_SIZE_DEC(hash);
1105 if (value != NULL) *value = 0;
1110ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1112 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1113 st_data_t *keys_start = keys, *keys_end = keys + size;
1115 for (i = 0; i < bound; i++) {
1116 if (keys == keys_end) {
1120 if (!ar_cleared_entry(hash, i)) {
1121 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1126 return keys - keys_start;
1130ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1132 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1133 st_data_t *values_start = values, *values_end = values + size;
1135 for (i = 0; i < bound; i++) {
1136 if (values == values_end) {
1140 if (!ar_cleared_entry(hash, i)) {
1141 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1146 return values - values_start;
1152 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1153 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1155 *new_tab = *old_tab;
1156 RHASH_AR_TABLE(hash1)->ar_hint.word = RHASH_AR_TABLE(hash2)->ar_hint.word;
1157 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1158 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1160 rb_gc_writebarrier_remember(hash1);
1168 if (RHASH_AR_TABLE(hash) != NULL) {
1169 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1170 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1173 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1174 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1179hash_st_free(
VALUE hash)
1181 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1183 st_table *tab = RHASH_ST_TABLE(hash);
1186 xfree(tab->entries);
1190hash_st_free_and_clear_table(
VALUE hash)
1194 RHASH_ST_CLEAR(hash);
1198rb_hash_free(
VALUE hash)
1200 if (RHASH_ST_TABLE_P(hash)) {
1205typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1209 st_foreach_func *func;
1214foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1219 if (error)
return ST_STOP;
1220 status = (*arg->func)(key, value, arg->arg);
1221 if (status == ST_CONTINUE) {
1233 arg.func = (st_foreach_func *)func;
1235 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1244 rb_foreach_func *func;
1249hash_iter_status_check(
int status)
1264hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1268 if (error)
return ST_STOP;
1270 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1273 return hash_iter_status_check(status);
1277hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1281 if (error)
return ST_STOP;
1283 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1284 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1286 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1290 return hash_iter_status_check(status);
1294iter_lev_in_ivar(
VALUE hash)
1299 HASH_ASSERT(lev >= 0);
1300 return (
unsigned long)lev;
1306iter_lev_in_ivar_set(
VALUE hash,
unsigned long lev)
1308 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1310 rb_ivar_set_internal(hash, id_hash_iter_lev,
LONG2FIX((
long)lev));
1313static inline unsigned long
1314iter_lev_in_flags(
VALUE hash)
1316 return (
unsigned long)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1320iter_lev_in_flags_set(
VALUE hash,
unsigned long lev)
1322 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1323 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1327hash_iterating_p(
VALUE hash)
1329 return iter_lev_in_flags(hash) > 0;
1333hash_iter_lev_inc(
VALUE hash)
1335 unsigned long lev = iter_lev_in_flags(hash);
1336 if (lev == RHASH_LEV_MAX) {
1337 lev = iter_lev_in_ivar(hash) + 1;
1344 iter_lev_in_flags_set(hash, lev);
1345 if (lev < RHASH_LEV_MAX)
return;
1347 iter_lev_in_ivar_set(hash, lev);
1351hash_iter_lev_dec(
VALUE hash)
1353 unsigned long lev = iter_lev_in_flags(hash);
1354 if (lev == RHASH_LEV_MAX) {
1355 lev = iter_lev_in_ivar(hash);
1356 if (lev > RHASH_LEV_MAX) {
1357 iter_lev_in_ivar_set(hash, lev-1);
1360 rb_attr_delete(hash, id_hash_iter_lev);
1362 else if (lev == 0) {
1365 iter_lev_in_flags_set(hash, lev - 1);
1369hash_foreach_ensure(
VALUE hash)
1371 hash_iter_lev_dec(hash);
1377rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1379 if (RHASH_AR_TABLE_P(hash)) {
1380 return ar_foreach(hash, func, arg);
1383 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1389rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1391 if (RHASH_AR_TABLE_P(hash)) {
1392 return ar_foreach_with_replace(hash, func, replace, arg);
1395 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1400hash_foreach_call(
VALUE arg)
1404 if (RHASH_AR_TABLE_P(hash)) {
1405 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1406 (st_data_t)arg, (st_data_t)
Qundef);
1408 else if (RHASH_ST_TABLE_P(hash)) {
1409 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1410 (st_data_t)arg, (st_data_t)
Qundef);
1413 rb_raise(
rb_eRuntimeError,
"ret: %d, hash modified during iteration", ret);
1419rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1423 if (RHASH_TABLE_EMPTY_P(hash))
1426 arg.func = (rb_foreach_func *)func;
1429 hash_foreach_call((
VALUE)&arg);
1432 hash_iter_lev_inc(hash);
1433 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1438void rb_st_compact_table(st_table *tab);
1441compact_after_delete(
VALUE hash)
1443 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1444 rb_st_compact_table(RHASH_ST_TABLE(hash));
1452 const size_t size =
sizeof(
struct RHash) + (st ? sizeof(st_table) : sizeof(ar_table));
1454 NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags, size, 0);
1462hash_alloc(VALUE klass)
1465 return hash_alloc_flags(klass, 0,
Qnil,
sizeof(st_table) >
sizeof(ar_table));
1469empty_hash_alloc(
VALUE klass)
1471 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1473 return hash_alloc(klass);
1485 if (rb_hash_compare_by_id_p(basis)) {
1486 return rb_hash_compare_by_id(hash);
1492rb_hash_new_with_size(st_index_t size)
1494 bool st = size > RHASH_AR_TABLE_MAX_SIZE;
1498 hash_st_table_init(ret, &objhash, size);
1507 return rb_hash_new_with_size((st_index_t)
capa);
1513 if (RHASH_AR_TABLE_P(hash)) {
1514 if (RHASH_AR_TABLE_P(ret)) {
1518 st_table *tab = RHASH_ST_TABLE(ret);
1519 st_init_existing_table_with_size(tab, &objhash, RHASH_AR_TABLE_SIZE(hash));
1521 int bound = RHASH_AR_TABLE_BOUND(hash);
1522 for (
int i = 0; i < bound; i++) {
1523 if (ar_cleared_entry(hash, i))
continue;
1525 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1526 st_add_direct(tab, pair->key, pair->val);
1533 HASH_ASSERT(
sizeof(st_table) <=
sizeof(ar_table));
1535 RHASH_SET_ST_FLAG(ret);
1536 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1538 rb_gc_writebarrier_remember(ret);
1544hash_dup_with_compare_by_id(
VALUE hash)
1547 if (RHASH_ST_TABLE_P(hash)) {
1548 RHASH_SET_ST_FLAG(dup);
1551 RHASH_UNSET_ST_FLAG(dup);
1554 return hash_copy(dup, hash);
1569 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1576rb_hash_resurrect(
VALUE hash)
1583rb_hash_modify_check(
VALUE hash)
1585 rb_check_frozen(hash);
1589rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1591 return ar_force_convert_table(hash, file, line);
1595rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1598 return rb_hash_tbl_raw(hash, file, line);
1602rb_hash_modify(
VALUE hash)
1604 rb_hash_modify_check(hash);
1607NORETURN(
static void no_new_key(
void));
1611 rb_raise(
rb_eRuntimeError,
"can't add a new key into hash during iteration");
1619#define NOINSERT_UPDATE_CALLBACK(func) \
1621func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1623 if (!existing) no_new_key(); \
1624 return func(key, val, (struct update_arg *)arg, existing); \
1628func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1630 return func(key, val, (struct update_arg *)arg, existing); \
1635 st_update_callback_func *func;
1641typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1644rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1646 if (RHASH_AR_TABLE_P(hash)) {
1647 int result = ar_update(hash, key, func, arg);
1649 ar_force_convert_table(hash, __FILE__, __LINE__);
1656 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1660tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1663 st_data_t old_key = *key;
1664 st_data_t old_value = *val;
1665 VALUE hash = p->hash;
1666 int ret = (p->func)(key, val, arg, existing);
1671 if (!existing || *key != old_key || *val != old_value) {
1672 rb_hash_modify(hash);
1679 rb_hash_modify(hash);
1687tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1690 .arg = optional_arg,
1694 .value = (
VALUE)optional_arg,
1697 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1706#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1708#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1709 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1712#define RHASH_UPDATE(hash, key, func, arg) \
1713 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1721 if (n != 2 && (n >= 0 || n < -3)) {
1722 if (n < 0) n = -n-1;
1723 rb_raise(
rb_eTypeError,
"default_proc takes two arguments (2 for %d)", n);
1734 rb_hash_modify(hash);
1736 if (capa_value !=
INT2FIX(0)) {
1738 if (
capa > 0 &&
RHASH_SIZE(hash) == 0 && RHASH_AR_TABLE_P(hash)) {
1739 hash_st_table_init(hash, &objhash,
capa);
1743 if (!
NIL_P(block)) {
1744 if (ifnone_unset !=
Qtrue) {
1748 SET_PROC_DEFAULT(hash, block);
1797rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1802 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1808 tmp = rb_hash_to_a(tmp);
1811 hash = hash_alloc(klass);
1813 hash_copy(hash, tmp);
1818 tmp = rb_check_array_type(argv[0]);
1824 hash = hash_alloc(klass);
1827 VALUE v = rb_check_array_type(e);
1831 rb_raise(rb_eArgError,
"wrong element type %s at %ld (expected array)",
1832 rb_builtin_class_name(e), i);
1836 rb_raise(rb_eArgError,
"invalid number of elements (%ld for 1..2)",
1842 rb_hash_aset(hash, key, val);
1848 if (argc % 2 != 0) {
1849 rb_raise(rb_eArgError,
"odd number of arguments for Hash");
1852 hash = hash_alloc(klass);
1853 rb_hash_bulk_insert(argc, argv, hash);
1859rb_to_hash_type(
VALUE hash)
1861 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1863#define to_hash rb_to_hash_type
1868 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1885rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1887 return rb_check_hash_type(hash);
1906rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1909 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1928rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
1931 VALUE tmp = rb_hash_dup(hash);
1933 rb_hash_compare_by_id(tmp);
1935 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1947 if (RHASH_AR_TABLE_P(arg)) {
1948 ar_insert(arg, (st_data_t)key, (st_data_t)value);
1951 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
1969rb_hash_rehash(
VALUE hash)
1974 if (hash_iterating_p(hash)) {
1977 rb_hash_modify_check(hash);
1978 if (RHASH_AR_TABLE_P(hash)) {
1979 tmp = hash_alloc(0);
1980 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1982 hash_ar_free_and_clear_table(hash);
1985 else if (RHASH_ST_TABLE_P(hash)) {
1986 st_table *old_tab = RHASH_ST_TABLE(hash);
1987 tmp = hash_alloc(0);
1989 hash_st_table_init(tmp, old_tab->type, old_tab->num_entries);
1990 tbl = RHASH_ST_TABLE(tmp);
1992 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1995 RHASH_ST_TABLE_SET(hash, tbl);
1996 RHASH_ST_CLEAR(tmp);
2005 VALUE args[2] = {hash, key};
2010rb_hash_default_unredefined(
VALUE hash)
2014 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2017 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2026 if (LIKELY(rb_hash_default_unredefined(hash))) {
2028 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2029 if (UNDEF_P(key))
return Qnil;
2030 return call_default_proc(ifnone, hash, key);
2038hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2042 if (RHASH_AR_TABLE_P(hash)) {
2043 return ar_lookup(hash, key, pval);
2046 extern st_index_t rb_iseq_cdhash_hash(
VALUE);
2048 RHASH_ST_TABLE(hash)->
type->hash == rb_ident_hash ||
2049 RHASH_ST_TABLE(hash)->
type->hash == rb_iseq_cdhash_hash);
2050 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2055rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2057 return hash_stlike_lookup(hash, key, pval);
2079 if (hash_stlike_lookup(hash, key, &val)) {
2083 return rb_hash_default_value(hash, key);
2092 if (hash_stlike_lookup(hash, key, &val)) {
2103 return rb_hash_lookup2(hash, key,
Qnil);
2130rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2140 if (block_given && argc == 2) {
2141 rb_warn(
"block supersedes default value argument");
2144 if (hash_stlike_lookup(hash, key, &val)) {
2151 else if (argc == 1) {
2157 rb_key_err_raise(rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2168 return rb_hash_fetch_m(1, &key, hash);
2192rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2198 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2199 if (argc == 0)
return Qnil;
2200 return call_default_proc(ifnone, hash, argv[0]);
2221 rb_hash_modify_check(hash);
2222 SET_DEFAULT(hash, ifnone);
2239rb_hash_default_proc(
VALUE hash)
2241 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2262rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2266 rb_hash_modify_check(hash);
2268 SET_DEFAULT(hash, proc);
2271 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2274 "wrong default_proc type %s (expected Proc)",
2278 SET_PROC_DEFAULT(hash, proc);
2315 rb_hash_foreach(hash, key_i, (
VALUE)args);
2321rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2323 if (RHASH_AR_TABLE_P(hash)) {
2324 return ar_delete(hash, pkey, pval);
2327 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2339 st_data_t ktmp = (st_data_t)key, val;
2341 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2357 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2359 if (!UNDEF_P(deleted_value)) {
2360 return deleted_value;
2399 rb_hash_modify_check(hash);
2400 val = rb_hash_delete_entry(hash, key);
2402 if (!UNDEF_P(val)) {
2403 compact_after_delete(hash);
2446rb_hash_shift(
VALUE hash)
2450 rb_hash_modify_check(hash);
2451 if (RHASH_AR_TABLE_P(hash)) {
2453 if (!hash_iterating_p(hash)) {
2454 if (ar_shift(hash, &var.key, &var.val)) {
2455 return rb_assoc_new(var.key, var.val);
2459 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2460 if (!UNDEF_P(var.key)) {
2461 rb_hash_delete_entry(hash, var.key);
2462 return rb_assoc_new(var.key, var.val);
2466 if (RHASH_ST_TABLE_P(hash)) {
2468 if (!hash_iterating_p(hash)) {
2469 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2470 return rb_assoc_new(var.key, var.val);
2474 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2475 if (!UNDEF_P(var.key)) {
2476 rb_hash_delete_entry(hash, var.key);
2477 return rb_assoc_new(var.key, var.val);
2488 rb_hash_modify(hash);
2497 return rb_hash_size(hash);
2521 rb_hash_modify_check(hash);
2522 if (!RHASH_TABLE_EMPTY_P(hash)) {
2523 rb_hash_foreach(hash, delete_if_i, hash);
2524 compact_after_delete(hash);
2548rb_hash_reject_bang(
VALUE hash)
2553 rb_hash_modify(hash);
2555 if (!n)
return Qnil;
2556 rb_hash_foreach(hash, delete_if_i, hash);
2580rb_hash_reject(
VALUE hash)
2585 result = hash_dup_with_compare_by_id(hash);
2587 rb_hash_foreach(result, delete_if_i, result);
2588 compact_after_delete(result);
2605rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2608 VALUE key, value, result;
2611 return copy_compare_by_id(rb_hash_new(), hash);
2613 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2615 for (i = 0; i < argc; i++) {
2617 value = rb_hash_lookup2(hash, key,
Qundef);
2618 if (!UNDEF_P(value))
2619 rb_hash_aset(result, key, value);
2637rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2642 result = hash_dup_with_compare_by_id(hash);
2644 for (i = 0; i < argc; i++) {
2646 rb_hash_delete(result, key);
2648 compact_after_delete(result);
2667rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2672 for (i=0; i<argc; i++) {
2673 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2699rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2704 for (i=0; i<argc; i++) {
2705 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2714 rb_hash_modify(hash);
2736rb_hash_select(
VALUE hash)
2741 result = hash_dup_with_compare_by_id(hash);
2743 rb_hash_foreach(result, keep_if_i, result);
2744 compact_after_delete(result);
2767rb_hash_select_bang(
VALUE hash)
2772 rb_hash_modify_check(hash);
2774 if (!n)
return Qnil;
2775 rb_hash_foreach(hash, keep_if_i, hash);
2798rb_hash_keep_if(
VALUE hash)
2801 rb_hash_modify_check(hash);
2802 if (!RHASH_TABLE_EMPTY_P(hash)) {
2803 rb_hash_foreach(hash, keep_if_i, hash);
2824 rb_hash_modify_check(hash);
2826 if (hash_iterating_p(hash)) {
2827 rb_hash_foreach(hash, clear_i, 0);
2829 else if (RHASH_AR_TABLE_P(hash)) {
2833 st_clear(RHASH_ST_TABLE(hash));
2834 compact_after_delete(hash);
2841hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2848rb_hash_key_str(
VALUE key)
2851 return rb_fstring(key);
2859hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2862 *key = rb_hash_key_str(*key);
2864 return hash_aset(key, val, arg, existing);
2867NOINSERT_UPDATE_CALLBACK(hash_aset)
2868NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2897 bool iter_p = hash_iterating_p(hash);
2899 rb_hash_modify(hash);
2901 if (!RHASH_STRING_KEY_P(hash, key)) {
2902 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
2905 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
2923 rb_hash_modify_check(hash);
2924 if (hash == hash2)
return hash;
2925 if (hash_iterating_p(hash)) {
2928 hash2 = to_hash(hash2);
2930 COPY_DEFAULT(hash, hash2);
2932 if (RHASH_AR_TABLE_P(hash)) {
2933 hash_ar_free_and_clear_table(hash);
2936 hash_st_free_and_clear_table(hash);
2939 hash_copy(hash, hash2);
2977rb_hash_empty_p(
VALUE hash)
3014rb_hash_each_value(
VALUE hash)
3017 rb_hash_foreach(hash, each_value_i, 0);
3052rb_hash_each_key(
VALUE hash)
3055 rb_hash_foreach(hash, each_key_i, 0);
3062 rb_yield(rb_assoc_new(key, value));
3103rb_hash_each_pair(
VALUE hash)
3106 if (rb_block_pair_yield_optimizable())
3107 rb_hash_foreach(hash, each_pair_i_fast, 0);
3109 rb_hash_foreach(hash, each_pair_i, 0);
3123 VALUE trans = p->trans, result = p->result;
3124 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3125 if (UNDEF_P(new_key)) {
3131 rb_hash_aset(result, new_key, value);
3139 rb_hash_aset(result, new_key, value);
3181rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3188 transarg.trans = to_hash(argv[0]);
3194 result = rb_hash_new();
3196 if (transarg.trans) {
3197 transarg.result = result;
3198 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3201 rb_hash_foreach(hash, transform_keys_i, result);
3221rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3224 int block_given = 0;
3228 trans = to_hash(argv[0]);
3234 rb_hash_modify_check(hash);
3235 if (!RHASH_TABLE_EMPTY_P(hash)) {
3237 VALUE new_keys = hash_alloc(0);
3239 rb_hash_foreach(hash, flatten_i, pairs);
3246 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3249 else if (block_given) {
3256 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3257 rb_hash_stlike_delete(hash, &key, NULL);
3259 rb_hash_aset(hash, new_key, val);
3260 rb_hash_aset(new_keys, new_key,
Qnil);
3262 rb_ary_clear(pairs);
3263 rb_hash_clear(new_keys);
3265 compact_after_delete(hash);
3270transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3276transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3280 rb_hash_modify(hash);
3286transform_values_call(
VALUE hash)
3288 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3293transform_values(
VALUE hash)
3295 hash_iter_lev_inc(hash);
3296 rb_ensure(transform_values_call, hash, hash_foreach_ensure, hash);
3320rb_hash_transform_values(
VALUE hash)
3325 result = hash_dup_with_compare_by_id(hash);
3326 SET_DEFAULT(result,
Qnil);
3329 transform_values(result);
3330 compact_after_delete(result);
3352rb_hash_transform_values_bang(
VALUE hash)
3355 rb_hash_modify_check(hash);
3357 if (!RHASH_TABLE_EMPTY_P(hash)) {
3358 transform_values(hash);
3367 rb_ary_push(ary, rb_assoc_new(key, value));
3382rb_hash_to_a(
VALUE hash)
3387 rb_hash_foreach(hash, to_a_i, ary);
3393symbol_key_needs_quote(
VALUE str)
3395 long len = RSTRING_LEN(str);
3396 if (
len == 0 || !rb_str_symname_p(str))
return true;
3397 const char *s = RSTRING_PTR(str);
3399 if (first ==
'@' || first ==
'$' || first ==
'!')
return true;
3400 if (!at_char_boundary(s, s +
len - 1,
RSTRING_END(str), rb_enc_get(str)))
return false;
3401 switch (s[
len - 1]) {
3432 quote = symbol_key_needs_quote(str2);
3437 if (RSTRING_LEN(str) > 1) {
3441 rb_enc_copy(str, str2);
3458inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3464 rb_hash_foreach(hash, inspect_i, str);
3482rb_hash_inspect(
VALUE hash)
3496rb_hash_to_hash(
VALUE hash)
3506 pair = rb_check_array_type(arg);
3508 rb_raise(
rb_eTypeError,
"wrong element type %s (expected array)",
3509 rb_builtin_class_name(arg));
3512 rb_raise(rb_eArgError,
"element has wrong array length (expected 2, was %ld)",
3527rb_hash_to_h_block(
VALUE hash)
3530 rb_hash_foreach(hash, to_h_i, h);
3554rb_hash_to_h(
VALUE hash)
3557 return rb_hash_to_h_block(hash);
3561 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3569 rb_ary_push(ary, key);
3583rb_hash_keys(
VALUE hash)
3586 VALUE keys = rb_ary_new_capa(size);
3588 if (size == 0)
return keys;
3590 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3592 if (RHASH_AR_TABLE_P(hash)) {
3593 size = ar_keys(hash, ptr, size);
3596 st_table *table = RHASH_ST_TABLE(hash);
3597 size = st_keys(table, ptr, size);
3600 rb_gc_writebarrier_remember(keys);
3601 rb_ary_set_len(keys, size);
3604 rb_hash_foreach(hash, keys_i, keys);
3613 rb_ary_push(ary, value);
3627rb_hash_values(
VALUE hash)
3632 values = rb_ary_new_capa(size);
3633 if (size == 0)
return values;
3635 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3636 if (RHASH_AR_TABLE_P(hash)) {
3637 rb_gc_writebarrier_remember(values);
3639 size = ar_values(hash, ptr, size);
3642 else if (RHASH_ST_TABLE_P(hash)) {
3643 st_table *table = RHASH_ST_TABLE(hash);
3644 rb_gc_writebarrier_remember(values);
3646 size = st_values(table, ptr, size);
3649 rb_ary_set_len(values, size);
3653 rb_hash_foreach(hash, values_i, values);
3672 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3702 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3718 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3732recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3736 if (recur)
return Qtrue;
3738 data->result =
Qtrue;
3739 rb_hash_foreach(hash, eql_i, dt);
3741 return data->result;
3749 if (hash1 == hash2)
return Qtrue;
3755 if (
rb_eql(hash2, hash1)) {
3768 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3769 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3781 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3809 return hash_equal(hash1, hash2, FALSE);
3833 return hash_equal(hash1, hash2, TRUE);
3839 st_index_t *hval = (st_index_t *)arg;
3840 st_index_t hdata[2];
3842 hdata[0] = rb_hash(key);
3843 hdata[1] = rb_hash(val);
3844 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3863rb_hash_hash(
VALUE hash)
3869 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
3878 rb_hash_aset(hash, value, key);
3898rb_hash_invert(
VALUE hash)
3902 rb_hash_foreach(hash, rb_hash_invert_i, h);
3909 rb_hash_aset(hash, key, value);
3914rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3916 st_data_t newvalue = arg->arg;
3921 else if (RHASH_STRING_KEY_P(arg->hash, *key) && !
RB_OBJ_FROZEN(*key)) {
3922 *key = rb_hash_key_str(*key);
3928NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3933 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
3986rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
3991 rb_hash_modify(self);
3992 for (i = 0; i < argc; i++){
3993 VALUE hash = to_hash(argv[i]);
3995 rb_hash_foreach(hash, rb_hash_update_block_i, self);
3998 rb_hash_foreach(hash, rb_hash_update_i, self);
4011rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4014 VALUE newvalue = uf_arg->value;
4017 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4023NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4029 VALUE hash = arg->hash;
4032 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4039 rb_hash_modify(hash1);
4040 hash2 = to_hash(hash2);
4045 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4048 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4105rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4107 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4122assoc_lookup(
VALUE arg)
4126 if (st_lookup(p->tbl, p->key, &data))
return (
VALUE)data;
4136 args[1] = rb_assoc_new(key, val);
4160 if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) {
4162 st_table assoctable = *RHASH_ST_TABLE(hash);
4163 assoctable.type = &(
struct st_hash_type){
4164 .compare = assoc_cmp,
4165 .hash = assoctable.type->hash,
4169 .key = (st_data_t)key,
4173 value = assoc_lookup(arg);
4176 hash_iter_lev_inc(hash);
4177 value =
rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4180 if (!UNDEF_P(value))
return rb_assoc_new(key, value);
4185 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4195 args[1] = rb_assoc_new(key, val);
4221 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4232 rb_ary_cat(ary, pair, 2);
4269rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4278 if (level == 0)
return rb_hash_to_a(hash);
4281 rb_hash_foreach(hash, flatten_i, ary);
4286 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4288 else if (level < 0) {
4290 rb_funcallv(ary, id_flatten_bang, 0, 0);
4295 rb_hash_foreach(hash, flatten_i, ary);
4321rb_hash_compact(
VALUE hash)
4323 VALUE result = rb_hash_dup(hash);
4325 rb_hash_foreach(result, delete_if_nil, result);
4326 compact_after_delete(result);
4328 else if (rb_hash_compare_by_id_p(hash)) {
4329 result = rb_hash_compare_by_id(result);
4346rb_hash_compact_bang(
VALUE hash)
4349 rb_hash_modify_check(hash);
4352 rb_hash_foreach(hash, delete_if_nil, hash);
4388rb_hash_compare_by_id(
VALUE hash)
4391 st_table *identtable;
4393 if (rb_hash_compare_by_id_p(hash))
return hash;
4395 rb_hash_modify_check(hash);
4396 if (hash_iterating_p(hash)) {
4400 if (RHASH_TABLE_EMPTY_P(hash)) {
4403 ar_force_convert_table(hash, __FILE__, __LINE__);
4404 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4406 RHASH_ST_TABLE(hash)->type = &identhash;
4411 tmp = hash_alloc(0);
4412 hash_st_table_init(tmp, &identhash,
RHASH_SIZE(hash));
4413 identtable = RHASH_ST_TABLE(tmp);
4415 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4420 RHASH_ST_TABLE_SET(hash, identtable);
4421 RHASH_ST_CLEAR(tmp);
4435rb_hash_compare_by_id_p(
VALUE hash)
4437 return RBOOL(RHASH_IDENTHASH_P(hash));
4441rb_ident_hash_new(
void)
4443 VALUE hash = rb_hash_new();
4444 hash_st_table_init(hash, &identhash, 0);
4449rb_ident_hash_new_with_size(st_index_t size)
4451 VALUE hash = rb_hash_new();
4452 hash_st_table_init(hash, &identhash, size);
4457rb_init_identtable(
void)
4459 return st_init_table(&identhash);
4530rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4539 rb_warn(
"given block not used");
4543 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4550 if (rb_block_pair_yield_optimizable())
4551 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4553 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4588rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4591 self = rb_hash_aref(self, *argv);
4592 if (!--argc)
return self;
4594 return rb_obj_dig(argc, argv, self,
Qnil);
4602 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4613 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4631 other = to_hash(other);
4633 return hash_le(hash, other);
4650 other = to_hash(other);
4652 return hash_le(hash, other);
4669 other = to_hash(other);
4671 return hash_le(other, hash);
4688 other = to_hash(other);
4690 return hash_le(other, hash);
4697 return rb_hash_aref(hash, *argv);
4713rb_hash_to_proc(
VALUE hash)
4715 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4720rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4726add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4729 if (existing)
return ST_STOP;
4748 if (RHASH_AR_TABLE_P(hash)) {
4749 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4753 ar_force_convert_table(hash, __FILE__, __LINE__);
4756 tbl = RHASH_TBL_RAW(hash);
4757 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4762key_stringify(
VALUE key)
4765 rb_hash_key_str(key) : key;
4769ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4772 for (i = 0; i < argc; ) {
4773 st_data_t k = key_stringify(argv[i++]);
4774 st_data_t v = argv[i++];
4775 ar_insert(hash, k, v);
4784 HASH_ASSERT(argc % 2 == 0);
4786 st_index_t size = argc / 2;
4788 if (RHASH_AR_TABLE_P(hash) &&
4789 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4790 ar_bulk_insert(hash, argc, argv);
4793 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4798static char **origenviron;
4800#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4801#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4802static char **my_environ;
4804#define environ my_environ
4806#define getenv(n) rb_w32_ugetenv(n)
4807#elif defined(__APPLE__)
4809#define environ (*_NSGetEnviron())
4810#define GET_ENVIRON(e) (e)
4811#define FREE_ENVIRON(e)
4813extern char **environ;
4814#define GET_ENVIRON(e) (e)
4815#define FREE_ENVIRON(e)
4817#ifdef ENV_IGNORECASE
4818#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4819#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4821#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4822#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4825#define ENV_LOCK() RB_VM_LOCK_ENTER()
4826#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4828static inline rb_encoding *
4832 return rb_utf8_encoding();
4834 return rb_locale_encoding();
4839env_enc_str_new(
const char *ptr,
long len, rb_encoding *enc)
4848env_str_new(
const char *ptr,
long len)
4850 return env_enc_str_new(ptr,
len, env_encoding());
4854env_str_new2(
const char *ptr)
4856 if (!ptr)
return Qnil;
4857 return env_str_new(ptr, strlen(ptr));
4861getenv_with_lock(
const char *name)
4866 const char *val = getenv(name);
4867 ret = env_str_new2(val);
4874has_env_with_lock(
const char *name)
4884 return val ? true :
false;
4887static const char TZ_ENV[] =
"TZ";
4890get_env_cstr(
VALUE str,
const char *name)
4893 rb_encoding *enc = rb_enc_get(str);
4894 if (!rb_enc_asciicompat(enc)) {
4895 rb_raise(rb_eArgError,
"bad environment variable %s: ASCII incompatible encoding: %s",
4896 name, rb_enc_name(enc));
4898 var = RSTRING_PTR(str);
4899 if (memchr(var,
'\0', RSTRING_LEN(str))) {
4900 rb_raise(rb_eArgError,
"bad environment variable %s: contains null byte", name);
4902 return rb_str_fill_terminator(str, 1);
4905#define get_env_ptr(var, val) \
4906 (var = get_env_cstr(val, #var))
4908static inline const char *
4909env_name(
volatile VALUE *s)
4913 get_env_ptr(name, *s);
4917#define env_name(s) env_name(&(s))
4922reset_by_modified_env(
const char *nam,
const char *val)
4930 if (ENVMATCH(nam, TZ_ENV)) {
4931 ruby_reset_timezone(val);
4936env_delete(
VALUE name)
4938 const char *nam = env_name(name);
4939 reset_by_modified_env(nam, NULL);
4940 VALUE val = getenv_with_lock(nam);
4943 ruby_setenv(nam, 0);
4978 val = env_delete(name);
4998 const char *nam = env_name(name);
4999 VALUE env = getenv_with_lock(nam);
5039 if (block_given && argc == 2) {
5040 rb_warn(
"block supersedes default value argument");
5042 nam = env_name(key);
5043 env = getenv_with_lock(nam);
5046 if (block_given)
return rb_yield(key);
5048 rb_key_err_raise(rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5055#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5058in_origenv(
const char *str)
5061 for (env = origenviron; *env; ++env) {
5062 if (*env == str)
return 1;
5068envix(
const char *nam)
5072 register int i,
len = strlen(nam);
5075 env = GET_ENVIRON(environ);
5076 for (i = 0; env[i]; i++) {
5077 if (ENVNMATCH(env[i],nam,
len) && env[i][
len] ==
'=')
5080 FREE_ENVIRON(environ);
5085#if defined(_WIN32) || \
5086 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5088NORETURN(
static void invalid_envname(
const char *name));
5091invalid_envname(
const char *name)
5097check_envname(
const char *name)
5099 if (strchr(name,
'=')) {
5100 invalid_envname(name);
5107ruby_setenv(
const char *name,
const char *value)
5115 check_envname(name);
5116 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5119 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5121 wvalue = wname +
len;
5122 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5123 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5127 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5128 wvalue = wname +
len;
5136 failed = _wputenv_s(wname, wvalue);
5143 if (!value || !*value) {
5145 if (!SetEnvironmentVariableW(wname, value ? wvalue : NULL) &&
5146 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5150 invalid_envname(name);
5152#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5157 ret = setenv(name, value, 1);
5161 if (ret) rb_sys_fail_sprintf(
"setenv(%s)", name);
5174 ret = unsetenv(name);
5178 if (ret) rb_sys_fail_sprintf(
"unsetenv(%s)", name);
5186 size_t len, mem_size;
5187 char **env_ptr, *str, *mem_ptr;
5189 check_envname(name);
5192 mem_size =
len + strlen(value) + 2;
5193 mem_ptr = malloc(mem_size);
5194 if (mem_ptr == NULL)
5195 rb_sys_fail_sprintf(
"malloc(%"PRIuSIZE
")", mem_size);
5196 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5201 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5202 if (!strncmp(str, name,
len) && str[
len] ==
'=') {
5203 if (!in_origenv(str)) free(str);
5204 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5215 ret = putenv(mem_ptr);
5221 rb_sys_fail_sprintf(
"putenv(%s)", name);
5232 if (environ == origenviron) {
5237 for (max = i; environ[max]; max++) ;
5238 tmpenv =
ALLOC_N(
char*, max+2);
5239 for (j=0; j<max; j++)
5246 char **envp = origenviron;
5247 while (*envp && *envp != environ[i]) envp++;
5251 while (environ[i]) {
5252 environ[i] = environ[i+1];
5259 if (!value)
goto finish;
5264 len = strlen(name) + strlen(value) + 2;
5266 snprintf(environ[i],
len,
"%s=%s",name,value);
5275ruby_unsetenv(
const char *name)
5277 ruby_setenv(name, 0);
5325 return env_aset(nm, val);
5341 get_env_ptr(name, nm);
5342 get_env_ptr(value, val);
5344 ruby_setenv(name, value);
5345 reset_by_modified_env(name, value);
5352 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5353 VALUE ary = rb_ary_new();
5357 char **env = GET_ENVIRON(environ);
5359 char *s = strchr(*env,
'=');
5361 const char *p = *env;
5364 rb_ary_push(ary, e);
5368 FREE_ENVIRON(environ);
5391 return env_keys(FALSE);
5402 env = GET_ENVIRON(environ);
5403 for (; *env ; ++env) {
5404 if (strchr(*env,
'=')) {
5408 FREE_ENVIRON(environ);
5433env_each_key(
VALUE ehash)
5439 keys = env_keys(FALSE);
5449 VALUE ary = rb_ary_new();
5453 char **env = GET_ENVIRON(environ);
5456 char *s = strchr(*env,
'=');
5458 rb_ary_push(ary, env_str_new2(s+1));
5462 FREE_ENVIRON(environ);
5484 return env_values();
5505env_each_value(
VALUE ehash)
5511 values = env_values();
5537env_each_pair(
VALUE ehash)
5543 VALUE ary = rb_ary_new();
5547 char **env = GET_ENVIRON(environ);
5550 char *s = strchr(*env,
'=');
5552 rb_ary_push(ary, env_str_new(*env, s-*env));
5553 rb_ary_push(ary, env_str_new2(s+1));
5557 FREE_ENVIRON(environ);
5561 if (rb_block_pair_yield_optimizable()) {
5598env_reject_bang(
VALUE ehash)
5605 keys = env_keys(FALSE);
5606 RBASIC_CLEAR_CLASS(keys);
5617 if (del == 0)
return Qnil;
5642env_delete_if(
VALUE ehash)
5645 env_reject_bang(ehash);
5672 result = rb_ary_new();
5673 for (i=0; i<argc; i++) {
5674 rb_ary_push(result, rb_f_getenv(
Qnil, argv[i]));
5699env_select(
VALUE ehash)
5706 result = rb_hash_new();
5707 keys = env_keys(FALSE);
5713 rb_hash_aset(result, key, val);
5758env_select_bang(
VALUE ehash)
5765 keys = env_keys(FALSE);
5766 RBASIC_CLEAR_CLASS(keys);
5777 if (del == 0)
return Qnil;
5800env_keep_if(
VALUE ehash)
5803 env_select_bang(ehash);
5823 VALUE key, value, result;
5826 return rb_hash_new();
5828 result = rb_hash_new_with_size(argc);
5830 for (i = 0; i < argc; i++) {
5832 value = rb_f_getenv(
Qnil, key);
5834 rb_hash_aset(result, key, value);
5846 keys = env_keys(TRUE);
5849 const char *nam = RSTRING_PTR(key);
5850 ruby_setenv(nam, 0);
5869 return rb_env_clear();
5897 rb_encoding *enc = env_encoding();
5901 char **env = GET_ENVIRON(environ);
5903 const char *s = strchr(*env,
'=');
5905 if (env != environ) {
5916 FREE_ENVIRON(environ);
5937 VALUE ary = rb_ary_new();
5941 char **env = GET_ENVIRON(environ);
5943 char *s = strchr(*env,
'=');
5945 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
5946 env_str_new2(s+1)));
5950 FREE_ENVIRON(environ);
5972env_size_with_lock(
void)
5978 char **env = GET_ENVIRON(environ);
5980 FREE_ENVIRON(environ);
6000 return INT2FIX(env_size_with_lock());
6020 char **env = GET_ENVIRON(environ);
6024 FREE_ENVIRON(environ);
6028 return RBOOL(empty);
6057 const char *s = env_name(key);
6058 return RBOOL(has_env_with_lock(s));
6084 const char *s = env_name(key);
6085 VALUE e = getenv_with_lock(s);
6088 return rb_assoc_new(key, e);
6117 char **env = GET_ENVIRON(environ);
6119 char *s = strchr(*env,
'=');
6121 long len = strlen(s);
6122 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6129 FREE_ENVIRON(environ);
6160 char **env = GET_ENVIRON(environ);
6163 const char *p = *env;
6164 char *s = strchr(p,
'=');
6166 long len = strlen(s);
6167 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6168 result = rb_assoc_new(
rb_str_new(p, s-p-1), obj);
6174 FREE_ENVIRON(environ);
6205 char **env = GET_ENVIRON(environ);
6207 char *s = strchr(*env,
'=');
6209 long len = strlen(s);
6210 if (RSTRING_LEN(value) ==
len && strncmp(s, RSTRING_PTR(value),
len) == 0) {
6211 str = env_str_new(*env, s-*env-1);
6217 FREE_ENVIRON(environ);
6227 VALUE hash = rb_hash_new();
6231 char **env = GET_ENVIRON(environ);
6233 char *s = strchr(*env,
'=');
6235 rb_hash_aset(hash, env_str_new(*env, s-*env),
6240 FREE_ENVIRON(environ);
6256 return env_to_hash();
6271 return env_to_hash();
6295 VALUE hash = env_to_hash();
6297 hash = rb_hash_to_h_block(hash);
6315 VALUE key, hash = env_to_hash();
6317 for (i = 0; i < argc; i++) {
6319 rb_hash_delete(hash, key);
6343 return rb_hash_delete_if(env_to_hash());
6346NORETURN(
static VALUE env_freeze(
VALUE self));
6355env_freeze(
VALUE self)
6384 char **env = GET_ENVIRON(environ);
6386 const char *p = *env;
6387 char *s = strchr(p,
'=');
6389 key = env_str_new(p, s-p);
6390 VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
6391 result = rb_assoc_new(key, val);
6394 FREE_ENVIRON(environ);
6423 return rb_hash_invert(env_to_hash());
6430 const char *keyptr, *eptr;
6438 if (elen != keylen)
continue;
6439 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6440 rb_ary_delete_at(keys, i);
6451 keylist_delete(keys, key);
6479 keys = env_keys(TRUE);
6480 if (env == hash)
return env;
6481 hash = to_hash(hash);
6482 rb_hash_foreach(hash, env_replace_i, keys);
6502 if (!
NIL_P(oldval)) {
6549 env_update_block_i : env_update_i;
6550 for (
int i = 0; i < argc; ++i) {
6551 VALUE hash = argv[i];
6552 if (env == hash)
continue;
6553 hash = to_hash(hash);
6554 rb_hash_foreach(hash, func, 0);
6574 rb_get_freeze_opt(1, &opt);
6578 rb_raise(
rb_eTypeError,
"Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
6592 rb_raise(
rb_eTypeError,
"Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
6603 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7088 id_hash_iter_lev = rb_make_internal_id();
7186 rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_new());
7187 rb_vm_register_global_object(rb_cHash_empty_frozen);
7353 origenviron = environ;
7422 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
7425#include "hash.rbinc"
#define RUBY_ASSERT(...)
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 implementation 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 TYPE(_)
Old name of rb_type.
#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 CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#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 POSFIXABLE
Old name of RB_POSFIXABLE.
#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_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 unless $VERBOSE is nil.
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.
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 RGENGC_WB_PROTECTED_HASH
This is a compile-time flag to enable/disable write barrier for struct RHash.
#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().
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_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.
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_inspect(VALUE str)
Generates a "readable" version of the receiver.
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().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#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_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
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.
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.