summaryrefslogtreecommitdiff
path: root/io_buffer.c
AgeCommit message (Collapse)Author
2025-12-17Rename to `struct rbimpl_size_overflow_tag`Nobuyoshi Nakada
This struct is used for addition not only for multiplication, so remove the word `mul`, and make the member names more descriptive.
2025-12-17IO::Buffer: Warn as experimental at allocationNobuyoshi Nakada
Previously, warned only in `new` and `map`, but not `for` and `string`.
2025-12-17[Bug #21787] IO::Buffer: Check addition overflowsNobuyoshi Nakada
https://hackerone.com/reports/3437743
2025-12-17IO::Buffer: Guard arguments from GCNobuyoshi Nakada
At least, `string` in `io_buffer_set_string` can be different from `argv[0]` after `rb_str_to_str` call. The other cases may not be necessary.
2025-12-08Fix strict aliasing warning in rb_int128_to_numericPeter Zhu
If we don't have uint128, then rb_int128_to_numeric emits a strict aliasing warning: numeric.c:3641:39: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 3641 | return rb_uint128_to_numeric(*(rb_uint128_t*)&n); | ^~~~~~~~~~~~~~~~~
2025-12-06Fix strict aliasing warning in ruby_swap128_intPeter Zhu
The following warnings are emitted. We can use type punning to prevent strict aliasing violations. io_buffer.c:1935:23: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 1935 | rb_uint128_t u = *(rb_uint128_t*)&x; | ^~~~~~~~~~~~~~~~~ io_buffer.c:1937:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 1937 | return *(rb_int128_t*)&swapped; |
2025-12-06Add support for `u128`, `U128`, `s128` and `S128` integers to `IO::Buffer`. ↵Samuel Williams
(#15399)
2025-11-27Fix argument handling in `IO::Buffer#each_byte` (#15309)TOMITA Masahiro
The method incorrectly ignored its first argument and treated the second argument as offset and the third as count. This change makes the first argument be treated as offset and the second as count. Also fix incorrect block parameter in comments.
2025-11-21Fix multiple bugs in `IO::Buffer.map` and update its documentation. (#15264)Alexander Bulancov
- Buffer's size did not account for offset when mapping the file, leading to possible crashes. - Size and offset were not checked properly, leading to many situations raising EINVAL errors with generic messages. - Documentation was wrong.
2025-10-02Don't call gc_mark from IO::buffer compactJohn Hawthorn
Previously on our mark_and_move we were calling rb_gc_mark, which isn't safe to call at compaction time. Co-authored-by: Luke Gruber <luke.gru@gmail.com>
2025-06-17io_buffer: Reimplement dcompact for IO::BufferKasumi Hanazuki
The `source` field in IO::Buffer can have a String or an IO::Buffer object, if not nil. - When the `source` is a String object. The `base` field points to the memory location of the String content, which can be embedded in RSTRING, and in that case, GC compaction can move the memory region along with the String object. Thus, IO::Buffer needs to pin the `source` object to prevent `base` pointer from becoming invalid. - When the `source` is an IO::Buffer, then `base` is a pointer to a malloced or mmapped memory region, managed by the source IO::Buffer. In this case, we don't need to pin the source IO::Buffer object, since the referred memory region won't get moved by GC. Closes: [Bug #21210] Notes: Merged: https://github.com/ruby/ruby/pull/13033
2025-06-17Revert "Mark rb_io_buffer_type references declaratively"Kasumi Hanazuki
This reverts commit 6012145299cfa4ab561360c78710c7f2941a7e9d. Notes: Merged: https://github.com/ruby/ruby/pull/13033
2025-06-16Add test for `IO::Buffer.for(frozen_string) {}` and omit rb_str_{,un}locktmp ↵Benoit Daloze
in that case Notes: Merged: https://github.com/ruby/ruby/pull/13615
2025-05-23Allow `IO#close` to interrupt IO operations on fibers using ↵Samuel Williams
`fiber_interrupt` hook. (#12839) Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2025-03-31Make free, memsize functions of IO::Buffer staticPeter Zhu
They are not used outside of io_buffer.c, so we can make them static. Notes: Merged: https://github.com/ruby/ruby/pull/13022
2025-03-31Mark rb_io_buffer_type references declarativelyMatt Valentine-House
allowing them to support compaction and be moved. Notes: Merged: https://github.com/ruby/ruby/pull/13018
2024-12-09[Bug #20933] Fix IO::Buffer overlap calculationPeter Zhu
The allocated buffers may be consecutive memory addresses. This will mean that `b->base == a->base + a->size` even though `a` and `b` are separate buffers. Notes: Merged: https://github.com/ruby/ruby/pull/12284
2024-11-21Annotate anonymous mmapKunshan Wang
Use PR_SET_VMA_ANON_NAME to set human-readable names for anonymous virtual memory areas mapped by `mmap()` when compiled and run on Linux 5.17 or higher. This makes it convenient for developers to debug mmap. Notes: Merged: https://github.com/ruby/ruby/pull/12119
2024-11-20Allow `io_buffer_memmove` to release the GVL for large buffers. (#12021)Samuel Williams
[Feature #20902] Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2024-11-06io_buffer.c: Allow copies between overlapping buffers with #copy and ↵Kasumi Hanazuki
#set_string (#11640) The current implementation of `IO::Buffer#copy` and `#set_string` has an undefined behavior when the source and destination memory overlaps, due to the underlying use of the `memcpy` C function. This patch guarantees the methods to be safe even when copying between overlapping buffers by replacing `memcpy` with `memmove`, Fixes: [Bug #20745] Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2024-10-05Introduce `rb_io_blocking_region` which takes `struct rb_io` argument. (#11795)Samuel Williams
This does not change any actual behaviour, but provides a choke point for blocking IO operations. * Update `IO::Buffer` to use `rb_io_blocking_region`. * Update `File` to use `rb_io_blocking_region`. * Update `IO` to use `rb_io_blocking_region`. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2024-10-03Update `IO::Buffer` documentation. (#11737)Samuel Williams
Update documentation. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2024-10-01[Bug #20755] Frozen string should not be writable via IO::BufferNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/11738
2024-09-30[Bug #20752] Slice of readonly `IO::Buffer` also should be readonlyNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/11735
2024-05-19Fix comment: Buffer.for(string) without block returns readonly bufferTOMITA Masahiro
2024-05-19Fix IO::Buffer commentTOMITA Masahiro
2024-05-09Fix format specifier for `DWORD`Nobuyoshi Nakada
2024-01-15Improve behavioural consistency of unallocated (zero length) `IO::Buffer`. ↵Samuel Williams
(#9532) This makes the behaviour of IO::Buffer.new(0) and IO::Buffer.new.slice(0, 0) consistent. Fixes https://bugs.ruby-lang.org/issues/19542 and https://bugs.ruby-lang.org/issues/18805.
2023-12-27Fix Window private file mapping unlink EACCES issue. (#9358)Samuel Williams
* Don't return early. * Add missing `mapping` assignment. * Make debug logs conditional.
2023-12-25Correctly release the underlying file mapping. (#9340)Samuel Williams
* Avoiding using `Tempfile` which was retaining the file preventing it from unlinking.
2023-12-25IO::Buffer improvements and documentation. (#9329)Samuel Williams
* Restore experimental warnings. * Documentation and code structure improvements. * Improved validation of flags, clarified documentation of argument handling. * Remove inconsistent use of `Example:` and add example to `null?`. * Expose `IO::Buffer#private?` and add test.
2023-12-19Implement IO::Buffer on VWAPeter Zhu
2023-12-18[DOC] Add document of IO::Buffer#hexdumpNobuyoshi Nakada
2023-12-14Implement Write Barriers on IO::BufferPeter Zhu
Benchmark: ``` require "benchmark" puts(Benchmark.measure do ary = 1_000_000.times.map { IO::Buffer.for("") } 10.times { GC.start(full_mark: false) } end) ``` Before: ``` 14.330119 0.051497 14.381616 ( 14.445106) ``` After: ``` 7.481152 0.040166 7.521318 ( 7.535209) ```
2023-12-12Use xfree for IO::BufferPeter Zhu
Since IO::Buffer is allocated using TypedData_Make_Struct, it must use xfree to free the buffer otherwise it will cause more major GC to run. Example: ``` 10.times do 1_000_000.times { IO::Buffer.new(0) } puts "oldmalloc_increase_bytes: #{GC.stat(:oldmalloc_increase_bytes)}, major_gc_count: #{GC.stat(:major_gc_count)}" end ``` Before: ``` oldmalloc_increase_bytes: 14904176, major_gc_count: 3 oldmalloc_increase_bytes: 2399424, major_gc_count: 5 oldmalloc_increase_bytes: 5204640, major_gc_count: 6 oldmalloc_increase_bytes: 2199936, major_gc_count: 7 oldmalloc_increase_bytes: 34199936, major_gc_count: 7 oldmalloc_increase_bytes: 24223360, major_gc_count: 8 oldmalloc_increase_bytes: 5967616, major_gc_count: 9 oldmalloc_increase_bytes: 37967616, major_gc_count: 9 oldmalloc_increase_bytes: 9689792, major_gc_count: 10 oldmalloc_increase_bytes: 41689792, major_gc_count: 10 ``` After: ``` oldmalloc_increase_bytes: 117392, major_gc_count: 2 oldmalloc_increase_bytes: 26128, major_gc_count: 2 oldmalloc_increase_bytes: 71600, major_gc_count: 2 oldmalloc_increase_bytes: 117072, major_gc_count: 2 oldmalloc_increase_bytes: 17296, major_gc_count: 2 oldmalloc_increase_bytes: 62768, major_gc_count: 2 oldmalloc_increase_bytes: 108240, major_gc_count: 2 oldmalloc_increase_bytes: 153712, major_gc_count: 2 oldmalloc_increase_bytes: 53936, major_gc_count: 2 oldmalloc_increase_bytes: 99408, major_gc_count: 2 ```
2023-12-06Don't warn generally for `IO::Buffer`, only on specific code paths e.g. ↵Samuel Williams
`map`. (#9131)
2023-09-14Fix `io_buffer_get_string` default length computation. (#8427)Samuel Williams
* Fix `io_buffer_get_string` default length computation. When an offset bigger than the size is given, the resulting length will be computed incorrectly. Raise an argument error in this case. * Validate all arguments. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-09-13[Bug #19754] Make `IO::Buffer#get_string` check `offset` range (#8016)Nobuyoshi Nakada
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-08-29[DOC] Improved documentation. (#8319)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-05-27Improve `read`/`write`/`pread`/`pwrite` consistency. (#7860)Samuel Williams
* Documentation consistency. * Improve consistency of `pread`/`pwrite` implementation when given length. * Remove HAVE_PREAD / HAVE_PWRITE - it is no longer optional. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-05-24Improvements to `IO::Buffer` `read`/`write`/`pread`/`pwrite`. (#7826)Samuel Williams
- Fix IO::Buffer `read`/`write` to use a minimum length. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-05-22Fix mutation on shared strings. (#7837)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-05-22Rename `data` -> `buffer` for better readability. (#7836)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-31Support `IO#pread` / `IO#pwrite` using fiber scheduler. (#7594)Samuel Williams
* Skip test if non-blocking file IO is not supported. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-25Fix incorrect usage of `rb_fiber_scheduler_io_(p)(read|write)`. (#7593)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-25IO::Buffer#resize: Free internal buffer if new size is zero (#7569)Kasumi Hanazuki
`#resize(0)` on an IO::Buffer with internal buffer allocated will result in calling `realloc(data->base, 0)`. The behavior of `realloc` with size = 0 is implementation-defined (glibc frees the object and returns NULL, while BSDs return an inaccessible object). And thus such usage is deprecated in standard C (upcoming C23 will make it UB). To avoid this problem, just `free`s the memory when the new size is zero. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-02-27Fix spelling (#7389)John Bampton
Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2023-02-26Adjust `else` style to be consistent in each files [ci skip]Nobuyoshi Nakada
2023-02-25Prefer RB_NUM2LONG for string length. (#7379)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-02-25Add `IO::Buffer.string` for efficient string creation. (#7364)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>