2#include "internal/gc.h"
3#include "internal/hash.h"
4#include "internal/proc.h"
5#include "internal/sanitizers.h"
42 st_free_table(w->table);
46wmap_memsize(
const void *ptr)
52 size += st_memsize(w->table);
54 size += st_table_size(w->table) * (2 *
sizeof(
VALUE));
66wmap_compact_table_each_i(st_data_t k, st_data_t v, st_data_t d,
int error)
73 VALUE moved_key = rb_gc_location(key);
74 VALUE moved_val = rb_gc_location(val);
78 if (key != moved_key) {
79 st_insert(table, (st_data_t)moved_key, (st_data_t)moved_val);
83 else if (val != moved_val) {
92wmap_compact_table_replace_i(st_data_t *k, st_data_t *v, st_data_t d,
int existing)
96 *v = (st_data_t)rb_gc_location((
VALUE)*v);
102wmap_compact(
void *ptr)
107 DURING_GC_COULD_MALLOC_REGION_START();
109 st_foreach_with_replace(w->table, wmap_compact_table_each_i, wmap_compact_table_replace_i, (st_data_t)w->table);
111 DURING_GC_COULD_MALLOC_REGION_END();
123 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
127wmap_cmp(st_data_t x, st_data_t y)
133wmap_hash(st_data_t n)
135 return st_numhash(n);
144rb_wmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
146 if (rb_gc_handle_weak_references_alive_p(key) &&
147 rb_gc_handle_weak_references_alive_p(val)) {
156rb_wmap_handle_weak_references(
VALUE self)
161 st_foreach(w->table, rb_wmap_handle_weak_references_i, (st_data_t)0);
165wmap_allocate(
VALUE klass)
170 w->table = st_init_table(&wmap_hash_type);
172 rb_gc_declare_weak_references(obj);
189wmap_inspect_i(st_data_t k, st_data_t v, st_data_t data)
195 if (RSTRING_PTR(str)[0] ==
'#') {
200 RSTRING_PTR(str)[0] =
'#';
203 wmap_inspect_append(str, key);
205 wmap_inspect_append(str, val);
211wmap_inspect(
VALUE self)
217 VALUE str = rb_sprintf(
"-<%"PRIsVALUE
":%p", c, (
void *)self);
219 st_foreach(w->table, wmap_inspect_i, (st_data_t)str);
221 RSTRING_PTR(str)[0] =
'#';
228wmap_each_i(st_data_t k, st_data_t v, st_data_t
_)
249 st_foreach(w->table, wmap_each_i, (st_data_t)0);
255wmap_each_key_i(st_data_t k, st_data_t _v, st_data_t _data)
271wmap_each_key(
VALUE self)
276 st_foreach(w->table, wmap_each_key_i, (st_data_t)0);
282wmap_each_value_i(st_data_t k, st_data_t v, st_data_t _data)
298wmap_each_value(
VALUE self)
303 st_foreach(w->table, wmap_each_value_i, (st_data_t)0);
309wmap_keys_i(st_data_t k, st_data_t v, st_data_t data)
332 st_foreach(w->table, wmap_keys_i, (st_data_t)ary);
338wmap_values_i(st_data_t k, st_data_t v, st_data_t data)
355wmap_values(
VALUE self)
361 st_foreach(w->table, wmap_values_i, (st_data_t)ary);
381 st_insert(w->table, (st_data_t)key, (st_data_t)val);
397 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
413 VALUE obj = wmap_lookup(self, key);
414 return !UNDEF_P(obj) ? obj :
Qnil;
451 st_data_t orig_key = (st_data_t)key;
453 if (st_delete(w->table, &orig_key, &orig_val)) {
454 return (
VALUE)orig_val;
474 return RBOOL(!UNDEF_P(wmap_lookup(self, key)));
489 st_index_t n = st_table_size(w->table);
491#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
521wkmap_mark_table_i(st_data_t key, st_data_t val_obj, st_data_t _data)
523 rb_gc_mark_movable((
VALUE)val_obj);
533 st_foreach(w->table, wkmap_mark_table_i, (st_data_t)0);
542 st_free_table(w->table);
546wkmap_memsize(
const void *ptr)
552 size += st_memsize(w->table);
554 size += st_table_size(w->table) *
sizeof(
VALUE);
561wkmap_compact_table_i(st_data_t key, st_data_t val, st_data_t _data,
int _error)
571wkmap_compact_table_replace(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t _data,
int existing)
575 *key_ptr = (st_data_t)rb_gc_location((
VALUE)*key_ptr);
576 *val_ptr = (st_data_t)rb_gc_location((
VALUE)*val_ptr);
582wkmap_compact(
void *ptr)
587 st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0);
599 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
603wkmap_cmp(st_data_t x, st_data_t y)
608 return rb_any_cmp(x_obj, y_obj);
612wkmap_hash(st_data_t n)
616 return rb_any_hash(obj);
625rb_wkmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
627 if (rb_gc_handle_weak_references_alive_p(key)) {
636rb_wkmap_handle_weak_references(
VALUE self)
641 st_foreach(w->table, rb_wkmap_handle_weak_references_i, (st_data_t)0);
645wkmap_allocate(
VALUE klass)
651 w->table = st_init_table(&wkmap_hash_type);
653 rb_gc_declare_weak_references(obj);
665 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
681 VALUE obj = wkmap_lookup(self, key);
682 return !UNDEF_P(obj) ? obj :
Qnil;
709 rb_raise(rb_eArgError,
"WeakKeyMap keys must be garbage collectable");
713 st_insert(w->table, (st_data_t)key, (st_data_t)val);
756 st_data_t orig_key = (st_data_t)key;
758 if (st_delete(w->table, &orig_key, &orig_val)) {
759 return (
VALUE)orig_val;
796 if (!st_get_key(w->table, (st_data_t)key, &orig_key))
return Qnil;
798 return (
VALUE)orig_key;
810 return RBOOL(!UNDEF_P(wkmap_lookup(self, key)));
820wkmap_clear(
VALUE self)
842wkmap_inspect(
VALUE self)
847 st_index_t n = st_table_size(w->table);
849#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
850 const char * format =
"#<%"PRIsVALUE
":%p size=%lu>";
852 const char * format =
"#<%"PRIsVALUE
":%p size=%llu>";
#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.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
Defines a top-level module.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define Qundef
Old name of RUBY_Qundef.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
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(VALUE val)
Yields the block.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.