Add struct weakmap_entry for WeakMap entries

This commit is contained in:
Peter Zhu 2024-08-19 11:50:21 -04:00
Родитель 2569413b1c
Коммит 9a9e74389c
1 изменённых файлов: 9 добавлений и 4 удалений

Просмотреть файл

@ -29,6 +29,11 @@ struct weakmap {
st_table *table;
};
struct weakmap_entry {
VALUE key;
VALUE val;
};
static bool
wmap_live_p(VALUE obj)
{
@ -42,7 +47,7 @@ wmap_free_entry(VALUE *key, VALUE *val)
/* We only need to free key because val is allocated beside key on in the
* same malloc call. */
ruby_sized_xfree(key, sizeof(VALUE) * 2);
ruby_sized_xfree(key, sizeof(struct weakmap_entry));
}
static int
@ -418,10 +423,10 @@ wmap_aset_replace(st_data_t *key, st_data_t *val, st_data_t new_key_ptr, int exi
RUBY_ASSERT(*(VALUE *)*key == new_key);
}
else {
VALUE *pair = xmalloc(sizeof(VALUE) * 2);
struct weakmap_entry *entry = xmalloc(sizeof(struct weakmap_entry));
*key = (st_data_t)pair;
*val = (st_data_t)(pair + 1);
*key = (st_data_t)&entry->key;;
*val = (st_data_t)&entry->val;
}
*(VALUE *)*key = new_key;