diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2021-10-22 15:05:00 +1300 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2021-11-12 16:46:08 +1300 |
commit | c833ece5f78b8c2e43263e08ccbd3ce1628bf610 (patch) | |
tree | f813cd893a32991422cd5e833b09236dd897bced /io_buffer.c | |
parent | 98b442e013afbb450f1c946d86ed625c39ab3233 (diff) |
Rework implementation of `IO::Buffer.for(string)` to use string locking.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5109
Diffstat (limited to 'io_buffer.c')
-rw-r--r-- | io_buffer.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/io_buffer.c b/io_buffer.c index 1acc942987..9b8f85ed4c 100644 --- a/io_buffer.c +++ b/io_buffer.c @@ -182,6 +182,10 @@ io_buffer_free(struct rb_io_buffer *data) io_buffer_unmap(data->base, data->size); } + if (RB_TYPE_P(data->source, T_STRING)) { + rb_str_unlocktmp(data->source); + } + data->base = NULL; #if defined(_WIN32) @@ -253,6 +257,21 @@ rb_io_buffer_type_allocate(VALUE self) } VALUE +rb_io_buffer_type_for(VALUE klass, VALUE string) +{ + VALUE instance = rb_io_buffer_type_allocate(klass); + + struct rb_io_buffer *data = NULL; + TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, data); + + rb_str_locktmp(string); + + io_buffer_initialize(data, RSTRING_PTR(string), RSTRING_LEN(string), RB_IO_BUFFER_EXTERNAL, string); + + return instance; +} + +VALUE rb_io_buffer_new(void *base, size_t size, enum rb_io_buffer_flags flags) { VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer); @@ -1029,6 +1048,7 @@ Init_IO_Buffer(void) rb_cIOBuffer = rb_define_class_under(rb_cIO, "Buffer", rb_cObject); rb_define_alloc_func(rb_cIOBuffer, rb_io_buffer_type_allocate); + rb_define_singleton_method(rb_cIOBuffer, "for", rb_io_buffer_type_for, 1); #ifdef _WIN32 SYSTEM_INFO info; |