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);
64wmap_compact_table_each_i(st_data_t k, st_data_t v, st_data_t d,
int error)
71 VALUE moved_key = rb_gc_location(key);
72 VALUE moved_val = rb_gc_location(val);
76 if (key != moved_key) {
77 st_insert(table, (st_data_t)moved_key, (st_data_t)moved_val);
81 else if (val != moved_val) {
90wmap_compact_table_replace_i(st_data_t *k, st_data_t *v, st_data_t d,
int existing)
94 *v = (st_data_t)rb_gc_location((
VALUE)*v);
100wmap_compact(
void *ptr)
105 DURING_GC_COULD_MALLOC_REGION_START();
107 st_foreach_with_replace(w->table, wmap_compact_table_each_i, wmap_compact_table_replace_i, (st_data_t)w->table);
109 DURING_GC_COULD_MALLOC_REGION_END();
114rb_wmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
116 if (rb_gc_handle_weak_references_alive_p(key) &&
117 rb_gc_handle_weak_references_alive_p(val)) {
126wmap_handle_weak_references(
void *ptr)
130 st_foreach(w->table, rb_wmap_handle_weak_references_i, (st_data_t)0);
140 wmap_handle_weak_references,
142 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
146wmap_cmp(st_data_t x, st_data_t y)
152wmap_hash(st_data_t n)
154 return st_numhash(n);
163wmap_allocate(
VALUE klass)
168 w->table = st_init_table(&wmap_hash_type);
170 rb_gc_declare_weak_references(obj);
187wmap_inspect_i(st_data_t k, st_data_t v, st_data_t data)
193 if (RSTRING_PTR(str)[0] ==
'#') {
198 RSTRING_PTR(str)[0] =
'#';
201 wmap_inspect_append(str, key);
203 wmap_inspect_append(str, val);
220wmap_inspect(
VALUE self)
226 VALUE str = rb_sprintf(
"-<%"PRIsVALUE
":%p", c, (
void *)self);
228 st_foreach(w->table, wmap_inspect_i, (st_data_t)str);
230 RSTRING_PTR(str)[0] =
'#';
237wmap_each_i(st_data_t k, st_data_t v, st_data_t
_)
258 st_foreach(w->table, wmap_each_i, (st_data_t)0);
264wmap_each_key_i(st_data_t k, st_data_t _v, st_data_t _data)
280wmap_each_key(
VALUE self)
285 st_foreach(w->table, wmap_each_key_i, (st_data_t)0);
291wmap_each_value_i(st_data_t k, st_data_t v, st_data_t _data)
307wmap_each_value(
VALUE self)
312 st_foreach(w->table, wmap_each_value_i, (st_data_t)0);
318wmap_keys_i(st_data_t k, st_data_t v, st_data_t data)
341 st_foreach(w->table, wmap_keys_i, (st_data_t)ary);
347wmap_values_i(st_data_t k, st_data_t v, st_data_t data)
364wmap_values(
VALUE self)
370 st_foreach(w->table, wmap_values_i, (st_data_t)ary);
390 st_insert(w->table, (st_data_t)key, (st_data_t)val);
406 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
422 VALUE obj = wmap_lookup(self, key);
423 return !UNDEF_P(obj) ? obj :
Qnil;
460 st_data_t orig_key = (st_data_t)key;
462 if (st_delete(w->table, &orig_key, &orig_val)) {
463 return (
VALUE)orig_val;
483 return RBOOL(!UNDEF_P(wmap_lookup(self, key)));
498 st_index_t n = st_table_size(w->table);
500#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
530wkmap_mark_table_i(st_data_t key, st_data_t val_obj, st_data_t _data)
532 rb_gc_mark_movable((
VALUE)val_obj);
542 st_foreach(w->table, wkmap_mark_table_i, (st_data_t)0);
551 st_free_table(w->table);
555wkmap_memsize(
const void *ptr)
561 size += st_memsize(w->table);
568wkmap_compact_table_i(st_data_t key, st_data_t val, st_data_t _data,
int _error)
578wkmap_compact_table_replace(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t _data,
int existing)
582 *key_ptr = (st_data_t)rb_gc_location((
VALUE)*key_ptr);
583 *val_ptr = (st_data_t)rb_gc_location((
VALUE)*val_ptr);
589wkmap_compact(
void *ptr)
594 st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0);
599rb_wkmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
601 if (rb_gc_handle_weak_references_alive_p(key)) {
610wkmap_handle_weak_references(
void *ptr)
614 st_foreach(w->table, rb_wkmap_handle_weak_references_i, (st_data_t)0);
624 wkmap_handle_weak_references,
626 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
630wkmap_cmp(st_data_t x, st_data_t y)
635 return rb_any_cmp(x_obj, y_obj);
639wkmap_hash(st_data_t n)
643 return rb_any_hash(obj);
652wkmap_allocate(
VALUE klass)
658 w->table = st_init_table(&wkmap_hash_type);
660 rb_gc_declare_weak_references(obj);
672 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
688 VALUE obj = wkmap_lookup(self, key);
689 return !UNDEF_P(obj) ? obj :
Qnil;
716 rb_raise(rb_eArgError,
"WeakKeyMap keys must be garbage collectable");
720 st_insert(w->table, (st_data_t)key, (st_data_t)val);
763 st_data_t orig_key = (st_data_t)key;
765 if (st_delete(w->table, &orig_key, &orig_val)) {
766 return (
VALUE)orig_val;
803 if (!st_get_key(w->table, (st_data_t)key, &orig_key))
return Qnil;
805 return (
VALUE)orig_key;
817 return RBOOL(!UNDEF_P(wkmap_lookup(self, key)));
827wkmap_clear(
VALUE self)
849wkmap_inspect(
VALUE self)
854 st_index_t n = st_table_size(w->table);
856#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
857 const char * format =
"#<%"PRIsVALUE
":%p size=%lu>";
859 const char * format =
"#<%"PRIsVALUE
":%p size=%llu>";
969 VALUE rb_mObjectSpace = rb_define_module(
"ObjectSpace");
971 VALUE rb_cWeakMap = rb_define_class_under(rb_mObjectSpace,
"WeakMap",
rb_cObject);
990 VALUE rb_cWeakKeyMap = rb_define_class_under(rb_mObjectSpace,
"WeakKeyMap",
rb_cObject);
#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.
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_cObject
Object class.
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.