Class: ObjectSpace::WeakMap
Overview
An ObjectSpace::WeakMap object holds references to any objects, but those objects can get garbage collected.
This class is mostly used internally by WeakRef, please use lib/weakref.rb
for the public interface.
Instance Method Summary collapse
-
#[](wmap) ⇒ Object
Retrieves a weakly referenced object with the given key.
-
#[]=(wmap, orig) ⇒ Object
Creates a weak reference from the given key to the given value.
-
#each ⇒ Object
Iterates over keys and objects in a weakly referenced object.
-
#each_key ⇒ Object
Iterates over keys and objects in a weakly referenced object.
-
#each_pair ⇒ Object
Iterates over keys and objects in a weakly referenced object.
-
#each_value ⇒ Object
Iterates over keys and objects in a weakly referenced object.
- #finalize(objid) ⇒ Object private
-
#include?(key) ⇒ Boolean
Returns
true
ifkey
is registered. - #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
Returns
true
ifkey
is registered. -
#keys ⇒ Object
Iterates over keys and objects in a weakly referenced object.
- #length ⇒ Object
-
#member?(key) ⇒ Boolean
Returns
true
ifkey
is registered. - #size ⇒ Object
-
#values ⇒ Object
Iterates over values and objects in a weakly referenced object.
Methods included from Enumerable
#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #inject, #lazy, #map, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #take, #take_while, #to_a, #to_h, #zip
Instance Method Details
#[](wmap) ⇒ Object
Retrieves a weakly referenced object with the given key
7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 |
# File 'gc.c', line 7930
static VALUE
wmap_aref(VALUE self, VALUE wmap)
{
st_data_t data;
VALUE obj;
struct weakmap *w;
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
if (!st_lookup(w->wmap2obj, (st_data_t)wmap, &data)) return Qnil;
obj = (VALUE)data;
if (!is_id_value(objspace, obj)) return Qnil;
if (!is_live_object(objspace, obj)) return Qnil;
return obj;
}
|
#[]=(wmap, orig) ⇒ Object
Creates a weak reference from the given key to the given value
7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 |
# File 'gc.c', line 7914
static VALUE
wmap_aset(VALUE self, VALUE wmap, VALUE orig)
{
struct weakmap *w;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
should_be_finalizable(orig);
should_be_finalizable(wmap);
define_final0(orig, w->final);
define_final0(wmap, w->final);
st_update(w->obj2wmap, (st_data_t)orig, wmap_aset_update, wmap);
st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig);
return nonspecial_obj_id(orig);
}
|
#each ⇒ Object
Iterates over keys and objects in a weakly referenced object
7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 |
# File 'gc.c', line 7781
static VALUE
wmap_each(VALUE self)
{
struct weakmap *w;
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
st_foreach(w->wmap2obj, wmap_each_i, (st_data_t)objspace);
return self;
}
|
#each_key ⇒ Object
Iterates over keys and objects in a weakly referenced object
7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 |
# File 'gc.c', line 7804
static VALUE
wmap_each_key(VALUE self)
{
struct weakmap *w;
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
st_foreach(w->wmap2obj, wmap_each_key_i, (st_data_t)objspace);
return self;
}
|
#each_pair ⇒ Object
Iterates over keys and objects in a weakly referenced object
7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 |
# File 'gc.c', line 7781
static VALUE
wmap_each(VALUE self)
{
struct weakmap *w;
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
st_foreach(w->wmap2obj, wmap_each_i, (st_data_t)objspace);
return self;
}
|
#each_value ⇒ Object
Iterates over keys and objects in a weakly referenced object
7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 |
# File 'gc.c', line 7827
static VALUE
wmap_each_value(VALUE self)
{
struct weakmap *w;
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
st_foreach(w->wmap2obj, wmap_each_value_i, (st_data_t)objspace);
return self;
}
|
#finalize(objid) ⇒ Object (private)
7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 |
# File 'gc.c', line 7692
static VALUE
wmap_finalize(VALUE self, VALUE objid)
{
st_data_t orig, wmap, data;
VALUE obj, *rids, i, size;
struct weakmap *w;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
/* Get reference from object id. */
obj = obj_id_to_ref(objid);
/* obj is original referenced object and/or weak reference. */
orig = (st_data_t)obj;
if (st_delete(w->obj2wmap, &orig, &data)) {
rids = (VALUE *)data;
size = *rids++;
for (i = 0; i < size; ++i) {
wmap = (st_data_t)rids[i];
st_delete(w->wmap2obj, &wmap, NULL);
}
ruby_sized_xfree((VALUE *)data, (size + 1) * sizeof(VALUE));
}
wmap = (st_data_t)obj;
if (st_delete(w->wmap2obj, &wmap, &orig)) {
wmap = (st_data_t)obj;
st_update(w->obj2wmap, orig, wmap_final_func, wmap);
}
return self;
}
|
#include?(key) ⇒ Boolean
Returns true
if key
is registered
7947 7948 7949 7950 7951 |
# File 'gc.c', line 7947
static VALUE
wmap_has_key(VALUE self, VALUE key)
{
return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}
|
#inspect ⇒ Object
7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 |
# File 'gc.c', line 7752
static VALUE
wmap_inspect(VALUE self)
{
VALUE str;
VALUE c = rb_class_name(CLASS_OF(self));
struct weakmap *w;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
if (w->wmap2obj) {
st_foreach(w->wmap2obj, wmap_inspect_i, str);
}
RSTRING_PTR(str)[0] = '#';
rb_str_cat2(str, ">");
return str;
}
|
#key?(key) ⇒ Boolean
Returns true
if key
is registered
7947 7948 7949 7950 7951 |
# File 'gc.c', line 7947
static VALUE
wmap_has_key(VALUE self, VALUE key)
{
return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}
|
#keys ⇒ Object
Iterates over keys and objects in a weakly referenced object
7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 |
# File 'gc.c', line 7852
static VALUE
wmap_keys(VALUE self)
{
struct weakmap *w;
struct wmap_iter_arg args;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
args.objspace = &rb_objspace;
args.value = rb_ary_new();
st_foreach(w->wmap2obj, wmap_keys_i, (st_data_t)&args);
return args.value;
}
|
#length ⇒ Object
7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 |
# File 'gc.c', line 7953
static VALUE
wmap_size(VALUE self)
{
struct weakmap *w;
st_index_t n;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
n = w->wmap2obj->num_entries;
#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
return ULONG2NUM(n);
#else
return ULL2NUM(n);
#endif
}
|
#member?(key) ⇒ Boolean
Returns true
if key
is registered
7947 7948 7949 7950 7951 |
# File 'gc.c', line 7947
static VALUE
wmap_has_key(VALUE self, VALUE key)
{
return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}
|
#size ⇒ Object
7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 |
# File 'gc.c', line 7953
static VALUE
wmap_size(VALUE self)
{
struct weakmap *w;
st_index_t n;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
n = w->wmap2obj->num_entries;
#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
return ULONG2NUM(n);
#else
return ULL2NUM(n);
#endif
}
|
#values ⇒ Object
Iterates over values and objects in a weakly referenced object
7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 |
# File 'gc.c', line 7879
static VALUE
wmap_values(VALUE self)
{
struct weakmap *w;
struct wmap_iter_arg args;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
args.objspace = &rb_objspace;
args.value = rb_ary_new();
st_foreach(w->wmap2obj, wmap_values_i, (st_data_t)&args);
return args.value;
}
|