Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
IO::Buffer::Storage.new
VALUE
rb_io_buffer_initialize(int argc, VALUE *argv, VALUE self)
{
rb_check_frozen(self);
rb_check_arity(argc, 0, 2);
size_t size;
if (argc > 0) {
size = io_buffer_extract_size(argv[0]);
}
else {
size = RUBY_IO_BUFFER_DEFAULT_SIZE;
}
enum rb_io_buffer_flags flags = 0;
if (argc >= 2) {
flags = io_buffer_extract_flags(argv[1]);
}
flags = io_buffer_flags_for_new(flags, size);
struct rb_io_buffer_storage *buffer = get_io_buffer_storage(self);
if (buffer->lock_count) rb_raise(rb_eIOBufferLockedError, "Cannot initialize locked buffer!");
io_buffer_storage_release(buffer);
io_buffer_storage_initialize(self, buffer, NULL, size, flags, Qnil);
return self;
}
Create a new zero-filled IO::Buffer of size bytes. By default, the buffer will be internal: directly allocated chunk of the memory. But if the requested size is more than OS-specific IO::Buffer::PAGE_SIZE, the buffer would be allocated using the virtual memory mechanism (anonymous mmap on Unix, VirtualAlloc on Windows). The behavior can be forced by passing IO::Buffer::MAPPED as a second parameter.
IO::Buffer::SHARED and IO::Buffer::PRIVATE imply IO::Buffer::MAPPED and are mutually exclusive. Otherwise, if flags do not include an allocation mode, IO::Buffer::INTERNAL or IO::Buffer::MAPPED is inferred from the requested size. The two allocation modes are mutually exclusive.
buffer = IO::Buffer.new(4) # => # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL> # 0x00000000 00 00 00 00 .... buffer.get_string(0, 1) # => "\x00" buffer.set_string("test") buffer # => # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL> # 0x00000000 74 65 73 74 test