Skip to content

Commit a731080

Browse files
committed
Make rb_gc_obj_optimal_size always return allocatable size
It may return sizes that aren't allocatable for arrays and strings.
1 parent 17efb77 commit a731080

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

gc.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,15 @@ rb_gc_obj_optimal_size(VALUE obj)
33173317
{
33183318
switch (BUILTIN_TYPE(obj)) {
33193319
case T_ARRAY:
3320-
return rb_ary_size_as_embedded(obj);
3320+
{
3321+
size_t size = rb_ary_size_as_embedded(obj);
3322+
if (rb_gc_size_allocatable_p(size)) {
3323+
return size;
3324+
}
3325+
else {
3326+
return sizeof(struct RArray);
3327+
}
3328+
}
33213329

33223330
case T_OBJECT:
33233331
if (rb_shape_obj_too_complex_p(obj)) {
@@ -3328,7 +3336,15 @@ rb_gc_obj_optimal_size(VALUE obj)
33283336
}
33293337

33303338
case T_STRING:
3331-
return rb_str_size_as_embedded(obj);
3339+
{
3340+
size_t size = rb_str_size_as_embedded(obj);
3341+
if (rb_gc_size_allocatable_p(size)) {
3342+
return size;
3343+
}
3344+
else {
3345+
return sizeof(struct RString);
3346+
}
3347+
}
33323348

33333349
case T_HASH:
33343350
return sizeof(struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));

gc/default/default.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,10 +5484,9 @@ gc_compact_destination_pool(rb_objspace_t *objspace, rb_heap_t *src_pool, VALUE
54845484
return src_pool;
54855485
}
54865486

5487-
size_t idx = 0;
5488-
if (rb_gc_impl_size_allocatable_p(obj_size)) {
5489-
idx = heap_idx_for_size(obj_size);
5490-
}
5487+
GC_ASSERT(rb_gc_impl_size_allocatable_p(obj_size));
5488+
5489+
size_t idx = heap_idx_for_size(obj_size);
54915490

54925491
return &heaps[idx];
54935492
}

0 commit comments

Comments
 (0)