Ruby: io_buffer.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

io_buffer.c
1/**********************************************************************
2
3 io_buffer.c
4
5 Copyright (C) 2021 Samuel Grant Dawson Williams
6
7**********************************************************************/
8
9#include "ruby/io/buffer.h"
10#include "ruby/fiber/scheduler.h"
11
12// For `rb_nogvl`.
13#include "ruby/thread.h"
14
15#include "internal.h"
16#include "internal/array.h"
17#include "internal/bits.h"
18#include "internal/error.h"
19#include "internal/gc.h"
20#include "internal/numeric.h"
21#include "internal/string.h"
22#include "internal/io.h"
23
24 VALUE rb_cIOBuffer;
25 VALUE rb_eIOBufferLockedError;
26 VALUE rb_eIOBufferAllocationError;
27 VALUE rb_eIOBufferAccessError;
28 VALUE rb_eIOBufferInvalidatedError;
29 VALUE rb_eIOBufferMaskError;
30
31size_t RUBY_IO_BUFFER_PAGE_SIZE;
32size_t RUBY_IO_BUFFER_DEFAULT_SIZE;
33
34#ifdef _WIN32
35#else
36#include <unistd.h>
37#include <sys/mman.h>
38#endif
39
40enum {
41 RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH = 16,
42
43 RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE = 256,
44 RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH = 16,
45
46 // This is used to validate the flags given by the user.
47 RB_IO_BUFFER_FLAGS_MASK = RB_IO_BUFFER_EXTERNAL | RB_IO_BUFFER_INTERNAL | RB_IO_BUFFER_MAPPED | RB_IO_BUFFER_SHARED | RB_IO_BUFFER_LOCKED | RB_IO_BUFFER_PRIVATE | RB_IO_BUFFER_READONLY,
48
49 RB_IO_BUFFER_DEBUG = 0,
50};
51
52 struct rb_io_buffer {
53 void *base;
54 size_t size;
55 enum rb_io_buffer_flags flags;
56
57#if defined(_WIN32)
58 HANDLE mapping;
59#endif
60
61 VALUE source;
62};
63
64static inline void *
65io_buffer_map_memory(size_t size, int flags)
66{
67#if defined(_WIN32)
68 void * base = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
69
70 if (!base) {
71 rb_sys_fail("io_buffer_map_memory:VirtualAlloc");
72 }
73#else
74 int mmap_flags = MAP_ANONYMOUS;
75 if (flags & RB_IO_BUFFER_SHARED) {
76 mmap_flags |= MAP_SHARED;
77 }
78 else {
79 mmap_flags |= MAP_PRIVATE;
80 }
81
82 void * base = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
83
84 if (base == MAP_FAILED) {
85 rb_sys_fail("io_buffer_map_memory:mmap");
86 }
87
88 ruby_annotate_mmap(base, size, "Ruby:io_buffer_map_memory");
89#endif
90
91 return base;
92}
93
94static void
95io_buffer_map_file(struct rb_io_buffer *buffer, int descriptor, size_t size, rb_off_t offset, enum rb_io_buffer_flags flags)
96{
97#if defined(_WIN32)
98 HANDLE file = (HANDLE)_get_osfhandle(descriptor);
99 if (!file) rb_sys_fail("io_buffer_map_descriptor:_get_osfhandle");
100
101 DWORD protect = PAGE_READONLY, access = FILE_MAP_READ;
102
103 if (flags & RB_IO_BUFFER_READONLY) {
104 buffer->flags |= RB_IO_BUFFER_READONLY;
105 }
106 else {
107 protect = PAGE_READWRITE;
108 access = FILE_MAP_WRITE;
109 }
110
111 if (flags & RB_IO_BUFFER_PRIVATE) {
112 protect = PAGE_WRITECOPY;
113 access = FILE_MAP_COPY;
114 buffer->flags |= RB_IO_BUFFER_PRIVATE;
115 }
116 else {
117 // This buffer refers to external buffer.
118 buffer->flags |= RB_IO_BUFFER_EXTERNAL;
119 buffer->flags |= RB_IO_BUFFER_SHARED;
120 }
121
122 HANDLE mapping = CreateFileMapping(file, NULL, protect, 0, 0, NULL);
123 if (RB_IO_BUFFER_DEBUG) fprintf(stderr, "io_buffer_map_file:CreateFileMapping -> %p\n", mapping);
124 if (!mapping) rb_sys_fail("io_buffer_map_descriptor:CreateFileMapping");
125
126 void *base = MapViewOfFile(mapping, access, (DWORD)(offset >> 32), (DWORD)(offset & 0xFFFFFFFF), size);
127
128 if (!base) {
129 CloseHandle(mapping);
130 rb_sys_fail("io_buffer_map_file:MapViewOfFile");
131 }
132
133 buffer->mapping = mapping;
134#else
135 int protect = PROT_READ, access = 0;
136
137 if (flags & RB_IO_BUFFER_READONLY) {
138 buffer->flags |= RB_IO_BUFFER_READONLY;
139 }
140 else {
141 protect |= PROT_WRITE;
142 }
143
144 if (flags & RB_IO_BUFFER_PRIVATE) {
145 buffer->flags |= RB_IO_BUFFER_PRIVATE;
146 access |= MAP_PRIVATE;
147 }
148 else {
149 // This buffer refers to external buffer.
150 buffer->flags |= RB_IO_BUFFER_EXTERNAL;
151 buffer->flags |= RB_IO_BUFFER_SHARED;
152 access |= MAP_SHARED;
153 }
154
155 void *base = mmap(NULL, size, protect, access, descriptor, offset);
156
157 if (base == MAP_FAILED) {
158 rb_sys_fail("io_buffer_map_file:mmap");
159 }
160#endif
161
162 buffer->base = base;
163 buffer->size = size;
164
165 buffer->flags |= RB_IO_BUFFER_MAPPED;
166 buffer->flags |= RB_IO_BUFFER_FILE;
167}
168
169static void
170io_buffer_experimental(void)
171{
172 static int warned = 0;
173
174 if (warned) return;
175
176 warned = 1;
177
178 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL)) {
179 rb_category_warn(RB_WARN_CATEGORY_EXPERIMENTAL,
180 "IO::Buffer is experimental and both the Ruby and C interface may change in the future!"
181 );
182 }
183}
184
185static void
186io_buffer_zero(struct rb_io_buffer *buffer)
187{
188 buffer->base = NULL;
189 buffer->size = 0;
190#if defined(_WIN32)
191 buffer->mapping = NULL;
192#endif
193 buffer->source = Qnil;
194}
195
196static void
197io_buffer_initialize(VALUE self, struct rb_io_buffer *buffer, void *base, size_t size, enum rb_io_buffer_flags flags, VALUE source)
198{
199 if (base) {
200 // If we are provided a pointer, we use it.
201 }
202 else if (size) {
203 // If we are provided a non-zero size, we allocate it:
204 if (flags & RB_IO_BUFFER_INTERNAL) {
205 base = calloc(size, 1);
206 }
207 else if (flags & RB_IO_BUFFER_MAPPED) {
208 base = io_buffer_map_memory(size, flags);
209 }
210
211 if (!base) {
212 rb_raise(rb_eIOBufferAllocationError, "Could not allocate buffer!");
213 }
214 }
215 else {
216 // Otherwise we don't do anything.
217 return;
218 }
219
220 buffer->base = base;
221 buffer->size = size;
222 buffer->flags = flags;
223 RB_OBJ_WRITE(self, &buffer->source, source);
224
225#if defined(_WIN32)
226 buffer->mapping = NULL;
227#endif
228}
229
230static void
231io_buffer_free(struct rb_io_buffer *buffer)
232{
233 if (buffer->base) {
234 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
235 free(buffer->base);
236 }
237
238 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
239#ifdef _WIN32
240 if (buffer->flags & RB_IO_BUFFER_FILE) {
241 UnmapViewOfFile(buffer->base);
242 }
243 else {
244 VirtualFree(buffer->base, 0, MEM_RELEASE);
245 }
246#else
247 munmap(buffer->base, buffer->size);
248#endif
249 }
250
251 // Previously we had this, but we found out due to the way GC works, we
252 // can't refer to any other Ruby objects here.
253 // if (RB_TYPE_P(buffer->source, T_STRING)) {
254 // rb_str_unlocktmp(buffer->source);
255 // }
256
257 buffer->base = NULL;
258
259 buffer->size = 0;
260 buffer->flags = 0;
261 buffer->source = Qnil;
262 }
263
264#if defined(_WIN32)
265 if (buffer->mapping) {
266 if (RB_IO_BUFFER_DEBUG) fprintf(stderr, "io_buffer_free:CloseHandle -> %p\n", buffer->mapping);
267 if (!CloseHandle(buffer->mapping)) {
268 fprintf(stderr, "io_buffer_free:GetLastError -> %lu\n", GetLastError());
269 }
270 buffer->mapping = NULL;
271 }
272#endif
273}
274
275static void
276rb_io_buffer_type_mark(void *_buffer)
277{
278 struct rb_io_buffer *buffer = _buffer;
279 if (buffer->source != Qnil) {
280 if (RB_TYPE_P(buffer->source, T_STRING)) {
281 // The `source` String has to be pinned, because the `base` may point to the embedded String content,
282 // which can be otherwise moved by GC compaction.
283 rb_gc_mark(buffer->source);
284 } else {
285 rb_gc_mark_movable(buffer->source);
286 }
287 }
288}
289
290static void
291rb_io_buffer_type_compact(void *_buffer)
292{
293 struct rb_io_buffer *buffer = _buffer;
294 if (buffer->source != Qnil) {
295 if (RB_TYPE_P(buffer->source, T_STRING)) {
296 // The `source` String has to be pinned, because the `base` may point to the embedded String content,
297 // which can be otherwise moved by GC compaction.
298 } else {
299 buffer->source = rb_gc_location(buffer->source);
300 }
301 }
302}
303
304static void
305rb_io_buffer_type_free(void *_buffer)
306{
307 struct rb_io_buffer *buffer = _buffer;
308
309 io_buffer_free(buffer);
310}
311
312static size_t
313rb_io_buffer_type_size(const void *_buffer)
314{
315 const struct rb_io_buffer *buffer = _buffer;
316 size_t total = sizeof(struct rb_io_buffer);
317
318 if (buffer->flags) {
319 total += buffer->size;
320 }
321
322 return total;
323}
324
325static const rb_data_type_t rb_io_buffer_type = {
326 .wrap_struct_name = "IO::Buffer",
327 .function = {
328 .dmark = rb_io_buffer_type_mark,
329 .dfree = rb_io_buffer_type_free,
330 .dsize = rb_io_buffer_type_size,
331 .dcompact = rb_io_buffer_type_compact,
332 },
333 .data = NULL,
334 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
335};
336
337static inline enum rb_io_buffer_flags
338io_buffer_extract_flags(VALUE argument)
339{
340 if (rb_int_negative_p(argument)) {
341 rb_raise(rb_eArgError, "Flags can't be negative!");
342 }
343
344 enum rb_io_buffer_flags flags = RB_NUM2UINT(argument);
345
346 // We deliberately ignore unknown flags. Any future flags which are exposed this way should be safe to ignore.
347 return flags & RB_IO_BUFFER_FLAGS_MASK;
348}
349
350// Extract an offset argument, which must be a non-negative integer.
351static inline size_t
352io_buffer_extract_offset(VALUE argument)
353{
354 if (rb_int_negative_p(argument)) {
355 rb_raise(rb_eArgError, "Offset can't be negative!");
356 }
357
358 return NUM2SIZET(argument);
359}
360
361// Extract a length argument, which must be a non-negative integer.
362// Length is generally considered a mutable property of an object and
363// semantically should be considered a subset of "size" as a concept.
364static inline size_t
365io_buffer_extract_length(VALUE argument)
366{
367 if (rb_int_negative_p(argument)) {
368 rb_raise(rb_eArgError, "Length can't be negative!");
369 }
370
371 return NUM2SIZET(argument);
372}
373
374// Extract a size argument, which must be a non-negative integer.
375// Size is generally considered an immutable property of an object.
376static inline size_t
377io_buffer_extract_size(VALUE argument)
378{
379 if (rb_int_negative_p(argument)) {
380 rb_raise(rb_eArgError, "Size can't be negative!");
381 }
382
383 return NUM2SIZET(argument);
384}
385
386// Extract a width argument, which must be a non-negative integer, and must be
387// at least the given minimum.
388static inline size_t
389io_buffer_extract_width(VALUE argument, size_t minimum)
390{
391 if (rb_int_negative_p(argument)) {
392 rb_raise(rb_eArgError, "Width can't be negative!");
393 }
394
395 size_t width = NUM2SIZET(argument);
396
397 if (width < minimum) {
398 rb_raise(rb_eArgError, "Width must be at least %" PRIuSIZE "!", minimum);
399 }
400
401 return width;
402}
403
404// Compute the default length for a buffer, given an offset into that buffer.
405// The default length is the size of the buffer minus the offset. The offset
406// must be less than the size of the buffer otherwise the length will be
407// invalid; in that case, an ArgumentError exception will be raised.
408static inline size_t
409io_buffer_default_length(const struct rb_io_buffer *buffer, size_t offset)
410{
411 if (offset > buffer->size) {
412 rb_raise(rb_eArgError, "The given offset is bigger than the buffer size!");
413 }
414
415 // Note that the "length" is computed by the size the offset.
416 return buffer->size - offset;
417}
418
419// Extract the optional length and offset arguments, returning the buffer.
420// The length and offset are optional, but if they are provided, they must be
421// positive integers. If the length is not provided, the default length is
422// computed from the buffer size and offset. If the offset is not provided, it
423// defaults to zero.
424static inline struct rb_io_buffer *
425io_buffer_extract_length_offset(VALUE self, int argc, VALUE argv[], size_t *length, size_t *offset)
426{
427 struct rb_io_buffer *buffer = NULL;
428 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
429
430 if (argc >= 2 && !NIL_P(argv[1])) {
431 *offset = io_buffer_extract_offset(argv[1]);
432 }
433 else {
434 *offset = 0;
435 }
436
437 if (argc >= 1 && !NIL_P(argv[0])) {
438 *length = io_buffer_extract_length(argv[0]);
439 }
440 else {
441 *length = io_buffer_default_length(buffer, *offset);
442 }
443
444 return buffer;
445}
446
447// Extract the optional offset and length arguments, returning the buffer.
448// Similar to `io_buffer_extract_length_offset` but with the order of arguments
449// reversed.
450//
451// After much consideration, I decided to accept both forms.
452// The `(offset, length)` order is more natural when referring about data,
453// while the `(length, offset)` order is more natural when referring to
454// read/write operations. In many cases, with the latter form, `offset`
455// is usually not supplied.
456static inline struct rb_io_buffer *
457io_buffer_extract_offset_length(VALUE self, int argc, VALUE argv[], size_t *offset, size_t *length)
458{
459 struct rb_io_buffer *buffer = NULL;
460 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
461
462 if (argc >= 1 && !NIL_P(argv[0])) {
463 *offset = io_buffer_extract_offset(argv[0]);
464 }
465 else {
466 *offset = 0;
467 }
468
469 if (argc >= 2 && !NIL_P(argv[1])) {
470 *length = io_buffer_extract_length(argv[1]);
471 }
472 else {
473 *length = io_buffer_default_length(buffer, *offset);
474 }
475
476 return buffer;
477}
478
479 VALUE
480rb_io_buffer_type_allocate(VALUE self)
481{
482 io_buffer_experimental();
483
484 struct rb_io_buffer *buffer = NULL;
485 VALUE instance = TypedData_Make_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
486
487 io_buffer_zero(buffer);
488
489 return instance;
490}
491
492static VALUE io_buffer_for_make_instance(VALUE klass, VALUE string, enum rb_io_buffer_flags flags)
493{
494 VALUE instance = rb_io_buffer_type_allocate(klass);
495
496 struct rb_io_buffer *buffer = NULL;
497 TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, buffer);
498
499 flags |= RB_IO_BUFFER_EXTERNAL;
500
501 if (RB_OBJ_FROZEN(string))
502 flags |= RB_IO_BUFFER_READONLY;
503
504 if (!(flags & RB_IO_BUFFER_READONLY))
505 rb_str_modify(string);
506
507 io_buffer_initialize(instance, buffer, RSTRING_PTR(string), RSTRING_LEN(string), flags, string);
508
509 return instance;
510}
511
512 struct io_buffer_for_yield_instance_arguments {
513 VALUE klass;
514 VALUE string;
515 VALUE instance;
516 enum rb_io_buffer_flags flags;
517};
518
519static VALUE
520io_buffer_for_yield_instance(VALUE _arguments)
521{
522 struct io_buffer_for_yield_instance_arguments *arguments = (struct io_buffer_for_yield_instance_arguments *)_arguments;
523
524 arguments->instance = io_buffer_for_make_instance(arguments->klass, arguments->string, arguments->flags);
525
526 if (!RB_OBJ_FROZEN(arguments->string)) {
527 rb_str_locktmp(arguments->string);
528 }
529
530 return rb_yield(arguments->instance);
531}
532
533static VALUE
534io_buffer_for_yield_instance_ensure(VALUE _arguments)
535{
536 struct io_buffer_for_yield_instance_arguments *arguments = (struct io_buffer_for_yield_instance_arguments *)_arguments;
537
538 if (arguments->instance != Qnil) {
539 rb_io_buffer_free(arguments->instance);
540 }
541
542 if (!RB_OBJ_FROZEN(arguments->string)) {
543 rb_str_unlocktmp(arguments->string);
544 }
545
546 return Qnil;
547}
548
549/*
550 * call-seq:
551 * IO::Buffer.for(string) -> readonly io_buffer
552 * IO::Buffer.for(string) {|io_buffer| ... read/write io_buffer ...}
553 *
554 * Creates a zero-copy IO::Buffer from the given string's memory. Without a
555 * block a frozen internal copy of the string is created efficiently and used
556 * as the buffer source. When a block is provided, the buffer is associated
557 * directly with the string's internal buffer and updating the buffer will
558 * update the string.
559 *
560 * Until #free is invoked on the buffer, either explicitly or via the garbage
561 * collector, the source string will be locked and cannot be modified.
562 *
563 * If the string is frozen, it will create a read-only buffer which cannot be
564 * modified. If the string is shared, it may trigger a copy-on-write when
565 * using the block form.
566 *
567 * string = 'test'
568 * buffer = IO::Buffer.for(string)
569 * buffer.external? #=> true
570 *
571 * buffer.get_string(0, 1)
572 * # => "t"
573 * string
574 * # => "test"
575 *
576 * buffer.resize(100)
577 * # in `resize': Cannot resize external buffer! (IO::Buffer::AccessError)
578 *
579 * IO::Buffer.for(string) do |buffer|
580 * buffer.set_string("T")
581 * string
582 * # => "Test"
583 * end
584 */
585 VALUE
586rb_io_buffer_type_for(VALUE klass, VALUE string)
587{
588 StringValue(string);
589
590 // If the string is frozen, both code paths are okay.
591 // If the string is not frozen, if a block is not given, it must be frozen.
592 if (rb_block_given_p()) {
593 struct io_buffer_for_yield_instance_arguments arguments = {
594 .klass = klass,
595 .string = string,
596 .instance = Qnil,
597 .flags = 0,
598 };
599
600 return rb_ensure(io_buffer_for_yield_instance, (VALUE)&arguments, io_buffer_for_yield_instance_ensure, (VALUE)&arguments);
601 }
602 else {
603 // This internally returns the source string if it's already frozen.
604 string = rb_str_tmp_frozen_acquire(string);
605 return io_buffer_for_make_instance(klass, string, RB_IO_BUFFER_READONLY);
606 }
607}
608
609/*
610 * call-seq:
611 * IO::Buffer.string(length) {|io_buffer| ... read/write io_buffer ...} -> string
612 *
613 * Creates a new string of the given length and yields a zero-copy IO::Buffer
614 * instance to the block which uses the string as a source. The block is
615 * expected to write to the buffer and the string will be returned.
616 *
617 * IO::Buffer.string(4) do |buffer|
618 * buffer.set_string("Ruby")
619 * end
620 * # => "Ruby"
621 */
622 VALUE
623rb_io_buffer_type_string(VALUE klass, VALUE length)
624{
625 VALUE string = rb_str_new(NULL, RB_NUM2LONG(length));
626
627 struct io_buffer_for_yield_instance_arguments arguments = {
628 .klass = klass,
629 .string = string,
630 .instance = Qnil,
631 };
632
633 rb_ensure(io_buffer_for_yield_instance, (VALUE)&arguments, io_buffer_for_yield_instance_ensure, (VALUE)&arguments);
634
635 return string;
636}
637
638 VALUE
639rb_io_buffer_new(void *base, size_t size, enum rb_io_buffer_flags flags)
640{
641 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
642
643 struct rb_io_buffer *buffer = NULL;
644 TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, buffer);
645
646 io_buffer_initialize(instance, buffer, base, size, flags, Qnil);
647
648 return instance;
649}
650
651 VALUE
652rb_io_buffer_map(VALUE io, size_t size, rb_off_t offset, enum rb_io_buffer_flags flags)
653{
654 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
655
656 struct rb_io_buffer *buffer = NULL;
657 TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, buffer);
658
659 int descriptor = rb_io_descriptor(io);
660
661 io_buffer_map_file(buffer, descriptor, size, offset, flags);
662
663 return instance;
664}
665
666/*
667 * call-seq: IO::Buffer.map(file, [size, [offset, [flags]]]) -> io_buffer
668 *
669 * Create an IO::Buffer for reading from +file+ by memory-mapping the file.
670 * +file+ should be a +File+ instance, opened for reading or reading and writing.
671 *
672 * Optional +size+ and +offset+ of mapping can be specified.
673 * Trying to map an empty file or specify +size+ of 0 will raise an error.
674 * Valid values for +offset+ are system-dependent.
675 *
676 * By default, the buffer is writable and expects the file to be writable.
677 * It is also shared, so several processes can use the same mapping.
678 *
679 * You can pass IO::Buffer::READONLY in +flags+ argument to make a read-only buffer;
680 * this allows to work with files opened only for reading.
681 * Specifying IO::Buffer::PRIVATE in +flags+ creates a private mapping,
682 * which will not impact other processes or the underlying file.
683 * It also allows updating a buffer created from a read-only file.
684 *
685 * File.write('test.txt', 'test')
686 *
687 * buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::READONLY)
688 * # => #<IO::Buffer 0x00000001014a0000+4 EXTERNAL MAPPED FILE SHARED READONLY>
689 *
690 * buffer.readonly? # => true
691 *
692 * buffer.get_string
693 * # => "test"
694 *
695 * buffer.set_string('b', 0)
696 * # 'IO::Buffer#set_string': Buffer is not writable! (IO::Buffer::AccessError)
697 *
698 * # create read/write mapping: length 4 bytes, offset 0, flags 0
699 * buffer = IO::Buffer.map(File.open('test.txt', 'r+'), 4, 0)
700 * buffer.set_string('b', 0)
701 * # => 1
702 *
703 * # Check it
704 * File.read('test.txt')
705 * # => "best"
706 *
707 * Note that some operating systems may not have cache coherency between mapped
708 * buffers and file reads.
709 */
710static VALUE
711io_buffer_map(int argc, VALUE *argv, VALUE klass)
712{
713 rb_check_arity(argc, 1, 4);
714
715 // We might like to handle a string path?
716 VALUE io = argv[0];
717
718 rb_off_t file_size = rb_file_size(io);
719 // Compiler can confirm that we handled file_size <= 0 case:
720 if (UNLIKELY(file_size <= 0)) {
721 rb_raise(rb_eArgError, "Invalid negative or zero file size!");
722 }
723 // Here, we assume that file_size is positive:
724 else if (UNLIKELY((uintmax_t)file_size > SIZE_MAX)) {
725 rb_raise(rb_eArgError, "File larger than address space!");
726 }
727
728 size_t size;
729 if (argc >= 2 && !RB_NIL_P(argv[1])) {
730 size = io_buffer_extract_size(argv[1]);
731 if (UNLIKELY(size == 0)) {
732 rb_raise(rb_eArgError, "Size can't be zero!");
733 }
734 if (UNLIKELY(size > (size_t)file_size)) {
735 rb_raise(rb_eArgError, "Size can't be larger than file size!");
736 }
737 }
738 else {
739 // This conversion should be safe:
740 size = (size_t)file_size;
741 }
742
743 // This is the file offset, not the buffer offset:
744 rb_off_t offset = 0;
745 if (argc >= 3) {
746 offset = NUM2OFFT(argv[2]);
747 if (UNLIKELY(offset < 0)) {
748 rb_raise(rb_eArgError, "Offset can't be negative!");
749 }
750 if (UNLIKELY(offset >= file_size)) {
751 rb_raise(rb_eArgError, "Offset too large!");
752 }
753 if (RB_NIL_P(argv[1])) {
754 // Decrease size if it's set from the actual file size:
755 size = (size_t)(file_size - offset);
756 }
757 else if (UNLIKELY((size_t)(file_size - offset) < size)) {
758 rb_raise(rb_eArgError, "Offset too large!");
759 }
760 }
761
762 enum rb_io_buffer_flags flags = 0;
763 if (argc >= 4) {
764 flags = io_buffer_extract_flags(argv[3]);
765 }
766
767 return rb_io_buffer_map(io, size, offset, flags);
768}
769
770// Compute the optimal allocation flags for a buffer of the given size.
771static inline enum rb_io_buffer_flags
772io_flags_for_size(size_t size)
773{
774 if (size >= RUBY_IO_BUFFER_PAGE_SIZE) {
775 return RB_IO_BUFFER_MAPPED;
776 }
777
778 return RB_IO_BUFFER_INTERNAL;
779}
780
781/*
782 * call-seq: IO::Buffer.new([size = DEFAULT_SIZE, [flags = 0]]) -> io_buffer
783 *
784 * Create a new zero-filled IO::Buffer of +size+ bytes.
785 * By default, the buffer will be _internal_: directly allocated chunk
786 * of the memory. But if the requested +size+ is more than OS-specific
787 * IO::Buffer::PAGE_SIZE, the buffer would be allocated using the
788 * virtual memory mechanism (anonymous +mmap+ on Unix, +VirtualAlloc+
789 * on Windows). The behavior can be forced by passing IO::Buffer::MAPPED
790 * as a second parameter.
791 *
792 * buffer = IO::Buffer.new(4)
793 * # =>
794 * # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL>
795 * # 0x00000000 00 00 00 00 ....
796 *
797 * buffer.get_string(0, 1) # => "\x00"
798 *
799 * buffer.set_string("test")
800 * buffer
801 * # =>
802 * # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL>
803 * # 0x00000000 74 65 73 74 test
804 */
805 VALUE
806rb_io_buffer_initialize(int argc, VALUE *argv, VALUE self)
807{
808 rb_check_arity(argc, 0, 2);
809
810 struct rb_io_buffer *buffer = NULL;
811 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
812
813 size_t size;
814 if (argc > 0) {
815 size = io_buffer_extract_size(argv[0]);
816 }
817 else {
818 size = RUBY_IO_BUFFER_DEFAULT_SIZE;
819 }
820
821 enum rb_io_buffer_flags flags = 0;
822 if (argc >= 2) {
823 flags = io_buffer_extract_flags(argv[1]);
824 }
825 else {
826 flags |= io_flags_for_size(size);
827 }
828
829 io_buffer_initialize(self, buffer, NULL, size, flags, Qnil);
830
831 return self;
832}
833
834static int
835io_buffer_validate_slice(VALUE source, void *base, size_t size)
836{
837 void *source_base = NULL;
838 size_t source_size = 0;
839
840 if (RB_TYPE_P(source, T_STRING)) {
841 RSTRING_GETMEM(source, source_base, source_size);
842 }
843 else {
844 rb_io_buffer_get_bytes(source, &source_base, &source_size);
845 }
846
847 // Source is invalid:
848 if (source_base == NULL) return 0;
849
850 // Base is out of range:
851 if (base < source_base) return 0;
852
853 const void *source_end = (char*)source_base + source_size;
854 const void *end = (char*)base + size;
855
856 // End is out of range:
857 if (end > source_end) return 0;
858
859 // It seems okay:
860 return 1;
861}
862
863static int
864io_buffer_validate(struct rb_io_buffer *buffer)
865{
866 if (buffer->source != Qnil) {
867 // Only slices incur this overhead, unfortunately... better safe than sorry!
868 return io_buffer_validate_slice(buffer->source, buffer->base, buffer->size);
869 }
870 else {
871 return 1;
872 }
873}
874
875enum rb_io_buffer_flags
876rb_io_buffer_get_bytes(VALUE self, void **base, size_t *size)
877{
878 struct rb_io_buffer *buffer = NULL;
879 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
880
881 if (io_buffer_validate(buffer)) {
882 if (buffer->base) {
883 *base = buffer->base;
884 *size = buffer->size;
885
886 return buffer->flags;
887 }
888 }
889
890 *base = NULL;
891 *size = 0;
892
893 return 0;
894}
895
896// Internal function for accessing bytes for writing, wil
897static inline void
898io_buffer_get_bytes_for_writing(struct rb_io_buffer *buffer, void **base, size_t *size)
899{
900 if (buffer->flags & RB_IO_BUFFER_READONLY ||
901 (!NIL_P(buffer->source) && OBJ_FROZEN(buffer->source))) {
902 rb_raise(rb_eIOBufferAccessError, "Buffer is not writable!");
903 }
904
905 if (!io_buffer_validate(buffer)) {
906 rb_raise(rb_eIOBufferInvalidatedError, "Buffer is invalid!");
907 }
908
909 if (buffer->base) {
910 *base = buffer->base;
911 *size = buffer->size;
912 } else {
913 *base = NULL;
914 *size = 0;
915 }
916}
917
918void
919rb_io_buffer_get_bytes_for_writing(VALUE self, void **base, size_t *size)
920{
921 struct rb_io_buffer *buffer = NULL;
922 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
923
924 io_buffer_get_bytes_for_writing(buffer, base, size);
925}
926
927static void
928io_buffer_get_bytes_for_reading(struct rb_io_buffer *buffer, const void **base, size_t *size)
929{
930 if (!io_buffer_validate(buffer)) {
931 rb_raise(rb_eIOBufferInvalidatedError, "Buffer has been invalidated!");
932 }
933
934 if (buffer->base) {
935 *base = buffer->base;
936 *size = buffer->size;
937 } else {
938 *base = NULL;
939 *size = 0;
940 }
941}
942
943void
944rb_io_buffer_get_bytes_for_reading(VALUE self, const void **base, size_t *size)
945{
946 struct rb_io_buffer *buffer = NULL;
947 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
948
949 io_buffer_get_bytes_for_reading(buffer, base, size);
950}
951
952/*
953 * call-seq: to_s -> string
954 *
955 * Short representation of the buffer. It includes the address, size and
956 * symbolic flags. This format is subject to change.
957 *
958 * puts IO::Buffer.new(4) # uses to_s internally
959 * # #<IO::Buffer 0x000055769f41b1a0+4 INTERNAL>
960 */
961 VALUE
962rb_io_buffer_to_s(VALUE self)
963{
964 struct rb_io_buffer *buffer = NULL;
965 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
966
967 VALUE result = rb_str_new_cstr("#<");
968
969 rb_str_append(result, rb_class_name(CLASS_OF(self)));
970 rb_str_catf(result, " %p+%"PRIdSIZE, buffer->base, buffer->size);
971
972 if (buffer->base == NULL) {
973 rb_str_cat2(result, " NULL");
974 }
975
976 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
977 rb_str_cat2(result, " EXTERNAL");
978 }
979
980 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
981 rb_str_cat2(result, " INTERNAL");
982 }
983
984 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
985 rb_str_cat2(result, " MAPPED");
986 }
987
988 if (buffer->flags & RB_IO_BUFFER_FILE) {
989 rb_str_cat2(result, " FILE");
990 }
991
992 if (buffer->flags & RB_IO_BUFFER_SHARED) {
993 rb_str_cat2(result, " SHARED");
994 }
995
996 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
997 rb_str_cat2(result, " LOCKED");
998 }
999
1000 if (buffer->flags & RB_IO_BUFFER_PRIVATE) {
1001 rb_str_cat2(result, " PRIVATE");
1002 }
1003
1004 if (buffer->flags & RB_IO_BUFFER_READONLY) {
1005 rb_str_cat2(result, " READONLY");
1006 }
1007
1008 if (buffer->source != Qnil) {
1009 rb_str_cat2(result, " SLICE");
1010 }
1011
1012 if (!io_buffer_validate(buffer)) {
1013 rb_str_cat2(result, " INVALID");
1014 }
1015
1016 return rb_str_cat2(result, ">");
1017}
1018
1019// Compute the output size of a hexdump of the given width (bytes per line), total size, and whether it is the first line in the output.
1020// This is used to preallocate the output string.
1021inline static size_t
1022io_buffer_hexdump_output_size(size_t width, size_t size, int first)
1023{
1024 // The preview on the right hand side is 1:1:
1025 size_t total = size;
1026
1027 size_t whole_lines = (size / width);
1028 size_t partial_line = (size % width) ? 1 : 0;
1029
1030 // For each line:
1031 // 1 byte 10 bytes 1 byte width*3 bytes 1 byte size bytes
1032 // (newline) (address) (space) (hexdump ) (space) (preview)
1033 total += (whole_lines + partial_line) * (1 + 10 + width*3 + 1 + 1);
1034
1035 // If the hexdump is the first line, one less newline will be emitted:
1036 if (size && first) total -= 1;
1037
1038 return total;
1039}
1040
1041// Append a hexdump of the given width (bytes per line), base address, size, and whether it is the first line in the output.
1042// If the hexdump is not the first line, it will prepend a newline if there is any output at all.
1043// If formatting here is adjusted, please update io_buffer_hexdump_output_size accordingly.
1044static VALUE
1045io_buffer_hexdump(VALUE string, size_t width, const char *base, size_t length, size_t offset, int first)
1046{
1047 char *text = alloca(width+1);
1048 text[width] = '\0';
1049
1050 for (; offset < length; offset += width) {
1051 memset(text, '\0', width);
1052 if (first) {
1053 rb_str_catf(string, "0x%08" PRIxSIZE " ", offset);
1054 first = 0;
1055 }
1056 else {
1057 rb_str_catf(string, "\n0x%08" PRIxSIZE " ", offset);
1058 }
1059
1060 for (size_t i = 0; i < width; i += 1) {
1061 if (offset+i < length) {
1062 unsigned char value = ((unsigned char*)base)[offset+i];
1063
1064 if (value < 127 && isprint(value)) {
1065 text[i] = (char)value;
1066 }
1067 else {
1068 text[i] = '.';
1069 }
1070
1071 rb_str_catf(string, " %02x", value);
1072 }
1073 else {
1074 rb_str_cat2(string, " ");
1075 }
1076 }
1077
1078 rb_str_catf(string, " %s", text);
1079 }
1080
1081 return string;
1082}
1083
1084/*
1085 * call-seq: inspect -> string
1086 *
1087 * Inspect the buffer and report useful information about it's internal state.
1088 * Only a limited portion of the buffer will be displayed in a hexdump style
1089 * format.
1090 *
1091 * buffer = IO::Buffer.for("Hello World")
1092 * puts buffer.inspect
1093 * # #<IO::Buffer 0x000000010198ccd8+11 EXTERNAL READONLY SLICE>
1094 * # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
1095 */
1096 VALUE
1097rb_io_buffer_inspect(VALUE self)
1098{
1099 struct rb_io_buffer *buffer = NULL;
1100 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1101
1102 VALUE result = rb_io_buffer_to_s(self);
1103
1104 if (io_buffer_validate(buffer)) {
1105 // Limit the maximum size generated by inspect:
1106 size_t size = buffer->size;
1107 int clamped = 0;
1108
1109 if (size > RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE) {
1110 size = RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE;
1111 clamped = 1;
1112 }
1113
1114 io_buffer_hexdump(result, RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH, buffer->base, size, 0, 0);
1115
1116 if (clamped) {
1117 rb_str_catf(result, "\n(and %" PRIuSIZE " more bytes not printed)", buffer->size - size);
1118 }
1119 }
1120
1121 return result;
1122}
1123
1124/*
1125 * call-seq: size -> integer
1126 *
1127 * Returns the size of the buffer that was explicitly set (on creation with ::new
1128 * or on #resize), or deduced on buffer's creation from string or file.
1129 */
1130 VALUE
1131rb_io_buffer_size(VALUE self)
1132{
1133 struct rb_io_buffer *buffer = NULL;
1134 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1135
1136 return SIZET2NUM(buffer->size);
1137}
1138
1139/*
1140 * call-seq: valid? -> true or false
1141 *
1142 * Returns whether the buffer buffer is accessible.
1143 *
1144 * A buffer becomes invalid if it is a slice of another buffer (or string)
1145 * which has been freed or re-allocated at a different address.
1146 */
1147static VALUE
1148rb_io_buffer_valid_p(VALUE self)
1149{
1150 struct rb_io_buffer *buffer = NULL;
1151 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1152
1153 return RBOOL(io_buffer_validate(buffer));
1154}
1155
1156/*
1157 * call-seq: null? -> true or false
1158 *
1159 * If the buffer was freed with #free, transferred with #transfer, or was
1160 * never allocated in the first place.
1161 *
1162 * buffer = IO::Buffer.new(0)
1163 * buffer.null? #=> true
1164 *
1165 * buffer = IO::Buffer.new(4)
1166 * buffer.null? #=> false
1167 * buffer.free
1168 * buffer.null? #=> true
1169 */
1170static VALUE
1171rb_io_buffer_null_p(VALUE self)
1172{
1173 struct rb_io_buffer *buffer = NULL;
1174 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1175
1176 return RBOOL(buffer->base == NULL);
1177}
1178
1179/*
1180 * call-seq: empty? -> true or false
1181 *
1182 * If the buffer has 0 size: it is created by ::new with size 0, or with ::for
1183 * from an empty string. (Note that empty files can't be mapped, so the buffer
1184 * created with ::map will never be empty.)
1185 */
1186static VALUE
1187rb_io_buffer_empty_p(VALUE self)
1188{
1189 struct rb_io_buffer *buffer = NULL;
1190 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1191
1192 return RBOOL(buffer->size == 0);
1193}
1194
1195/*
1196 * call-seq: external? -> true or false
1197 *
1198 * The buffer is _external_ if it references the memory which is not
1199 * allocated or mapped by the buffer itself.
1200 *
1201 * A buffer created using ::for has an external reference to the string's
1202 * memory.
1203 *
1204 * External buffer can't be resized.
1205 */
1206static VALUE
1207rb_io_buffer_external_p(VALUE self)
1208{
1209 struct rb_io_buffer *buffer = NULL;
1210 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1211
1212 return RBOOL(buffer->flags & RB_IO_BUFFER_EXTERNAL);
1213}
1214
1215/*
1216 * call-seq: internal? -> true or false
1217 *
1218 * If the buffer is _internal_, meaning it references memory allocated by the
1219 * buffer itself.
1220 *
1221 * An internal buffer is not associated with any external memory (e.g. string)
1222 * or file mapping.
1223 *
1224 * Internal buffers are created using ::new and is the default when the
1225 * requested size is less than the IO::Buffer::PAGE_SIZE and it was not
1226 * requested to be mapped on creation.
1227 *
1228 * Internal buffers can be resized, and such an operation will typically
1229 * invalidate all slices, but not always.
1230 */
1231static VALUE
1232rb_io_buffer_internal_p(VALUE self)
1233{
1234 struct rb_io_buffer *buffer = NULL;
1235 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1236
1237 return RBOOL(buffer->flags & RB_IO_BUFFER_INTERNAL);
1238}
1239
1240/*
1241 * call-seq: mapped? -> true or false
1242 *
1243 * If the buffer is _mapped_, meaning it references memory mapped by the
1244 * buffer.
1245 *
1246 * Mapped buffers are either anonymous, if created by ::new with the
1247 * IO::Buffer::MAPPED flag or if the size was at least IO::Buffer::PAGE_SIZE,
1248 * or backed by a file if created with ::map.
1249 *
1250 * Mapped buffers can usually be resized, and such an operation will typically
1251 * invalidate all slices, but not always.
1252 */
1253static VALUE
1254rb_io_buffer_mapped_p(VALUE self)
1255{
1256 struct rb_io_buffer *buffer = NULL;
1257 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1258
1259 return RBOOL(buffer->flags & RB_IO_BUFFER_MAPPED);
1260}
1261
1262/*
1263 * call-seq: shared? -> true or false
1264 *
1265 * If the buffer is _shared_, meaning it references memory that can be shared
1266 * with other processes (and thus might change without being modified
1267 * locally).
1268 *
1269 * # Create a test file:
1270 * File.write('test.txt', 'test')
1271 *
1272 * # Create a shared mapping from the given file, the file must be opened in
1273 * # read-write mode unless we also specify IO::Buffer::READONLY:
1274 * buffer = IO::Buffer.map(File.open('test.txt', 'r+'), nil, 0)
1275 * # => #<IO::Buffer 0x00007f1bffd5e000+4 EXTERNAL MAPPED SHARED>
1276 *
1277 * # Write to the buffer, which will modify the mapped file:
1278 * buffer.set_string('b', 0)
1279 * # => 1
1280 *
1281 * # The file itself is modified:
1282 * File.read('test.txt')
1283 * # => "best"
1284 */
1285static VALUE
1286rb_io_buffer_shared_p(VALUE self)
1287{
1288 struct rb_io_buffer *buffer = NULL;
1289 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1290
1291 return RBOOL(buffer->flags & RB_IO_BUFFER_SHARED);
1292}
1293
1294/*
1295 * call-seq: locked? -> true or false
1296 *
1297 * If the buffer is _locked_, meaning it is inside #locked block execution.
1298 * Locked buffer can't be resized or freed, and another lock can't be acquired
1299 * on it.
1300 *
1301 * Locking is not thread safe, but is a semantic used to ensure buffers don't
1302 * move while being used by a system call.
1303 *
1304 * buffer.locked do
1305 * buffer.write(io) # theoretical system call interface
1306 * end
1307 */
1308static VALUE
1309rb_io_buffer_locked_p(VALUE self)
1310{
1311 struct rb_io_buffer *buffer = NULL;
1312 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1313
1314 return RBOOL(buffer->flags & RB_IO_BUFFER_LOCKED);
1315}
1316
1317/* call-seq: private? -> true or false
1318 *
1319 * If the buffer is _private_, meaning modifications to the buffer will not
1320 * be replicated to the underlying file mapping.
1321 *
1322 * # Create a test file:
1323 * File.write('test.txt', 'test')
1324 *
1325 * # Create a private mapping from the given file. Note that the file here
1326 * # is opened in read-only mode, but it doesn't matter due to the private
1327 * # mapping:
1328 * buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::PRIVATE)
1329 * # => #<IO::Buffer 0x00007fce63f11000+4 MAPPED PRIVATE>
1330 *
1331 * # Write to the buffer (invoking CoW of the underlying file buffer):
1332 * buffer.set_string('b', 0)
1333 * # => 1
1334 *
1335 * # The file itself is not modified:
1336 * File.read('test.txt')
1337 * # => "test"
1338 */
1339static VALUE
1340rb_io_buffer_private_p(VALUE self)
1341{
1342 struct rb_io_buffer *buffer = NULL;
1343 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1344
1345 return RBOOL(buffer->flags & RB_IO_BUFFER_PRIVATE);
1346}
1347
1348int
1349rb_io_buffer_readonly_p(VALUE self)
1350{
1351 struct rb_io_buffer *buffer = NULL;
1352 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1353
1354 return buffer->flags & RB_IO_BUFFER_READONLY;
1355}
1356
1357/*
1358 * call-seq: readonly? -> true or false
1359 *
1360 * If the buffer is <i>read only</i>, meaning the buffer cannot be modified using
1361 * #set_value, #set_string or #copy and similar.
1362 *
1363 * Frozen strings and read-only files create read-only buffers.
1364 */
1365static VALUE
1366io_buffer_readonly_p(VALUE self)
1367{
1368 return RBOOL(rb_io_buffer_readonly_p(self));
1369}
1370
1371static void
1372io_buffer_lock(struct rb_io_buffer *buffer)
1373{
1374 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1375 rb_raise(rb_eIOBufferLockedError, "Buffer already locked!");
1376 }
1377
1378 buffer->flags |= RB_IO_BUFFER_LOCKED;
1379}
1380
1381 VALUE
1382rb_io_buffer_lock(VALUE self)
1383{
1384 struct rb_io_buffer *buffer = NULL;
1385 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1386
1387 io_buffer_lock(buffer);
1388
1389 return self;
1390}
1391
1392static void
1393io_buffer_unlock(struct rb_io_buffer *buffer)
1394{
1395 if (!(buffer->flags & RB_IO_BUFFER_LOCKED)) {
1396 rb_raise(rb_eIOBufferLockedError, "Buffer not locked!");
1397 }
1398
1399 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1400}
1401
1402 VALUE
1403rb_io_buffer_unlock(VALUE self)
1404{
1405 struct rb_io_buffer *buffer = NULL;
1406 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1407
1408 io_buffer_unlock(buffer);
1409
1410 return self;
1411}
1412
1413int
1414rb_io_buffer_try_unlock(VALUE self)
1415{
1416 struct rb_io_buffer *buffer = NULL;
1417 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1418
1419 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1420 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1421 return 1;
1422 }
1423
1424 return 0;
1425}
1426
1427/*
1428 * call-seq: locked { ... }
1429 *
1430 * Allows to process a buffer in exclusive way, for concurrency-safety. While
1431 * the block is performed, the buffer is considered locked, and no other code
1432 * can enter the lock. Also, locked buffer can't be changed with #resize or
1433 * #free.
1434 *
1435 * The following operations acquire a lock: #resize, #free.
1436 *
1437 * Locking is not thread safe. It is designed as a safety net around
1438 * non-blocking system calls. You can only share a buffer between threads with
1439 * appropriate synchronisation techniques.
1440 *
1441 * buffer = IO::Buffer.new(4)
1442 * buffer.locked? #=> false
1443 *
1444 * Fiber.schedule do
1445 * buffer.locked do
1446 * buffer.write(io) # theoretical system call interface
1447 * end
1448 * end
1449 *
1450 * Fiber.schedule do
1451 * # in `locked': Buffer already locked! (IO::Buffer::LockedError)
1452 * buffer.locked do
1453 * buffer.set_string("test", 0)
1454 * end
1455 * end
1456 */
1457 VALUE
1458rb_io_buffer_locked(VALUE self)
1459{
1460 struct rb_io_buffer *buffer = NULL;
1461 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1462
1463 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1464 rb_raise(rb_eIOBufferLockedError, "Buffer already locked!");
1465 }
1466
1467 buffer->flags |= RB_IO_BUFFER_LOCKED;
1468
1469 VALUE result = rb_yield(self);
1470
1471 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1472
1473 return result;
1474}
1475
1476/*
1477 * call-seq: free -> self
1478 *
1479 * If the buffer references memory, release it back to the operating system.
1480 * * for a _mapped_ buffer (e.g. from file): unmap.
1481 * * for a buffer created from scratch: free memory.
1482 * * for a buffer created from string: undo the association.
1483 *
1484 * After the buffer is freed, no further operations can't be performed on it.
1485 *
1486 * You can resize a freed buffer to re-allocate it.
1487 *
1488 * buffer = IO::Buffer.for('test')
1489 * buffer.free
1490 * # => #<IO::Buffer 0x0000000000000000+0 NULL>
1491 *
1492 * buffer.get_value(:U8, 0)
1493 * # in `get_value': The buffer is not allocated! (IO::Buffer::AllocationError)
1494 *
1495 * buffer.get_string
1496 * # in `get_string': The buffer is not allocated! (IO::Buffer::AllocationError)
1497 *
1498 * buffer.null?
1499 * # => true
1500 */
1501 VALUE
1502rb_io_buffer_free(VALUE self)
1503{
1504 struct rb_io_buffer *buffer = NULL;
1505 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1506
1507 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1508 rb_raise(rb_eIOBufferLockedError, "Buffer is locked!");
1509 }
1510
1511 io_buffer_free(buffer);
1512
1513 return self;
1514}
1515
1516 VALUE rb_io_buffer_free_locked(VALUE self)
1517{
1518 struct rb_io_buffer *buffer = NULL;
1519 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1520
1521 io_buffer_unlock(buffer);
1522 io_buffer_free(buffer);
1523
1524 return self;
1525}
1526
1527static bool
1528size_sum_is_bigger_than(size_t a, size_t b, size_t x)
1529{
1530 struct rbimpl_size_overflow_tag size = rbimpl_size_add_overflow(a, b);
1531 return size.overflowed || size.result > x;
1532}
1533
1534// Validate that access to the buffer is within bounds, assuming you want to
1535// access length bytes from the specified offset.
1536static inline void
1537io_buffer_validate_range(struct rb_io_buffer *buffer, size_t offset, size_t length)
1538{
1539 if (size_sum_is_bigger_than(offset, length, buffer->size)) {
1540 rb_raise(rb_eArgError, "Specified offset+length is bigger than the buffer size!");
1541 }
1542}
1543
1544/*
1545 * call-seq: hexdump([offset, [length, [width]]]) -> string
1546 *
1547 * Returns a human-readable string representation of the buffer. The exact
1548 * format is subject to change.
1549 *
1550 * buffer = IO::Buffer.for("Hello World")
1551 * puts buffer.hexdump
1552 * # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
1553 *
1554 * As buffers are usually fairly big, you may want to limit the output by
1555 * specifying the offset and length:
1556 *
1557 * puts buffer.hexdump(6, 5)
1558 * # 0x00000006 57 6f 72 6c 64 World
1559 */
1560static VALUE
1561rb_io_buffer_hexdump(int argc, VALUE *argv, VALUE self)
1562{
1563 rb_check_arity(argc, 0, 3);
1564
1565 size_t offset, length;
1566 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1567
1568 size_t width = RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH;
1569 if (argc >= 3) {
1570 width = io_buffer_extract_width(argv[2], 1);
1571 }
1572
1573 // This may raise an exception if the offset/length is invalid:
1574 io_buffer_validate_range(buffer, offset, length);
1575
1576 VALUE result = Qnil;
1577
1578 if (io_buffer_validate(buffer) && buffer->base) {
1579 result = rb_str_buf_new(io_buffer_hexdump_output_size(width, length, 1));
1580
1581 io_buffer_hexdump(result, width, buffer->base, offset+length, offset, 1);
1582 }
1583
1584 return result;
1585}
1586
1587static VALUE
1588rb_io_buffer_slice(struct rb_io_buffer *buffer, VALUE self, size_t offset, size_t length)
1589{
1590 io_buffer_validate_range(buffer, offset, length);
1591
1592 VALUE instance = rb_io_buffer_type_allocate(rb_class_of(self));
1593 struct rb_io_buffer *slice = NULL;
1594 TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, slice);
1595
1596 slice->flags |= (buffer->flags & RB_IO_BUFFER_READONLY);
1597 slice->base = (char*)buffer->base + offset;
1598 slice->size = length;
1599
1600 // The source should be the root buffer:
1601 if (buffer->source != Qnil) {
1602 RB_OBJ_WRITE(instance, &slice->source, buffer->source);
1603 }
1604 else {
1605 RB_OBJ_WRITE(instance, &slice->source, self);
1606 }
1607
1608 return instance;
1609}
1610
1611/*
1612 * call-seq: slice([offset, [length]]) -> io_buffer
1613 *
1614 * Produce another IO::Buffer which is a slice (or view into) the current one
1615 * starting at +offset+ bytes and going for +length+ bytes.
1616 *
1617 * The slicing happens without copying of memory, and the slice keeps being
1618 * associated with the original buffer's source (string, or file), if any.
1619 *
1620 * If the offset is not given, it will be zero. If the offset is negative, it
1621 * will raise an ArgumentError.
1622 *
1623 * If the length is not given, the slice will be as long as the original
1624 * buffer minus the specified offset. If the length is negative, it will raise
1625 * an ArgumentError.
1626 *
1627 * Raises RuntimeError if the <tt>offset+length</tt> is out of the current
1628 * buffer's bounds.
1629 *
1630 * string = 'test'
1631 * buffer = IO::Buffer.for(string).dup
1632 *
1633 * slice = buffer.slice
1634 * # =>
1635 * # #<IO::Buffer 0x0000000108338e68+4 SLICE>
1636 * # 0x00000000 74 65 73 74 test
1637 *
1638 * buffer.slice(2)
1639 * # =>
1640 * # #<IO::Buffer 0x0000000108338e6a+2 SLICE>
1641 * # 0x00000000 73 74 st
1642 *
1643 * slice = buffer.slice(1, 2)
1644 * # =>
1645 * # #<IO::Buffer 0x00007fc3d34ebc49+2 SLICE>
1646 * # 0x00000000 65 73 es
1647 *
1648 * # Put "o" into 0s position of the slice
1649 * slice.set_string('o', 0)
1650 * slice
1651 * # =>
1652 * # #<IO::Buffer 0x00007fc3d34ebc49+2 SLICE>
1653 * # 0x00000000 6f 73 os
1654 *
1655 * # it is also visible at position 1 of the original buffer
1656 * buffer
1657 * # =>
1658 * # #<IO::Buffer 0x00007fc3d31e2d80+4 INTERNAL>
1659 * # 0x00000000 74 6f 73 74 tost
1660 */
1661static VALUE
1662io_buffer_slice(int argc, VALUE *argv, VALUE self)
1663{
1664 rb_check_arity(argc, 0, 2);
1665
1666 size_t offset, length;
1667 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1668
1669 return rb_io_buffer_slice(buffer, self, offset, length);
1670}
1671
1672/*
1673 * call-seq: transfer -> new_io_buffer
1674 *
1675 * Transfers ownership of the underlying memory to a new buffer, causing the
1676 * current buffer to become uninitialized.
1677 *
1678 * buffer = IO::Buffer.new('test')
1679 * other = buffer.transfer
1680 * other
1681 * # =>
1682 * # #<IO::Buffer 0x00007f136a15f7b0+4 SLICE>
1683 * # 0x00000000 74 65 73 74 test
1684 * buffer
1685 * # =>
1686 * # #<IO::Buffer 0x0000000000000000+0 NULL>
1687 * buffer.null?
1688 * # => true
1689 */
1690 VALUE
1691rb_io_buffer_transfer(VALUE self)
1692{
1693 struct rb_io_buffer *buffer = NULL;
1694 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1695
1696 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1697 rb_raise(rb_eIOBufferLockedError, "Cannot transfer ownership of locked buffer!");
1698 }
1699
1700 VALUE instance = rb_io_buffer_type_allocate(rb_class_of(self));
1701 struct rb_io_buffer *transferred;
1702 TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, transferred);
1703
1704 *transferred = *buffer;
1705 io_buffer_zero(buffer);
1706
1707 return instance;
1708}
1709
1710static void
1711io_buffer_resize_clear(struct rb_io_buffer *buffer, void* base, size_t size)
1712{
1713 if (size > buffer->size) {
1714 memset((unsigned char*)base+buffer->size, 0, size - buffer->size);
1715 }
1716}
1717
1718static void
1719io_buffer_resize_copy(VALUE self, struct rb_io_buffer *buffer, size_t size)
1720{
1721 // Slow path:
1722 struct rb_io_buffer resized;
1723 io_buffer_initialize(self, &resized, NULL, size, io_flags_for_size(size), Qnil);
1724
1725 if (buffer->base) {
1726 size_t preserve = buffer->size;
1727 if (preserve > size) preserve = size;
1728 memcpy(resized.base, buffer->base, preserve);
1729
1730 io_buffer_resize_clear(buffer, resized.base, size);
1731 }
1732
1733 io_buffer_free(buffer);
1734 *buffer = resized;
1735}
1736
1737void
1738rb_io_buffer_resize(VALUE self, size_t size)
1739{
1740 struct rb_io_buffer *buffer = NULL;
1741 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
1742
1743 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1744 rb_raise(rb_eIOBufferLockedError, "Cannot resize locked buffer!");
1745 }
1746
1747 if (buffer->base == NULL) {
1748 io_buffer_initialize(self, buffer, NULL, size, io_flags_for_size(size), Qnil);
1749 return;
1750 }
1751
1752 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
1753 rb_raise(rb_eIOBufferAccessError, "Cannot resize external buffer!");
1754 }
1755
1756#if defined(HAVE_MREMAP) && defined(MREMAP_MAYMOVE)
1757 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
1758 void *base = mremap(buffer->base, buffer->size, size, MREMAP_MAYMOVE);
1759
1760 if (base == MAP_FAILED) {
1761 rb_sys_fail("rb_io_buffer_resize:mremap");
1762 }
1763
1764 io_buffer_resize_clear(buffer, base, size);
1765
1766 buffer->base = base;
1767 buffer->size = size;
1768
1769 return;
1770 }
1771#endif
1772
1773 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
1774 if (size == 0) {
1775 io_buffer_free(buffer);
1776 return;
1777 }
1778
1779 void *base = realloc(buffer->base, size);
1780
1781 if (!base) {
1782 rb_sys_fail("rb_io_buffer_resize:realloc");
1783 }
1784
1785 io_buffer_resize_clear(buffer, base, size);
1786
1787 buffer->base = base;
1788 buffer->size = size;
1789
1790 return;
1791 }
1792
1793 io_buffer_resize_copy(self, buffer, size);
1794}
1795
1796/*
1797 * call-seq: resize(new_size) -> self
1798 *
1799 * Resizes a buffer to a +new_size+ bytes, preserving its content.
1800 * Depending on the old and new size, the memory area associated with
1801 * the buffer might be either extended, or rellocated at different
1802 * address with content being copied.
1803 *
1804 * buffer = IO::Buffer.new(4)
1805 * buffer.set_string("test", 0)
1806 * buffer.resize(8) # resize to 8 bytes
1807 * # =>
1808 * # #<IO::Buffer 0x0000555f5d1a1630+8 INTERNAL>
1809 * # 0x00000000 74 65 73 74 00 00 00 00 test....
1810 *
1811 * External buffer (created with ::for), and locked buffer
1812 * can not be resized.
1813 */
1814static VALUE
1815io_buffer_resize(VALUE self, VALUE size)
1816{
1817 rb_io_buffer_resize(self, io_buffer_extract_size(size));
1818
1819 return self;
1820}
1821
1822/*
1823 * call-seq: <=>(other) -> true or false
1824 *
1825 * Buffers are compared by size and exact contents of the memory they are
1826 * referencing using +memcmp+.
1827 */
1828static VALUE
1829rb_io_buffer_compare(VALUE self, VALUE other)
1830{
1831 const void *ptr1, *ptr2;
1832 size_t size1, size2;
1833
1834 rb_io_buffer_get_bytes_for_reading(self, &ptr1, &size1);
1835 rb_io_buffer_get_bytes_for_reading(other, &ptr2, &size2);
1836
1837 if (size1 < size2) {
1838 return RB_INT2NUM(-1);
1839 }
1840
1841 if (size1 > size2) {
1842 return RB_INT2NUM(1);
1843 }
1844
1845 return RB_INT2NUM(memcmp(ptr1, ptr2, size1));
1846}
1847
1848static void
1849io_buffer_validate_type(size_t size, size_t offset, size_t extend)
1850{
1851 if (size_sum_is_bigger_than(offset, extend, size)) {
1852 rb_raise(rb_eArgError, "Type extends beyond end of buffer! (offset=%"PRIdSIZE" > size=%"PRIdSIZE")", offset, size);
1853 }
1854}
1855
1856// Lower case: little endian.
1857// Upper case: big endian (network endian).
1858//
1859// :U8 | unsigned 8-bit integer.
1860// :S8 | signed 8-bit integer.
1861//
1862// :u16, :U16 | unsigned 16-bit integer.
1863// :s16, :S16 | signed 16-bit integer.
1864//
1865// :u32, :U32 | unsigned 32-bit integer.
1866// :s32, :S32 | signed 32-bit integer.
1867//
1868// :u64, :U64 | unsigned 64-bit integer.
1869// :s64, :S64 | signed 64-bit integer.
1870//
1871// :u128, :U128 | unsigned 128-bit integer.
1872// :s128, :S128 | signed 128-bit integer.
1873//
1874// :f32, :F32 | 32-bit floating point number.
1875// :f64, :F64 | 64-bit floating point number.
1876
1877#define ruby_swap8(value) value
1878
1879 union swapf32 {
1880 uint32_t integral;
1881 float value;
1882};
1883
1884static float
1885ruby_swapf32(float value)
1886{
1887 union swapf32 swap = {.value = value};
1888 swap.integral = ruby_swap32(swap.integral);
1889 return swap.value;
1890}
1891
1892 union swapf64 {
1893 uint64_t integral;
1894 double value;
1895};
1896
1897static double
1898ruby_swapf64(double value)
1899{
1900 union swapf64 swap = {.value = value};
1901 swap.integral = ruby_swap64(swap.integral);
1902 return swap.value;
1903}
1904
1905// Structures and conversion functions are now in numeric.h/numeric.c
1906// Unified swap function for 128-bit integers (works with both signed and unsigned)
1907// Since both rb_uint128_t and rb_int128_t have the same memory layout,
1908// we can use a union to make the swap function work with both types
1909static inline rb_uint128_t
1910ruby_swap128_uint(rb_uint128_t x)
1911{
1912 rb_uint128_t result;
1913#ifdef HAVE_UINT128_T
1914#if __has_builtin(__builtin_bswap128)
1915 result.value = __builtin_bswap128(x.value);
1916#else
1917 // Manual byte swap for 128-bit integers
1918 uint64_t low = (uint64_t)x.value;
1919 uint64_t high = (uint64_t)(x.value >> 64);
1920 low = ruby_swap64(low);
1921 high = ruby_swap64(high);
1922 result.value = ((uint128_t)low << 64) | high;
1923#endif
1924#else
1925 // Fallback swap function using two 64-bit integers
1926 // For big-endian data on little-endian host (or vice versa):
1927 // 1. Swap bytes within each 64-bit part
1928 // 2. Swap the order of the parts (since big-endian stores high first, little-endian stores low first)
1929 result.parts.low = ruby_swap64(x.parts.high);
1930 result.parts.high = ruby_swap64(x.parts.low);
1931#endif
1932 return result;
1933}
1934
1935static inline rb_int128_t
1936ruby_swap128_int(rb_int128_t x)
1937{
1938 union uint128_int128_conversion conversion = {
1939 .int128 = x
1940 };
1941 conversion.uint128 = ruby_swap128_uint(conversion.uint128);
1942 return conversion.int128;
1943}
1944
1945#define IO_BUFFER_DECLARE_TYPE(name, type, endian, wrap, unwrap, swap) \
1946static ID RB_IO_BUFFER_DATA_TYPE_##name; \
1947\
1948static VALUE \
1949io_buffer_read_##name(const void* base, size_t size, size_t *offset) \
1950{ \
1951 io_buffer_validate_type(size, *offset, sizeof(type)); \
1952 type value; \
1953 memcpy(&value, (char*)base + *offset, sizeof(type)); \
1954 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1955 *offset += sizeof(type); \
1956 return wrap(value); \
1957} \
1958\
1959static void \
1960io_buffer_write_##name(const void* base, size_t size, size_t *offset, VALUE _value) \
1961{ \
1962 io_buffer_validate_type(size, *offset, sizeof(type)); \
1963 type value = unwrap(_value); \
1964 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1965 memcpy((char*)base + *offset, &value, sizeof(type)); \
1966 *offset += sizeof(type); \
1967} \
1968\
1969enum { \
1970 RB_IO_BUFFER_DATA_TYPE_##name##_SIZE = sizeof(type) \
1971};
1972
1973IO_BUFFER_DECLARE_TYPE(U8, uint8_t, RB_IO_BUFFER_BIG_ENDIAN, RB_UINT2NUM, RB_NUM2UINT, ruby_swap8)
1974IO_BUFFER_DECLARE_TYPE(S8, int8_t, RB_IO_BUFFER_BIG_ENDIAN, RB_INT2NUM, RB_NUM2INT, ruby_swap8)
1975
1976IO_BUFFER_DECLARE_TYPE(u16, uint16_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_UINT2NUM, RB_NUM2UINT, ruby_swap16)
1977IO_BUFFER_DECLARE_TYPE(U16, uint16_t, RB_IO_BUFFER_BIG_ENDIAN, RB_UINT2NUM, RB_NUM2UINT, ruby_swap16)
1978IO_BUFFER_DECLARE_TYPE(s16, int16_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_INT2NUM, RB_NUM2INT, ruby_swap16)
1979IO_BUFFER_DECLARE_TYPE(S16, int16_t, RB_IO_BUFFER_BIG_ENDIAN, RB_INT2NUM, RB_NUM2INT, ruby_swap16)
1980
1981IO_BUFFER_DECLARE_TYPE(u32, uint32_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_UINT2NUM, RB_NUM2UINT, ruby_swap32)
1982IO_BUFFER_DECLARE_TYPE(U32, uint32_t, RB_IO_BUFFER_BIG_ENDIAN, RB_UINT2NUM, RB_NUM2UINT, ruby_swap32)
1983IO_BUFFER_DECLARE_TYPE(s32, int32_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_INT2NUM, RB_NUM2INT, ruby_swap32)
1984IO_BUFFER_DECLARE_TYPE(S32, int32_t, RB_IO_BUFFER_BIG_ENDIAN, RB_INT2NUM, RB_NUM2INT, ruby_swap32)
1985
1986IO_BUFFER_DECLARE_TYPE(u64, uint64_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_ULL2NUM, RB_NUM2ULL, ruby_swap64)
1987IO_BUFFER_DECLARE_TYPE(U64, uint64_t, RB_IO_BUFFER_BIG_ENDIAN, RB_ULL2NUM, RB_NUM2ULL, ruby_swap64)
1988IO_BUFFER_DECLARE_TYPE(s64, int64_t, RB_IO_BUFFER_LITTLE_ENDIAN, RB_LL2NUM, RB_NUM2LL, ruby_swap64)
1989IO_BUFFER_DECLARE_TYPE(S64, int64_t, RB_IO_BUFFER_BIG_ENDIAN, RB_LL2NUM, RB_NUM2LL, ruby_swap64)
1990
1991IO_BUFFER_DECLARE_TYPE(u128, rb_uint128_t, RB_IO_BUFFER_LITTLE_ENDIAN, rb_uint128_to_numeric, rb_numeric_to_uint128, ruby_swap128_uint)
1992IO_BUFFER_DECLARE_TYPE(U128, rb_uint128_t, RB_IO_BUFFER_BIG_ENDIAN, rb_uint128_to_numeric, rb_numeric_to_uint128, ruby_swap128_uint)
1993IO_BUFFER_DECLARE_TYPE(s128, rb_int128_t, RB_IO_BUFFER_LITTLE_ENDIAN, rb_int128_to_numeric, rb_numeric_to_int128, ruby_swap128_int)
1994IO_BUFFER_DECLARE_TYPE(S128, rb_int128_t, RB_IO_BUFFER_BIG_ENDIAN, rb_int128_to_numeric, rb_numeric_to_int128, ruby_swap128_int)
1995
1996IO_BUFFER_DECLARE_TYPE(f32, float, RB_IO_BUFFER_LITTLE_ENDIAN, DBL2NUM, NUM2DBL, ruby_swapf32)
1997IO_BUFFER_DECLARE_TYPE(F32, float, RB_IO_BUFFER_BIG_ENDIAN, DBL2NUM, NUM2DBL, ruby_swapf32)
1998IO_BUFFER_DECLARE_TYPE(f64, double, RB_IO_BUFFER_LITTLE_ENDIAN, DBL2NUM, NUM2DBL, ruby_swapf64)
1999IO_BUFFER_DECLARE_TYPE(F64, double, RB_IO_BUFFER_BIG_ENDIAN, DBL2NUM, NUM2DBL, ruby_swapf64)
2000#undef IO_BUFFER_DECLARE_TYPE
2001
2002static inline size_t
2003io_buffer_buffer_type_size(ID buffer_type)
2004{
2005#define IO_BUFFER_DATA_TYPE_SIZE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return RB_IO_BUFFER_DATA_TYPE_##name##_SIZE;
2006 IO_BUFFER_DATA_TYPE_SIZE(U8)
2007 IO_BUFFER_DATA_TYPE_SIZE(S8)
2008 IO_BUFFER_DATA_TYPE_SIZE(u16)
2009 IO_BUFFER_DATA_TYPE_SIZE(U16)
2010 IO_BUFFER_DATA_TYPE_SIZE(s16)
2011 IO_BUFFER_DATA_TYPE_SIZE(S16)
2012 IO_BUFFER_DATA_TYPE_SIZE(u32)
2013 IO_BUFFER_DATA_TYPE_SIZE(U32)
2014 IO_BUFFER_DATA_TYPE_SIZE(s32)
2015 IO_BUFFER_DATA_TYPE_SIZE(S32)
2016 IO_BUFFER_DATA_TYPE_SIZE(u64)
2017 IO_BUFFER_DATA_TYPE_SIZE(U64)
2018 IO_BUFFER_DATA_TYPE_SIZE(s64)
2019 IO_BUFFER_DATA_TYPE_SIZE(S64)
2020 IO_BUFFER_DATA_TYPE_SIZE(u128)
2021 IO_BUFFER_DATA_TYPE_SIZE(U128)
2022 IO_BUFFER_DATA_TYPE_SIZE(s128)
2023 IO_BUFFER_DATA_TYPE_SIZE(S128)
2024 IO_BUFFER_DATA_TYPE_SIZE(f32)
2025 IO_BUFFER_DATA_TYPE_SIZE(F32)
2026 IO_BUFFER_DATA_TYPE_SIZE(f64)
2027 IO_BUFFER_DATA_TYPE_SIZE(F64)
2028#undef IO_BUFFER_DATA_TYPE_SIZE
2029
2030 rb_raise(rb_eArgError, "Invalid type name!");
2031}
2032
2033/*
2034 * call-seq:
2035 * size_of(buffer_type) -> byte size
2036 * size_of(array of buffer_type) -> byte size
2037 *
2038 * Returns the size of the given buffer type(s) in bytes.
2039 *
2040 * IO::Buffer.size_of(:u32) # => 4
2041 * IO::Buffer.size_of([:u32, :u32]) # => 8
2042 */
2043static VALUE
2044io_buffer_size_of(VALUE klass, VALUE buffer_type)
2045{
2046 if (RB_TYPE_P(buffer_type, T_ARRAY)) {
2047 size_t total = 0;
2048 for (long i = 0; i < RARRAY_LEN(buffer_type); i++) {
2049 total += io_buffer_buffer_type_size(RB_SYM2ID(RARRAY_AREF(buffer_type, i)));
2050 }
2051 return SIZET2NUM(total);
2052 }
2053 else {
2054 return SIZET2NUM(io_buffer_buffer_type_size(RB_SYM2ID(buffer_type)));
2055 }
2056}
2057
2058static inline VALUE
2059rb_io_buffer_get_value(const void* base, size_t size, ID buffer_type, size_t *offset)
2060{
2061#define IO_BUFFER_GET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return io_buffer_read_##name(base, size, offset);
2062 IO_BUFFER_GET_VALUE(U8)
2063 IO_BUFFER_GET_VALUE(S8)
2064
2065 IO_BUFFER_GET_VALUE(u16)
2066 IO_BUFFER_GET_VALUE(U16)
2067 IO_BUFFER_GET_VALUE(s16)
2068 IO_BUFFER_GET_VALUE(S16)
2069
2070 IO_BUFFER_GET_VALUE(u32)
2071 IO_BUFFER_GET_VALUE(U32)
2072 IO_BUFFER_GET_VALUE(s32)
2073 IO_BUFFER_GET_VALUE(S32)
2074
2075 IO_BUFFER_GET_VALUE(u64)
2076 IO_BUFFER_GET_VALUE(U64)
2077 IO_BUFFER_GET_VALUE(s64)
2078 IO_BUFFER_GET_VALUE(S64)
2079
2080 IO_BUFFER_GET_VALUE(u128)
2081 IO_BUFFER_GET_VALUE(U128)
2082 IO_BUFFER_GET_VALUE(s128)
2083 IO_BUFFER_GET_VALUE(S128)
2084
2085 IO_BUFFER_GET_VALUE(f32)
2086 IO_BUFFER_GET_VALUE(F32)
2087 IO_BUFFER_GET_VALUE(f64)
2088 IO_BUFFER_GET_VALUE(F64)
2089#undef IO_BUFFER_GET_VALUE
2090
2091 rb_raise(rb_eArgError, "Invalid type name!");
2092}
2093
2094/*
2095 * call-seq: get_value(buffer_type, offset) -> numeric
2096 *
2097 * Read from buffer a value of +type+ at +offset+. +buffer_type+ should be one
2098 * of symbols:
2099 *
2100 * * +:U8+: unsigned integer, 1 byte
2101 * * +:S8+: signed integer, 1 byte
2102 * * +:u16+: unsigned integer, 2 bytes, little-endian
2103 * * +:U16+: unsigned integer, 2 bytes, big-endian
2104 * * +:s16+: signed integer, 2 bytes, little-endian
2105 * * +:S16+: signed integer, 2 bytes, big-endian
2106 * * +:u32+: unsigned integer, 4 bytes, little-endian
2107 * * +:U32+: unsigned integer, 4 bytes, big-endian
2108 * * +:s32+: signed integer, 4 bytes, little-endian
2109 * * +:S32+: signed integer, 4 bytes, big-endian
2110 * * +:u64+: unsigned integer, 8 bytes, little-endian
2111 * * +:U64+: unsigned integer, 8 bytes, big-endian
2112 * * +:s64+: signed integer, 8 bytes, little-endian
2113 * * +:S64+: signed integer, 8 bytes, big-endian
2114 * * +:u128+: unsigned integer, 16 bytes, little-endian
2115 * * +:U128+: unsigned integer, 16 bytes, big-endian
2116 * * +:s128+: signed integer, 16 bytes, little-endian
2117 * * +:S128+: signed integer, 16 bytes, big-endian
2118 * * +:f32+: float, 4 bytes, little-endian
2119 * * +:F32+: float, 4 bytes, big-endian
2120 * * +:f64+: double, 8 bytes, little-endian
2121 * * +:F64+: double, 8 bytes, big-endian
2122 *
2123 * A buffer type refers specifically to the type of binary buffer that is stored
2124 * in the buffer. For example, a +:u32+ buffer type is a 32-bit unsigned
2125 * integer in little-endian format.
2126 *
2127 * string = [1.5].pack('f')
2128 * # => "\x00\x00\xC0?"
2129 * IO::Buffer.for(string).get_value(:f32, 0)
2130 * # => 1.5
2131 */
2132static VALUE
2133io_buffer_get_value(VALUE self, VALUE type, VALUE _offset)
2134{
2135 const void *base;
2136 size_t size;
2137 size_t offset = io_buffer_extract_offset(_offset);
2138
2139 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2140
2141 return rb_io_buffer_get_value(base, size, RB_SYM2ID(type), &offset);
2142}
2143
2144/*
2145 * call-seq: get_values(buffer_types, offset) -> array
2146 *
2147 * Similar to #get_value, except that it can handle multiple buffer types and
2148 * returns an array of values.
2149 *
2150 * string = [1.5, 2.5].pack('ff')
2151 * IO::Buffer.for(string).get_values([:f32, :f32], 0)
2152 * # => [1.5, 2.5]
2153 */
2154static VALUE
2155io_buffer_get_values(VALUE self, VALUE buffer_types, VALUE _offset)
2156{
2157 size_t offset = io_buffer_extract_offset(_offset);
2158
2159 const void *base;
2160 size_t size;
2161 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2162
2163 if (!RB_TYPE_P(buffer_types, T_ARRAY)) {
2164 rb_raise(rb_eArgError, "Argument buffer_types should be an array!");
2165 }
2166
2167 VALUE array = rb_ary_new_capa(RARRAY_LEN(buffer_types));
2168
2169 for (long i = 0; i < RARRAY_LEN(buffer_types); i++) {
2170 VALUE type = rb_ary_entry(buffer_types, i);
2171 VALUE value = rb_io_buffer_get_value(base, size, RB_SYM2ID(type), &offset);
2172 rb_ary_push(array, value);
2173 }
2174
2175 return array;
2176}
2177
2178// Extract a count argument, which must be a positive integer.
2179// Count is generally considered relative to the number of things.
2180static inline size_t
2181io_buffer_extract_count(VALUE argument)
2182{
2183 if (rb_int_negative_p(argument)) {
2184 rb_raise(rb_eArgError, "Count can't be negative!");
2185 }
2186
2187 return NUM2SIZET(argument);
2188}
2189
2190static inline void
2191io_buffer_extract_offset_count(ID buffer_type, size_t size, int argc, VALUE *argv, size_t *offset, size_t *count)
2192{
2193 if (argc >= 1) {
2194 *offset = io_buffer_extract_offset(argv[0]);
2195 }
2196 else {
2197 *offset = 0;
2198 }
2199
2200 if (argc >= 2) {
2201 *count = io_buffer_extract_count(argv[1]);
2202 }
2203 else {
2204 if (*offset > size) {
2205 rb_raise(rb_eArgError, "The given offset is bigger than the buffer size!");
2206 }
2207
2208 *count = (size - *offset) / io_buffer_buffer_type_size(buffer_type);
2209 }
2210}
2211
2212/*
2213 * call-seq:
2214 * each(buffer_type, [offset, [count]]) {|offset, value| ...} -> self
2215 * each(buffer_type, [offset, [count]]) -> enumerator
2216 *
2217 * Iterates over the buffer, yielding each +value+ of +buffer_type+ starting
2218 * from +offset+.
2219 *
2220 * If +count+ is given, only +count+ values will be yielded.
2221 *
2222 * IO::Buffer.for("Hello World").each(:U8, 2, 2) do |offset, value|
2223 * puts "#{offset}: #{value}"
2224 * end
2225 * # 2: 108
2226 * # 3: 108
2227 */
2228static VALUE
2229io_buffer_each(int argc, VALUE *argv, VALUE self)
2230{
2231 RETURN_ENUMERATOR_KW(self, argc, argv, RB_NO_KEYWORDS);
2232
2233 const void *base;
2234 size_t size;
2235
2236 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2237
2238 ID buffer_type;
2239 if (argc >= 1) {
2240 buffer_type = RB_SYM2ID(argv[0]);
2241 }
2242 else {
2243 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2244 }
2245
2246 size_t offset, count;
2247 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2248
2249 for (size_t i = 0; i < count; i++) {
2250 size_t current_offset = offset;
2251 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2252 rb_yield_values(2, SIZET2NUM(current_offset), value);
2253 }
2254
2255 return self;
2256}
2257
2258/*
2259 * call-seq: values(buffer_type, [offset, [count]]) -> array
2260 *
2261 * Returns an array of values of +buffer_type+ starting from +offset+.
2262 *
2263 * If +count+ is given, only +count+ values will be returned.
2264 *
2265 * IO::Buffer.for("Hello World").values(:U8, 2, 2)
2266 * # => [108, 108]
2267 */
2268static VALUE
2269io_buffer_values(int argc, VALUE *argv, VALUE self)
2270{
2271 const void *base;
2272 size_t size;
2273
2274 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2275
2276 ID buffer_type;
2277 if (argc >= 1) {
2278 buffer_type = RB_SYM2ID(argv[0]);
2279 }
2280 else {
2281 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2282 }
2283
2284 size_t offset, count;
2285 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2286
2287 VALUE array = rb_ary_new_capa(count);
2288
2289 for (size_t i = 0; i < count; i++) {
2290 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2291 rb_ary_push(array, value);
2292 }
2293
2294 return array;
2295}
2296
2297/*
2298 * call-seq:
2299 * each_byte([offset, [count]]) {|byte| ...} -> self
2300 * each_byte([offset, [count]]) -> enumerator
2301 *
2302 * Iterates over the buffer, yielding each byte starting from +offset+.
2303 *
2304 * If +count+ is given, only +count+ bytes will be yielded.
2305 *
2306 * IO::Buffer.for("Hello World").each_byte(2, 2) do |offset, byte|
2307 * puts "#{offset}: #{byte}"
2308 * end
2309 * # 2: 108
2310 * # 3: 108
2311 */
2312static VALUE
2313io_buffer_each_byte(int argc, VALUE *argv, VALUE self)
2314{
2315 RETURN_ENUMERATOR_KW(self, argc, argv, RB_NO_KEYWORDS);
2316
2317 const void *base;
2318 size_t size;
2319
2320 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2321
2322 size_t offset, count;
2323 io_buffer_extract_offset_count(RB_IO_BUFFER_DATA_TYPE_U8, size, argc, argv, &offset, &count);
2324
2325 for (size_t i = 0; i < count; i++) {
2326 unsigned char *value = (unsigned char *)base + i + offset;
2327 rb_yield(RB_INT2FIX(*value));
2328 }
2329
2330 return self;
2331}
2332
2333static inline void
2334rb_io_buffer_set_value(const void* base, size_t size, ID buffer_type, size_t *offset, VALUE value)
2335{
2336#define IO_BUFFER_SET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) {io_buffer_write_##name(base, size, offset, value); return;}
2337 IO_BUFFER_SET_VALUE(U8);
2338 IO_BUFFER_SET_VALUE(S8);
2339
2340 IO_BUFFER_SET_VALUE(u16);
2341 IO_BUFFER_SET_VALUE(U16);
2342 IO_BUFFER_SET_VALUE(s16);
2343 IO_BUFFER_SET_VALUE(S16);
2344
2345 IO_BUFFER_SET_VALUE(u32);
2346 IO_BUFFER_SET_VALUE(U32);
2347 IO_BUFFER_SET_VALUE(s32);
2348 IO_BUFFER_SET_VALUE(S32);
2349
2350 IO_BUFFER_SET_VALUE(u64);
2351 IO_BUFFER_SET_VALUE(U64);
2352 IO_BUFFER_SET_VALUE(s64);
2353 IO_BUFFER_SET_VALUE(S64);
2354
2355 IO_BUFFER_SET_VALUE(u128);
2356 IO_BUFFER_SET_VALUE(U128);
2357 IO_BUFFER_SET_VALUE(s128);
2358 IO_BUFFER_SET_VALUE(S128);
2359
2360 IO_BUFFER_SET_VALUE(f32);
2361 IO_BUFFER_SET_VALUE(F32);
2362 IO_BUFFER_SET_VALUE(f64);
2363 IO_BUFFER_SET_VALUE(F64);
2364#undef IO_BUFFER_SET_VALUE
2365
2366 rb_raise(rb_eArgError, "Invalid type name!");
2367}
2368
2369/*
2370 * call-seq: set_value(type, offset, value) -> offset
2371 *
2372 * Write to a buffer a +value+ of +type+ at +offset+. +type+ should be one of
2373 * symbols described in #get_value.
2374 *
2375 * buffer = IO::Buffer.new(8)
2376 * # =>
2377 * # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
2378 * # 0x00000000 00 00 00 00 00 00 00 00
2379 *
2380 * buffer.set_value(:U8, 1, 111)
2381 * # => 1
2382 *
2383 * buffer
2384 * # =>
2385 * # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
2386 * # 0x00000000 00 6f 00 00 00 00 00 00 .o......
2387 *
2388 * Note that if the +type+ is integer and +value+ is Float, the implicit truncation is performed:
2389 *
2390 * buffer = IO::Buffer.new(8)
2391 * buffer.set_value(:U32, 0, 2.5)
2392 *
2393 * buffer
2394 * # =>
2395 * # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
2396 * # 0x00000000 00 00 00 02 00 00 00 00
2397 * # ^^ the same as if we'd pass just integer 2
2398 */
2399static VALUE
2400io_buffer_set_value(VALUE self, VALUE type, VALUE _offset, VALUE value)
2401{
2402 void *base;
2403 size_t size;
2404 size_t offset = io_buffer_extract_offset(_offset);
2405
2406 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2407
2408 rb_io_buffer_set_value(base, size, RB_SYM2ID(type), &offset, value);
2409
2410 return SIZET2NUM(offset);
2411}
2412
2413/*
2414 * call-seq: set_values(buffer_types, offset, values) -> offset
2415 *
2416 * Write +values+ of +buffer_types+ at +offset+ to the buffer. +buffer_types+
2417 * should be an array of symbols as described in #get_value. +values+ should
2418 * be an array of values to write.
2419 *
2420 * buffer = IO::Buffer.new(8)
2421 * buffer.set_values([:U8, :U16], 0, [1, 2])
2422 * buffer
2423 * # =>
2424 * # #<IO::Buffer 0x696f717561746978+8 INTERNAL>
2425 * # 0x00000000 01 00 02 00 00 00 00 00 ........
2426 */
2427static VALUE
2428io_buffer_set_values(VALUE self, VALUE buffer_types, VALUE _offset, VALUE values)
2429{
2430 if (!RB_TYPE_P(buffer_types, T_ARRAY)) {
2431 rb_raise(rb_eArgError, "Argument buffer_types should be an array!");
2432 }
2433
2434 if (!RB_TYPE_P(values, T_ARRAY)) {
2435 rb_raise(rb_eArgError, "Argument values should be an array!");
2436 }
2437
2438 if (RARRAY_LEN(buffer_types) != RARRAY_LEN(values)) {
2439 rb_raise(rb_eArgError, "Argument buffer_types and values should have the same length!");
2440 }
2441
2442 size_t offset = io_buffer_extract_offset(_offset);
2443
2444 void *base;
2445 size_t size;
2446 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2447
2448 for (long i = 0; i < RARRAY_LEN(buffer_types); i++) {
2449 VALUE type = rb_ary_entry(buffer_types, i);
2450 VALUE value = rb_ary_entry(values, i);
2451 rb_io_buffer_set_value(base, size, RB_SYM2ID(type), &offset, value);
2452 }
2453
2454 return SIZET2NUM(offset);
2455}
2456
2457static size_t IO_BUFFER_BLOCKING_SIZE = 1024*1024;
2458
2459 struct io_buffer_memmove_arguments {
2460 unsigned char * destination;
2461 const unsigned char * source;
2462 size_t length;
2463};
2464
2465static void *
2466io_buffer_memmove_blocking(void *data)
2467{
2468 struct io_buffer_memmove_arguments *arguments = (struct io_buffer_memmove_arguments *)data;
2469
2470 memmove(arguments->destination, arguments->source, arguments->length);
2471
2472 return NULL;
2473}
2474
2475static void
2476io_buffer_memmove_unblock(void *data)
2477{
2478 // No safe way to interrupt.
2479}
2480
2481static void
2482io_buffer_memmove(struct rb_io_buffer *buffer, size_t offset, const void *source_base, size_t source_offset, size_t source_size, size_t length)
2483{
2484 void *base;
2485 size_t size;
2486 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2487
2488 io_buffer_validate_range(buffer, offset, length);
2489
2490 if (size_sum_is_bigger_than(source_offset, length, source_size)) {
2491 rb_raise(rb_eArgError, "The computed source range exceeds the size of the source buffer!");
2492 }
2493
2494 struct io_buffer_memmove_arguments arguments = {
2495 .destination = (unsigned char*)base+offset,
2496 .source = (unsigned char*)source_base+source_offset,
2497 .length = length
2498 };
2499
2500 if (arguments.length >= IO_BUFFER_BLOCKING_SIZE) {
2501 rb_nogvl(io_buffer_memmove_blocking, &arguments, io_buffer_memmove_unblock, &arguments, RB_NOGVL_OFFLOAD_SAFE);
2502 } else if (arguments.length != 0) {
2503 memmove(arguments.destination, arguments.source, arguments.length);
2504 }
2505}
2506
2507// (offset, length, source_offset) -> length
2508static VALUE
2509io_buffer_copy_from(struct rb_io_buffer *buffer, const void *source_base, size_t source_size, int argc, VALUE *argv)
2510{
2511 size_t offset = 0;
2512 size_t length;
2513 size_t source_offset;
2514
2515 // The offset we copy into the buffer:
2516 if (argc >= 1) {
2517 offset = io_buffer_extract_offset(argv[0]);
2518 }
2519
2520 // The offset we start from within the string:
2521 if (argc >= 3) {
2522 source_offset = io_buffer_extract_offset(argv[2]);
2523
2524 if (source_offset > source_size) {
2525 rb_raise(rb_eArgError, "The given source offset is bigger than the source itself!");
2526 }
2527 }
2528 else {
2529 source_offset = 0;
2530 }
2531
2532 // The length we are going to copy:
2533 if (argc >= 2 && !RB_NIL_P(argv[1])) {
2534 length = io_buffer_extract_length(argv[1]);
2535 }
2536 else {
2537 // Default to the source offset -> source size:
2538 length = source_size - source_offset;
2539 }
2540
2541 io_buffer_memmove(buffer, offset, source_base, source_offset, source_size, length);
2542
2543 return SIZET2NUM(length);
2544}
2545
2546/*
2547 * call-seq:
2548 * dup -> io_buffer
2549 * clone -> io_buffer
2550 *
2551 * Make an internal copy of the source buffer. Updates to the copy will not
2552 * affect the source buffer.
2553 *
2554 * source = IO::Buffer.for("Hello World")
2555 * # =>
2556 * # #<IO::Buffer 0x00007fd598466830+11 EXTERNAL READONLY SLICE>
2557 * # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
2558 * buffer = source.dup
2559 * # =>
2560 * # #<IO::Buffer 0x0000558cbec03320+11 INTERNAL>
2561 * # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
2562 */
2563static VALUE
2564rb_io_buffer_initialize_copy(VALUE self, VALUE source)
2565{
2566 struct rb_io_buffer *buffer = NULL;
2567 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
2568
2569 const void *source_base;
2570 size_t source_size;
2571
2572 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2573
2574 io_buffer_initialize(self, buffer, NULL, source_size, io_flags_for_size(source_size), Qnil);
2575
2576 VALUE result = io_buffer_copy_from(buffer, source_base, source_size, 0, NULL);
2577 RB_GC_GUARD(source);
2578 return result;
2579}
2580
2581/*
2582 * call-seq:
2583 * copy(source, [offset, [length, [source_offset]]]) -> size
2584 *
2585 * Efficiently copy from a source IO::Buffer into the buffer, at +offset+
2586 * using +memmove+. For copying String instances, see #set_string.
2587 *
2588 * buffer = IO::Buffer.new(32)
2589 * # =>
2590 * # #<IO::Buffer 0x0000555f5ca22520+32 INTERNAL>
2591 * # 0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
2592 * # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ *
2593 *
2594 * buffer.copy(IO::Buffer.for("test"), 8)
2595 * # => 4 -- size of buffer copied
2596 * buffer
2597 * # =>
2598 * # #<IO::Buffer 0x0000555f5cf8fe40+32 INTERNAL>
2599 * # 0x00000000 00 00 00 00 00 00 00 00 74 65 73 74 00 00 00 00 ........test....
2600 * # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ *
2601 *
2602 * #copy can be used to put buffer into strings associated with buffer:
2603 *
2604 * string = "data: "
2605 * # => "data: "
2606 * buffer = IO::Buffer.for(string) do |buffer|
2607 * buffer.copy(IO::Buffer.for("test"), 5)
2608 * end
2609 * # => 4
2610 * string
2611 * # => "data:test"
2612 *
2613 * Attempt to copy into a read-only buffer will fail:
2614 *
2615 * File.write('test.txt', 'test')
2616 * buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::READONLY)
2617 * buffer.copy(IO::Buffer.for("test"), 8)
2618 * # in `copy': Buffer is not writable! (IO::Buffer::AccessError)
2619 *
2620 * See ::map for details of creation of mutable file mappings, this will
2621 * work:
2622 *
2623 * buffer = IO::Buffer.map(File.open('test.txt', 'r+'))
2624 * buffer.copy(IO::Buffer.for("boom"), 0)
2625 * # => 4
2626 * File.read('test.txt')
2627 * # => "boom"
2628 *
2629 * Attempt to copy the buffer which will need place outside of buffer's
2630 * bounds will fail:
2631 *
2632 * buffer = IO::Buffer.new(2)
2633 * buffer.copy(IO::Buffer.for('test'), 0)
2634 * # in `copy': Specified offset+length is bigger than the buffer size! (ArgumentError)
2635 *
2636 * It is safe to copy between memory regions that overlaps each other.
2637 * In such case, the data is copied as if the data was first copied from the source buffer to
2638 * a temporary buffer, and then copied from the temporary buffer to the destination buffer.
2639 *
2640 * buffer = IO::Buffer.new(10)
2641 * buffer.set_string("0123456789")
2642 * buffer.copy(buffer, 3, 7)
2643 * # => 7
2644 * buffer
2645 * # =>
2646 * # #<IO::Buffer 0x000056494f8ce440+10 INTERNAL>
2647 * # 0x00000000 30 31 32 30 31 32 33 34 35 36 0120123456
2648 */
2649static VALUE
2650io_buffer_copy(int argc, VALUE *argv, VALUE self)
2651{
2652 rb_check_arity(argc, 1, 4);
2653
2654 struct rb_io_buffer *buffer = NULL;
2655 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
2656
2657 VALUE source = argv[0];
2658 const void *source_base;
2659 size_t source_size;
2660
2661 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2662
2663 VALUE result = io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2664 RB_GC_GUARD(source);
2665 return result;
2666}
2667
2668/*
2669 * call-seq: get_string([offset, [length, [encoding]]]) -> string
2670 *
2671 * Read a chunk or all of the buffer into a string, in the specified
2672 * +encoding+. If no encoding is provided +Encoding::BINARY+ is used.
2673 *
2674 * buffer = IO::Buffer.for('test')
2675 * buffer.get_string
2676 * # => "test"
2677 * buffer.get_string(2)
2678 * # => "st"
2679 * buffer.get_string(2, 1)
2680 * # => "s"
2681 */
2682static VALUE
2683io_buffer_get_string(int argc, VALUE *argv, VALUE self)
2684{
2685 rb_check_arity(argc, 0, 3);
2686
2687 size_t offset, length;
2688 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
2689
2690 const void *base;
2691 size_t size;
2692 io_buffer_get_bytes_for_reading(buffer, &base, &size);
2693
2694 rb_encoding *encoding;
2695 if (argc >= 3) {
2696 encoding = rb_find_encoding(argv[2]);
2697 }
2698 else {
2699 encoding = rb_ascii8bit_encoding();
2700 }
2701
2702 io_buffer_validate_range(buffer, offset, length);
2703
2704 return rb_enc_str_new((const char*)base + offset, length, encoding);
2705}
2706
2707/*
2708 * call-seq: set_string(string, [offset, [length, [source_offset]]]) -> size
2709 *
2710 * Efficiently copy from a source String into the buffer, at +offset+ using
2711 * +memmove+.
2712 *
2713 * buf = IO::Buffer.new(8)
2714 * # =>
2715 * # #<IO::Buffer 0x0000557412714a20+8 INTERNAL>
2716 * # 0x00000000 00 00 00 00 00 00 00 00 ........
2717 *
2718 * # set buffer starting from offset 1, take 2 bytes starting from string's
2719 * # second
2720 * buf.set_string('test', 1, 2, 1)
2721 * # => 2
2722 * buf
2723 * # =>
2724 * # #<IO::Buffer 0x0000557412714a20+8 INTERNAL>
2725 * # 0x00000000 00 65 73 00 00 00 00 00 .es.....
2726 *
2727 * See also #copy for examples of how buffer writing might be used for changing
2728 * associated strings and files.
2729 */
2730static VALUE
2731io_buffer_set_string(int argc, VALUE *argv, VALUE self)
2732{
2733 rb_check_arity(argc, 1, 4);
2734
2735 struct rb_io_buffer *buffer = NULL;
2736 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
2737
2738 VALUE string = rb_str_to_str(argv[0]);
2739
2740 const void *source_base = RSTRING_PTR(string);
2741 size_t source_size = RSTRING_LEN(string);
2742
2743 VALUE result = io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2744 RB_GC_GUARD(string);
2745 return result;
2746}
2747
2748void
2749rb_io_buffer_clear(VALUE self, uint8_t value, size_t offset, size_t length)
2750{
2751 struct rb_io_buffer *buffer = NULL;
2752 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
2753
2754 void *base;
2755 size_t size;
2756 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2757
2758 io_buffer_validate_range(buffer, offset, length);
2759
2760 memset((char*)base + offset, value, length);
2761}
2762
2763/*
2764 * call-seq: clear(value = 0, [offset, [length]]) -> self
2765 *
2766 * Fill buffer with +value+, starting with +offset+ and going for +length+
2767 * bytes.
2768 *
2769 * buffer = IO::Buffer.for('test').dup
2770 * # =>
2771 * # <IO::Buffer 0x00007fca40087c38+4 INTERNAL>
2772 * # 0x00000000 74 65 73 74 test
2773 *
2774 * buffer.clear
2775 * # =>
2776 * # <IO::Buffer 0x00007fca40087c38+4 INTERNAL>
2777 * # 0x00000000 00 00 00 00 ....
2778 *
2779 * buf.clear(1) # fill with 1
2780 * # =>
2781 * # <IO::Buffer 0x00007fca40087c38+4 INTERNAL>
2782 * # 0x00000000 01 01 01 01 ....
2783 *
2784 * buffer.clear(2, 1, 2) # fill with 2, starting from offset 1, for 2 bytes
2785 * # =>
2786 * # <IO::Buffer 0x00007fca40087c38+4 INTERNAL>
2787 * # 0x00000000 01 02 02 01 ....
2788 *
2789 * buffer.clear(2, 1) # fill with 2, starting from offset 1
2790 * # =>
2791 * # <IO::Buffer 0x00007fca40087c38+4 INTERNAL>
2792 * # 0x00000000 01 02 02 02 ....
2793 */
2794static VALUE
2795io_buffer_clear(int argc, VALUE *argv, VALUE self)
2796{
2797 rb_check_arity(argc, 0, 3);
2798
2799 uint8_t value = 0;
2800 if (argc >= 1) {
2801 value = NUM2UINT(argv[0]);
2802 }
2803
2804 size_t offset, length;
2805 io_buffer_extract_offset_length(self, argc-1, argv+1, &offset, &length);
2806
2807 rb_io_buffer_clear(self, value, offset, length);
2808
2809 return self;
2810}
2811
2812static size_t
2813io_buffer_default_size(size_t page_size)
2814{
2815 // Platform agnostic default size, based on empirical performance observation:
2816 const size_t platform_agnostic_default_size = 64*1024;
2817
2818 // Allow user to specify custom default buffer size:
2819 const char *default_size = getenv("RUBY_IO_BUFFER_DEFAULT_SIZE");
2820 if (default_size) {
2821 // For the purpose of setting a default size, 2^31 is an acceptable maximum:
2822 int value = atoi(default_size);
2823
2824 // assuming sizeof(int) <= sizeof(size_t)
2825 if (value > 0) {
2826 return value;
2827 }
2828 }
2829
2830 if (platform_agnostic_default_size < page_size) {
2831 return page_size;
2832 }
2833
2834 return platform_agnostic_default_size;
2835}
2836
2837 struct io_buffer_blocking_region_argument {
2838 struct rb_io *io;
2839 struct rb_io_buffer *buffer;
2840 rb_blocking_function_t *function;
2841 void *data;
2842};
2843
2844static VALUE
2845io_buffer_blocking_region_begin(VALUE _argument)
2846{
2847 struct io_buffer_blocking_region_argument *argument = (void*)_argument;
2848
2849 return rb_io_blocking_region(argument->io, argument->function, argument->data);
2850}
2851
2852static VALUE
2853io_buffer_blocking_region_ensure(VALUE _argument)
2854{
2855 struct io_buffer_blocking_region_argument *argument = (void*)_argument;
2856
2857 io_buffer_unlock(argument->buffer);
2858
2859 return Qnil;
2860}
2861
2862static VALUE
2863io_buffer_blocking_region(VALUE io, struct rb_io_buffer *buffer, rb_blocking_function_t *function, void *data)
2864{
2865 struct rb_io *ioptr;
2866 RB_IO_POINTER(io, ioptr);
2867
2868 struct io_buffer_blocking_region_argument argument = {
2869 .io = ioptr,
2870 .buffer = buffer,
2871 .function = function,
2872 .data = data,
2873 };
2874
2875 // If the buffer is already locked, we can skip the ensure (unlock):
2876 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
2877 return io_buffer_blocking_region_begin((VALUE)&argument);
2878 }
2879 else {
2880 // The buffer should be locked for the duration of the blocking region:
2881 io_buffer_lock(buffer);
2882
2883 return rb_ensure(io_buffer_blocking_region_begin, (VALUE)&argument, io_buffer_blocking_region_ensure, (VALUE)&argument);
2884 }
2885}
2886
2887 struct io_buffer_read_internal_argument {
2888 // The file descriptor to read from:
2889 int descriptor;
2890 // The base pointer to read from:
2891 char *base;
2892 // The size of the buffer:
2893 size_t size;
2894 // The minimum number of bytes to read:
2895 size_t length;
2896};
2897
2898static VALUE
2899io_buffer_read_internal(void *_argument)
2900{
2901 size_t total = 0;
2902 struct io_buffer_read_internal_argument *argument = _argument;
2903
2904 while (true) {
2905 ssize_t result = read(argument->descriptor, argument->base, argument->size);
2906
2907 if (result < 0) {
2908 return rb_fiber_scheduler_io_result(result, errno);
2909 }
2910 else if (result == 0) {
2911 return rb_fiber_scheduler_io_result(total, 0);
2912 }
2913 else {
2914 total += result;
2915
2916 if (total >= argument->length) {
2917 return rb_fiber_scheduler_io_result(total, 0);
2918 }
2919
2920 argument->base = argument->base + result;
2921 argument->size = argument->size - result;
2922 }
2923 }
2924}
2925
2926 VALUE
2927rb_io_buffer_read(VALUE self, VALUE io, size_t length, size_t offset)
2928{
2929 io = rb_io_get_io(io);
2930
2931 VALUE scheduler = rb_fiber_scheduler_current();
2932 if (scheduler != Qnil) {
2933 VALUE result = rb_fiber_scheduler_io_read(scheduler, io, self, length, offset);
2934
2935 if (!UNDEF_P(result)) {
2936 return result;
2937 }
2938 }
2939
2940 struct rb_io_buffer *buffer = NULL;
2941 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
2942
2943 io_buffer_validate_range(buffer, offset, length);
2944
2945 int descriptor = rb_io_descriptor(io);
2946
2947 void * base;
2948 size_t size;
2949 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2950
2951 base = (unsigned char*)base + offset;
2952 size = size - offset;
2953
2954 struct io_buffer_read_internal_argument argument = {
2955 .descriptor = descriptor,
2956 .base = base,
2957 .size = size,
2958 .length = length,
2959 };
2960
2961 return io_buffer_blocking_region(io, buffer, io_buffer_read_internal, &argument);
2962}
2963
2964/*
2965 * call-seq: read(io, [length, [offset]]) -> read length or -errno
2966 *
2967 * Read at least +length+ bytes from the +io+, into the buffer starting at
2968 * +offset+. If an error occurs, return <tt>-errno</tt>.
2969 *
2970 * If +length+ is not given or +nil+, it defaults to the size of the buffer
2971 * minus the offset, i.e. the entire buffer.
2972 *
2973 * If +length+ is zero, exactly one <tt>read</tt> operation will occur.
2974 *
2975 * If +offset+ is not given, it defaults to zero, i.e. the beginning of the
2976 * buffer.
2977 *
2978 * IO::Buffer.for('test') do |buffer|
2979 * p buffer
2980 * # =>
2981 * # <IO::Buffer 0x00007fca40087c38+4 SLICE>
2982 * # 0x00000000 74 65 73 74 test
2983 * buffer.read(File.open('/dev/urandom', 'rb'), 2)
2984 * p buffer
2985 * # =>
2986 * # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
2987 * # 0x00000000 05 35 73 74 .5st
2988 * end
2989 */
2990static VALUE
2991io_buffer_read(int argc, VALUE *argv, VALUE self)
2992{
2993 rb_check_arity(argc, 1, 3);
2994
2995 VALUE io = argv[0];
2996
2997 size_t length, offset;
2998 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
2999
3000 return rb_io_buffer_read(self, io, length, offset);
3001}
3002
3003 struct io_buffer_pread_internal_argument {
3004 // The file descriptor to read from:
3005 int descriptor;
3006 // The base pointer to read from:
3007 char *base;
3008 // The size of the buffer:
3009 size_t size;
3010 // The minimum number of bytes to read:
3011 size_t length;
3012 // The offset to read from:
3013 off_t offset;
3014};
3015
3016static VALUE
3017io_buffer_pread_internal(void *_argument)
3018{
3019 size_t total = 0;
3020 struct io_buffer_pread_internal_argument *argument = _argument;
3021
3022 while (true) {
3023 ssize_t result = pread(argument->descriptor, argument->base, argument->size, argument->offset);
3024
3025 if (result < 0) {
3026 return rb_fiber_scheduler_io_result(result, errno);
3027 }
3028 else if (result == 0) {
3029 return rb_fiber_scheduler_io_result(total, 0);
3030 }
3031 else {
3032 total += result;
3033
3034 if (total >= argument->length) {
3035 return rb_fiber_scheduler_io_result(total, 0);
3036 }
3037
3038 argument->base = argument->base + result;
3039 argument->size = argument->size - result;
3040 argument->offset = argument->offset + result;
3041 }
3042 }
3043}
3044
3045 VALUE
3046rb_io_buffer_pread(VALUE self, VALUE io, rb_off_t from, size_t length, size_t offset)
3047{
3048 io = rb_io_get_io(io);
3049
3050 VALUE scheduler = rb_fiber_scheduler_current();
3051 if (scheduler != Qnil) {
3052 VALUE result = rb_fiber_scheduler_io_pread(scheduler, io, from, self, length, offset);
3053
3054 if (!UNDEF_P(result)) {
3055 return result;
3056 }
3057 }
3058
3059 struct rb_io_buffer *buffer = NULL;
3060 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3061
3062 io_buffer_validate_range(buffer, offset, length);
3063
3064 int descriptor = rb_io_descriptor(io);
3065
3066 void * base;
3067 size_t size;
3068 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3069
3070 base = (unsigned char*)base + offset;
3071 size = size - offset;
3072
3073 struct io_buffer_pread_internal_argument argument = {
3074 .descriptor = descriptor,
3075 .base = base,
3076 .size = size,
3077 .length = length,
3078 .offset = from,
3079 };
3080
3081 return io_buffer_blocking_region(io, buffer, io_buffer_pread_internal, &argument);
3082}
3083
3084/*
3085 * call-seq: pread(io, from, [length, [offset]]) -> read length or -errno
3086 *
3087 * Read at least +length+ bytes from the +io+ starting at the specified +from+
3088 * position, into the buffer starting at +offset+. If an error occurs,
3089 * return <tt>-errno</tt>.
3090 *
3091 * If +length+ is not given or +nil+, it defaults to the size of the buffer
3092 * minus the offset, i.e. the entire buffer.
3093 *
3094 * If +length+ is zero, exactly one <tt>pread</tt> operation will occur.
3095 *
3096 * If +offset+ is not given, it defaults to zero, i.e. the beginning of the
3097 * buffer.
3098 *
3099 * IO::Buffer.for('test') do |buffer|
3100 * p buffer
3101 * # =>
3102 * # <IO::Buffer 0x00007fca40087c38+4 SLICE>
3103 * # 0x00000000 74 65 73 74 test
3104 *
3105 * # take 2 bytes from the beginning of urandom,
3106 * # put them in buffer starting from position 2
3107 * buffer.pread(File.open('/dev/urandom', 'rb'), 0, 2, 2)
3108 * p buffer
3109 * # =>
3110 * # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
3111 * # 0x00000000 05 35 73 74 te.5
3112 * end
3113 */
3114static VALUE
3115io_buffer_pread(int argc, VALUE *argv, VALUE self)
3116{
3117 rb_check_arity(argc, 2, 4);
3118
3119 VALUE io = argv[0];
3120 rb_off_t from = NUM2OFFT(argv[1]);
3121
3122 size_t length, offset;
3123 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
3124
3125 return rb_io_buffer_pread(self, io, from, length, offset);
3126}
3127
3128 struct io_buffer_write_internal_argument {
3129 // The file descriptor to write to:
3130 int descriptor;
3131 // The base pointer to write from:
3132 const char *base;
3133 // The size of the buffer:
3134 size_t size;
3135 // The minimum length to write:
3136 size_t length;
3137};
3138
3139static VALUE
3140io_buffer_write_internal(void *_argument)
3141{
3142 size_t total = 0;
3143 struct io_buffer_write_internal_argument *argument = _argument;
3144
3145 while (true) {
3146 ssize_t result = write(argument->descriptor, argument->base, argument->size);
3147
3148 if (result < 0) {
3149 return rb_fiber_scheduler_io_result(result, errno);
3150 }
3151 else if (result == 0) {
3152 return rb_fiber_scheduler_io_result(total, 0);
3153 }
3154 else {
3155 total += result;
3156
3157 if (total >= argument->length) {
3158 return rb_fiber_scheduler_io_result(total, 0);
3159 }
3160
3161 argument->base = argument->base + result;
3162 argument->size = argument->size - result;
3163 }
3164 }
3165}
3166
3167 VALUE
3168rb_io_buffer_write(VALUE self, VALUE io, size_t length, size_t offset)
3169{
3170 io = rb_io_get_write_io(rb_io_get_io(io));
3171
3172 VALUE scheduler = rb_fiber_scheduler_current();
3173 if (scheduler != Qnil) {
3174 VALUE result = rb_fiber_scheduler_io_write(scheduler, io, self, length, offset);
3175
3176 if (!UNDEF_P(result)) {
3177 return result;
3178 }
3179 }
3180
3181 struct rb_io_buffer *buffer = NULL;
3182 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3183
3184 io_buffer_validate_range(buffer, offset, length);
3185
3186 int descriptor = rb_io_descriptor(io);
3187
3188 const void * base;
3189 size_t size;
3190 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3191
3192 base = (unsigned char*)base + offset;
3193 size = size - offset;
3194
3195 struct io_buffer_write_internal_argument argument = {
3196 .descriptor = descriptor,
3197 .base = base,
3198 .size = size,
3199 .length = length,
3200 };
3201
3202 return io_buffer_blocking_region(io, buffer, io_buffer_write_internal, &argument);
3203}
3204
3205/*
3206 * call-seq: write(io, [length, [offset]]) -> written length or -errno
3207 *
3208 * Write at least +length+ bytes from the buffer starting at +offset+, into the +io+.
3209 * If an error occurs, return <tt>-errno</tt>.
3210 *
3211 * If +length+ is not given or +nil+, it defaults to the size of the buffer
3212 * minus the offset, i.e. the entire buffer.
3213 *
3214 * If +length+ is zero, exactly one <tt>write</tt> operation will occur.
3215 *
3216 * If +offset+ is not given, it defaults to zero, i.e. the beginning of the
3217 * buffer.
3218 *
3219 * out = File.open('output.txt', 'wb')
3220 * IO::Buffer.for('1234567').write(out, 3)
3221 *
3222 * This leads to +123+ being written into <tt>output.txt</tt>
3223 */
3224static VALUE
3225io_buffer_write(int argc, VALUE *argv, VALUE self)
3226{
3227 rb_check_arity(argc, 1, 3);
3228
3229 VALUE io = argv[0];
3230
3231 size_t length, offset;
3232 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
3233
3234 return rb_io_buffer_write(self, io, length, offset);
3235}
3236
3237 struct io_buffer_pwrite_internal_argument {
3238 // The file descriptor to write to:
3239 int descriptor;
3240 // The base pointer to write from:
3241 const char *base;
3242 // The size of the buffer:
3243 size_t size;
3244 // The minimum length to write:
3245 size_t length;
3246 // The offset to write to:
3247 off_t offset;
3248};
3249
3250static VALUE
3251io_buffer_pwrite_internal(void *_argument)
3252{
3253 size_t total = 0;
3254 struct io_buffer_pwrite_internal_argument *argument = _argument;
3255
3256 while (true) {
3257 ssize_t result = pwrite(argument->descriptor, argument->base, argument->size, argument->offset);
3258
3259 if (result < 0) {
3260 return rb_fiber_scheduler_io_result(result, errno);
3261 }
3262 else if (result == 0) {
3263 return rb_fiber_scheduler_io_result(total, 0);
3264 }
3265 else {
3266 total += result;
3267
3268 if (total >= argument->length) {
3269 return rb_fiber_scheduler_io_result(total, 0);
3270 }
3271
3272 argument->base = argument->base + result;
3273 argument->size = argument->size - result;
3274 argument->offset = argument->offset + result;
3275 }
3276 }
3277}
3278
3279 VALUE
3280rb_io_buffer_pwrite(VALUE self, VALUE io, rb_off_t from, size_t length, size_t offset)
3281{
3282 io = rb_io_get_write_io(rb_io_get_io(io));
3283
3284 VALUE scheduler = rb_fiber_scheduler_current();
3285 if (scheduler != Qnil) {
3286 VALUE result = rb_fiber_scheduler_io_pwrite(scheduler, io, from, self, length, offset);
3287
3288 if (!UNDEF_P(result)) {
3289 return result;
3290 }
3291 }
3292
3293 struct rb_io_buffer *buffer = NULL;
3294 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3295
3296 io_buffer_validate_range(buffer, offset, length);
3297
3298 int descriptor = rb_io_descriptor(io);
3299
3300 const void * base;
3301 size_t size;
3302 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3303
3304 base = (unsigned char*)base + offset;
3305 size = size - offset;
3306
3307 struct io_buffer_pwrite_internal_argument argument = {
3308 .descriptor = descriptor,
3309
3310 // Move the base pointer to the offset:
3311 .base = base,
3312
3313 // And the size to the length of buffer we want to read:
3314 .size = size,
3315
3316 // And the length of the buffer we want to write:
3317 .length = length,
3318
3319 // And the offset in the file we want to write from:
3320 .offset = from,
3321 };
3322
3323 return io_buffer_blocking_region(io, buffer, io_buffer_pwrite_internal, &argument);
3324}
3325
3326/*
3327 * call-seq: pwrite(io, from, [length, [offset]]) -> written length or -errno
3328 *
3329 * Write at least +length+ bytes from the buffer starting at +offset+, into
3330 * the +io+ starting at the specified +from+ position. If an error occurs,
3331 * return <tt>-errno</tt>.
3332 *
3333 * If +length+ is not given or +nil+, it defaults to the size of the buffer
3334 * minus the offset, i.e. the entire buffer.
3335 *
3336 * If +length+ is zero, exactly one <tt>pwrite</tt> operation will occur.
3337 *
3338 * If +offset+ is not given, it defaults to zero, i.e. the beginning of the
3339 * buffer.
3340 *
3341 * If the +from+ position is beyond the end of the file, the gap will be
3342 * filled with null (0 value) bytes.
3343 *
3344 * out = File.open('output.txt', File::RDWR) # open for read/write, no truncation
3345 * IO::Buffer.for('1234567').pwrite(out, 2, 3, 1)
3346 *
3347 * This leads to +234+ (3 bytes, starting from position 1) being written into
3348 * <tt>output.txt</tt>, starting from file position 2.
3349 */
3350static VALUE
3351io_buffer_pwrite(int argc, VALUE *argv, VALUE self)
3352{
3353 rb_check_arity(argc, 2, 4);
3354
3355 VALUE io = argv[0];
3356 rb_off_t from = NUM2OFFT(argv[1]);
3357
3358 size_t length, offset;
3359 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
3360
3361 return rb_io_buffer_pwrite(self, io, from, length, offset);
3362}
3363
3364static inline void
3365io_buffer_check_mask(const struct rb_io_buffer *buffer)
3366{
3367 if (buffer->size == 0)
3368 rb_raise(rb_eIOBufferMaskError, "Zero-length mask given!");
3369}
3370
3371static void
3372memory_and(unsigned char * restrict output, unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3373{
3374 for (size_t offset = 0; offset < size; offset += 1) {
3375 output[offset] = base[offset] & mask[offset % mask_size];
3376 }
3377}
3378
3379/*
3380 * call-seq:
3381 * source & mask -> io_buffer
3382 *
3383 * Generate a new buffer the same size as the source by applying the binary AND
3384 * operation to the source, using the mask, repeating as necessary.
3385 *
3386 * IO::Buffer.for("1234567890") & IO::Buffer.for("\xFF\x00\x00\xFF")
3387 * # =>
3388 * # #<IO::Buffer 0x00005589b2758480+4 INTERNAL>
3389 * # 0x00000000 31 00 00 34 35 00 00 38 39 00 1..45..89.
3390 */
3391static VALUE
3392io_buffer_and(VALUE self, VALUE mask)
3393{
3394 struct rb_io_buffer *buffer = NULL;
3395 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3396
3397 struct rb_io_buffer *mask_buffer = NULL;
3398 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3399
3400 io_buffer_check_mask(mask_buffer);
3401
3402 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3403 struct rb_io_buffer *output_buffer = NULL;
3404 TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer);
3405
3406 memory_and(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3407
3408 return output;
3409}
3410
3411static void
3412memory_or(unsigned char * restrict output, unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3413{
3414 for (size_t offset = 0; offset < size; offset += 1) {
3415 output[offset] = base[offset] | mask[offset % mask_size];
3416 }
3417}
3418
3419/*
3420 * call-seq:
3421 * source | mask -> io_buffer
3422 *
3423 * Generate a new buffer the same size as the source by applying the binary OR
3424 * operation to the source, using the mask, repeating as necessary.
3425 *
3426 * IO::Buffer.for("1234567890") | IO::Buffer.for("\xFF\x00\x00\xFF")
3427 * # =>
3428 * # #<IO::Buffer 0x0000561785ae3480+10 INTERNAL>
3429 * # 0x00000000 ff 32 33 ff ff 36 37 ff ff 30 .23..67..0
3430 */
3431static VALUE
3432io_buffer_or(VALUE self, VALUE mask)
3433{
3434 struct rb_io_buffer *buffer = NULL;
3435 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3436
3437 struct rb_io_buffer *mask_buffer = NULL;
3438 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3439
3440 io_buffer_check_mask(mask_buffer);
3441
3442 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3443 struct rb_io_buffer *output_buffer = NULL;
3444 TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer);
3445
3446 memory_or(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3447
3448 return output;
3449}
3450
3451static void
3452memory_xor(unsigned char * restrict output, unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3453{
3454 for (size_t offset = 0; offset < size; offset += 1) {
3455 output[offset] = base[offset] ^ mask[offset % mask_size];
3456 }
3457}
3458
3459/*
3460 * call-seq:
3461 * source ^ mask -> io_buffer
3462 *
3463 * Generate a new buffer the same size as the source by applying the binary XOR
3464 * operation to the source, using the mask, repeating as necessary.
3465 *
3466 * IO::Buffer.for("1234567890") ^ IO::Buffer.for("\xFF\x00\x00\xFF")
3467 * # =>
3468 * # #<IO::Buffer 0x000055a2d5d10480+10 INTERNAL>
3469 * # 0x00000000 ce 32 33 cb ca 36 37 c7 c6 30 .23..67..0
3470 */
3471static VALUE
3472io_buffer_xor(VALUE self, VALUE mask)
3473{
3474 struct rb_io_buffer *buffer = NULL;
3475 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3476
3477 struct rb_io_buffer *mask_buffer = NULL;
3478 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3479
3480 io_buffer_check_mask(mask_buffer);
3481
3482 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3483 struct rb_io_buffer *output_buffer = NULL;
3484 TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer);
3485
3486 memory_xor(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3487
3488 return output;
3489}
3490
3491static void
3492memory_not(unsigned char * restrict output, unsigned char * restrict base, size_t size)
3493{
3494 for (size_t offset = 0; offset < size; offset += 1) {
3495 output[offset] = ~base[offset];
3496 }
3497}
3498
3499/*
3500 * call-seq:
3501 * ~source -> io_buffer
3502 *
3503 * Generate a new buffer the same size as the source by applying the binary NOT
3504 * operation to the source.
3505 *
3506 * ~IO::Buffer.for("1234567890")
3507 * # =>
3508 * # #<IO::Buffer 0x000055a5ac42f120+10 INTERNAL>
3509 * # 0x00000000 ce cd cc cb ca c9 c8 c7 c6 cf ..........
3510 */
3511static VALUE
3512io_buffer_not(VALUE self)
3513{
3514 struct rb_io_buffer *buffer = NULL;
3515 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3516
3517 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3518 struct rb_io_buffer *output_buffer = NULL;
3519 TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer);
3520
3521 memory_not(output_buffer->base, buffer->base, buffer->size);
3522
3523 return output;
3524}
3525
3526static inline int
3527io_buffer_overlaps(const struct rb_io_buffer *a, const struct rb_io_buffer *b)
3528{
3529 if (a->base > b->base) {
3530 return io_buffer_overlaps(b, a);
3531 }
3532
3533 return (b->base >= a->base) && (b->base < (void*)((unsigned char *)a->base + a->size));
3534}
3535
3536static inline void
3537io_buffer_check_overlaps(struct rb_io_buffer *a, struct rb_io_buffer *b)
3538{
3539 if (io_buffer_overlaps(a, b))
3540 rb_raise(rb_eIOBufferMaskError, "Mask overlaps source buffer!");
3541}
3542
3543static void
3544memory_and_inplace(unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3545{
3546 for (size_t offset = 0; offset < size; offset += 1) {
3547 base[offset] &= mask[offset % mask_size];
3548 }
3549}
3550
3551/*
3552 * call-seq:
3553 * source.and!(mask) -> io_buffer
3554 *
3555 * Modify the source buffer in place by applying the binary AND
3556 * operation to the source, using the mask, repeating as necessary.
3557 *
3558 * source = IO::Buffer.for("1234567890").dup # Make a read/write copy.
3559 * # =>
3560 * # #<IO::Buffer 0x000056307a0d0c20+10 INTERNAL>
3561 * # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890
3562 *
3563 * source.and!(IO::Buffer.for("\xFF\x00\x00\xFF"))
3564 * # =>
3565 * # #<IO::Buffer 0x000056307a0d0c20+10 INTERNAL>
3566 * # 0x00000000 31 00 00 34 35 00 00 38 39 00 1..45..89.
3567 */
3568static VALUE
3569io_buffer_and_inplace(VALUE self, VALUE mask)
3570{
3571 struct rb_io_buffer *buffer = NULL;
3572 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3573
3574 struct rb_io_buffer *mask_buffer = NULL;
3575 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3576
3577 io_buffer_check_mask(mask_buffer);
3578 io_buffer_check_overlaps(buffer, mask_buffer);
3579
3580 void *base;
3581 size_t size;
3582 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3583
3584 memory_and_inplace(base, size, mask_buffer->base, mask_buffer->size);
3585
3586 return self;
3587}
3588
3589static void
3590memory_or_inplace(unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3591{
3592 for (size_t offset = 0; offset < size; offset += 1) {
3593 base[offset] |= mask[offset % mask_size];
3594 }
3595}
3596
3597/*
3598 * call-seq:
3599 * source.or!(mask) -> io_buffer
3600 *
3601 * Modify the source buffer in place by applying the binary OR
3602 * operation to the source, using the mask, repeating as necessary.
3603 *
3604 * source = IO::Buffer.for("1234567890").dup # Make a read/write copy.
3605 * # =>
3606 * # #<IO::Buffer 0x000056307a272350+10 INTERNAL>
3607 * # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890
3608 *
3609 * source.or!(IO::Buffer.for("\xFF\x00\x00\xFF"))
3610 * # =>
3611 * # #<IO::Buffer 0x000056307a272350+10 INTERNAL>
3612 * # 0x00000000 ff 32 33 ff ff 36 37 ff ff 30 .23..67..0
3613 */
3614static VALUE
3615io_buffer_or_inplace(VALUE self, VALUE mask)
3616{
3617 struct rb_io_buffer *buffer = NULL;
3618 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3619
3620 struct rb_io_buffer *mask_buffer = NULL;
3621 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3622
3623 io_buffer_check_mask(mask_buffer);
3624 io_buffer_check_overlaps(buffer, mask_buffer);
3625
3626 void *base;
3627 size_t size;
3628 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3629
3630 memory_or_inplace(base, size, mask_buffer->base, mask_buffer->size);
3631
3632 return self;
3633}
3634
3635static void
3636memory_xor_inplace(unsigned char * restrict base, size_t size, unsigned char * restrict mask, size_t mask_size)
3637{
3638 for (size_t offset = 0; offset < size; offset += 1) {
3639 base[offset] ^= mask[offset % mask_size];
3640 }
3641}
3642
3643/*
3644 * call-seq:
3645 * source.xor!(mask) -> io_buffer
3646 *
3647 * Modify the source buffer in place by applying the binary XOR
3648 * operation to the source, using the mask, repeating as necessary.
3649 *
3650 * source = IO::Buffer.for("1234567890").dup # Make a read/write copy.
3651 * # =>
3652 * # #<IO::Buffer 0x000056307a25b3e0+10 INTERNAL>
3653 * # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890
3654 *
3655 * source.xor!(IO::Buffer.for("\xFF\x00\x00\xFF"))
3656 * # =>
3657 * # #<IO::Buffer 0x000056307a25b3e0+10 INTERNAL>
3658 * # 0x00000000 ce 32 33 cb ca 36 37 c7 c6 30 .23..67..0
3659 */
3660static VALUE
3661io_buffer_xor_inplace(VALUE self, VALUE mask)
3662{
3663 struct rb_io_buffer *buffer = NULL;
3664 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3665
3666 struct rb_io_buffer *mask_buffer = NULL;
3667 TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer);
3668
3669 io_buffer_check_mask(mask_buffer);
3670 io_buffer_check_overlaps(buffer, mask_buffer);
3671
3672 void *base;
3673 size_t size;
3674 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3675
3676 memory_xor_inplace(base, size, mask_buffer->base, mask_buffer->size);
3677
3678 return self;
3679}
3680
3681static void
3682memory_not_inplace(unsigned char * restrict base, size_t size)
3683{
3684 for (size_t offset = 0; offset < size; offset += 1) {
3685 base[offset] = ~base[offset];
3686 }
3687}
3688
3689/*
3690 * call-seq:
3691 * source.not! -> io_buffer
3692 *
3693 * Modify the source buffer in place by applying the binary NOT
3694 * operation to the source.
3695 *
3696 * source = IO::Buffer.for("1234567890").dup # Make a read/write copy.
3697 * # =>
3698 * # #<IO::Buffer 0x000056307a33a450+10 INTERNAL>
3699 * # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890
3700 *
3701 * source.not!
3702 * # =>
3703 * # #<IO::Buffer 0x000056307a33a450+10 INTERNAL>
3704 * # 0x00000000 ce cd cc cb ca c9 c8 c7 c6 cf ..........
3705 */
3706static VALUE
3707io_buffer_not_inplace(VALUE self)
3708{
3709 struct rb_io_buffer *buffer = NULL;
3710 TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer);
3711
3712 void *base;
3713 size_t size;
3714 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3715
3716 memory_not_inplace(base, size);
3717
3718 return self;
3719}
3720
3721/*
3722 * Document-class: IO::Buffer
3723 *
3724 * IO::Buffer is a efficient zero-copy buffer for input/output. There are
3725 * typical use cases:
3726 *
3727 * * Create an empty buffer with ::new, fill it with buffer using #copy or
3728 * #set_value, #set_string, get buffer with #get_string or write it directly
3729 * to some file with #write.
3730 * * Create a buffer mapped to some string with ::for, then it could be used
3731 * both for reading with #get_string or #get_value, and writing (writing will
3732 * change the source string, too).
3733 * * Create a buffer mapped to some file with ::map, then it could be used for
3734 * reading and writing the underlying file.
3735 * * Create a string of a fixed size with ::string, then #read into it, or
3736 * modify it using #set_value.
3737 *
3738 * Interaction with string and file memory is performed by efficient low-level
3739 * C mechanisms like `memcpy`.
3740 *
3741 * The class is meant to be an utility for implementing more high-level mechanisms
3742 * like Fiber::Scheduler#io_read and Fiber::Scheduler#io_write and parsing binary
3743 * protocols.
3744 *
3745 * == Examples of Usage
3746 *
3747 * Empty buffer:
3748 *
3749 * buffer = IO::Buffer.new(8) # create empty 8-byte buffer
3750 * # =>
3751 * # #<IO::Buffer 0x0000555f5d1a5c50+8 INTERNAL>
3752 * # ...
3753 * buffer
3754 * # =>
3755 * # <IO::Buffer 0x0000555f5d156ab0+8 INTERNAL>
3756 * # 0x00000000 00 00 00 00 00 00 00 00
3757 * buffer.set_string('test', 2) # put there bytes of the "test" string, starting from offset 2
3758 * # => 4
3759 * buffer.get_string # get the result
3760 * # => "\x00\x00test\x00\x00"
3761 *
3762 * \Buffer from string:
3763 *
3764 * string = 'data'
3765 * IO::Buffer.for(string) do |buffer|
3766 * buffer
3767 * # =>
3768 * # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE>
3769 * # 0x00000000 64 61 74 61 data
3770 *
3771 * buffer.get_string(2) # read content starting from offset 2
3772 * # => "ta"
3773 * buffer.set_string('---', 1) # write content, starting from offset 1
3774 * # => 3
3775 * buffer
3776 * # =>
3777 * # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE>
3778 * # 0x00000000 64 2d 2d 2d d---
3779 * string # original string changed, too
3780 * # => "d---"
3781 * end
3782 *
3783 * \Buffer from file:
3784 *
3785 * File.write('test.txt', 'test data')
3786 * # => 9
3787 * buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::READONLY)
3788 * # =>
3789 * # #<IO::Buffer 0x00007f3f0768c000+9 EXTERNAL MAPPED FILE SHARED READONLY>
3790 * # ...
3791 * buffer.get_string(5, 2) # read 2 bytes, starting from offset 5
3792 * # => "da"
3793 * buffer.set_string('---', 1) # attempt to write
3794 * # in `set_string': Buffer is not writable! (IO::Buffer::AccessError)
3795 *
3796 * # To create writable file-mapped buffer
3797 * # Open file for read-write, pass size, offset, and flags=0
3798 * buffer = IO::Buffer.map(File.open('test.txt', 'r+'), 9, 0, 0)
3799 * buffer.set_string('---', 1)
3800 * # => 3 -- bytes written
3801 * File.read('test.txt')
3802 * # => "t--- data"
3803 *
3804 * <b>The class is experimental and the interface is subject to change, this
3805 * is especially true of file mappings which may be removed entirely in
3806 * the future.</b>
3807 */
3808void
3809Init_IO_Buffer(void)
3810{
3811 rb_cIOBuffer = rb_define_class_under(rb_cIO, "Buffer", rb_cObject);
3812
3813 /* Raised when an operation would resize or re-allocate a locked buffer. */
3814 rb_eIOBufferLockedError = rb_define_class_under(rb_cIOBuffer, "LockedError", rb_eRuntimeError);
3815
3816 /* Raised when the buffer cannot be allocated for some reason, or you try to use a buffer that's not allocated. */
3817 rb_eIOBufferAllocationError = rb_define_class_under(rb_cIOBuffer, "AllocationError", rb_eRuntimeError);
3818
3819 /* Raised when you try to write to a read-only buffer, or resize an external buffer. */
3820 rb_eIOBufferAccessError = rb_define_class_under(rb_cIOBuffer, "AccessError", rb_eRuntimeError);
3821
3822 /* Raised if you try to access a buffer slice which no longer references a valid memory range of the underlying source. */
3823 rb_eIOBufferInvalidatedError = rb_define_class_under(rb_cIOBuffer, "InvalidatedError", rb_eRuntimeError);
3824
3825 /* Raised if the mask given to a binary operation is invalid, e.g. zero length or overlaps the target buffer. */
3826 rb_eIOBufferMaskError = rb_define_class_under(rb_cIOBuffer, "MaskError", rb_eArgError);
3827
3828 rb_define_alloc_func(rb_cIOBuffer, rb_io_buffer_type_allocate);
3829 rb_define_singleton_method(rb_cIOBuffer, "for", rb_io_buffer_type_for, 1);
3830 rb_define_singleton_method(rb_cIOBuffer, "string", rb_io_buffer_type_string, 1);
3831
3832#ifdef _WIN32
3833 SYSTEM_INFO info;
3834 GetSystemInfo(&info);
3835 RUBY_IO_BUFFER_PAGE_SIZE = info.dwPageSize;
3836#else /* not WIN32 */
3837 RUBY_IO_BUFFER_PAGE_SIZE = sysconf(_SC_PAGESIZE);
3838#endif
3839
3840 RUBY_IO_BUFFER_DEFAULT_SIZE = io_buffer_default_size(RUBY_IO_BUFFER_PAGE_SIZE);
3841
3842 /* The operating system page size. Used for efficient page-aligned memory allocations. */
3843 rb_define_const(rb_cIOBuffer, "PAGE_SIZE", SIZET2NUM(RUBY_IO_BUFFER_PAGE_SIZE));
3844
3845 /* The default buffer size, typically a (small) multiple of the PAGE_SIZE.
3846 Can be explicitly specified by setting the RUBY_IO_BUFFER_DEFAULT_SIZE
3847 environment variable. */
3848 rb_define_const(rb_cIOBuffer, "DEFAULT_SIZE", SIZET2NUM(RUBY_IO_BUFFER_DEFAULT_SIZE));
3849
3850 rb_define_singleton_method(rb_cIOBuffer, "map", io_buffer_map, -1);
3851
3852 rb_define_method(rb_cIOBuffer, "initialize", rb_io_buffer_initialize, -1);
3853 rb_define_method(rb_cIOBuffer, "initialize_copy", rb_io_buffer_initialize_copy, 1);
3854 rb_define_method(rb_cIOBuffer, "inspect", rb_io_buffer_inspect, 0);
3855 rb_define_method(rb_cIOBuffer, "hexdump", rb_io_buffer_hexdump, -1);
3856 rb_define_method(rb_cIOBuffer, "to_s", rb_io_buffer_to_s, 0);
3857 rb_define_method(rb_cIOBuffer, "size", rb_io_buffer_size, 0);
3858 rb_define_method(rb_cIOBuffer, "valid?", rb_io_buffer_valid_p, 0);
3859
3860 rb_define_method(rb_cIOBuffer, "transfer", rb_io_buffer_transfer, 0);
3861
3862 /* Indicates that the memory in the buffer is owned by someone else. See #external? for more details. */
3863 rb_define_const(rb_cIOBuffer, "EXTERNAL", RB_INT2NUM(RB_IO_BUFFER_EXTERNAL));
3864
3865 /* Indicates that the memory in the buffer is owned by the buffer. See #internal? for more details. */
3866 rb_define_const(rb_cIOBuffer, "INTERNAL", RB_INT2NUM(RB_IO_BUFFER_INTERNAL));
3867
3868 /* Indicates that the memory in the buffer is mapped by the operating system. See #mapped? for more details. */
3869 rb_define_const(rb_cIOBuffer, "MAPPED", RB_INT2NUM(RB_IO_BUFFER_MAPPED));
3870
3871 /* Indicates that the memory in the buffer is also mapped such that it can be shared with other processes. See #shared? for more details. */
3872 rb_define_const(rb_cIOBuffer, "SHARED", RB_INT2NUM(RB_IO_BUFFER_SHARED));
3873
3874 /* Indicates that the memory in the buffer is locked and cannot be resized or freed. See #locked? and #locked for more details. */
3875 rb_define_const(rb_cIOBuffer, "LOCKED", RB_INT2NUM(RB_IO_BUFFER_LOCKED));
3876
3877 /* Indicates that the memory in the buffer is mapped privately and changes won't be replicated to the underlying file. See #private? for more details. */
3878 rb_define_const(rb_cIOBuffer, "PRIVATE", RB_INT2NUM(RB_IO_BUFFER_PRIVATE));
3879
3880 /* Indicates that the memory in the buffer is read only, and attempts to modify it will fail. See #readonly? for more details.*/
3881 rb_define_const(rb_cIOBuffer, "READONLY", RB_INT2NUM(RB_IO_BUFFER_READONLY));
3882
3883 /* Refers to little endian byte order, where the least significant byte is stored first. See #get_value for more details. */
3884 rb_define_const(rb_cIOBuffer, "LITTLE_ENDIAN", RB_INT2NUM(RB_IO_BUFFER_LITTLE_ENDIAN));
3885
3886 /* Refers to big endian byte order, where the most significant byte is stored first. See #get_value for more details. */
3887 rb_define_const(rb_cIOBuffer, "BIG_ENDIAN", RB_INT2NUM(RB_IO_BUFFER_BIG_ENDIAN));
3888
3889 /* Refers to the byte order of the host machine. See #get_value for more details. */
3890 rb_define_const(rb_cIOBuffer, "HOST_ENDIAN", RB_INT2NUM(RB_IO_BUFFER_HOST_ENDIAN));
3891
3892 /* Refers to network byte order, which is the same as big endian. See #get_value for more details. */
3893 rb_define_const(rb_cIOBuffer, "NETWORK_ENDIAN", RB_INT2NUM(RB_IO_BUFFER_NETWORK_ENDIAN));
3894
3895 rb_define_method(rb_cIOBuffer, "null?", rb_io_buffer_null_p, 0);
3896 rb_define_method(rb_cIOBuffer, "empty?", rb_io_buffer_empty_p, 0);
3897 rb_define_method(rb_cIOBuffer, "external?", rb_io_buffer_external_p, 0);
3898 rb_define_method(rb_cIOBuffer, "internal?", rb_io_buffer_internal_p, 0);
3899 rb_define_method(rb_cIOBuffer, "mapped?", rb_io_buffer_mapped_p, 0);
3900 rb_define_method(rb_cIOBuffer, "shared?", rb_io_buffer_shared_p, 0);
3901 rb_define_method(rb_cIOBuffer, "locked?", rb_io_buffer_locked_p, 0);
3902 rb_define_method(rb_cIOBuffer, "private?", rb_io_buffer_private_p, 0);
3903 rb_define_method(rb_cIOBuffer, "readonly?", io_buffer_readonly_p, 0);
3904
3905 // Locking to prevent changes while using pointer:
3906 // rb_define_method(rb_cIOBuffer, "lock", rb_io_buffer_lock, 0);
3907 // rb_define_method(rb_cIOBuffer, "unlock", rb_io_buffer_unlock, 0);
3908 rb_define_method(rb_cIOBuffer, "locked", rb_io_buffer_locked, 0);
3909
3910 // Manipulation:
3911 rb_define_method(rb_cIOBuffer, "slice", io_buffer_slice, -1);
3912 rb_define_method(rb_cIOBuffer, "<=>", rb_io_buffer_compare, 1);
3913 rb_define_method(rb_cIOBuffer, "resize", io_buffer_resize, 1);
3914 rb_define_method(rb_cIOBuffer, "clear", io_buffer_clear, -1);
3915 rb_define_method(rb_cIOBuffer, "free", rb_io_buffer_free, 0);
3916
3917 rb_include_module(rb_cIOBuffer, rb_mComparable);
3918
3919#define IO_BUFFER_DEFINE_DATA_TYPE(name) RB_IO_BUFFER_DATA_TYPE_##name = rb_intern_const(#name)
3920 IO_BUFFER_DEFINE_DATA_TYPE(U8);
3921 IO_BUFFER_DEFINE_DATA_TYPE(S8);
3922
3923 IO_BUFFER_DEFINE_DATA_TYPE(u16);
3924 IO_BUFFER_DEFINE_DATA_TYPE(U16);
3925 IO_BUFFER_DEFINE_DATA_TYPE(s16);
3926 IO_BUFFER_DEFINE_DATA_TYPE(S16);
3927
3928 IO_BUFFER_DEFINE_DATA_TYPE(u32);
3929 IO_BUFFER_DEFINE_DATA_TYPE(U32);
3930 IO_BUFFER_DEFINE_DATA_TYPE(s32);
3931 IO_BUFFER_DEFINE_DATA_TYPE(S32);
3932
3933 IO_BUFFER_DEFINE_DATA_TYPE(u64);
3934 IO_BUFFER_DEFINE_DATA_TYPE(U64);
3935 IO_BUFFER_DEFINE_DATA_TYPE(s64);
3936 IO_BUFFER_DEFINE_DATA_TYPE(S64);
3937
3938 IO_BUFFER_DEFINE_DATA_TYPE(u128);
3939 IO_BUFFER_DEFINE_DATA_TYPE(U128);
3940 IO_BUFFER_DEFINE_DATA_TYPE(s128);
3941 IO_BUFFER_DEFINE_DATA_TYPE(S128);
3942
3943 IO_BUFFER_DEFINE_DATA_TYPE(f32);
3944 IO_BUFFER_DEFINE_DATA_TYPE(F32);
3945 IO_BUFFER_DEFINE_DATA_TYPE(f64);
3946 IO_BUFFER_DEFINE_DATA_TYPE(F64);
3947#undef IO_BUFFER_DEFINE_DATA_TYPE
3948
3949 rb_define_singleton_method(rb_cIOBuffer, "size_of", io_buffer_size_of, 1);
3950
3951 // Data access:
3952 rb_define_method(rb_cIOBuffer, "get_value", io_buffer_get_value, 2);
3953 rb_define_method(rb_cIOBuffer, "get_values", io_buffer_get_values, 2);
3954 rb_define_method(rb_cIOBuffer, "each", io_buffer_each, -1);
3955 rb_define_method(rb_cIOBuffer, "values", io_buffer_values, -1);
3956 rb_define_method(rb_cIOBuffer, "each_byte", io_buffer_each_byte, -1);
3957 rb_define_method(rb_cIOBuffer, "set_value", io_buffer_set_value, 3);
3958 rb_define_method(rb_cIOBuffer, "set_values", io_buffer_set_values, 3);
3959
3960 rb_define_method(rb_cIOBuffer, "copy", io_buffer_copy, -1);
3961
3962 rb_define_method(rb_cIOBuffer, "get_string", io_buffer_get_string, -1);
3963 rb_define_method(rb_cIOBuffer, "set_string", io_buffer_set_string, -1);
3964
3965 // Binary buffer manipulations:
3966 rb_define_method(rb_cIOBuffer, "&", io_buffer_and, 1);
3967 rb_define_method(rb_cIOBuffer, "|", io_buffer_or, 1);
3968 rb_define_method(rb_cIOBuffer, "^", io_buffer_xor, 1);
3969 rb_define_method(rb_cIOBuffer, "~", io_buffer_not, 0);
3970
3971 rb_define_method(rb_cIOBuffer, "and!", io_buffer_and_inplace, 1);
3972 rb_define_method(rb_cIOBuffer, "or!", io_buffer_or_inplace, 1);
3973 rb_define_method(rb_cIOBuffer, "xor!", io_buffer_xor_inplace, 1);
3974 rb_define_method(rb_cIOBuffer, "not!", io_buffer_not_inplace, 0);
3975
3976 // IO operations:
3977 rb_define_method(rb_cIOBuffer, "read", io_buffer_read, -1);
3978 rb_define_method(rb_cIOBuffer, "pread", io_buffer_pread, -1);
3979 rb_define_method(rb_cIOBuffer, "write", io_buffer_write, -1);
3980 rb_define_method(rb_cIOBuffer, "pwrite", io_buffer_pwrite, -1);
3981}
buffer.h
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
RB_OBJ_FROZEN
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition fl_type.h:892
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1685
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1509
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1010
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
OBJ_FROZEN
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:136
rb_str_cat2
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
SIZET2NUM
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
NUM2UINT
#define NUM2UINT
Old name of RB_NUM2UINT.
Definition int.h:45
NUM2DBL
#define NUM2DBL
Old name of rb_num2dbl.
Definition double.h:27
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
T_ARRAY
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
DBL2NUM
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
NUM2SIZET
#define NUM2SIZET
Old name of RB_NUM2SIZE.
Definition size_t.h:61
rb_category_warn
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
RB_WARN_CATEGORY_EXPERIMENTAL
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
Definition error.h:51
rb_cIO
VALUE rb_cIO
IO class.
Definition io.c:187
rb_class_of
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
Definition globals.h:174
rb_mComparable
VALUE rb_mComparable
Comparable module.
Definition compar.c:19
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
rb_ascii8bit_encoding
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition encoding.c:1523
rb_ary_new_capa
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
RETURN_ENUMERATOR_KW
#define RETURN_ENUMERATOR_KW(obj, argc, argv, kw_splat)
Identical to RETURN_SIZED_ENUMERATOR_KW(), except its size is unknown.
Definition enumerator.h:260
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3799
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
rb_str_locktmp
VALUE rb_str_locktmp(VALUE str)
Obtains a "temporary lock" of the string.
rb_str_unlocktmp
VALUE rb_str_unlocktmp(VALUE str)
Releases a lock formerly obtained by rb_str_locktmp().
Definition string.c:3371
rb_str_buf_new
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1718
rb_str_new_cstr
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
rb_class_name
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:500
rb_define_alloc_func
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
RB_SYM2ID
#define RB_SYM2ID
Just another name of rb_sym2id.
Definition symbol.h:43
rb_io_get_io
VALUE rb_io_get_io(VALUE io)
Identical to rb_io_check_io(), except it raises exceptions on conversion failures.
Definition io.c:811
rb_io_descriptor
int rb_io_descriptor(VALUE io)
Returns an integer representing the numeric file descriptor for io.
Definition io.c:2927
RB_IO_POINTER
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
Definition io.h:436
rb_io_get_write_io
VALUE rb_io_get_write_io(VALUE io)
Queries the tied IO for writing.
Definition io.c:823
thread.h
rb_nogvl
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
Definition thread.c:1593
RB_NOGVL_OFFLOAD_SAFE
#define RB_NOGVL_OFFLOAD_SAFE
Passing this flag to rb_nogvl() indicates that the passed function is safe to offload to a background...
Definition thread.h:73
RB_NUM2INT
#define RB_NUM2INT
Just another name of rb_num2int_inline.
Definition int.h:38
RB_UINT2NUM
#define RB_UINT2NUM
Just another name of rb_uint2num_inline.
Definition int.h:39
RB_INT2NUM
#define RB_INT2NUM
Just another name of rb_int2num_inline.
Definition int.h:37
RB_NUM2UINT
static unsigned int RB_NUM2UINT(VALUE x)
Converts an instance of rb_cNumeric into C's unsigned int.
Definition int.h:185
RB_LL2NUM
#define RB_LL2NUM
Just another name of rb_ll2num_inline.
Definition long_long.h:28
RB_ULL2NUM
#define RB_ULL2NUM
Just another name of rb_ull2num_inline.
Definition long_long.h:29
RB_NUM2ULL
#define RB_NUM2ULL
Just another name of rb_num2ull_inline.
Definition long_long.h:33
RB_NUM2LL
#define RB_NUM2LL
Just another name of rb_num2ll_inline.
Definition long_long.h:32
rb_yield_values
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1395
rb_yield
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1372
RB_INT2FIX
static VALUE RB_INT2FIX(long i)
Converts a C's long into an instance of rb_cInteger.
Definition long.h:111
RB_NUM2LONG
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
Definition long.h:57
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::rb_ensure
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
Definition cxxanyargs.hpp:310
NUM2OFFT
#define NUM2OFFT
Converts an instance of rb_cNumeric into C's off_t.
Definition off_t.h:44
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
rb_str_to_str
VALUE rb_str_to_str(VALUE obj)
Identical to rb_check_string_type(), except it raises exceptions in case of conversion failures.
Definition string.c:1779
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:649
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:508
errno
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
scheduler.h
Scheduler APIs.
rb_fiber_scheduler_current
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
Definition scheduler.c:471
rb_fiber_scheduler_io_pwrite
VALUE rb_fiber_scheduler_io_pwrite(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
Non-blocking write to the passed IO at the specified offset.
Definition scheduler.c:962
rb_fiber_scheduler_io_read
VALUE rb_fiber_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
Non-blocking read from the passed IO.
Definition scheduler.c:830
rb_fiber_scheduler_io_result
static VALUE rb_fiber_scheduler_io_result(ssize_t result, int error)
Wrap a ssize_t and int errno into a single VALUE.
Definition scheduler.h:50
rb_fiber_scheduler_io_pread
VALUE rb_fiber_scheduler_io_pread(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
Non-blocking read from the passed IO at the specified offset.
Definition scheduler.c:869
rb_fiber_scheduler_io_write
VALUE rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
Non-blocking write to the passed IO.
Definition scheduler.c:922
RB_NIL_P
static bool RB_NIL_P(VALUE obj)
Checks if the given object is nil.
Definition special_consts.h:173
io_buffer_blocking_region_argument
Definition io_buffer.c:2837
io_buffer_for_yield_instance_arguments
Definition io_buffer.c:512
io_buffer_memmove_arguments
Definition io_buffer.c:2459
io_buffer_pread_internal_argument
Definition io_buffer.c:3003
io_buffer_pwrite_internal_argument
Definition io_buffer.c:3237
io_buffer_read_internal_argument
Definition io_buffer.c:2887
io_buffer_write_internal_argument
Definition io_buffer.c:3128
rb_io_buffer
Definition io_buffer.c:52
rb_io
Ruby's IO, metadata and buffers.
Definition io.h:295
swapf32
Definition io_buffer.c:1879
swapf64
Definition io_buffer.c:1892
uint128_int128_conversion
Definition numeric.h:167
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376