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"
25VALUE rb_eIOBufferLockedError;
26VALUE rb_eIOBufferAllocationError;
27VALUE rb_eIOBufferAccessError;
28VALUE rb_eIOBufferInvalidatedError;
29VALUE rb_eIOBufferMaskError;
31size_t RUBY_IO_BUFFER_PAGE_SIZE;
32size_t RUBY_IO_BUFFER_DEFAULT_SIZE;
41 RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH = 16,
43 RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE = 256,
44 RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH = 16,
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,
49 RB_IO_BUFFER_DEBUG = 0,
55 enum rb_io_buffer_flags flags;
65io_buffer_map_memory(
size_t size,
int flags)
68 void * base = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
71 rb_sys_fail(
"io_buffer_map_memory:VirtualAlloc");
74 int mmap_flags = MAP_ANONYMOUS;
75 if (flags & RB_IO_BUFFER_SHARED) {
76 mmap_flags |= MAP_SHARED;
79 mmap_flags |= MAP_PRIVATE;
82 void * base = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
84 if (base == MAP_FAILED) {
85 rb_sys_fail(
"io_buffer_map_memory:mmap");
88 ruby_annotate_mmap(base, size,
"Ruby:io_buffer_map_memory");
95io_buffer_map_file(
struct rb_io_buffer *buffer,
int descriptor,
size_t size, rb_off_t offset,
enum rb_io_buffer_flags flags)
98 HANDLE file = (HANDLE)_get_osfhandle(descriptor);
99 if (!file) rb_sys_fail(
"io_buffer_map_descriptor:_get_osfhandle");
101 DWORD protect = PAGE_READONLY, access = FILE_MAP_READ;
103 if (flags & RB_IO_BUFFER_READONLY) {
104 buffer->flags |= RB_IO_BUFFER_READONLY;
107 protect = PAGE_READWRITE;
108 access = FILE_MAP_WRITE;
111 if (flags & RB_IO_BUFFER_PRIVATE) {
112 protect = PAGE_WRITECOPY;
113 access = FILE_MAP_COPY;
114 buffer->flags |= RB_IO_BUFFER_PRIVATE;
118 buffer->flags |= RB_IO_BUFFER_EXTERNAL;
119 buffer->flags |= RB_IO_BUFFER_SHARED;
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");
126 void *base = MapViewOfFile(mapping, access, (DWORD)(offset >> 32), (DWORD)(offset & 0xFFFFFFFF), size);
129 CloseHandle(mapping);
130 rb_sys_fail(
"io_buffer_map_file:MapViewOfFile");
133 buffer->mapping = mapping;
135 int protect = PROT_READ, access = 0;
137 if (flags & RB_IO_BUFFER_READONLY) {
138 buffer->flags |= RB_IO_BUFFER_READONLY;
141 protect |= PROT_WRITE;
144 if (flags & RB_IO_BUFFER_PRIVATE) {
145 buffer->flags |= RB_IO_BUFFER_PRIVATE;
146 access |= MAP_PRIVATE;
150 buffer->flags |= RB_IO_BUFFER_EXTERNAL;
151 buffer->flags |= RB_IO_BUFFER_SHARED;
152 access |= MAP_SHARED;
155 void *base = mmap(NULL, size, protect, access, descriptor, offset);
157 if (base == MAP_FAILED) {
158 rb_sys_fail(
"io_buffer_map_file:mmap");
165 buffer->flags |= RB_IO_BUFFER_MAPPED;
166 buffer->flags |= RB_IO_BUFFER_FILE;
170io_buffer_experimental(
void)
172 static int warned = 0;
180 "IO::Buffer is experimental and both the Ruby and C interface may change in the future!"
191 buffer->mapping = NULL;
193 buffer->source =
Qnil;
197io_buffer_initialize(
VALUE self,
struct rb_io_buffer *buffer,
void *base,
size_t size,
enum rb_io_buffer_flags flags,
VALUE source)
204 if (flags & RB_IO_BUFFER_INTERNAL) {
205 base = calloc(size, 1);
207 else if (flags & RB_IO_BUFFER_MAPPED) {
208 base = io_buffer_map_memory(size, flags);
212 rb_raise(rb_eIOBufferAllocationError,
"Could not allocate buffer!");
222 buffer->flags = flags;
226 buffer->mapping = NULL;
234 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
238 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
240 if (buffer->flags & RB_IO_BUFFER_FILE) {
241 UnmapViewOfFile(buffer->base);
244 VirtualFree(buffer->base, 0, MEM_RELEASE);
247 munmap(buffer->base, buffer->size);
261 buffer->source =
Qnil;
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());
270 buffer->mapping = NULL;
276rb_io_buffer_type_free(
void *_buffer)
280 io_buffer_free(buffer);
284rb_io_buffer_type_size(
const void *_buffer)
290 total += buffer->size;
296RUBY_REFERENCES(io_buffer_refs) = {
304 .dmark = RUBY_REFS_LIST_PTR(io_buffer_refs),
305 .dfree = rb_io_buffer_type_free,
306 .dsize = rb_io_buffer_type_size,
309 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_DECL_MARKING,
312static inline enum rb_io_buffer_flags
313io_buffer_extract_flags(
VALUE argument)
315 if (rb_int_negative_p(argument)) {
316 rb_raise(rb_eArgError,
"Flags can't be negative!");
319 enum rb_io_buffer_flags flags =
RB_NUM2UINT(argument);
322 return flags & RB_IO_BUFFER_FLAGS_MASK;
327io_buffer_extract_offset(
VALUE argument)
329 if (rb_int_negative_p(argument)) {
330 rb_raise(rb_eArgError,
"Offset can't be negative!");
340io_buffer_extract_length(
VALUE argument)
342 if (rb_int_negative_p(argument)) {
343 rb_raise(rb_eArgError,
"Length can't be negative!");
352io_buffer_extract_size(
VALUE argument)
354 if (rb_int_negative_p(argument)) {
355 rb_raise(rb_eArgError,
"Size can't be negative!");
364io_buffer_extract_width(
VALUE argument,
size_t minimum)
366 if (rb_int_negative_p(argument)) {
367 rb_raise(rb_eArgError,
"Width can't be negative!");
372 if (width < minimum) {
373 rb_raise(rb_eArgError,
"Width must be at least %" PRIuSIZE
"!", minimum);
384io_buffer_default_length(
const struct rb_io_buffer *buffer,
size_t offset)
386 if (offset > buffer->size) {
387 rb_raise(rb_eArgError,
"The given offset is bigger than the buffer size!");
391 return buffer->size - offset;
400io_buffer_extract_length_offset(
VALUE self,
int argc,
VALUE argv[],
size_t *length,
size_t *offset)
405 if (argc >= 2 && !
NIL_P(argv[1])) {
406 *offset = io_buffer_extract_offset(argv[1]);
412 if (argc >= 1 && !
NIL_P(argv[0])) {
413 *length = io_buffer_extract_length(argv[0]);
416 *length = io_buffer_default_length(buffer, *offset);
432io_buffer_extract_offset_length(
VALUE self,
int argc,
VALUE argv[],
size_t *offset,
size_t *length)
437 if (argc >= 1 && !
NIL_P(argv[0])) {
438 *offset = io_buffer_extract_offset(argv[0]);
444 if (argc >= 2 && !
NIL_P(argv[1])) {
445 *length = io_buffer_extract_length(argv[1]);
448 *length = io_buffer_default_length(buffer, *offset);
455rb_io_buffer_type_allocate(
VALUE self)
460 io_buffer_zero(buffer);
465static VALUE io_buffer_for_make_instance(
VALUE klass,
VALUE string,
enum rb_io_buffer_flags flags)
467 VALUE instance = rb_io_buffer_type_allocate(klass);
472 flags |= RB_IO_BUFFER_EXTERNAL;
475 flags |= RB_IO_BUFFER_READONLY;
477 if (!(flags & RB_IO_BUFFER_READONLY))
478 rb_str_modify(
string);
480 io_buffer_initialize(instance, buffer, RSTRING_PTR(
string), RSTRING_LEN(
string), flags,
string);
489 enum rb_io_buffer_flags flags;
493io_buffer_for_yield_instance(
VALUE _arguments)
497 arguments->instance = io_buffer_for_make_instance(arguments->klass, arguments->string, arguments->flags);
501 return rb_yield(arguments->instance);
505io_buffer_for_yield_instance_ensure(
VALUE _arguments)
509 if (arguments->instance !=
Qnil) {
510 rb_io_buffer_free(arguments->instance);
555rb_io_buffer_type_for(
VALUE klass,
VALUE string)
569 return rb_ensure(io_buffer_for_yield_instance, (
VALUE)&arguments, io_buffer_for_yield_instance_ensure, (
VALUE)&arguments);
573 string = rb_str_tmp_frozen_acquire(
string);
574 return io_buffer_for_make_instance(klass,
string, RB_IO_BUFFER_READONLY);
592rb_io_buffer_type_string(
VALUE klass,
VALUE length)
602 rb_ensure(io_buffer_for_yield_instance, (
VALUE)&arguments, io_buffer_for_yield_instance_ensure, (
VALUE)&arguments);
608rb_io_buffer_new(
void *base,
size_t size,
enum rb_io_buffer_flags flags)
610 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
615 io_buffer_initialize(instance, buffer, base, size, flags,
Qnil);
621rb_io_buffer_map(
VALUE io,
size_t size, rb_off_t offset,
enum rb_io_buffer_flags flags)
623 io_buffer_experimental();
625 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
632 io_buffer_map_file(buffer, descriptor, size, offset, flags);
675io_buffer_map(
int argc,
VALUE *argv,
VALUE klass)
683 if (argc >= 2 && !
RB_NIL_P(argv[1])) {
684 size = io_buffer_extract_size(argv[1]);
687 rb_off_t file_size = rb_file_size(io);
691 rb_raise(rb_eArgError,
"Invalid negative file size!");
694 else if ((uintmax_t)file_size > SIZE_MAX) {
695 rb_raise(rb_eArgError,
"File larger than address space!");
699 size = (size_t)file_size;
709 enum rb_io_buffer_flags flags = 0;
711 flags = io_buffer_extract_flags(argv[3]);
714 return rb_io_buffer_map(io, size, offset, flags);
718static inline enum rb_io_buffer_flags
719io_flags_for_size(
size_t size)
721 if (size >= RUBY_IO_BUFFER_PAGE_SIZE) {
722 return RB_IO_BUFFER_MAPPED;
725 return RB_IO_BUFFER_INTERNAL;
753rb_io_buffer_initialize(
int argc,
VALUE *argv,
VALUE self)
755 io_buffer_experimental();
764 size = io_buffer_extract_size(argv[0]);
767 size = RUBY_IO_BUFFER_DEFAULT_SIZE;
770 enum rb_io_buffer_flags flags = 0;
772 flags = io_buffer_extract_flags(argv[1]);
775 flags |= io_flags_for_size(size);
778 io_buffer_initialize(self, buffer, NULL, size, flags,
Qnil);
784io_buffer_validate_slice(
VALUE source,
void *base,
size_t size)
786 void *source_base = NULL;
787 size_t source_size = 0;
793 rb_io_buffer_get_bytes(source, &source_base, &source_size);
797 if (source_base == NULL)
return 0;
800 if (base < source_base)
return 0;
802 const void *source_end = (
char*)source_base + source_size;
803 const void *end = (
char*)base + size;
806 if (end > source_end)
return 0;
815 if (buffer->source !=
Qnil) {
817 return io_buffer_validate_slice(buffer->source, buffer->base, buffer->size);
824enum rb_io_buffer_flags
825rb_io_buffer_get_bytes(
VALUE self,
void **base,
size_t *size)
830 if (io_buffer_validate(buffer)) {
832 *base = buffer->base;
833 *size = buffer->size;
835 return buffer->flags;
847io_buffer_get_bytes_for_writing(
struct rb_io_buffer *buffer,
void **base,
size_t *size)
849 if (buffer->flags & RB_IO_BUFFER_READONLY ||
851 rb_raise(rb_eIOBufferAccessError,
"Buffer is not writable!");
854 if (!io_buffer_validate(buffer)) {
855 rb_raise(rb_eIOBufferInvalidatedError,
"Buffer is invalid!");
859 *base = buffer->base;
860 *size = buffer->size;
868rb_io_buffer_get_bytes_for_writing(
VALUE self,
void **base,
size_t *size)
873 io_buffer_get_bytes_for_writing(buffer, base, size);
877io_buffer_get_bytes_for_reading(
struct rb_io_buffer *buffer,
const void **base,
size_t *size)
879 if (!io_buffer_validate(buffer)) {
880 rb_raise(rb_eIOBufferInvalidatedError,
"Buffer has been invalidated!");
884 *base = buffer->base;
885 *size = buffer->size;
893rb_io_buffer_get_bytes_for_reading(
VALUE self,
const void **base,
size_t *size)
898 io_buffer_get_bytes_for_reading(buffer, base, size);
911rb_io_buffer_to_s(
VALUE self)
919 rb_str_catf(result,
" %p+%"PRIdSIZE, buffer->base, buffer->size);
921 if (buffer->base == NULL) {
925 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
929 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
933 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
937 if (buffer->flags & RB_IO_BUFFER_FILE) {
941 if (buffer->flags & RB_IO_BUFFER_SHARED) {
945 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
949 if (buffer->flags & RB_IO_BUFFER_PRIVATE) {
953 if (buffer->flags & RB_IO_BUFFER_READONLY) {
957 if (buffer->source !=
Qnil) {
961 if (!io_buffer_validate(buffer)) {
971io_buffer_hexdump_output_size(
size_t width,
size_t size,
int first)
976 size_t whole_lines = (size / width);
977 size_t partial_line = (size % width) ? 1 : 0;
982 total += (whole_lines + partial_line) * (1 + 10 + width*3 + 1 + 1);
985 if (size && first) total -= 1;
994io_buffer_hexdump(
VALUE string,
size_t width,
const char *base,
size_t length,
size_t offset,
int first)
996 char *text = alloca(width+1);
999 for (; offset < length; offset += width) {
1000 memset(text,
'\0', width);
1002 rb_str_catf(
string,
"0x%08" PRIxSIZE
" ", offset);
1006 rb_str_catf(
string,
"\n0x%08" PRIxSIZE
" ", offset);
1009 for (
size_t i = 0; i < width; i += 1) {
1010 if (offset+i < length) {
1011 unsigned char value = ((
unsigned char*)base)[offset+i];
1013 if (value < 127 && isprint(value)) {
1014 text[i] = (char)value;
1020 rb_str_catf(
string,
" %02x", value);
1027 rb_str_catf(
string,
" %s", text);
1046rb_io_buffer_inspect(
VALUE self)
1051 VALUE result = rb_io_buffer_to_s(self);
1053 if (io_buffer_validate(buffer)) {
1055 size_t size = buffer->size;
1058 if (size > RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE) {
1059 size = RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE;
1063 io_buffer_hexdump(result, RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH, buffer->base, size, 0, 0);
1066 rb_str_catf(result,
"\n(and %" PRIuSIZE
" more bytes not printed)", buffer->size - size);
1080rb_io_buffer_size(
VALUE self)
1097rb_io_buffer_valid_p(
VALUE self)
1102 return RBOOL(io_buffer_validate(buffer));
1120rb_io_buffer_null_p(
VALUE self)
1125 return RBOOL(buffer->base == NULL);
1136rb_io_buffer_empty_p(
VALUE self)
1141 return RBOOL(buffer->size == 0);
1156rb_io_buffer_external_p(
VALUE self)
1161 return RBOOL(buffer->flags & RB_IO_BUFFER_EXTERNAL);
1181rb_io_buffer_internal_p(
VALUE self)
1186 return RBOOL(buffer->flags & RB_IO_BUFFER_INTERNAL);
1203rb_io_buffer_mapped_p(
VALUE self)
1208 return RBOOL(buffer->flags & RB_IO_BUFFER_MAPPED);
1235rb_io_buffer_shared_p(
VALUE self)
1240 return RBOOL(buffer->flags & RB_IO_BUFFER_SHARED);
1258rb_io_buffer_locked_p(
VALUE self)
1263 return RBOOL(buffer->flags & RB_IO_BUFFER_LOCKED);
1289rb_io_buffer_private_p(
VALUE self)
1294 return RBOOL(buffer->flags & RB_IO_BUFFER_PRIVATE);
1298rb_io_buffer_readonly_p(
VALUE self)
1303 return buffer->flags & RB_IO_BUFFER_READONLY;
1315io_buffer_readonly_p(
VALUE self)
1317 return RBOOL(rb_io_buffer_readonly_p(self));
1323 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1324 rb_raise(rb_eIOBufferLockedError,
"Buffer already locked!");
1327 buffer->flags |= RB_IO_BUFFER_LOCKED;
1331rb_io_buffer_lock(
VALUE self)
1336 io_buffer_lock(buffer);
1344 if (!(buffer->flags & RB_IO_BUFFER_LOCKED)) {
1345 rb_raise(rb_eIOBufferLockedError,
"Buffer not locked!");
1348 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1352rb_io_buffer_unlock(
VALUE self)
1357 io_buffer_unlock(buffer);
1363rb_io_buffer_try_unlock(
VALUE self)
1368 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1369 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1407rb_io_buffer_locked(
VALUE self)
1412 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1413 rb_raise(rb_eIOBufferLockedError,
"Buffer already locked!");
1416 buffer->flags |= RB_IO_BUFFER_LOCKED;
1420 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1451rb_io_buffer_free(
VALUE self)
1456 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1457 rb_raise(rb_eIOBufferLockedError,
"Buffer is locked!");
1460 io_buffer_free(buffer);
1470 io_buffer_unlock(buffer);
1471 io_buffer_free(buffer);
1479io_buffer_validate_range(
struct rb_io_buffer *buffer,
size_t offset,
size_t length)
1482 if (offset + length > buffer->size) {
1483 rb_raise(rb_eArgError,
"Specified offset+length is bigger than the buffer size!");
1504rb_io_buffer_hexdump(
int argc,
VALUE *argv,
VALUE self)
1508 size_t offset, length;
1509 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1511 size_t width = RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH;
1513 width = io_buffer_extract_width(argv[2], 1);
1517 io_buffer_validate_range(buffer, offset, length);
1521 if (io_buffer_validate(buffer) && buffer->base) {
1522 result =
rb_str_buf_new(io_buffer_hexdump_output_size(width, length, 1));
1524 io_buffer_hexdump(result, width, buffer->base, offset+length, offset, 1);
1531rb_io_buffer_slice(
struct rb_io_buffer *buffer,
VALUE self,
size_t offset,
size_t length)
1533 io_buffer_validate_range(buffer, offset, length);
1539 slice->flags |= (buffer->flags & RB_IO_BUFFER_READONLY);
1540 slice->base = (
char*)buffer->base + offset;
1541 slice->size = length;
1544 if (buffer->source !=
Qnil) {
1545 RB_OBJ_WRITE(instance, &slice->source, buffer->source);
1605io_buffer_slice(
int argc,
VALUE *argv,
VALUE self)
1609 size_t offset, length;
1610 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1612 return rb_io_buffer_slice(buffer, self, offset, length);
1634rb_io_buffer_transfer(
VALUE self)
1639 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1640 rb_raise(rb_eIOBufferLockedError,
"Cannot transfer ownership of locked buffer!");
1647 *transferred = *buffer;
1648 io_buffer_zero(buffer);
1654io_buffer_resize_clear(
struct rb_io_buffer *buffer,
void* base,
size_t size)
1656 if (size > buffer->size) {
1657 memset((
unsigned char*)base+buffer->size, 0, size - buffer->size);
1666 io_buffer_initialize(self, &resized, NULL, size, io_flags_for_size(size),
Qnil);
1669 size_t preserve = buffer->size;
1670 if (preserve > size) preserve = size;
1671 memcpy(resized.base, buffer->base, preserve);
1673 io_buffer_resize_clear(buffer, resized.base, size);
1676 io_buffer_free(buffer);
1681rb_io_buffer_resize(
VALUE self,
size_t size)
1686 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1687 rb_raise(rb_eIOBufferLockedError,
"Cannot resize locked buffer!");
1690 if (buffer->base == NULL) {
1691 io_buffer_initialize(self, buffer, NULL, size, io_flags_for_size(size),
Qnil);
1695 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
1696 rb_raise(rb_eIOBufferAccessError,
"Cannot resize external buffer!");
1699#if defined(HAVE_MREMAP) && defined(MREMAP_MAYMOVE)
1700 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
1701 void *base = mremap(buffer->base, buffer->size, size, MREMAP_MAYMOVE);
1703 if (base == MAP_FAILED) {
1704 rb_sys_fail(
"rb_io_buffer_resize:mremap");
1707 io_buffer_resize_clear(buffer, base, size);
1709 buffer->base = base;
1710 buffer->size = size;
1716 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
1718 io_buffer_free(buffer);
1722 void *base = realloc(buffer->base, size);
1725 rb_sys_fail(
"rb_io_buffer_resize:realloc");
1728 io_buffer_resize_clear(buffer, base, size);
1730 buffer->base = base;
1731 buffer->size = size;
1736 io_buffer_resize_copy(self, buffer, size);
1760 rb_io_buffer_resize(self, io_buffer_extract_size(size));
1774 const void *ptr1, *ptr2;
1775 size_t size1, size2;
1777 rb_io_buffer_get_bytes_for_reading(self, &ptr1, &size1);
1778 rb_io_buffer_get_bytes_for_reading(other, &ptr2, &size2);
1780 if (size1 < size2) {
1784 if (size1 > size2) {
1788 return RB_INT2NUM(memcmp(ptr1, ptr2, size1));
1792io_buffer_validate_type(
size_t size,
size_t offset)
1794 if (offset > size) {
1795 rb_raise(rb_eArgError,
"Type extends beyond end of buffer! (offset=%"PRIdSIZE
" > size=%"PRIdSIZE
")", offset, size);
1817#define ruby_swap8(value) value
1825ruby_swapf32(
float value)
1827 union swapf32 swap = {.value = value};
1828 swap.integral = ruby_swap32(swap.integral);
1838ruby_swapf64(
double value)
1840 union swapf64 swap = {.value = value};
1841 swap.integral = ruby_swap64(swap.integral);
1845#define IO_BUFFER_DECLARE_TYPE(name, type, endian, wrap, unwrap, swap) \
1846static ID RB_IO_BUFFER_DATA_TYPE_##name; \
1849io_buffer_read_##name(const void* base, size_t size, size_t *offset) \
1851 io_buffer_validate_type(size, *offset + sizeof(type)); \
1853 memcpy(&value, (char*)base + *offset, sizeof(type)); \
1854 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1855 *offset += sizeof(type); \
1856 return wrap(value); \
1860io_buffer_write_##name(const void* base, size_t size, size_t *offset, VALUE _value) \
1862 io_buffer_validate_type(size, *offset + sizeof(type)); \
1863 type value = unwrap(_value); \
1864 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1865 memcpy((char*)base + *offset, &value, sizeof(type)); \
1866 *offset += sizeof(type); \
1870 RB_IO_BUFFER_DATA_TYPE_##name##_SIZE = sizeof(type) \
1878IO_BUFFER_DECLARE_TYPE(s16, int16_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap16)
1879IO_BUFFER_DECLARE_TYPE(S16, int16_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap16)
1883IO_BUFFER_DECLARE_TYPE(s32, int32_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap32)
1884IO_BUFFER_DECLARE_TYPE(S32, int32_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap32)
1886IO_BUFFER_DECLARE_TYPE(u64, uint64_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_ULL2NUM,
RB_NUM2ULL, ruby_swap64)
1887IO_BUFFER_DECLARE_TYPE(U64, uint64_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_ULL2NUM,
RB_NUM2ULL, ruby_swap64)
1888IO_BUFFER_DECLARE_TYPE(s64, int64_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_LL2NUM,
RB_NUM2LL, ruby_swap64)
1889IO_BUFFER_DECLARE_TYPE(S64, int64_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_LL2NUM,
RB_NUM2LL, ruby_swap64)
1891IO_BUFFER_DECLARE_TYPE(f32,
float, RB_IO_BUFFER_LITTLE_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf32)
1892IO_BUFFER_DECLARE_TYPE(F32,
float, RB_IO_BUFFER_BIG_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf32)
1893IO_BUFFER_DECLARE_TYPE(f64,
double, RB_IO_BUFFER_LITTLE_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf64)
1894IO_BUFFER_DECLARE_TYPE(F64,
double, RB_IO_BUFFER_BIG_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf64)
1895#undef IO_BUFFER_DECLARE_TYPE
1898io_buffer_buffer_type_size(
ID buffer_type)
1900#define IO_BUFFER_DATA_TYPE_SIZE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return RB_IO_BUFFER_DATA_TYPE_##name##_SIZE;
1901 IO_BUFFER_DATA_TYPE_SIZE(U8)
1902 IO_BUFFER_DATA_TYPE_SIZE(S8)
1903 IO_BUFFER_DATA_TYPE_SIZE(u16)
1904 IO_BUFFER_DATA_TYPE_SIZE(U16)
1905 IO_BUFFER_DATA_TYPE_SIZE(s16)
1906 IO_BUFFER_DATA_TYPE_SIZE(S16)
1907 IO_BUFFER_DATA_TYPE_SIZE(u32)
1908 IO_BUFFER_DATA_TYPE_SIZE(U32)
1909 IO_BUFFER_DATA_TYPE_SIZE(s32)
1910 IO_BUFFER_DATA_TYPE_SIZE(S32)
1911 IO_BUFFER_DATA_TYPE_SIZE(u64)
1912 IO_BUFFER_DATA_TYPE_SIZE(U64)
1913 IO_BUFFER_DATA_TYPE_SIZE(s64)
1914 IO_BUFFER_DATA_TYPE_SIZE(S64)
1915 IO_BUFFER_DATA_TYPE_SIZE(f32)
1916 IO_BUFFER_DATA_TYPE_SIZE(F32)
1917 IO_BUFFER_DATA_TYPE_SIZE(f64)
1918 IO_BUFFER_DATA_TYPE_SIZE(F64)
1919#undef IO_BUFFER_DATA_TYPE_SIZE
1921 rb_raise(rb_eArgError,
"Invalid type name!");
1935io_buffer_size_of(
VALUE klass,
VALUE buffer_type)
1939 for (
long i = 0; i <
RARRAY_LEN(buffer_type); i++) {
1950rb_io_buffer_get_value(
const void* base,
size_t size,
ID buffer_type,
size_t *offset)
1952#define IO_BUFFER_GET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return io_buffer_read_##name(base, size, offset);
1953 IO_BUFFER_GET_VALUE(U8)
1954 IO_BUFFER_GET_VALUE(S8)
1956 IO_BUFFER_GET_VALUE(u16)
1957 IO_BUFFER_GET_VALUE(U16)
1958 IO_BUFFER_GET_VALUE(s16)
1959 IO_BUFFER_GET_VALUE(S16)
1961 IO_BUFFER_GET_VALUE(u32)
1962 IO_BUFFER_GET_VALUE(U32)
1963 IO_BUFFER_GET_VALUE(s32)
1964 IO_BUFFER_GET_VALUE(S32)
1966 IO_BUFFER_GET_VALUE(u64)
1967 IO_BUFFER_GET_VALUE(U64)
1968 IO_BUFFER_GET_VALUE(s64)
1969 IO_BUFFER_GET_VALUE(S64)
1971 IO_BUFFER_GET_VALUE(f32)
1972 IO_BUFFER_GET_VALUE(F32)
1973 IO_BUFFER_GET_VALUE(f64)
1974 IO_BUFFER_GET_VALUE(F64)
1975#undef IO_BUFFER_GET_VALUE
1977 rb_raise(rb_eArgError,
"Invalid type name!");
2019 size_t offset = io_buffer_extract_offset(_offset);
2021 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2023 return rb_io_buffer_get_value(base, size,
RB_SYM2ID(
type), &offset);
2039 size_t offset = io_buffer_extract_offset(_offset);
2043 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2046 rb_raise(rb_eArgError,
"Argument buffer_types should be an array!");
2051 for (
long i = 0; i <
RARRAY_LEN(buffer_types); i++) {
2052 VALUE type = rb_ary_entry(buffer_types, i);
2054 rb_ary_push(array, value);
2063io_buffer_extract_count(
VALUE argument)
2065 if (rb_int_negative_p(argument)) {
2066 rb_raise(rb_eArgError,
"Count can't be negative!");
2073io_buffer_extract_offset_count(
ID buffer_type,
size_t size,
int argc,
VALUE *argv,
size_t *offset,
size_t *count)
2076 *offset = io_buffer_extract_offset(argv[0]);
2083 *count = io_buffer_extract_count(argv[1]);
2086 if (*offset > size) {
2087 rb_raise(rb_eArgError,
"The given offset is bigger than the buffer size!");
2090 *count = (size - *offset) / io_buffer_buffer_type_size(buffer_type);
2111io_buffer_each(
int argc,
VALUE *argv,
VALUE self)
2118 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2125 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2128 size_t offset, count;
2129 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2131 for (
size_t i = 0; i < count; i++) {
2132 size_t current_offset = offset;
2133 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2151io_buffer_values(
int argc,
VALUE *argv,
VALUE self)
2156 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2163 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2166 size_t offset, count;
2167 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2169 VALUE array = rb_ary_new_capa(count);
2171 for (
size_t i = 0; i < count; i++) {
2172 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2173 rb_ary_push(array, value);
2195io_buffer_each_byte(
int argc,
VALUE *argv,
VALUE self)
2202 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2204 size_t offset, count;
2205 io_buffer_extract_offset_count(RB_IO_BUFFER_DATA_TYPE_U8, size, argc-1, argv+1, &offset, &count);
2207 for (
size_t i = 0; i < count; i++) {
2208 unsigned char *value = (
unsigned char *)base + i + offset;
2216rb_io_buffer_set_value(
const void* base,
size_t size,
ID buffer_type,
size_t *offset,
VALUE value)
2218#define IO_BUFFER_SET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) {io_buffer_write_##name(base, size, offset, value); return;}
2219 IO_BUFFER_SET_VALUE(U8);
2220 IO_BUFFER_SET_VALUE(S8);
2222 IO_BUFFER_SET_VALUE(u16);
2223 IO_BUFFER_SET_VALUE(U16);
2224 IO_BUFFER_SET_VALUE(s16);
2225 IO_BUFFER_SET_VALUE(S16);
2227 IO_BUFFER_SET_VALUE(u32);
2228 IO_BUFFER_SET_VALUE(U32);
2229 IO_BUFFER_SET_VALUE(s32);
2230 IO_BUFFER_SET_VALUE(S32);
2232 IO_BUFFER_SET_VALUE(u64);
2233 IO_BUFFER_SET_VALUE(U64);
2234 IO_BUFFER_SET_VALUE(s64);
2235 IO_BUFFER_SET_VALUE(S64);
2237 IO_BUFFER_SET_VALUE(f32);
2238 IO_BUFFER_SET_VALUE(F32);
2239 IO_BUFFER_SET_VALUE(f64);
2240 IO_BUFFER_SET_VALUE(F64);
2241#undef IO_BUFFER_SET_VALUE
2243 rb_raise(rb_eArgError,
"Invalid type name!");
2281 size_t offset = io_buffer_extract_offset(_offset);
2283 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2285 rb_io_buffer_set_value(base, size,
RB_SYM2ID(
type), &offset, value);
2308 rb_raise(rb_eArgError,
"Argument buffer_types should be an array!");
2312 rb_raise(rb_eArgError,
"Argument values should be an array!");
2316 rb_raise(rb_eArgError,
"Argument buffer_types and values should have the same length!");
2319 size_t offset = io_buffer_extract_offset(_offset);
2323 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2325 for (
long i = 0; i <
RARRAY_LEN(buffer_types); i++) {
2326 VALUE type = rb_ary_entry(buffer_types, i);
2327 VALUE value = rb_ary_entry(values, i);
2328 rb_io_buffer_set_value(base, size,
RB_SYM2ID(
type), &offset, value);
2334static size_t IO_BUFFER_BLOCKING_SIZE = 1024*1024;
2337 unsigned char * destination;
2338 const unsigned char * source;
2343io_buffer_memmove_blocking(
void *data)
2347 memmove(arguments->destination, arguments->source, arguments->length);
2353io_buffer_memmove_unblock(
void *data)
2359io_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)
2363 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2365 io_buffer_validate_range(buffer, offset, length);
2367 if (source_offset + length > source_size) {
2368 rb_raise(rb_eArgError,
"The computed source range exceeds the size of the source buffer!");
2372 .destination = (
unsigned char*)base+offset,
2373 .source = (
unsigned char*)source_base+source_offset,
2377 if (arguments.length >= IO_BUFFER_BLOCKING_SIZE) {
2379 }
else if (arguments.length != 0) {
2380 memmove(arguments.destination, arguments.source, arguments.length);
2386io_buffer_copy_from(
struct rb_io_buffer *buffer,
const void *source_base,
size_t source_size,
int argc,
VALUE *argv)
2390 size_t source_offset;
2394 offset = io_buffer_extract_offset(argv[0]);
2399 source_offset = io_buffer_extract_offset(argv[2]);
2401 if (source_offset > source_size) {
2402 rb_raise(rb_eArgError,
"The given source offset is bigger than the source itself!");
2410 if (argc >= 2 && !
RB_NIL_P(argv[1])) {
2411 length = io_buffer_extract_length(argv[1]);
2415 length = source_size - source_offset;
2418 io_buffer_memmove(buffer, offset, source_base, source_offset, source_size, length);
2441rb_io_buffer_initialize_copy(
VALUE self,
VALUE source)
2446 const void *source_base;
2449 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2451 io_buffer_initialize(self, buffer, NULL, source_size, io_flags_for_size(source_size),
Qnil);
2453 return io_buffer_copy_from(buffer, source_base, source_size, 0, NULL);
2525io_buffer_copy(
int argc,
VALUE *argv,
VALUE self)
2532 VALUE source = argv[0];
2533 const void *source_base;
2536 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2538 return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2556io_buffer_get_string(
int argc,
VALUE *argv,
VALUE self)
2560 size_t offset, length;
2561 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
2565 io_buffer_get_bytes_for_reading(buffer, &base, &size);
2569 encoding = rb_find_encoding(argv[2]);
2572 encoding = rb_ascii8bit_encoding();
2575 io_buffer_validate_range(buffer, offset, length);
2577 return rb_enc_str_new((
const char*)base + offset, length, encoding);
2604io_buffer_set_string(
int argc,
VALUE *argv,
VALUE self)
2613 const void *source_base = RSTRING_PTR(
string);
2614 size_t source_size = RSTRING_LEN(
string);
2616 return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2620rb_io_buffer_clear(
VALUE self, uint8_t value,
size_t offset,
size_t length)
2627 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2629 io_buffer_validate_range(buffer, offset, length);
2631 memset((
char*)base + offset, value, length);
2666io_buffer_clear(
int argc,
VALUE *argv,
VALUE self)
2675 size_t offset, length;
2676 io_buffer_extract_offset_length(self, argc-1, argv+1, &offset, &length);
2678 rb_io_buffer_clear(self, value, offset, length);
2684io_buffer_default_size(
size_t page_size)
2687 const size_t platform_agnostic_default_size = 64*1024;
2690 const char *default_size = getenv(
"RUBY_IO_BUFFER_DEFAULT_SIZE");
2693 int value = atoi(default_size);
2701 if (platform_agnostic_default_size < page_size) {
2705 return platform_agnostic_default_size;
2711 rb_blocking_function_t *function;
2716io_buffer_blocking_region_begin(
VALUE _argument)
2720 return rb_io_blocking_region(argument->io, argument->function, argument->data);
2724io_buffer_blocking_region_ensure(
VALUE _argument)
2728 io_buffer_unlock(argument->buffer);
2734io_buffer_blocking_region(
VALUE io,
struct rb_io_buffer *buffer, rb_blocking_function_t *function,
void *data)
2737 struct rb_io *ioptr;
2743 .function = function,
2748 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
2749 return io_buffer_blocking_region_begin((
VALUE)&argument);
2753 io_buffer_lock(buffer);
2755 return rb_ensure(io_buffer_blocking_region_begin, (
VALUE)&argument, io_buffer_blocking_region_ensure, (
VALUE)&argument);
2771io_buffer_read_internal(
void *_argument)
2777 ssize_t result = read(argument->descriptor, argument->base, argument->size);
2782 else if (result == 0) {
2788 if (total >= argument->length) {
2792 argument->base = argument->base + result;
2793 argument->size = argument->size - result;
2799rb_io_buffer_read(
VALUE self,
VALUE io,
size_t length,
size_t offset)
2802 if (scheduler !=
Qnil) {
2805 if (!UNDEF_P(result)) {
2813 io_buffer_validate_range(buffer, offset, length);
2819 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2821 base = (
unsigned char*)base + offset;
2822 size = size - offset;
2825 .descriptor = descriptor,
2831 return io_buffer_blocking_region(io, buffer, io_buffer_read_internal, &argument);
2861io_buffer_read(
int argc,
VALUE *argv,
VALUE self)
2867 size_t length, offset;
2868 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
2870 return rb_io_buffer_read(self, io, length, offset);
2887io_buffer_pread_internal(
void *_argument)
2893 ssize_t result = pread(argument->descriptor, argument->base, argument->size, argument->offset);
2898 else if (result == 0) {
2904 if (total >= argument->length) {
2908 argument->base = argument->base + result;
2909 argument->size = argument->size - result;
2910 argument->offset = argument->offset + result;
2916rb_io_buffer_pread(
VALUE self,
VALUE io, rb_off_t from,
size_t length,
size_t offset)
2919 if (scheduler !=
Qnil) {
2922 if (!UNDEF_P(result)) {
2930 io_buffer_validate_range(buffer, offset, length);
2936 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2938 base = (
unsigned char*)base + offset;
2939 size = size - offset;
2942 .descriptor = descriptor,
2949 return io_buffer_blocking_region(io, buffer, io_buffer_pread_internal, &argument);
2983io_buffer_pread(
int argc,
VALUE *argv,
VALUE self)
2990 size_t length, offset;
2991 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
2993 return rb_io_buffer_pread(self, io, from, length, offset);
3008io_buffer_write_internal(
void *_argument)
3014 ssize_t result = write(argument->descriptor, argument->base, argument->size);
3019 else if (result == 0) {
3025 if (total >= argument->length) {
3029 argument->base = argument->base + result;
3030 argument->size = argument->size - result;
3036rb_io_buffer_write(
VALUE self,
VALUE io,
size_t length,
size_t offset)
3039 if (scheduler !=
Qnil) {
3042 if (!UNDEF_P(result)) {
3050 io_buffer_validate_range(buffer, offset, length);
3056 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3058 base = (
unsigned char*)base + offset;
3059 size = size - offset;
3062 .descriptor = descriptor,
3068 return io_buffer_blocking_region(io, buffer, io_buffer_write_internal, &argument);
3091io_buffer_write(
int argc,
VALUE *argv,
VALUE self)
3097 size_t length, offset;
3098 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
3100 return rb_io_buffer_write(self, io, length, offset);
3116io_buffer_pwrite_internal(
void *_argument)
3122 ssize_t result = pwrite(argument->descriptor, argument->base, argument->size, argument->offset);
3127 else if (result == 0) {
3133 if (total >= argument->length) {
3137 argument->base = argument->base + result;
3138 argument->size = argument->size - result;
3139 argument->offset = argument->offset + result;
3145rb_io_buffer_pwrite(
VALUE self,
VALUE io, rb_off_t from,
size_t length,
size_t offset)
3148 if (scheduler !=
Qnil) {
3151 if (!UNDEF_P(result)) {
3159 io_buffer_validate_range(buffer, offset, length);
3165 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3167 base = (
unsigned char*)base + offset;
3168 size = size - offset;
3171 .descriptor = descriptor,
3186 return io_buffer_blocking_region(io, buffer, io_buffer_pwrite_internal, &argument);
3214io_buffer_pwrite(
int argc,
VALUE *argv,
VALUE self)
3221 size_t length, offset;
3222 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
3224 return rb_io_buffer_pwrite(self, io, from, length, offset);
3230 if (buffer->size == 0)
3231 rb_raise(rb_eIOBufferMaskError,
"Zero-length mask given!");
3235memory_and(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3237 for (
size_t offset = 0; offset < size; offset += 1) {
3238 output[offset] = base[offset] & mask[offset % mask_size];
3263 io_buffer_check_mask(mask_buffer);
3265 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3269 memory_and(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3275memory_or(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3277 for (
size_t offset = 0; offset < size; offset += 1) {
3278 output[offset] = base[offset] | mask[offset % mask_size];
3303 io_buffer_check_mask(mask_buffer);
3305 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3309 memory_or(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3315memory_xor(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3317 for (
size_t offset = 0; offset < size; offset += 1) {
3318 output[offset] = base[offset] ^ mask[offset % mask_size];
3343 io_buffer_check_mask(mask_buffer);
3345 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3349 memory_xor(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3355memory_not(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size)
3357 for (
size_t offset = 0; offset < size; offset += 1) {
3358 output[offset] = ~base[offset];
3375io_buffer_not(
VALUE self)
3380 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3384 memory_not(output_buffer->base, buffer->base, buffer->size);
3392 if (a->base > b->base) {
3393 return io_buffer_overlaps(b, a);
3396 return (b->base >= a->base) && (b->base < (
void*)((
unsigned char *)a->base + a->size));
3402 if (io_buffer_overlaps(a, b))
3403 rb_raise(rb_eIOBufferMaskError,
"Mask overlaps source buffer!");
3407memory_and_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3409 for (
size_t offset = 0; offset < size; offset += 1) {
3410 base[offset] &= mask[offset % mask_size];
3440 io_buffer_check_mask(mask_buffer);
3441 io_buffer_check_overlaps(buffer, mask_buffer);
3445 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3447 memory_and_inplace(base, size, mask_buffer->base, mask_buffer->size);
3453memory_or_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3455 for (
size_t offset = 0; offset < size; offset += 1) {
3456 base[offset] |= mask[offset % mask_size];
3486 io_buffer_check_mask(mask_buffer);
3487 io_buffer_check_overlaps(buffer, mask_buffer);
3491 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3493 memory_or_inplace(base, size, mask_buffer->base, mask_buffer->size);
3499memory_xor_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3501 for (
size_t offset = 0; offset < size; offset += 1) {
3502 base[offset] ^= mask[offset % mask_size];
3532 io_buffer_check_mask(mask_buffer);
3533 io_buffer_check_overlaps(buffer, mask_buffer);
3537 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3539 memory_xor_inplace(base, size, mask_buffer->base, mask_buffer->size);
3545memory_not_inplace(
unsigned char * restrict base,
size_t size)
3547 for (
size_t offset = 0; offset < size; offset += 1) {
3548 base[offset] = ~base[offset];
3570io_buffer_not_inplace(
VALUE self)
3577 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3579 memory_not_inplace(base, size);
3697 GetSystemInfo(&info);
3698 RUBY_IO_BUFFER_PAGE_SIZE = info.dwPageSize;
3700 RUBY_IO_BUFFER_PAGE_SIZE = sysconf(_SC_PAGESIZE);
3703 RUBY_IO_BUFFER_DEFAULT_SIZE = io_buffer_default_size(RUBY_IO_BUFFER_PAGE_SIZE);
3706 rb_define_const(rb_cIOBuffer,
"PAGE_SIZE",
SIZET2NUM(RUBY_IO_BUFFER_PAGE_SIZE));
3711 rb_define_const(rb_cIOBuffer,
"DEFAULT_SIZE",
SIZET2NUM(RUBY_IO_BUFFER_DEFAULT_SIZE));
3716 rb_define_method(rb_cIOBuffer,
"initialize_copy", rb_io_buffer_initialize_copy, 1);
3726 rb_define_const(rb_cIOBuffer,
"EXTERNAL",
RB_INT2NUM(RB_IO_BUFFER_EXTERNAL));
3729 rb_define_const(rb_cIOBuffer,
"INTERNAL",
RB_INT2NUM(RB_IO_BUFFER_INTERNAL));
3732 rb_define_const(rb_cIOBuffer,
"MAPPED",
RB_INT2NUM(RB_IO_BUFFER_MAPPED));
3735 rb_define_const(rb_cIOBuffer,
"SHARED",
RB_INT2NUM(RB_IO_BUFFER_SHARED));
3738 rb_define_const(rb_cIOBuffer,
"LOCKED",
RB_INT2NUM(RB_IO_BUFFER_LOCKED));
3741 rb_define_const(rb_cIOBuffer,
"PRIVATE",
RB_INT2NUM(RB_IO_BUFFER_PRIVATE));
3744 rb_define_const(rb_cIOBuffer,
"READONLY",
RB_INT2NUM(RB_IO_BUFFER_READONLY));
3747 rb_define_const(rb_cIOBuffer,
"LITTLE_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_LITTLE_ENDIAN));
3750 rb_define_const(rb_cIOBuffer,
"BIG_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_BIG_ENDIAN));
3753 rb_define_const(rb_cIOBuffer,
"HOST_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_HOST_ENDIAN));
3756 rb_define_const(rb_cIOBuffer,
"NETWORK_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_NETWORK_ENDIAN));
3782#define IO_BUFFER_DEFINE_DATA_TYPE(name) RB_IO_BUFFER_DATA_TYPE_##name = rb_intern_const(#name)
3783 IO_BUFFER_DEFINE_DATA_TYPE(U8);
3784 IO_BUFFER_DEFINE_DATA_TYPE(S8);
3786 IO_BUFFER_DEFINE_DATA_TYPE(u16);
3787 IO_BUFFER_DEFINE_DATA_TYPE(U16);
3788 IO_BUFFER_DEFINE_DATA_TYPE(s16);
3789 IO_BUFFER_DEFINE_DATA_TYPE(S16);
3791 IO_BUFFER_DEFINE_DATA_TYPE(u32);
3792 IO_BUFFER_DEFINE_DATA_TYPE(U32);
3793 IO_BUFFER_DEFINE_DATA_TYPE(s32);
3794 IO_BUFFER_DEFINE_DATA_TYPE(S32);
3796 IO_BUFFER_DEFINE_DATA_TYPE(u64);
3797 IO_BUFFER_DEFINE_DATA_TYPE(U64);
3798 IO_BUFFER_DEFINE_DATA_TYPE(s64);
3799 IO_BUFFER_DEFINE_DATA_TYPE(S64);
3801 IO_BUFFER_DEFINE_DATA_TYPE(f32);
3802 IO_BUFFER_DEFINE_DATA_TYPE(F32);
3803 IO_BUFFER_DEFINE_DATA_TYPE(f64);
3804 IO_BUFFER_DEFINE_DATA_TYPE(F64);
3805#undef IO_BUFFER_DEFINE_DATA_TYPE
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define T_STRING
Old name of RUBY_T_STRING.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define CLASS_OF
Old name of rb_class_of.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define NUM2DBL
Old name of rb_num2dbl.
#define Qnil
Old name of RUBY_Qnil.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eRuntimeError
RuntimeError exception.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
VALUE rb_mComparable
Comparable module.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RETURN_ENUMERATOR_KW(obj, argc, argv, kw_splat)
Identical to RETURN_SIZED_ENUMERATOR_KW(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_locktmp(VALUE str)
Obtains a "temporary lock" of the string.
VALUE rb_str_unlocktmp(VALUE str)
Releases a lock formerly obtained by rb_str_locktmp().
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define RB_SYM2ID
Just another name of rb_sym2id.
VALUE rb_io_get_io(VALUE io)
Identical to rb_io_check_io(), except it raises exceptions on conversion failures.
int rb_io_descriptor(VALUE io)
Returns an integer representing the numeric file descriptor for io.
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
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...
#define RB_NOGVL_OFFLOAD_SAFE
Passing this flag to rb_nogvl() indicates that the passed function is safe to offload to a background...
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_UINT2NUM
Just another name of rb_uint2num_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
static unsigned int RB_NUM2UINT(VALUE x)
Converts an instance of rb_cNumeric into C's unsigned int.
#define RB_LL2NUM
Just another name of rb_ll2num_inline.
#define RB_ULL2NUM
Just another name of rb_ull2num_inline.
#define RB_NUM2ULL
Just another name of rb_num2ull_inline.
#define RB_NUM2LL
Just another name of rb_num2ll_inline.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield(VALUE val)
Yields the block.
static VALUE RB_INT2FIX(long i)
Converts a C's long into an instance of rb_cInteger.
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define NUM2OFFT
Converts an instance of rb_cNumeric into C's off_t.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
VALUE rb_str_to_str(VALUE obj)
Identical to rb_check_string_type(), except it raises exceptions in case of conversion failures.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#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...
#define errno
Ractor-aware version of errno.
#define RB_NO_KEYWORDS
Do not pass keywords.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
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.
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.
static VALUE rb_fiber_scheduler_io_result(ssize_t result, int error)
Wrap a ssize_t and int errno into a single VALUE.
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.
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.
static bool RB_NIL_P(VALUE obj)
Checks if the given object is nil.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
Ruby's IO, metadata and buffers.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.