2#include "internal/array.h"
3#include "internal/gc.h"
4#include "internal/hash.h"
5#include "internal/proc.h"
6#include "internal/sanitizers.h"
43 st_free_table(w->table);
47wmap_memsize(
const void *ptr)
53 size += st_memsize(w->table);
65wmap_compact_table_each_i(st_data_t k, st_data_t v, st_data_t d,
int error)
67 st_table *table = (st_table *)d;
72 VALUE moved_key = rb_gc_location(key);
73 VALUE moved_val = rb_gc_location(val);
77 if (key != moved_key) {
78 st_insert(table, (st_data_t)moved_key, (st_data_t)moved_val);
82 else if (val != moved_val) {
91wmap_compact_table_replace_i(st_data_t *k, st_data_t *v, st_data_t d,
int existing)
95 rb_gc_update_moved((
VALUE *)v);
101wmap_compact(
void *ptr)
106 DURING_GC_COULD_MALLOC_REGION_START();
108 st_foreach_with_replace(w->table, wmap_compact_table_each_i, wmap_compact_table_replace_i, (st_data_t)w->table);
110 DURING_GC_COULD_MALLOC_REGION_END();
115rb_wmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
119 if (rb_gc_handle_weak_references_alive_p(
objspace, key) &&
120 rb_gc_handle_weak_references_alive_p(
objspace, val)) {
129wmap_handle_weak_references(
void *ptr,
void *
objspace)
143 wmap_handle_weak_references,
145 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
149wmap_cmp(st_data_t x, st_data_t y)
155wmap_hash(st_data_t n)
157 return st_numhash(n);
160static const struct st_hash_type wmap_hash_type = {
166wmap_allocate(
VALUE klass)
171 w->table = st_init_table(&wmap_hash_type);
173 rb_gc_declare_weak_references(obj);
179wmap_foreach_snapshot_i(st_data_t k, st_data_t v, st_data_t data)
198 st_foreach(w->table, wmap_foreach_snapshot_i, (st_data_t)ary);
200 for (
long i = 0; i <
RARRAY_LEN(ary); i += 2) {
204 if (st_lookup(w->table, (st_data_t)key, &val)) {
205 func(key, (
VALUE)val, arg);
226 if (RSTRING_PTR(str)[0] ==
'#') {
231 RSTRING_PTR(str)[0] =
'#';
234 wmap_inspect_append(str, key);
236 wmap_inspect_append(str, val);
251wmap_inspect(
VALUE self)
257 VALUE str = rb_sprintf(
"-<%"PRIsVALUE
":%p", c, (
void *)self);
259 wmap_foreach(w, wmap_inspect_i, str);
261 RSTRING_PTR(str)[0] =
'#';
287 wmap_foreach(w, wmap_each_i,
Qnil);
307wmap_each_key(
VALUE self)
312 wmap_foreach(w, wmap_each_key_i,
Qnil);
332wmap_each_value(
VALUE self)
337 wmap_foreach(w, wmap_each_value_i,
Qnil);
343wmap_keys_i(st_data_t k, st_data_t v, st_data_t data)
366 st_foreach(w->table, wmap_keys_i, (st_data_t)ary);
372wmap_values_i(st_data_t k, st_data_t v, st_data_t data)
389wmap_values(
VALUE self)
395 st_foreach(w->table, wmap_values_i, (st_data_t)ary);
415 st_insert(w->table, (st_data_t)key, (st_data_t)val);
431 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
447 VALUE obj = wmap_lookup(self, key);
448 return !UNDEF_P(obj) ? obj :
Qnil;
485 st_data_t orig_key = (st_data_t)key;
487 if (st_delete(w->table, &orig_key, &orig_val)) {
488 return (
VALUE)orig_val;
508 return RBOOL(!UNDEF_P(wmap_lookup(self, key)));
523 st_index_t n = st_table_size(w->table);
525#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
533rb_wmap_new_hidden(
void)
535 return wmap_allocate(0);
541 return wmap_lookup(self, key);
547 wmap_aset(self, key, val);
573wkmap_mark_table_i(st_data_t key, st_data_t val_obj, st_data_t _data)
575 rb_gc_mark_movable((
VALUE)val_obj);
585 st_foreach(w->table, wkmap_mark_table_i, (st_data_t)0);
594 st_free_table(w->table);
598wkmap_memsize(
const void *ptr)
604 size += st_memsize(w->table);
611wkmap_compact_table_i(st_data_t key, st_data_t val, st_data_t _data,
int _error)
621wkmap_compact_table_replace(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t _data,
int existing)
625 rb_gc_update_moved((
VALUE *)key_ptr);
626 rb_gc_update_moved((
VALUE *)val_ptr);
632wkmap_compact(
void *ptr)
637 st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0);
642rb_wkmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
644 if (rb_gc_handle_weak_references_alive_p((
void *)arg, key)) {
653wkmap_handle_weak_references(
void *ptr,
void *
objspace)
667 wkmap_handle_weak_references,
669 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
673wkmap_cmp(st_data_t x, st_data_t y)
678 return rb_any_cmp(x_obj, y_obj);
682wkmap_hash(st_data_t n)
686 return rb_any_hash(obj);
689static const struct st_hash_type wkmap_hash_type = {
695wkmap_allocate(
VALUE klass)
701 w->table = st_init_table(&wkmap_hash_type);
703 rb_gc_declare_weak_references(obj);
715 if (!st_lookup(w->table, (st_data_t)key, &data))
return Qundef;
731 VALUE obj = wkmap_lookup(self, key);
732 return !UNDEF_P(obj) ? obj :
Qnil;
759 rb_raise(rb_eArgError,
"WeakKeyMap keys must be garbage collectable");
763 st_insert(w->table, (st_data_t)key, (st_data_t)val);
806 st_data_t orig_key = (st_data_t)key;
808 if (st_delete(w->table, &orig_key, &orig_val)) {
809 return (
VALUE)orig_val;
846 if (!st_get_key(w->table, (st_data_t)key, &orig_key))
return Qnil;
848 return (
VALUE)orig_key;
860 return RBOOL(!UNDEF_P(wkmap_lookup(self, key)));
870wkmap_clear(
VALUE self)
892wkmap_inspect(
VALUE self)
897 st_index_t n = st_table_size(w->table);
899#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
900 const char * format =
"#<%"PRIsVALUE
":%p size=%lu>";
902 const char * format =
"#<%"PRIsVALUE
":%p size=%llu>";
1012 VALUE rb_mObjectSpace = rb_define_module(
"ObjectSpace");
1014 VALUE rb_cWeakMap = rb_define_class_under(rb_mObjectSpace,
"WeakMap",
rb_cObject);
1033 VALUE rb_cWeakKeyMap = rb_define_class_under(rb_mObjectSpace,
"WeakKeyMap",
rb_cObject);
#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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) 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 RB_GC_GUARD(v)
Prevents premature destruction of local objects.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
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.