diff options
-rw-r--r-- | ext/-test-/typeddata/typeddata.c | 24 | ||||
-rw-r--r-- | gc.c | 50 | ||||
-rw-r--r-- | test/-ext-/typeddata/test_typeddata.rb | 11 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 67 insertions, 20 deletions
diff --git a/ext/-test-/typeddata/typeddata.c b/ext/-test-/typeddata/typeddata.c index 1c5d677713..ae060960cd 100644 --- a/ext/-test-/typeddata/typeddata.c +++ b/ext/-test-/typeddata/typeddata.c @@ -2,19 +2,43 @@ static const rb_data_type_t test_data = { "typed_data", + {NULL, ruby_xfree, NULL}, + NULL, NULL, + 0/* deferred free */, }; static VALUE +test_alloc(VALUE klass) +{ + char *p; + return TypedData_Make_Struct(klass, char, &test_data, p); +} + +static VALUE test_check(VALUE self, VALUE obj) { rb_check_typeddata(obj, &test_data); return obj; } +static VALUE +test_make(VALUE klass, VALUE num) +{ + unsigned long i, n = NUM2UINT(num); + + for (i = 0; i < n; i++) { + test_alloc(klass); + } + + return Qnil; +} + void Init_typeddata(void) { VALUE mBug = rb_define_module("Bug"); VALUE klass = rb_define_class_under(mBug, "TypedData", rb_cData); + rb_define_alloc_func(klass, test_alloc); rb_define_singleton_method(klass, "check", test_check, 1); + rb_define_singleton_method(klass, "make", test_make, 1); } @@ -1358,6 +1358,29 @@ rb_objspace_free(rb_objspace_t *objspace) } static void +heap_pages_expand_sorted_to(rb_objspace_t *objspace, size_t next_length) +{ + struct heap_page **sorted; + size_t size = next_length * sizeof(struct heap_page *); + + gc_report(3, objspace, "heap_pages_expand_sorted: next_length: %d, size: %d\n", (int)next_length, (int)size); + + if (heap_pages_sorted_length > 0) { + sorted = (struct heap_page **)realloc(heap_pages_sorted, size); + if (sorted) heap_pages_sorted = sorted; + } + else { + sorted = heap_pages_sorted = (struct heap_page **)malloc(size); + } + + if (sorted == 0) { + rb_memerror(); + } + + heap_pages_sorted_length = next_length; +} + +static void heap_pages_expand_sorted(rb_objspace_t *objspace) { size_t next_length = heap_allocatable_pages; @@ -1365,24 +1388,7 @@ heap_pages_expand_sorted(rb_objspace_t *objspace) next_length += heap_tomb->total_pages; if (next_length > heap_pages_sorted_length) { - struct heap_page **sorted; - size_t size = next_length * sizeof(struct heap_page *); - - gc_report(3, objspace, "heap_pages_expand_sorted: next_length: %d, size: %d\n", (int)next_length, (int)size); - - if (heap_pages_sorted_length > 0) { - sorted = (struct heap_page **)realloc(heap_pages_sorted, size); - if (sorted) heap_pages_sorted = sorted; - } - else { - sorted = heap_pages_sorted = (struct heap_page **)malloc(size); - } - - if (sorted == 0) { - rb_memerror(); - } - - heap_pages_sorted_length = next_length; + heap_pages_expand_sorted_to(objspace, next_length); } } @@ -1520,6 +1526,9 @@ heap_page_allocate(rb_objspace_t *objspace) rb_bug("same heap page is allocated: %p at %"PRIuVALUE, (void *)page_body, (VALUE)mid); } } + if (heap_allocated_pages >= heap_pages_sorted_length) { + heap_pages_expand_sorted_to(objspace, heap_allocated_pages + 1); + } if (hi < heap_allocated_pages) { MEMMOVE(&heap_pages_sorted[hi+1], &heap_pages_sorted[hi], struct heap_page_header*, heap_allocated_pages - hi); } @@ -1529,7 +1538,10 @@ heap_page_allocate(rb_objspace_t *objspace) heap_allocated_pages++; objspace->profile.total_allocated_pages++; - if (RGENGC_CHECK_MODE) assert(heap_allocated_pages <= heap_pages_sorted_length); + if (heap_allocated_pages > heap_pages_sorted_length) { + rb_bug("heap_page_allocate: allocated(%"PRIdSIZE") > sorted(%"PRIdSIZE")", + heap_allocated_pages, heap_pages_sorted_length); + } if (heap_pages_lomem == 0 || heap_pages_lomem > start) heap_pages_lomem = start; if (heap_pages_himem < end) heap_pages_himem = end; diff --git a/test/-ext-/typeddata/test_typeddata.rb b/test/-ext-/typeddata/test_typeddata.rb index daeda7611a..a8a7c965c9 100644 --- a/test/-ext-/typeddata/test_typeddata.rb +++ b/test/-ext-/typeddata/test_typeddata.rb @@ -17,4 +17,15 @@ class Test_TypedData < Test::Unit::TestCase obj = eval("class C\u{1f5ff}; self; end").new assert_raise_with_message(TypeError, /C\u{1f5ff}/) {Bug::TypedData.check(obj)} end + + def test_deferred_free + assert_ruby_status([], "#{<<-"begin;"}\n#{<<-"end;"}") + require "-test-/typeddata" + begin; + n = 1 << 20 + Bug::TypedData.make(n) + end; + rescue MiniTest::Assertion => e + skip e.message + end end @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.2" #define RUBY_RELEASE_DATE "2017-07-10" -#define RUBY_PATCHLEVEL 142 +#define RUBY_PATCHLEVEL 143 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 7 |