Actions
Misc #22506
openClarify read-only, lock and freeze in IO::Buffer
Misc #22506:
Clarify read-only, lock and freeze in IO::Buffer
Status:
Open
Assignee:
-
Description
Following https://bugs.ruby-lang.org/issues/22274#note-16, I'd like to agree on what each of these means for IO::Buffer, so that the remaining fixes follow one model. This is a proposal, and I'd appreciate corrections.
| What it prevents | Duration | |
|---|---|---|
| read-only | writes through this buffer | set at creation |
| lock | freeing, resizing or transferring the memory | while a native operation borrows it |
| freeze | any change made through this buffer, including writes to its bytes | permanent |
None of them promises that the bytes cannot change through another view or another process.
Proposed rules:
- A frozen buffer refuses writes (
FrozenError), and a read-only one refuses writes (AccessError), like a frozenFileand aFileopened with "r". readonly?and theREADONLYflag fromrb_io_buffer_get_bytesreport whether the memory may be written through this buffer, not whether the buffer is frozen. A frozen source makes its slices read-only, but freezing a buffer does not make the buffer itself read-only, so C code should write throughrb_io_buffer_get_bytes_for_writing, which checks both.- A frozen buffer cannot be freed, resized or transferred, so locking it is unnecessary and can be a no-op.
- A locked buffer cannot be frozen (https://bugs.ruby-lang.org/issues/22274#note-10), like a temporarily locked String.
Open questions:
- The owner of borrowed memory (a buffer from
rb_io_buffer_newwithout a source) has to invalidate the buffer before reclaiming the memory, even if the code it was lent to has frozen or locked it. Should invalidation by the owner be an exception to the rules above, or should such a buffer refuse to be frozen? - With this model, a frozen buffer is no longer writable. If a buffer that stays writable while its memory is guaranteed to stay accessible is needed beyond what holding a lock provides, I wouldn't object to expressing that state with a separate name such as
#pin(https://bugs.ruby-lang.org/issues/22274#note-18). @ioquatix (Samuel Williams), is there a reason or use case for expressing it withfreezeinstead?
Actions