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/gc.h"
36#include "internal/hash.h"
37#include "internal/object.h"
38#include "internal/proc.h"
39#include "internal/ractor.h"
40#include "internal/st.h"
41#include "internal/symbol.h"
42#include "internal/thread.h"
43#include "internal/time.h"
44#include "internal/vm.h"
48#include "ruby_assert.h"
81#define SET_DEFAULT(hash, ifnone) ( \
82 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
83 RHASH_SET_IFNONE(hash, ifnone))
85#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
87#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
89#define RHASH_TYPE(hash) (FL_TEST_RAW(hash, RHASH_COMPARE_BY_IDENTITY) ? &identhash : &objhash)
92copy_default(
struct RHash *hash,
const struct RHash *hash2)
94 hash->basic.
flags &= ~RHASH_PROC_DEFAULT;
95 hash->basic.
flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
111static int ar_compact_table(
VALUE hash);
117 if (!
OBJ_FROZEN(hash) && RHASH_AR_TABLE_P(hash)) {
118 ar_compact_table(hash);
124VALUE rb_cHash_empty_frozen;
127static ID id_hash, id_flatten_bang;
128static ID id_hash_iter_lev;
130#define id_default idDefault
142 if (a == b)
return 0;
147 if (UNDEF_P(a) || UNDEF_P(b))
return -1;
156hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
158 if (recurse)
return INT2FIX(0);
162static long rb_objid_hash(st_index_t index);
165dbl_to_index(
double d)
167 union {
double d; st_index_t i;} u;
173rb_dbl_long_hash(
double d)
176 if (d == 0.0) d = 0.0;
177#if SIZEOF_INT == SIZEOF_VOIDP
180 return rb_objid_hash(dbl_to_index(d));
185any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
197 hnum = RSHIFT(RSYMBOL(a)->hashval, 1);
204 hnum = rb_objid_hash((st_index_t)a);
210 hval = rb_big_hash(a);
214 hnum = rb_dbl_long_hash(rb_float_value(a));
217 hnum = other_func(a);
230obj_any_hash(
VALUE obj)
236 if (cme && METHOD_ENTRY_BASIC(cme)) {
238 if (cme->def->type == VM_METHOD_TYPE_CFUNC && cme->def->body.cfunc.func == (rb_cfunc_t)rb_obj_hash) {
239 hval = rb_obj_hash(obj);
242 hval = rb_vm_call0(GET_EC(), obj, id_hash, 0, 0, cme, 0);
248 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
255 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
273 return any_hash(a, obj_any_hash);
279 return LONG2FIX(any_hash(obj, obj_any_hash));
288static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
289static const uint32_t prime2 = 0x830fcab9;
292static inline uint64_t
293mult_and_mix(uint64_t m1, uint64_t m2)
295#if defined HAVE_UINT128_T
296 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
297 return (uint64_t) (r >> 64) ^ (uint64_t) r;
299 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
300 uint64_t lm1 = m1, lm2 = m2;
301 uint64_t v64_128 = hm1 * hm2;
302 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
303 uint64_t v1_32 = lm1 * lm2;
305 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
309static inline uint64_t
310key64_hash(uint64_t key, uint32_t seed)
312 return mult_and_mix(key + seed, prime1);
316#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
319rb_objid_hash(st_index_t index)
321 return (
long)st_index_hash(index);
327 VALUE object_id = rb_obj_id(obj);
329 object_id = rb_big_hash(object_id);
331#if SIZEOF_LONG == SIZEOF_VOIDP
332 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
333#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
334 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
369rb_obj_hash(
VALUE obj)
371 long hnum = any_hash(obj, objid_hash);
380#define rb_ident_cmp st_numcmp
383rb_ident_hash(st_data_t n)
392 n ^= dbl_to_index(rb_float_value(n));
396 return (st_index_t)st_index_hash((st_index_t)n);
399#define identhash rb_hashtype_ident
405#define RHASH_IDENTHASH_P(hash) FL_TEST_RAW(hash, RHASH_COMPARE_BY_IDENTITY)
406#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
408typedef st_index_t st_hash_t;
418static inline unsigned int
419RHASH_AR_TABLE_MAX_BOUND(
VALUE h)
421 size_t usable_space = rb_obj_shape_slot_size(h) -
sizeof(
struct RHash) - offsetof(
ar_table, pairs);
425 return (
unsigned)usable_space;
427 return usable_space <= RHASH_AR_TABLE_MAX_SIZE ? (unsigned)usable_space : RHASH_AR_TABLE_MAX_SIZE;
431#define RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE (RHASH_AR_TABLE_MAX_SIZE + 1)
432#define RHASH_AR_TABLE_MISS RHASH_AR_TABLE_MAX_SIZE
434#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
435#define RHASH_AR_CLEARED_HINT 0x00
436#define RHASH_AR_SUBSTITUTION_HINT 0x01
438static inline st_hash_t
439ar_do_hash(
VALUE hash, st_data_t key)
441 if (RHASH_IDENTHASH_P(hash)) {
442 return (st_hash_t)rb_ident_hash(key);
444 return (st_hash_t)rb_any_hash(key);
447static inline ar_hint_t
448ar_do_hash_hint(st_hash_t hash_value)
450 ar_hint_t hint = (ar_hint_t)hash_value;
451 return hint == RHASH_AR_CLEARED_HINT ? RHASH_AR_SUBSTITUTION_HINT : hint;
454static inline ar_hint_t
455ar_hint(
VALUE hash,
unsigned int index)
457 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
461ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
463 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
467ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
469 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
473ar_clear_entry(
VALUE hash,
unsigned int index)
477 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
481ar_cleared_entry(
VALUE hash,
unsigned int index)
483 return ar_hint(hash, index) == RHASH_AR_CLEARED_HINT;
487ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
492 ar_hint_set(hash, index, hash_value);
495#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
496 RHASH_AR_TABLE_SIZE_RAW(h))
498#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
501#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
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)) {
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);
530 HASH_ASSERT(!!RHASH_IDENTHASH_P(hash) == (RHASH_ST_TABLE(hash)->
type == &identhash));
537#define hash_verify(h) ((void)0)
541RHASH_TABLE_EMPTY_P(
VALUE hash)
546#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
547#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
550hash_st_table_init(
VALUE hash, st_index_t size)
553 st_init_existing_table_with_size(RHASH_ST_TABLE(hash), RHASH_TYPE(hash), size);
554 RHASH_SET_ST_FLAG(hash);
560 HASH_ASSERT(st != NULL);
561 RHASH_SET_ST_FLAG(hash);
563 *RHASH_ST_TABLE(hash) = *st;
567RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
569 HASH_ASSERT(RHASH_AR_TABLE_P(h));
570 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND(h));
572 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
573 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
577RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
579 HASH_ASSERT(RHASH_AR_TABLE_P(h));
580 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND(h));
582 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
583 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
587HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
589 HASH_ASSERT(RHASH_AR_TABLE_P(h));
591 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
596#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
599RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
601 HASH_ASSERT(RHASH_AR_TABLE_P(h));
602 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
605 RHASH_AR_TABLE_SIZE_SET(h, new_size);
608 RHASH_AR_TABLE_SIZE_SET(h, 0);
609 RHASH_AR_TABLE_BOUND_SET(h, 0);
615RHASH_AR_TABLE_CLEAR(
VALUE h)
617 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
618 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
620 memset(RHASH_AR_TABLE(h), 0, rb_obj_shape_slot_size(h) -
sizeof(
struct RHash));
628 if (RHASH_IDENTHASH_P(hash)) {
631 return rb_any_cmp(x, y) == 0;
636#define AR_HINT_BASE_MASK 0x101010101010101
637#define AR_HINT_NORMALIZE_MASK 0x7F7F7F7F7F7F7F7F
638#ifdef WORDS_BIGENDIAN
639#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (nlz_int64(x) / CHAR_BIT)
641#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (ntz_int64(x) / CHAR_BIT)
644#define AR_HINT_BASE_MASK 0x1010101
645#define AR_HINT_NORMALIZE_MASK 0x7F7F7F7F
646#ifdef WORDS_BIGENDIAN
647#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (nlz_int32(x) / CHAR_BIT)
649#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (ntz_int32(x) / CHAR_BIT)
653static inline unsigned int
654ar_hint_first_match(ar_hint_t needle,
VALUE haystack)
658 VALUE search_mask = (
VALUE)AR_HINT_BASE_MASK * needle;
659 VALUE matches = haystack ^ search_mask;
662 matches = ~((((matches & AR_HINT_NORMALIZE_MASK) + AR_HINT_NORMALIZE_MASK) | matches) | AR_HINT_NORMALIZE_MASK);
663 unsigned index = AR_HINT_FIND_FIRST_ZERO_BYTE(matches);
671ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
673 unsigned first_match = ar_hint_first_match(hint, RHASH_AR_TABLE(hash)->ar_hint.word);
675 if (LIKELY(first_match >= RHASH_AR_TABLE_BOUND(hash))) {
676 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
677 return RHASH_AR_TABLE_MISS;
680 RUBY_ASSERT(RHASH_AR_TABLE(hash)->ar_hint.ary[first_match] == hint);
681 int eq = ar_equal(hash, key, RHASH_AR_TABLE_REF(hash, first_match)->key);
682 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
683 return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE;
686 RB_DEBUG_COUNTER_INC(artable_hint_hit);
693 for (
unsigned i = first_match + 1; i < RHASH_AR_TABLE_BOUND(hash); i++) {
694 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
695 if (UNLIKELY(hints[i] == hint)) {
696 eq = ar_equal(hash, key, RHASH_AR_TABLE_REF(hash, i)->key);
697 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
698 return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE;
701 RB_DEBUG_COUNTER_INC(artable_hint_hit);
705 RB_DEBUG_COUNTER_INC(artable_hint_miss);
711 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
712 return RHASH_AR_TABLE_MISS;
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);
723hash_ar_free_and_clear_table(
VALUE hash)
725 RHASH_AR_TABLE_CLEAR(hash);
727 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
728 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
731void rb_st_add_direct_with_hash(
st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
733enum ar_each_key_type {
740ar_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)
742 for (
int i = 0; i < max; i++) {
746 case ar_each_key_copy:
747 dst_keys[i] = pair->key;
749 case ar_each_key_cmp:
750 if (dst_keys[i] != pair->key)
return 1;
752 case ar_each_key_insert:
753 if (UNDEF_P(pair->key))
continue;
754 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
763ar_force_convert_table(
VALUE hash,
const char *file,
int line)
765 if (RHASH_ST_TABLE_P(hash)) {
766 return RHASH_ST_TABLE(hash);
769 ar_table *ar = RHASH_AR_TABLE(hash);
770 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
771 unsigned int bound, size;
778 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
779 bound = RHASH_AR_TABLE_BOUND(hash);
780 size = RHASH_AR_TABLE_SIZE(hash);
781 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
783 for (
unsigned int i = 0; i < bound; i++) {
785 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(hash, keys[i]);
789 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
790 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
791 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
799 st_init_existing_table_with_size(new_tab,
type, size);
800 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
801 hash_ar_free_and_clear_table(hash);
802 rb_hash_st_table_set(hash, new_tab);
803 return RHASH_ST_TABLE(hash);
813 const unsigned src_bound = RHASH_AR_TABLE_BOUND(src);
814 const unsigned src_size = RHASH_AR_TABLE_SIZE(src);
817 for (
unsigned i = 0; i < src_bound; i++) {
818 if (!ar_cleared_entry(src, i)) {
819 dst_pairs[j] = src_pairs[i];
820 ar_hint_set_hint(dst, j, (st_hash_t)ar_hint(src, i));
824 RHASH_AR_TABLE_BOUND_SET(dst, src_size);
825 RHASH_AR_TABLE_SIZE_SET(dst, src_size);
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_BOUND(hash)) {
877 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND(hash))) {
878 bin = ar_compact_table(hash);
880 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
882 ar_set_entry(hash, bin, key, val, hash_value);
883 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
884 RHASH_AR_TABLE_SIZE_INC(hash);
890ensure_ar_table(
VALUE hash)
892 if (!RHASH_AR_TABLE_P(hash)) {
893 rb_raise(
rb_eRuntimeError,
"hash representation was changed during iteration");
898ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
900 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
901 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
903 for (i = 0; i < bound; i++) {
904 if (ar_cleared_entry(hash, i))
continue;
907 st_data_t key = (st_data_t)pair->key;
908 st_data_t val = (st_data_t)pair->val;
909 enum st_retval retval = (*func)(key, val, arg, 0);
910 ensure_ar_table(hash);
921 (*replace)(&key, &val, arg, TRUE);
924 HASH_ASSERT(pair == RHASH_AR_TABLE_REF(hash, i));
926 pair->key = (
VALUE)key;
927 pair->val = (
VALUE)val;
931 ar_clear_entry(hash, i);
932 RHASH_AR_TABLE_SIZE_DEC(hash);
941ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
943 return ar_general_foreach(hash, func, replace, arg);
947 st_foreach_callback_func *func;
952apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
954 const struct functor *f = (
void *)d;
955 return f->func(k, v, f->arg);
959ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
961 const struct functor f = { func, arg };
962 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
966ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
969 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
970 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
971 enum st_retval retval;
976 for (i = 0; i < bound; i++) {
977 if (ar_cleared_entry(hash, i))
continue;
979 pair = RHASH_AR_TABLE_REF(hash, i);
981 hint = ar_hint(hash, i);
983 retval = (*func)(key, pair->val, arg, 0);
984 ensure_ar_table(hash);
989 pair = RHASH_AR_TABLE_REF(hash, i);
990 if (pair->key == never)
break;
991 ret = ar_find_entry_hint(hash, hint, key);
992 if (UNLIKELY(ret == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
993 ensure_ar_table(hash);
995 if (ret == RHASH_AR_TABLE_MISS) {
996 (*func)(0, 0, arg, 1);
1006 if (!ar_cleared_entry(hash, i)) {
1007 ar_clear_entry(hash, i);
1008 RHASH_AR_TABLE_SIZE_DEC(hash);
1019ar_update(
VALUE hash, st_data_t key,
1020 st_update_callback_func *func, st_data_t arg)
1022 int retval, existing;
1023 unsigned bin = RHASH_AR_TABLE_MISS;
1024 st_data_t value = 0, old_key;
1025 st_hash_t hash_value = ar_do_hash(hash, key);
1027 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1032 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1033 bin = ar_find_entry(hash, hash_value, key);
1034 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1037 existing = (bin != RHASH_AR_TABLE_MISS);
1049 retval = (*func)(&key, &value, arg, existing);
1051 ensure_ar_table(hash);
1056 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1062 if (old_key != key) {
1070 ar_clear_entry(hash, bin);
1071 RHASH_AR_TABLE_SIZE_DEC(hash);
1079ar_insert_direct(
VALUE hash, st_data_t key, st_data_t value, st_hash_t hash_value)
1081 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1082 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1087 bin = ar_find_entry(hash, hash_value, key);
1088 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1092 if (bin == RHASH_AR_TABLE_MISS) {
1093 if (RHASH_AR_TABLE_SIZE(hash) == RHASH_AR_TABLE_MAX_BOUND(hash)) {
1097 bin = ar_compact_table(hash);
1098 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
1100 ar_set_entry(hash, bin, key, value, hash_value);
1101 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1102 RHASH_AR_TABLE_SIZE_INC(hash);
1106 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1112ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1114 st_hash_t hash_value = ar_do_hash(hash, key);
1115 return ar_insert_direct(hash, key, value, hash_value);
1119ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1121 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1125 st_hash_t hash_value = ar_do_hash(hash, key);
1126 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1128 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1130 unsigned bin = ar_find_entry(hash, hash_value, key);
1132 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1133 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1136 if (bin == RHASH_AR_TABLE_MISS) {
1140 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
1141 if (value != NULL) {
1142 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1149ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1152 st_hash_t hash_value = ar_do_hash(hash, *key);
1154 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1156 return st_delete(RHASH_ST_TABLE(hash), key, value);
1159 bin = ar_find_entry(hash, hash_value, *key);
1160 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1161 return st_delete(RHASH_ST_TABLE(hash), key, value);
1164 if (bin == RHASH_AR_TABLE_MISS) {
1165 if (value != 0) *value = 0;
1173 ar_clear_entry(hash, bin);
1174 RHASH_AR_TABLE_SIZE_DEC(hash);
1180ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1182 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1183 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1185 for (i = 0; i < bound; i++) {
1186 if (!ar_cleared_entry(hash, i)) {
1188 if (value != 0) *value = pair->val;
1190 ar_clear_entry(hash, i);
1191 RHASH_AR_TABLE_SIZE_DEC(hash);
1196 if (value != NULL) *value = 0;
1201ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1203 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1204 st_data_t *keys_start = keys, *keys_end = keys + size;
1206 for (i = 0; i < bound; i++) {
1207 if (keys == keys_end) {
1211 if (!ar_cleared_entry(hash, i)) {
1212 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1217 return keys - keys_start;
1221ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1223 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1224 st_data_t *values_start = values, *values_end = values + size;
1226 for (i = 0; i < bound; i++) {
1227 if (values == values_end) {
1231 if (!ar_cleared_entry(hash, i)) {
1232 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1237 return values - values_start;
1244 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1246 unsigned int bound = RHASH_AR_TABLE_BOUND(hash2);
1247 unsigned int size = RHASH_AR_TABLE_SIZE(hash2);
1248 if (UNLIKELY(bound != size)) {
1249 ar_compact_into(hash1, hash2);
1253 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1254 new_tab->ar_hint.word = old_tab->ar_hint.word;
1256 RHASH_AR_TABLE_BOUND_SET(hash1, bound);
1257 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1258 rb_gc_writebarrier_remember(hash1);
1266 if (RHASH_AR_TABLE(hash) != NULL) {
1267 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1268 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1271 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1272 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1277hash_st_free(
VALUE hash)
1279 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1281 rb_st_free_embedded_table(RHASH_ST_TABLE(hash));
1285hash_st_free_and_clear_table(
VALUE hash)
1288 RHASH_ST_CLEAR(hash);
1292rb_hash_free(
VALUE hash)
1294 if (RHASH_ST_TABLE_P(hash)) {
1299typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1303 st_foreach_func *func;
1308foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1313 if (error)
return ST_STOP;
1314 status = (*arg->func)(key, value, arg->arg);
1315 if (status == ST_CONTINUE) {
1327 arg.func = (st_foreach_func *)func;
1329 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1338 rb_foreach_func *func;
1343hash_iter_status_check(
int status)
1358hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1362 if (error)
return ST_STOP;
1364 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1366 return hash_iter_status_check(status);
1370hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1374 if (error)
return ST_STOP;
1376 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1378 return hash_iter_status_check(status);
1382iter_lev_in_ivar(
VALUE hash)
1387 HASH_ASSERT(lev >= 0);
1388 return (
unsigned long)lev;
1394iter_lev_in_ivar_set(
VALUE hash,
unsigned long lev)
1396 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1398 rb_ivar_set_internal(hash, id_hash_iter_lev,
LONG2FIX((
long)lev));
1401static inline unsigned long
1402iter_lev_in_flags(
VALUE hash)
1404 return (
unsigned long)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1408iter_lev_in_flags_set(
VALUE hash,
unsigned long lev)
1410 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1411 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1415hash_iterating_p(
VALUE hash)
1417 return iter_lev_in_flags(hash) > 0;
1421hash_iter_lev_inc(
VALUE hash)
1423 unsigned long lev = iter_lev_in_flags(hash);
1424 if (lev == RHASH_LEV_MAX) {
1425 lev = iter_lev_in_ivar(hash) + 1;
1432 iter_lev_in_flags_set(hash, lev);
1433 if (lev < RHASH_LEV_MAX)
return;
1435 iter_lev_in_ivar_set(hash, lev);
1439hash_iter_lev_dec(
VALUE hash)
1441 unsigned long lev = iter_lev_in_flags(hash);
1442 if (lev == RHASH_LEV_MAX) {
1443 lev = iter_lev_in_ivar(hash);
1444 if (lev > RHASH_LEV_MAX) {
1445 iter_lev_in_ivar_set(hash, lev-1);
1448 rb_attr_delete(hash, id_hash_iter_lev);
1450 else if (lev == 0) {
1453 iter_lev_in_flags_set(hash, lev - 1);
1457hash_foreach_ensure(
VALUE hash)
1459 hash_iter_lev_dec(hash);
1465rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1467 if (RHASH_AR_TABLE_P(hash)) {
1468 return ar_foreach(hash, func, arg);
1471 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1477rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1479 if (RHASH_AR_TABLE_P(hash)) {
1480 return ar_foreach_with_replace(hash, func, replace, arg);
1483 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1488hash_foreach_call(
VALUE arg)
1492 if (RHASH_AR_TABLE_P(hash)) {
1493 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1494 (st_data_t)arg, (st_data_t)
Qundef);
1496 else if (RHASH_ST_TABLE_P(hash)) {
1497 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1498 (st_data_t)arg, (st_data_t)
Qundef);
1501 rb_raise(
rb_eRuntimeError,
"ret: %d, hash modified during iteration", ret);
1507rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1511 if (RHASH_TABLE_EMPTY_P(hash))
1514 arg.func = (rb_foreach_func *)func;
1517 hash_foreach_call((
VALUE)&arg);
1520 hash_iter_lev_inc(hash);
1521 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1526void rb_st_compact_table(
st_table *tab);
1529compact_after_delete(
VALUE hash)
1531 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1532 rb_st_compact_table(RHASH_ST_TABLE(hash));
1537hash_slot_size(
size_t capa,
bool frozen)
1539 if (
capa <= RHASH_AR_TABLE_MAX_SIZE) {
1540 const size_t ar_size = RHASH_AR_SLOT_SIZE(
capa);
1543 if (frozen || ar_size >= RHASH_ST_SLOT_SIZE) {
1548 return RHASH_ST_SLOT_SIZE;
1552hash_alloc(
VALUE klass,
VALUE flags,
VALUE ifnone,
size_t size,
bool frozen)
1554 VALUE hash = rb_newobj_of(klass,
T_HASH | flags, hash_slot_size(size, frozen));
1555 rb_hash_set_ifnone(hash, ifnone);
1557 RHASH_AR_TABLE(hash)->ar_hint.word = 0;
1560 if (hash_slot_size(size, frozen) >=
sizeof(
struct RHash) +
sizeof(
st_table)) {
1561 RHASH_ST_TABLE(hash)->num_entries = 0;
1562 RHASH_ST_TABLE(hash)->entries = NULL;
1570hash_init_capa(
VALUE hash,
size_t size)
1572 if (size > RHASH_AR_TABLE_MAX_SIZE) {
1573 hash_st_table_init(hash, size);
1576 RUBY_ASSERT(RHASH_AR_TABLE_MAX_BOUND(hash) >= size);
1582hash_hidden_new(
size_t size)
1584 return hash_init_capa(hash_alloc(0, 0,
Qnil, size,
false), size);
1588hash_alloc_capa(
VALUE klass,
size_t size)
1590 return hash_alloc(klass, 0,
Qnil, size,
false);
1596 return hash_alloc_capa(klass,
RHASH_SIZE(src));
1600empty_hash_alloc(
VALUE klass)
1602 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1604 return hash_alloc_capa(klass, 0);
1610 if (rb_hash_compare_by_id_p(basis)) {
1611 return rb_hash_compare_by_id(hash);
1619 return hash_init_capa(hash_alloc_capa(klass,
capa),
capa);
1626 rb_raise(rb_eArgError,
"negative hash size (or size too big)");
1634 return rb_hash_new_capa(0);
1638rb_hash_alloc_fixed_size(
VALUE klass, st_index_t size)
1640 return hash_init_capa(hash_alloc(klass, 0,
Qnil, size,
true), size);
1644ar_add_direct_i(st_data_t key, st_data_t value, st_data_t hash_value, st_data_t arg)
1647 ar_insert_direct(ret, key, value, hash_value);
1655 if (RHASH_ST_TABLE_P(ret)) {
1656 RUBY_ASSERT(RHASH_ST_TABLE(ret)->entries == NULL);
1657 RHASH_UNSET_ST_FLAG(ret);
1660 bool compare_by_id = RHASH_IDENTHASH_P(hash);
1662 if (compare_by_id) {
1663 rb_gc_register_pinning_obj(ret);
1670 if (RHASH_AR_TABLE_MAX_BOUND(ret) <
RHASH_SIZE(hash)) {
1671 RHASH_SET_ST_FLAG(ret);
1674 if (RHASH_AR_TABLE_P(hash)) {
1675 if (RHASH_AR_TABLE_P(ret)) {
1679 st_table *tab = RHASH_ST_TABLE(ret);
1681 st_init_existing_table_with_size(RHASH_ST_TABLE(ret),
1682 compare_by_id ? &identhash : &objhash,
1685 int bound = RHASH_AR_TABLE_BOUND(hash);
1686 for (
int i = 0; i < bound; i++) {
1687 if (ar_cleared_entry(hash, i))
continue;
1690 st_add_direct(tab, pair->key, pair->val);
1697 if (RHASH_AR_TABLE_P(ret)) {
1698 rb_st_foreach_with_hash(RHASH_ST_TABLE(hash), ar_add_direct_i, (st_data_t)ret);
1701 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1702 rb_gc_writebarrier_remember(ret);
1709hash_dup_with_compare_by_id(
VALUE hash)
1712 if (RHASH_ST_TABLE_P(hash)) {
1713 RHASH_SET_ST_FLAG(dup);
1716 return hash_copy(dup, hash);
1723 return hash_copy(dup, hash);
1730 if (
capa > RHASH_AR_TABLE_MAX_SIZE) {
1731 RHASH_SET_ST_FLAG(ret);
1732 RHASH_ST_CLEAR(ret);
1737 hash_copy(ret, hash);
1742rb_hash_dup_capa(
VALUE hash,
size_t capa)
1755 return rb_hash_dup_capa(hash,
RHASH_SIZE(hash));
1759rb_hash_resurrect(
VALUE hash)
1766rb_zjit_hash_new_size(
VALUE *flags_out,
size_t size)
1770 return hash_slot_size(size,
false);
1774rb_zjit_hash_dup_can_fastpath(
VALUE hash,
size_t *alloc_size_out,
VALUE *flags_out,
VALUE *ifnone_out,
long *bound_out)
1776 if (!RHASH_AR_TABLE_P(hash))
return false;
1777 if (rb_hash_compare_by_id_p(hash))
return false;
1779 const unsigned int bound = RHASH_AR_TABLE_BOUND(hash);
1781 *alloc_size_out = hash_slot_size(bound,
false);
1783 | ((
VALUE)RHASH_AR_TABLE_SIZE(hash) << RHASH_AR_TABLE_SIZE_SHIFT)
1784 | ((
VALUE)bound << RHASH_AR_TABLE_BOUND_SHIFT);
1786 *bound_out = (long)bound;
1792rb_hash_modify_check(
VALUE hash)
1794 rb_check_frozen(hash);
1798rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1800 return ar_force_convert_table(hash, file, line);
1804rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1807 return rb_hash_tbl_raw(hash, file, line);
1811rb_hash_modify(
VALUE hash)
1813 rb_hash_modify_check(hash);
1816NORETURN(
static void no_new_key(
void));
1820 rb_raise(
rb_eRuntimeError,
"can't add a new key into hash during iteration");
1828#define NOINSERT_UPDATE_CALLBACK(func) \
1830func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1832 if (!existing) no_new_key(); \
1833 return func(key, val, (struct update_arg *)arg, existing); \
1837func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1839 return func(key, val, (struct update_arg *)arg, existing); \
1844 st_update_callback_func *func;
1850typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1853rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1855 if (RHASH_AR_TABLE_P(hash)) {
1856 int result = ar_update(hash, key, func, arg);
1858 ar_force_convert_table(hash, __FILE__, __LINE__);
1865 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1869tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1872 st_data_t old_key = *key;
1873 st_data_t old_value = *val;
1874 VALUE hash = p->hash;
1875 int ret = (p->func)(key, val, arg, existing);
1880 if (!existing || *key != old_key || *val != old_value) {
1881 rb_hash_modify(hash);
1888 rb_hash_modify(hash);
1896tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1899 .arg = optional_arg,
1906 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1915#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1917#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1918 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1921#define RHASH_UPDATE(hash, key, func, arg) \
1922 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1930 if (n != 2 && (n >= 0 || n < -3)) {
1931 if (n < 0) n = -n-1;
1932 rb_raise(
rb_eTypeError,
"default_proc takes two arguments (2 for %d)", n);
1943 rb_hash_modify(hash);
1945 if (capa_value !=
INT2FIX(0)) {
1947 if (
capa > 0 && RHASH_AR_TABLE_P(hash) &&
RHASH_SIZE(hash) == 0 &&
1948 (
unsigned long)
capa > RHASH_AR_TABLE_MAX_BOUND(hash)) {
1949 hash_st_table_init(hash,
capa);
1953 if (!
NIL_P(block)) {
1954 if (ifnone_unset !=
Qtrue) {
1958 SET_PROC_DEFAULT(hash, block);
1970static VALUE hash_new_with_bulk_insert(
VALUE klass,
long argc,
const VALUE *argv);
2010rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
2015 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
2018 return hash_new_capa(klass, 0);
2021 if (rb_hash_compare_by_id_p(tmp)) {
2025 tmp = rb_hash_to_a(tmp);
2028 hash = hash_alloc_capa(klass,
RHASH_SIZE(tmp));
2029 return hash_copy(hash, tmp);
2038 return hash_new_capa(klass, 0);
2049 rb_raise(rb_eArgError,
"wrong element type %s at %ld (expected array)",
2050 rb_builtin_class_name(e), i);
2056 hash = hash_new_capa(klass,
RARRAY_LEN(tmp));
2059 hash = hash_new_capa(klass,
RARRAY_LEN(tmp) / 1);
2066 rb_raise(rb_eArgError,
"invalid number of elements (%ld for 1..2)",
2073 rb_hash_aset(hash, key, val);
2079 if (argc % 2 != 0) {
2080 rb_raise(rb_eArgError,
"odd number of arguments for Hash");
2083 hash = hash_new_with_bulk_insert(klass, argc, argv);
2089rb_to_hash_type(
VALUE hash)
2091 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
2093#define to_hash rb_to_hash_type
2098 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
2114rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
2116 return rb_check_hash_type(hash);
2139rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
2142 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
2165rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
2168 VALUE tmp = rb_hash_dup(hash);
2170 rb_hash_compare_by_id(tmp);
2172 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2184 if (RHASH_AR_TABLE_P(arg)) {
2185 ar_insert(arg, (st_data_t)key, (st_data_t)value);
2188 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
2210rb_hash_rehash(
VALUE hash)
2215 if (hash_iterating_p(hash)) {
2218 rb_hash_modify_check(hash);
2219 if (RHASH_AR_TABLE_P(hash)) {
2221 if (RHASH_IDENTHASH_P(hash)) {
2224 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2226 hash_ar_free_and_clear_table(hash);
2229 else if (RHASH_ST_TABLE_P(hash)) {
2230 st_table *old_tab = RHASH_ST_TABLE(hash);
2231 tmp = hash_alloc_capa(0, 0);
2232 if (old_tab->type == &identhash) {
2236 hash_st_table_init(tmp, old_tab->num_entries);
2237 RHASH_ST_TABLE(tmp)->type = old_tab->type;
2238 tbl = RHASH_ST_TABLE(tmp);
2240 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2243 rb_hash_st_table_set(hash, tbl);
2244 RHASH_ST_CLEAR(tmp);
2253 VALUE args[2] = {hash, key};
2258rb_hash_default_unredefined(
VALUE hash)
2262 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2274 if (LIKELY(rb_hash_default_unredefined(hash))) {
2276 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2277 if (UNDEF_P(key))
return Qnil;
2278 return call_default_proc(ifnone, hash, key);
2286hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2290 if (RHASH_AR_TABLE_P(hash)) {
2291 return ar_lookup(hash, key, pval);
2294 extern st_index_t rb_iseq_cdhash_hash(
VALUE);
2296 RHASH_ST_TABLE(hash)->
type->hash == rb_ident_hash ||
2297 RHASH_ST_TABLE(hash)->
type->hash == rb_iseq_cdhash_hash);
2298 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2303rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2305 return hash_stlike_lookup(hash, key, pval);
2330 if (hash_stlike_lookup(hash, key, &val)) {
2334 return rb_hash_default_value(hash, key);
2343 if (hash_stlike_lookup(hash, key, &val)) {
2354 return rb_hash_lookup2(hash, key,
Qnil);
2384rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2394 if (block_given && argc == 2) {
2395 rb_warn(
"block supersedes default value argument");
2398 if (hash_stlike_lookup(hash, key, &val)) {
2405 else if (argc == 1) {
2411 rb_key_err_raise(rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2422 return rb_hash_fetch_m(1, &key, hash);
2446rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2452 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2453 if (argc == 0)
return Qnil;
2454 return call_default_proc(ifnone, hash, argv[0]);
2475 rb_hash_modify_check(hash);
2476 SET_DEFAULT(hash, ifnone);
2493rb_hash_default_proc(
VALUE hash)
2495 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2516rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2520 rb_hash_modify_check(hash);
2522 SET_DEFAULT(hash, proc);
2525 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2528 "wrong default_proc type %s (expected Proc)",
2532 SET_PROC_DEFAULT(hash, proc);
2572 rb_hash_foreach(hash, key_i, (
VALUE)args);
2578rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2580 if (RHASH_AR_TABLE_P(hash)) {
2581 return ar_delete(hash, pkey, pval);
2584 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2596 st_data_t ktmp = (st_data_t)key, val;
2598 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2614 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2616 if (!UNDEF_P(deleted_value)) {
2617 return deleted_value;
2663 rb_hash_modify_check(hash);
2664 val = rb_hash_delete_entry(hash, key);
2666 if (!UNDEF_P(val)) {
2667 compact_after_delete(hash);
2712rb_hash_shift(
VALUE hash)
2716 rb_hash_modify_check(hash);
2717 if (RHASH_AR_TABLE_P(hash)) {
2719 if (!hash_iterating_p(hash)) {
2720 if (ar_shift(hash, &var.key, &var.val)) {
2725 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2726 if (!UNDEF_P(var.key)) {
2727 rb_hash_delete_entry(hash, var.key);
2732 if (RHASH_ST_TABLE_P(hash)) {
2734 if (!hash_iterating_p(hash)) {
2735 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2740 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2741 if (!UNDEF_P(var.key)) {
2742 rb_hash_delete_entry(hash, var.key);
2754 rb_hash_modify(hash);
2763 return rb_hash_size(hash);
2787 rb_hash_modify_check(hash);
2788 if (!RHASH_TABLE_EMPTY_P(hash)) {
2789 rb_hash_foreach(hash, delete_if_i, hash);
2790 compact_after_delete(hash);
2815rb_hash_reject_bang(
VALUE hash)
2820 rb_hash_modify(hash);
2822 if (!n)
return Qnil;
2823 rb_hash_foreach(hash, delete_if_i, hash);
2848rb_hash_reject(
VALUE hash)
2853 result = hash_dup_with_compare_by_id(hash);
2855 rb_hash_foreach(result, delete_if_i, result);
2856 compact_after_delete(result);
2875rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2878 VALUE key, value, result;
2881 return copy_compare_by_id(rb_hash_new_capa(0), hash);
2883 result = copy_compare_by_id(rb_hash_new_capa(argc), hash);
2885 for (i = 0; i < argc; i++) {
2887 value = rb_hash_lookup2(hash, key,
Qundef);
2888 if (!UNDEF_P(value))
2889 rb_hash_aset(result, key, value);
2910rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2915 result = hash_dup_with_compare_by_id(hash);
2917 for (i = 0; i < argc; i++) {
2919 rb_hash_delete(result, key);
2921 compact_after_delete(result);
2944rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2949 for (i=0; i<argc; i++) {
2980rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2985 for (i=0; i<argc; i++) {
2986 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2995 rb_hash_modify(hash);
3018rb_hash_select(
VALUE hash)
3023 result = hash_dup_with_compare_by_id(hash);
3025 rb_hash_foreach(result, keep_if_i, result);
3026 compact_after_delete(result);
3052rb_hash_select_bang(
VALUE hash)
3057 rb_hash_modify_check(hash);
3059 if (!n)
return Qnil;
3060 rb_hash_foreach(hash, keep_if_i, hash);
3083rb_hash_keep_if(
VALUE hash)
3086 rb_hash_modify_check(hash);
3087 if (!RHASH_TABLE_EMPTY_P(hash)) {
3088 rb_hash_foreach(hash, keep_if_i, hash);
3111 rb_hash_modify_check(hash);
3113 if (hash_iterating_p(hash)) {
3114 rb_hash_foreach(hash, clear_i, 0);
3116 else if (RHASH_AR_TABLE_P(hash)) {
3120 st_clear(RHASH_ST_TABLE(hash));
3121 compact_after_delete(hash);
3128hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
3135rb_hash_key_str(
VALUE key)
3138 return rb_fstring(key);
3146hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
3149 *key = rb_hash_key_str(*key);
3151 return hash_aset(key, val, arg, existing);
3154NOINSERT_UPDATE_CALLBACK(hash_aset)
3155NOINSERT_UPDATE_CALLBACK(hash_aset_str)
3189 bool iter_p = hash_iterating_p(hash);
3191 rb_hash_modify(hash);
3193 if (!RHASH_STRING_KEY_P(hash, key)) {
3194 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
3197 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
3226 rb_hash_modify_check(hash);
3227 if (hash == hash2)
return hash;
3228 if (hash_iterating_p(hash)) {
3231 hash2 = to_hash(hash2);
3233 COPY_DEFAULT(hash, hash2);
3235 if (RHASH_AR_TABLE_P(hash)) {
3236 hash_ar_free_and_clear_table(hash);
3239 hash_st_free_and_clear_table(hash);
3242 hash_copy(hash, hash2);
3283rb_hash_empty_p(
VALUE hash)
3316rb_hash_each_value(
VALUE hash)
3319 rb_hash_foreach(hash, each_value_i, 0);
3350rb_hash_each_key(
VALUE hash)
3353 rb_hash_foreach(hash, each_key_i, 0);
3396rb_hash_each_pair(
VALUE hash)
3399 if (rb_block_pair_yield_optimizable())
3400 rb_hash_foreach(hash, each_pair_i_fast, 0);
3402 rb_hash_foreach(hash, each_pair_i, 0);
3416 VALUE trans = p->trans, result = p->result;
3417 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3418 if (UNDEF_P(new_key)) {
3424 rb_hash_aset(result, new_key, value);
3432 rb_hash_aset(result, new_key, value);
3525rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3532 transarg.trans = to_hash(argv[0]);
3540 if (transarg.trans) {
3541 transarg.result = result;
3542 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3545 rb_hash_foreach(hash, transform_keys_i, result);
3649rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3652 int block_given = 0;
3656 trans = to_hash(argv[0]);
3662 rb_hash_modify_check(hash);
3663 if (!RHASH_TABLE_EMPTY_P(hash)) {
3667 rb_hash_foreach(hash, flatten_i, pairs);
3674 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3677 else if (block_given) {
3684 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3685 rb_hash_stlike_delete(hash, &key, NULL);
3687 rb_hash_aset(hash, new_key, val);
3688 rb_hash_aset(new_keys, new_key,
Qnil);
3692 compact_after_delete(hash);
3697transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3703transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3707 rb_hash_modify(hash);
3713transform_values_call(
VALUE hash)
3715 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3720transform_values(
VALUE hash)
3722 hash_iter_lev_inc(hash);
3723 rb_ensure(transform_values_call, hash, hash_foreach_ensure, hash);
3745rb_hash_transform_values(
VALUE hash)
3750 result = hash_dup_with_compare_by_id(hash);
3751 SET_DEFAULT(result,
Qnil);
3754 transform_values(result);
3755 compact_after_delete(result);
3783rb_hash_transform_values_bang(
VALUE hash)
3786 rb_hash_modify_check(hash);
3788 if (!RHASH_TABLE_EMPTY_P(hash)) {
3789 transform_values(hash);
3816rb_hash_to_a(
VALUE hash)
3821 rb_hash_foreach(hash, to_a_i, ary);
3827symbol_key_needs_quote(
VALUE str)
3829 long len = RSTRING_LEN(str);
3830 if (
len == 0 || !rb_str_symname_p(str))
return true;
3831 const char *s = RSTRING_PTR(str);
3833 if (first ==
'@' || first ==
'$' || first ==
'!')
return true;
3834 if (!at_char_boundary(s, s +
len - 1,
RSTRING_END(str), rb_enc_get(str)))
return false;
3835 switch (s[
len - 1]) {
3866 quote = symbol_key_needs_quote(str2);
3871 if (RSTRING_LEN(str) > 1) {
3875 rb_enc_copy(str, str2);
3892inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3898 rb_hash_foreach(hash, inspect_i, str);
3917rb_hash_inspect(
VALUE hash)
3933rb_hash_to_hash(
VALUE hash)
3945 rb_raise(
rb_eTypeError,
"wrong element type %s (expected array)",
3946 rb_builtin_class_name(arg));
3949 rb_raise(rb_eArgError,
"element has wrong array length (expected 2, was %ld)",
3964rb_hash_to_h_block(
VALUE hash)
3967 rb_hash_foreach(hash, to_h_i, h);
3992rb_hash_to_h(
VALUE hash)
3995 return rb_hash_to_h_block(hash);
4024rb_hash_keys(
VALUE hash)
4029 if (size == 0)
return keys;
4031 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
4033 if (RHASH_AR_TABLE_P(hash)) {
4034 size = ar_keys(hash, ptr, size);
4037 st_table *table = RHASH_ST_TABLE(hash);
4038 size = st_keys(table, ptr, size);
4041 rb_gc_writebarrier_remember(keys);
4042 rb_ary_set_len(keys, size);
4045 rb_hash_foreach(hash, keys_i, keys);
4071rb_hash_values(
VALUE hash)
4077 if (size == 0)
return values;
4079 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
4080 if (RHASH_AR_TABLE_P(hash)) {
4081 rb_gc_writebarrier_remember(values);
4083 size = ar_values(hash, ptr, size);
4086 else if (RHASH_ST_TABLE_P(hash)) {
4087 st_table *table = RHASH_ST_TABLE(hash);
4088 rb_gc_writebarrier_remember(values);
4090 size = st_values(table, ptr, size);
4093 rb_ary_set_len(values, size);
4096 rb_hash_foreach(hash, values_i, values);
4118 return RBOOL(hash_stlike_lookup(hash, key, NULL));
4149 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
4165 if (!hash_stlike_lookup(data->hash, key, &val2)) {
4179recursive_eql(
VALUE hash,
VALUE dt,
int recur)
4183 if (recur)
return Qtrue;
4185 data->result =
Qtrue;
4186 rb_hash_foreach(hash, eql_i, dt);
4188 return data->result;
4196 if (hash1 == hash2)
return Qtrue;
4202 if (
rb_eql(hash2, hash1)) {
4215 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
4216 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
4228 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
4260 return hash_equal(hash1, hash2, FALSE);
4287 return hash_equal(hash1, hash2, TRUE);
4293 st_index_t *hval = (st_index_t *)arg;
4294 st_index_t hdata[2];
4296 hdata[0] = rb_hash(key);
4297 hdata[1] = rb_hash(val);
4298 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
4320rb_hash_hash(
VALUE hash)
4326 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
4335 rb_hash_aset(hash, value, key);
4359rb_hash_invert(
VALUE hash)
4363 rb_hash_foreach(hash, rb_hash_invert_i, h);
4370 rb_hash_aset(hash, key, value);
4375 VALUE hash, newvalue, *argv;
4382rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4386 VALUE newvalue = ua->newvalue, hash = arg->hash;
4389 hash_iter_lev_inc(hash);
4390 ua->iterating =
true;
4392 hash_iter_lev_dec(hash);
4393 ua->iterating =
false;
4395 else if (RHASH_STRING_KEY_P(hash, k) && !
RB_OBJ_FROZEN(k)) {
4396 *key = (st_data_t)rb_hash_key_str(k);
4398 *value = (st_data_t)newvalue;
4402NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
4408 ua->newvalue = value;
4409 RHASH_UPDATE(ua->hash, key, rb_hash_update_block_callback, args);
4414rb_hash_update_call(
VALUE args)
4418 for (
int i = 0; i < arg->argc; i++){
4419 VALUE hash = to_hash(arg->argv[i]);
4420 if (arg->block_given) {
4421 rb_hash_foreach(hash, rb_hash_update_block_i, args);
4424 rb_hash_foreach(hash, rb_hash_update_i, arg->hash);
4431rb_hash_update_ensure(
VALUE args)
4434 if (ua->iterating) hash_iter_lev_dec(ua->hash);
4483rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4494 rb_hash_modify(self);
4495 return rb_ensure(rb_hash_update_call, arg, rb_hash_update_ensure, arg);
4505rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4508 VALUE newvalue = uf_arg->value;
4511 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4517NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4523 VALUE hash = arg->hash;
4526 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4533 rb_hash_modify(hash1);
4534 hash2 = to_hash(hash2);
4539 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4542 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4548hash_merge_guess_size(
int argc,
VALUE *argv,
VALUE self)
4558 VALUE other = argv[0];
4564 unsigned bound = RHASH_AR_TABLE_BOUND(other);
4565 for (
unsigned i = 0; i < bound; i++) {
4566 VALUE key = RHASH_AR_TABLE_REF(other, i)->key;
4575 if (!hash_stlike_lookup(self, key, NULL)) {
4630rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4632 size_t guessed_size = hash_merge_guess_size(argc, argv, self);
4633 VALUE ret = guessed_size ? rb_hash_dup_capa(self, guessed_size) : rb_hash_dup(self);
4634 return rb_hash_update(argc, argv, copy_compare_by_id(ret, self));
4649assoc_lookup(
VALUE arg)
4653 if (st_lookup(p->tbl, p->key, &data))
return (
VALUE)data;
4691 if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) {
4693 st_table assoctable = *RHASH_ST_TABLE(hash);
4695 .compare = assoc_cmp,
4696 .hash = assoctable.type->hash,
4700 .key = (st_data_t)key,
4704 value = assoc_lookup(arg);
4707 hash_iter_lev_inc(hash);
4708 value =
rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4716 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4755 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4808rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4817 if (level == 0)
return rb_hash_to_a(hash);
4820 rb_hash_foreach(hash, flatten_i, ary);
4825 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4827 else if (level < 0) {
4834 rb_hash_foreach(hash, flatten_i, ary);
4862rb_hash_compact(
VALUE hash)
4864 VALUE result = rb_hash_dup(hash);
4866 rb_hash_foreach(result, delete_if_nil, result);
4867 compact_after_delete(result);
4869 else if (rb_hash_compare_by_id_p(hash)) {
4870 result = rb_hash_compare_by_id(result);
4892rb_hash_compact_bang(
VALUE hash)
4895 rb_hash_modify_check(hash);
4898 rb_hash_foreach(hash, delete_if_nil, hash);
4932rb_hash_compare_by_id(
VALUE hash)
4937 if (rb_hash_compare_by_id_p(hash))
return hash;
4939 rb_hash_modify_check(hash);
4940 if (hash_iterating_p(hash)) {
4944 if (RHASH_AR_TABLE_P(hash)) {
4945 unsigned int bound = RHASH_AR_TABLE_BOUND(hash);
4946 for (
unsigned int i = 0; i < bound; i++) {
4947 if (ar_cleared_entry(hash, i))
continue;
4950 ar_hint_set(hash, i, (st_hash_t)rb_ident_hash(pair->key));
4953 else if (RHASH_TABLE_EMPTY_P(hash)) {
4955 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4957 RHASH_ST_TABLE(hash)->type = &identhash;
4962 tmp = hash_alloc_capa(0, 0);
4965 identtable = RHASH_ST_TABLE(tmp);
4967 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4972 rb_hash_st_table_set(hash, identtable);
4973 RHASH_ST_CLEAR(tmp);
4978 rb_gc_register_pinning_obj(hash);
4999rb_hash_compare_by_id_p(
VALUE hash)
5001 return RBOOL(RHASH_IDENTHASH_P(hash));
5005rb_ident_hash_new(
void)
5007 VALUE hash = rb_hash_new_capa(0);
5009 hash_st_table_init(hash, 0);
5010 rb_gc_register_pinning_obj(hash);
5015rb_ident_hash_new_capa(
long size)
5017 VALUE hash = rb_hash_new_capa(0);
5019 hash_st_table_init(hash, size);
5020 rb_gc_register_pinning_obj(hash);
5025rb_init_identtable(
void)
5027 return st_init_table(&identhash);
5104rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
5113 rb_warn(
"given block not used");
5117 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
5124 if (rb_block_pair_yield_optimizable())
5125 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
5127 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
5168rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
5171 self = rb_hash_aref(self, *argv);
5172 if (!--argc)
return self;
5174 return rb_obj_dig(argc, argv, self,
Qnil);
5182 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
5193 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
5218 other = to_hash(other);
5220 return hash_le(hash, other);
5246 other = to_hash(other);
5248 return hash_le(hash, other);
5272 other = to_hash(other);
5274 return hash_le(other, hash);
5300 other = to_hash(other);
5302 return hash_le(other, hash);
5309 return rb_hash_aref(hash, *argv);
5330rb_hash_to_proc(
VALUE hash)
5332 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
5337rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
5343add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
5345 if (existing)
return ST_STOP;
5357 int ret = rb_hash_stlike_update(hash, key, add_new_i, val);
5369 return (RHASH_STRING_KEY_P(hash, key) && !
RB_OBJ_FROZEN(key)) ?
5370 rb_hash_key_str(key) : key;
5374ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
5377 for (i = 0; i < argc; ) {
5378 st_data_t k = key_stringify(hash, argv[i++]);
5379 st_data_t v = argv[i++];
5380 ar_insert(hash, k, v);
5389 HASH_ASSERT(argc % 2 == 0);
5391 st_index_t size = argc / 2;
5393 if (RHASH_AR_TABLE_P(hash) &&
5394 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_BOUND(hash))) {
5395 ar_bulk_insert(hash, argc, argv);
5398 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
5404hash_new_with_bulk_insert(
VALUE klass,
long argc,
const VALUE *argv)
5406 VALUE val = hash_new_capa(klass, argc / 2);
5407 rb_hash_bulk_insert(argc, argv, val);
5412rb_hash_new_with_bulk_insert(
long argc,
const VALUE *argv)
5414 return hash_new_with_bulk_insert(
rb_cHash, argc, argv);
5418rb_hash_merge2_bulk(
VALUE hash,
long argc,
const VALUE *argv,
bool dup)
5424 val = hash_dup_capa(val,
RHASH_SIZE(val) + argc / 2);
5426 rb_hash_bulk_insert(argc, argv, val);
5439 rb_hash_foreach(h2, rb_hash_update_i, val);
5443#undef USE_ORIGENVIRON
5444#if !defined(_WIN32) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5445# define USE_ORIGENVIRON 1
5446static char **origenviron;
5449#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
5450#define FREE_ENVIRON(e) rb_w32_free_environ(e)
5451static char **my_environ;
5453#define environ my_environ
5455#define getenv(n) rb_w32_ugetenv(n)
5456#elif defined(__APPLE__)
5458#define environ (*_NSGetEnviron())
5459#define GET_ENVIRON(e) (e)
5460#define FREE_ENVIRON(e)
5462extern char **environ;
5463#define GET_ENVIRON(e) (e)
5464#define FREE_ENVIRON(e)
5466#ifdef ENV_IGNORECASE
5467#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
5468#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
5470#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
5471#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
5474#define ENV_LOCKING() RB_VM_LOCKING()
5480 return rb_utf8_encoding();
5482 return rb_locale_encoding();
5498 return env_enc_str_new(ptr,
len, enc);
5504 if (!ptr)
return Qnil;
5505 return env_str_new(ptr, strlen(ptr), enc);
5509getenv_with_lock(
const char *name)
5514 const char *val = getenv(name);
5515 ret = env_str_new2(val, enc);
5521has_env_with_lock(
const char *name)
5529 return val ? true :
false;
5532static const char TZ_ENV[] =
"TZ";
5535get_env_cstr(
VALUE str,
const char *name)
5539 if (!rb_enc_asciicompat(enc)) {
5540 rb_raise(rb_eArgError,
"bad environment variable %s: ASCII incompatible encoding: %s",
5541 name, rb_enc_name(enc));
5543 var = RSTRING_PTR(str);
5544 if (memchr(var,
'\0', RSTRING_LEN(str))) {
5545 rb_raise(rb_eArgError,
"bad environment variable %s: contains null byte", name);
5547 return rb_str_fill_terminator(str, 1);
5550#define get_env_ptr(var, val) \
5551 (var = get_env_cstr(val, #var))
5553static inline const char *
5554env_name(
volatile VALUE *s)
5558 get_env_ptr(name, *s);
5562#define env_name(s) env_name(&(s))
5567reset_by_modified_env(
const char *nam,
const char *val)
5575 if (ENVMATCH(nam, TZ_ENV)) {
5576 ruby_reset_timezone(val);
5581env_delete(
VALUE name)
5583 const char *nam = env_name(name);
5584 reset_by_modified_env(nam, NULL);
5585 VALUE val = getenv_with_lock(nam);
5588 ruby_setenv(nam, 0);
5623 val = env_delete(name);
5643 const char *nam = env_name(name);
5644 VALUE env = getenv_with_lock(nam);
5684 if (block_given && argc == 2) {
5685 rb_warn(
"block supersedes default value argument");
5687 nam = env_name(key);
5688 env = getenv_with_lock(nam);
5691 if (block_given)
return rb_yield(key);
5693 rb_key_err_raise(rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5724env_fetch_values(
int argc,
VALUE *argv,
VALUE ehash)
5729 for (i=0; i<argc; i++) {
5730 rb_ary_push(result, env_fetch(1, &argv[i], ehash));
5736#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5739in_origenv(
const char *str)
5742 for (env = origenviron; *env; ++env) {
5743 if (*env == str)
return 1;
5749envix(
const char *nam)
5753 register int i,
len = strlen(nam);
5756 env = GET_ENVIRON(environ);
5757 for (i = 0; env[i]; i++) {
5758 if (ENVNMATCH(env[i],nam,
len) && env[i][
len] ==
'=')
5761 FREE_ENVIRON(environ);
5766#if defined(_WIN32) || \
5767 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5769NORETURN(
static void invalid_envname(
const char *name));
5772invalid_envname(
const char *name)
5778check_envname(
const char *name)
5780 if (strchr(name,
'=')) {
5781 invalid_envname(name);
5788ruby_setenv(
const char *name,
const char *value)
5796 check_envname(name);
5797 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5800 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5802 wvalue = wname +
len;
5803 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5804 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5808 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5809 wvalue = wname +
len;
5816 failed = _wputenv_s(wname, wvalue);
5822 if (!value || !*value) {
5824 if (!SetEnvironmentVariableW(wname, value ? wvalue : NULL) &&
5825 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5829 invalid_envname(name);
5831#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5835 ret = setenv(name, value, 1);
5838 if (ret) rb_sys_fail_sprintf(
"setenv(%s)", name);
5848 ret = unsetenv(name);
5851 if (ret) rb_sys_fail_sprintf(
"unsetenv(%s)", name);
5859 size_t len, mem_size;
5860 char **env_ptr, *str, *mem_ptr;
5862 check_envname(name);
5865 mem_size =
len + strlen(value) + 2;
5866 mem_ptr = malloc(mem_size);
5867 if (mem_ptr == NULL)
5868 rb_sys_fail_sprintf(
"malloc(%"PRIuSIZE
")", mem_size);
5869 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5873 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5874 if (!strncmp(str, name,
len) && str[
len] ==
'=') {
5875 if (!in_origenv(str)) free(str);
5876 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5885 ret = putenv(mem_ptr);
5890 rb_sys_fail_sprintf(
"putenv(%s)", name);
5900 if (environ == origenviron) {
5905 for (max = i; environ[max]; max++) ;
5906 tmpenv =
ALLOC_N(
char*, max+2);
5907 for (j=0; j<max; j++)
5914 char **envp = origenviron;
5915 while (*envp && *envp != environ[i]) envp++;
5919 while (environ[i]) {
5920 environ[i] = environ[i+1];
5927 if (!value)
goto finish;
5932 len = strlen(name) + strlen(value) + 2;
5934 snprintf(environ[i],
len,
"%s=%s",name,value);
5942ruby_unsetenv(
const char *name)
5944 ruby_setenv(name, 0);
5992 return env_aset(nm, val);
6008 get_env_ptr(name, nm);
6009 get_env_ptr(value, val);
6011 ruby_setenv(name, value);
6012 reset_by_modified_env(name, value);
6023 char **env = GET_ENVIRON(environ);
6025 char *s = strchr(*env,
'=');
6027 const char *p = *env;
6034 FREE_ENVIRON(environ);
6056 return env_keys(FALSE);
6066 env = GET_ENVIRON(environ);
6067 for (; *env ; ++env) {
6068 if (strchr(*env,
'=')) {
6072 FREE_ENVIRON(environ);
6096env_each_key(
VALUE ehash)
6102 keys = env_keys(FALSE);
6116 char **env = GET_ENVIRON(environ);
6119 char *s = strchr(*env,
'=');
6125 FREE_ENVIRON(environ);
6146 return env_values();
6167env_each_value(
VALUE ehash)
6173 values = env_values();
6199env_each_pair(
VALUE ehash)
6209 char **env = GET_ENVIRON(environ);
6212 char *s = strchr(*env,
'=');
6219 FREE_ENVIRON(environ);
6222 if (rb_block_pair_yield_optimizable()) {
6259env_reject_bang(
VALUE ehash)
6266 keys = env_keys(FALSE);
6267 RBASIC_CLEAR_CLASS(keys);
6278 if (del == 0)
return Qnil;
6298env_delete_if(
VALUE ehash)
6301 env_reject_bang(ehash);
6329 for (i=0; i<argc; i++) {
6351env_select(
VALUE ehash)
6358 result = rb_hash_new();
6359 keys = env_keys(FALSE);
6365 rb_hash_aset(result, key, val);
6398env_select_bang(
VALUE ehash)
6405 keys = env_keys(FALSE);
6406 RBASIC_CLEAR_CLASS(keys);
6417 if (del == 0)
return Qnil;
6436env_keep_if(
VALUE ehash)
6439 env_select_bang(ehash);
6459 VALUE key, value, result;
6461 result = rb_hash_new_capa(argc);
6463 for (i = 0; i < argc; i++) {
6465 value = rb_f_getenv(
Qnil, key);
6467 rb_hash_aset(result, key, value);
6479 keys = env_keys(TRUE);
6482 const char *nam = RSTRING_PTR(key);
6483 ruby_setenv(nam, 0);
6502 return rb_env_clear();
6533 char **env = GET_ENVIRON(environ);
6535 const char *s = strchr(*env,
'=');
6537 if (env != environ) {
6548 FREE_ENVIRON(environ);
6572 char **env = GET_ENVIRON(environ);
6574 char *s = strchr(*env,
'=');
6577 env_str_new2(s+1, enc)));
6581 FREE_ENVIRON(environ);
6602env_size_with_lock(
void)
6607 char **env = GET_ENVIRON(environ);
6609 FREE_ENVIRON(environ);
6628 return INT2FIX(env_size_with_lock());
6647 char **env = GET_ENVIRON(environ);
6651 FREE_ENVIRON(environ);
6654 return RBOOL(empty);
6683 const char *s = env_name(key);
6684 return RBOOL(has_env_with_lock(s));
6710 const char *s = env_name(key);
6711 VALUE e = getenv_with_lock(s);
6742 char **env = GET_ENVIRON(environ);
6744 char *s = strchr(*env,
'=');
6746 long len = strlen(s);
6747 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6754 FREE_ENVIRON(environ);
6783 char **env = GET_ENVIRON(environ);
6786 const char *p = *env;
6787 const char *s = strchr(p,
'=');
6789 long len = strlen(s);
6790 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6797 FREE_ENVIRON(environ);
6827 char **env = GET_ENVIRON(environ);
6829 char *s = strchr(*env,
'=');
6831 long len = strlen(s);
6832 if (RSTRING_LEN(value) ==
len && strncmp(s, RSTRING_PTR(value),
len) == 0) {
6833 str = env_str_new(*env, s-*env-1, enc);
6839 FREE_ENVIRON(environ);
6846environ_size(
char **env)
6863 char **env = GET_ENVIRON(environ);
6864 hash = rb_hash_new_capa(environ_size(env));
6866 char *s = strchr(*env,
'=');
6868 rb_hash_aset(hash, env_str_new(*env, s-*env, enc),
6869 env_str_new2(s+1, enc));
6873 FREE_ENVIRON(environ);
6888 return env_to_hash();
6903 return env_to_hash();
6927 VALUE hash = env_to_hash();
6929 hash = rb_hash_to_h_block(hash);
6947 VALUE key, hash = env_to_hash();
6949 for (i = 0; i < argc; i++) {
6951 rb_hash_delete(hash, key);
6975 return rb_hash_delete_if(env_to_hash());
6978NORETURN(
static VALUE env_freeze(
VALUE self));
6987env_freeze(
VALUE self)
7016 char **env = GET_ENVIRON(environ);
7018 const char *p = *env;
7019 const char *s = strchr(p,
'=');
7021 key = env_str_new(p, s-p, enc);
7022 VALUE val = env_str_new2(getenv(RSTRING_PTR(key)), enc);
7026 FREE_ENVIRON(environ);
7054 return rb_hash_invert(env_to_hash());
7061 const char *keyptr, *eptr;
7069 if (elen != keylen)
continue;
7070 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
7082 keylist_delete(keys, key);
7110 keys = env_keys(TRUE);
7111 if (env == hash)
return env;
7112 hash = to_hash(hash);
7113 rb_hash_foreach(hash, env_replace_i, keys);
7133 if (!
NIL_P(oldval)) {
7180 env_update_block_i : env_update_i;
7181 for (
int i = 0; i < argc; ++i) {
7182 VALUE hash = argv[i];
7183 if (env == hash)
continue;
7184 hash = to_hash(hash);
7185 rb_hash_foreach(hash, func, 0);
7205 rb_get_freeze_opt(1, &opt);
7209 rb_raise(
rb_eTypeError,
"Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
7223 rb_raise(
rb_eTypeError,
"Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
7234 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
7704 id_hash_iter_lev = rb_make_internal_id();
7802 rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_alloc_fixed_size(
rb_cHash, 0));
7804 rb_vm_register_global_object(rb_cHash_empty_frozen);
7971#ifdef USE_ORIGENVIRON
7972 origenviron = environ;
8042 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
8045#include "hash.rbinc"
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#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_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a 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 FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#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 OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#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 ASSUME
Old name of RBIMPL_ASSUME.
#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 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_cObject
Object class.
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)
Same as RB_OBJ_FREEZE(), but returns the given object.
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.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#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.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
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.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
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 MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#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.
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.