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_mark(
void *_buffer)
279 rb_gc_mark(buffer->source);
283rb_io_buffer_type_free(
void *_buffer)
287 io_buffer_free(buffer);
291rb_io_buffer_type_size(
const void *_buffer)
297 total += buffer->size;
306 .dmark = rb_io_buffer_type_mark,
307 .dfree = rb_io_buffer_type_free,
308 .dsize = rb_io_buffer_type_size,
311 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
314static inline enum rb_io_buffer_flags
315io_buffer_extract_flags(
VALUE argument)
317 if (rb_int_negative_p(argument)) {
318 rb_raise(rb_eArgError,
"Flags can't be negative!");
321 enum rb_io_buffer_flags flags =
RB_NUM2UINT(argument);
324 return flags & RB_IO_BUFFER_FLAGS_MASK;
329io_buffer_extract_offset(
VALUE argument)
331 if (rb_int_negative_p(argument)) {
332 rb_raise(rb_eArgError,
"Offset can't be negative!");
342io_buffer_extract_length(
VALUE argument)
344 if (rb_int_negative_p(argument)) {
345 rb_raise(rb_eArgError,
"Length can't be negative!");
354io_buffer_extract_size(
VALUE argument)
356 if (rb_int_negative_p(argument)) {
357 rb_raise(rb_eArgError,
"Size can't be negative!");
366io_buffer_extract_width(
VALUE argument,
size_t minimum)
368 if (rb_int_negative_p(argument)) {
369 rb_raise(rb_eArgError,
"Width can't be negative!");
374 if (width < minimum) {
375 rb_raise(rb_eArgError,
"Width must be at least %" PRIuSIZE
"!", minimum);
386io_buffer_default_length(
const struct rb_io_buffer *buffer,
size_t offset)
388 if (offset > buffer->size) {
389 rb_raise(rb_eArgError,
"The given offset is bigger than the buffer size!");
393 return buffer->size - offset;
402io_buffer_extract_length_offset(
VALUE self,
int argc,
VALUE argv[],
size_t *length,
size_t *offset)
407 if (argc >= 2 && !
NIL_P(argv[1])) {
408 *offset = io_buffer_extract_offset(argv[1]);
414 if (argc >= 1 && !
NIL_P(argv[0])) {
415 *length = io_buffer_extract_length(argv[0]);
418 *length = io_buffer_default_length(buffer, *offset);
434io_buffer_extract_offset_length(
VALUE self,
int argc,
VALUE argv[],
size_t *offset,
size_t *length)
439 if (argc >= 1 && !
NIL_P(argv[0])) {
440 *offset = io_buffer_extract_offset(argv[0]);
446 if (argc >= 2 && !
NIL_P(argv[1])) {
447 *length = io_buffer_extract_length(argv[1]);
450 *length = io_buffer_default_length(buffer, *offset);
457rb_io_buffer_type_allocate(
VALUE self)
462 io_buffer_zero(buffer);
467static VALUE io_buffer_for_make_instance(
VALUE klass,
VALUE string,
enum rb_io_buffer_flags flags)
469 VALUE instance = rb_io_buffer_type_allocate(klass);
474 flags |= RB_IO_BUFFER_EXTERNAL;
477 flags |= RB_IO_BUFFER_READONLY;
479 if (!(flags & RB_IO_BUFFER_READONLY))
480 rb_str_modify(
string);
482 io_buffer_initialize(instance, buffer, RSTRING_PTR(
string), RSTRING_LEN(
string), flags,
string);
491 enum rb_io_buffer_flags flags;
495io_buffer_for_yield_instance(
VALUE _arguments)
499 arguments->instance = io_buffer_for_make_instance(arguments->klass, arguments->string, arguments->flags);
503 return rb_yield(arguments->instance);
507io_buffer_for_yield_instance_ensure(
VALUE _arguments)
511 if (arguments->instance !=
Qnil) {
512 rb_io_buffer_free(arguments->instance);
557rb_io_buffer_type_for(
VALUE klass,
VALUE string)
571 return rb_ensure(io_buffer_for_yield_instance, (
VALUE)&arguments, io_buffer_for_yield_instance_ensure, (
VALUE)&arguments);
575 string = rb_str_tmp_frozen_acquire(
string);
576 return io_buffer_for_make_instance(klass,
string, RB_IO_BUFFER_READONLY);
594rb_io_buffer_type_string(
VALUE klass,
VALUE length)
604 rb_ensure(io_buffer_for_yield_instance, (
VALUE)&arguments, io_buffer_for_yield_instance_ensure, (
VALUE)&arguments);
610rb_io_buffer_new(
void *base,
size_t size,
enum rb_io_buffer_flags flags)
612 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
617 io_buffer_initialize(instance, buffer, base, size, flags,
Qnil);
623rb_io_buffer_map(
VALUE io,
size_t size, rb_off_t offset,
enum rb_io_buffer_flags flags)
625 io_buffer_experimental();
627 VALUE instance = rb_io_buffer_type_allocate(rb_cIOBuffer);
634 io_buffer_map_file(buffer, descriptor, size, offset, flags);
677io_buffer_map(
int argc,
VALUE *argv,
VALUE klass)
685 if (argc >= 2 && !
RB_NIL_P(argv[1])) {
686 size = io_buffer_extract_size(argv[1]);
689 rb_off_t file_size = rb_file_size(io);
693 rb_raise(rb_eArgError,
"Invalid negative file size!");
696 else if ((uintmax_t)file_size > SIZE_MAX) {
697 rb_raise(rb_eArgError,
"File larger than address space!");
701 size = (size_t)file_size;
711 enum rb_io_buffer_flags flags = 0;
713 flags = io_buffer_extract_flags(argv[3]);
716 return rb_io_buffer_map(io, size, offset, flags);
720static inline enum rb_io_buffer_flags
721io_flags_for_size(
size_t size)
723 if (size >= RUBY_IO_BUFFER_PAGE_SIZE) {
724 return RB_IO_BUFFER_MAPPED;
727 return RB_IO_BUFFER_INTERNAL;
755rb_io_buffer_initialize(
int argc,
VALUE *argv,
VALUE self)
757 io_buffer_experimental();
766 size = io_buffer_extract_size(argv[0]);
769 size = RUBY_IO_BUFFER_DEFAULT_SIZE;
772 enum rb_io_buffer_flags flags = 0;
774 flags = io_buffer_extract_flags(argv[1]);
777 flags |= io_flags_for_size(size);
780 io_buffer_initialize(self, buffer, NULL, size, flags,
Qnil);
786io_buffer_validate_slice(
VALUE source,
void *base,
size_t size)
788 void *source_base = NULL;
789 size_t source_size = 0;
795 rb_io_buffer_get_bytes(source, &source_base, &source_size);
799 if (source_base == NULL)
return 0;
802 if (base < source_base)
return 0;
804 const void *source_end = (
char*)source_base + source_size;
805 const void *end = (
char*)base + size;
808 if (end > source_end)
return 0;
817 if (buffer->source !=
Qnil) {
819 return io_buffer_validate_slice(buffer->source, buffer->base, buffer->size);
826enum rb_io_buffer_flags
827rb_io_buffer_get_bytes(
VALUE self,
void **base,
size_t *size)
832 if (io_buffer_validate(buffer)) {
834 *base = buffer->base;
835 *size = buffer->size;
837 return buffer->flags;
849io_buffer_get_bytes_for_writing(
struct rb_io_buffer *buffer,
void **base,
size_t *size)
851 if (buffer->flags & RB_IO_BUFFER_READONLY ||
853 rb_raise(rb_eIOBufferAccessError,
"Buffer is not writable!");
856 if (!io_buffer_validate(buffer)) {
857 rb_raise(rb_eIOBufferInvalidatedError,
"Buffer is invalid!");
861 *base = buffer->base;
862 *size = buffer->size;
870rb_io_buffer_get_bytes_for_writing(
VALUE self,
void **base,
size_t *size)
875 io_buffer_get_bytes_for_writing(buffer, base, size);
879io_buffer_get_bytes_for_reading(
struct rb_io_buffer *buffer,
const void **base,
size_t *size)
881 if (!io_buffer_validate(buffer)) {
882 rb_raise(rb_eIOBufferInvalidatedError,
"Buffer has been invalidated!");
886 *base = buffer->base;
887 *size = buffer->size;
895rb_io_buffer_get_bytes_for_reading(
VALUE self,
const void **base,
size_t *size)
900 io_buffer_get_bytes_for_reading(buffer, base, size);
913rb_io_buffer_to_s(
VALUE self)
921 rb_str_catf(result,
" %p+%"PRIdSIZE, buffer->base, buffer->size);
923 if (buffer->base == NULL) {
927 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
931 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
935 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
939 if (buffer->flags & RB_IO_BUFFER_FILE) {
943 if (buffer->flags & RB_IO_BUFFER_SHARED) {
947 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
951 if (buffer->flags & RB_IO_BUFFER_PRIVATE) {
955 if (buffer->flags & RB_IO_BUFFER_READONLY) {
959 if (buffer->source !=
Qnil) {
963 if (!io_buffer_validate(buffer)) {
973io_buffer_hexdump_output_size(
size_t width,
size_t size,
int first)
978 size_t whole_lines = (size / width);
979 size_t partial_line = (size % width) ? 1 : 0;
984 total += (whole_lines + partial_line) * (1 + 10 + width*3 + 1 + 1);
987 if (size && first) total -= 1;
996io_buffer_hexdump(
VALUE string,
size_t width,
const char *base,
size_t length,
size_t offset,
int first)
998 char *text = alloca(width+1);
1001 for (; offset < length; offset += width) {
1002 memset(text,
'\0', width);
1004 rb_str_catf(
string,
"0x%08" PRIxSIZE
" ", offset);
1008 rb_str_catf(
string,
"\n0x%08" PRIxSIZE
" ", offset);
1011 for (
size_t i = 0; i < width; i += 1) {
1012 if (offset+i < length) {
1013 unsigned char value = ((
unsigned char*)base)[offset+i];
1015 if (value < 127 && isprint(value)) {
1016 text[i] = (char)value;
1022 rb_str_catf(
string,
" %02x", value);
1029 rb_str_catf(
string,
" %s", text);
1048rb_io_buffer_inspect(
VALUE self)
1053 VALUE result = rb_io_buffer_to_s(self);
1055 if (io_buffer_validate(buffer)) {
1057 size_t size = buffer->size;
1060 if (size > RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE) {
1061 size = RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE;
1065 io_buffer_hexdump(result, RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH, buffer->base, size, 0, 0);
1068 rb_str_catf(result,
"\n(and %" PRIuSIZE
" more bytes not printed)", buffer->size - size);
1082rb_io_buffer_size(
VALUE self)
1099rb_io_buffer_valid_p(
VALUE self)
1104 return RBOOL(io_buffer_validate(buffer));
1122rb_io_buffer_null_p(
VALUE self)
1127 return RBOOL(buffer->base == NULL);
1138rb_io_buffer_empty_p(
VALUE self)
1143 return RBOOL(buffer->size == 0);
1158rb_io_buffer_external_p(
VALUE self)
1163 return RBOOL(buffer->flags & RB_IO_BUFFER_EXTERNAL);
1183rb_io_buffer_internal_p(
VALUE self)
1188 return RBOOL(buffer->flags & RB_IO_BUFFER_INTERNAL);
1205rb_io_buffer_mapped_p(
VALUE self)
1210 return RBOOL(buffer->flags & RB_IO_BUFFER_MAPPED);
1237rb_io_buffer_shared_p(
VALUE self)
1242 return RBOOL(buffer->flags & RB_IO_BUFFER_SHARED);
1260rb_io_buffer_locked_p(
VALUE self)
1265 return RBOOL(buffer->flags & RB_IO_BUFFER_LOCKED);
1291rb_io_buffer_private_p(
VALUE self)
1296 return RBOOL(buffer->flags & RB_IO_BUFFER_PRIVATE);
1300rb_io_buffer_readonly_p(
VALUE self)
1305 return buffer->flags & RB_IO_BUFFER_READONLY;
1317io_buffer_readonly_p(
VALUE self)
1319 return RBOOL(rb_io_buffer_readonly_p(self));
1325 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1326 rb_raise(rb_eIOBufferLockedError,
"Buffer already locked!");
1329 buffer->flags |= RB_IO_BUFFER_LOCKED;
1333rb_io_buffer_lock(
VALUE self)
1338 io_buffer_lock(buffer);
1346 if (!(buffer->flags & RB_IO_BUFFER_LOCKED)) {
1347 rb_raise(rb_eIOBufferLockedError,
"Buffer not locked!");
1350 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1354rb_io_buffer_unlock(
VALUE self)
1359 io_buffer_unlock(buffer);
1365rb_io_buffer_try_unlock(
VALUE self)
1370 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1371 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1409rb_io_buffer_locked(
VALUE self)
1414 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1415 rb_raise(rb_eIOBufferLockedError,
"Buffer already locked!");
1418 buffer->flags |= RB_IO_BUFFER_LOCKED;
1422 buffer->flags &= ~RB_IO_BUFFER_LOCKED;
1453rb_io_buffer_free(
VALUE self)
1458 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1459 rb_raise(rb_eIOBufferLockedError,
"Buffer is locked!");
1462 io_buffer_free(buffer);
1472 io_buffer_unlock(buffer);
1473 io_buffer_free(buffer);
1481io_buffer_validate_range(
struct rb_io_buffer *buffer,
size_t offset,
size_t length)
1484 if (offset + length > buffer->size) {
1485 rb_raise(rb_eArgError,
"Specified offset+length is bigger than the buffer size!");
1506rb_io_buffer_hexdump(
int argc,
VALUE *argv,
VALUE self)
1510 size_t offset, length;
1511 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1513 size_t width = RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH;
1515 width = io_buffer_extract_width(argv[2], 1);
1519 io_buffer_validate_range(buffer, offset, length);
1523 if (io_buffer_validate(buffer) && buffer->base) {
1524 result =
rb_str_buf_new(io_buffer_hexdump_output_size(width, length, 1));
1526 io_buffer_hexdump(result, width, buffer->base, offset+length, offset, 1);
1533rb_io_buffer_slice(
struct rb_io_buffer *buffer,
VALUE self,
size_t offset,
size_t length)
1535 io_buffer_validate_range(buffer, offset, length);
1541 slice->flags |= (buffer->flags & RB_IO_BUFFER_READONLY);
1542 slice->base = (
char*)buffer->base + offset;
1543 slice->size = length;
1546 if (buffer->source !=
Qnil) {
1547 RB_OBJ_WRITE(instance, &slice->source, buffer->source);
1607io_buffer_slice(
int argc,
VALUE *argv,
VALUE self)
1611 size_t offset, length;
1612 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
1614 return rb_io_buffer_slice(buffer, self, offset, length);
1636rb_io_buffer_transfer(
VALUE self)
1641 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1642 rb_raise(rb_eIOBufferLockedError,
"Cannot transfer ownership of locked buffer!");
1649 *transferred = *buffer;
1650 io_buffer_zero(buffer);
1656io_buffer_resize_clear(
struct rb_io_buffer *buffer,
void* base,
size_t size)
1658 if (size > buffer->size) {
1659 memset((
unsigned char*)base+buffer->size, 0, size - buffer->size);
1668 io_buffer_initialize(self, &resized, NULL, size, io_flags_for_size(size),
Qnil);
1671 size_t preserve = buffer->size;
1672 if (preserve > size) preserve = size;
1673 memcpy(resized.base, buffer->base, preserve);
1675 io_buffer_resize_clear(buffer, resized.base, size);
1678 io_buffer_free(buffer);
1683rb_io_buffer_resize(
VALUE self,
size_t size)
1688 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
1689 rb_raise(rb_eIOBufferLockedError,
"Cannot resize locked buffer!");
1692 if (buffer->base == NULL) {
1693 io_buffer_initialize(self, buffer, NULL, size, io_flags_for_size(size),
Qnil);
1697 if (buffer->flags & RB_IO_BUFFER_EXTERNAL) {
1698 rb_raise(rb_eIOBufferAccessError,
"Cannot resize external buffer!");
1701#if defined(HAVE_MREMAP) && defined(MREMAP_MAYMOVE)
1702 if (buffer->flags & RB_IO_BUFFER_MAPPED) {
1703 void *base = mremap(buffer->base, buffer->size, size, MREMAP_MAYMOVE);
1705 if (base == MAP_FAILED) {
1706 rb_sys_fail(
"rb_io_buffer_resize:mremap");
1709 io_buffer_resize_clear(buffer, base, size);
1711 buffer->base = base;
1712 buffer->size = size;
1718 if (buffer->flags & RB_IO_BUFFER_INTERNAL) {
1720 io_buffer_free(buffer);
1724 void *base = realloc(buffer->base, size);
1727 rb_sys_fail(
"rb_io_buffer_resize:realloc");
1730 io_buffer_resize_clear(buffer, base, size);
1732 buffer->base = base;
1733 buffer->size = size;
1738 io_buffer_resize_copy(self, buffer, size);
1762 rb_io_buffer_resize(self, io_buffer_extract_size(size));
1776 const void *ptr1, *ptr2;
1777 size_t size1, size2;
1779 rb_io_buffer_get_bytes_for_reading(self, &ptr1, &size1);
1780 rb_io_buffer_get_bytes_for_reading(other, &ptr2, &size2);
1782 if (size1 < size2) {
1786 if (size1 > size2) {
1790 return RB_INT2NUM(memcmp(ptr1, ptr2, size1));
1794io_buffer_validate_type(
size_t size,
size_t offset)
1796 if (offset > size) {
1797 rb_raise(rb_eArgError,
"Type extends beyond end of buffer! (offset=%"PRIdSIZE
" > size=%"PRIdSIZE
")", offset, size);
1819#define ruby_swap8(value) value
1827ruby_swapf32(
float value)
1829 union swapf32 swap = {.value = value};
1830 swap.integral = ruby_swap32(swap.integral);
1840ruby_swapf64(
double value)
1842 union swapf64 swap = {.value = value};
1843 swap.integral = ruby_swap64(swap.integral);
1847#define IO_BUFFER_DECLARE_TYPE(name, type, endian, wrap, unwrap, swap) \
1848static ID RB_IO_BUFFER_DATA_TYPE_##name; \
1851io_buffer_read_##name(const void* base, size_t size, size_t *offset) \
1853 io_buffer_validate_type(size, *offset + sizeof(type)); \
1855 memcpy(&value, (char*)base + *offset, sizeof(type)); \
1856 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1857 *offset += sizeof(type); \
1858 return wrap(value); \
1862io_buffer_write_##name(const void* base, size_t size, size_t *offset, VALUE _value) \
1864 io_buffer_validate_type(size, *offset + sizeof(type)); \
1865 type value = unwrap(_value); \
1866 if (endian != RB_IO_BUFFER_HOST_ENDIAN) value = swap(value); \
1867 memcpy((char*)base + *offset, &value, sizeof(type)); \
1868 *offset += sizeof(type); \
1872 RB_IO_BUFFER_DATA_TYPE_##name##_SIZE = sizeof(type) \
1880IO_BUFFER_DECLARE_TYPE(s16, int16_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap16)
1881IO_BUFFER_DECLARE_TYPE(S16, int16_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap16)
1885IO_BUFFER_DECLARE_TYPE(s32, int32_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap32)
1886IO_BUFFER_DECLARE_TYPE(S32, int32_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_INT2NUM,
RB_NUM2INT, ruby_swap32)
1888IO_BUFFER_DECLARE_TYPE(u64, uint64_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_ULL2NUM,
RB_NUM2ULL, ruby_swap64)
1889IO_BUFFER_DECLARE_TYPE(U64, uint64_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_ULL2NUM,
RB_NUM2ULL, ruby_swap64)
1890IO_BUFFER_DECLARE_TYPE(s64, int64_t, RB_IO_BUFFER_LITTLE_ENDIAN,
RB_LL2NUM,
RB_NUM2LL, ruby_swap64)
1891IO_BUFFER_DECLARE_TYPE(S64, int64_t, RB_IO_BUFFER_BIG_ENDIAN,
RB_LL2NUM,
RB_NUM2LL, ruby_swap64)
1893IO_BUFFER_DECLARE_TYPE(f32,
float, RB_IO_BUFFER_LITTLE_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf32)
1894IO_BUFFER_DECLARE_TYPE(F32,
float, RB_IO_BUFFER_BIG_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf32)
1895IO_BUFFER_DECLARE_TYPE(f64,
double, RB_IO_BUFFER_LITTLE_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf64)
1896IO_BUFFER_DECLARE_TYPE(F64,
double, RB_IO_BUFFER_BIG_ENDIAN,
DBL2NUM,
NUM2DBL, ruby_swapf64)
1897#undef IO_BUFFER_DECLARE_TYPE
1900io_buffer_buffer_type_size(
ID buffer_type)
1902#define IO_BUFFER_DATA_TYPE_SIZE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return RB_IO_BUFFER_DATA_TYPE_##name##_SIZE;
1903 IO_BUFFER_DATA_TYPE_SIZE(U8)
1904 IO_BUFFER_DATA_TYPE_SIZE(S8)
1905 IO_BUFFER_DATA_TYPE_SIZE(u16)
1906 IO_BUFFER_DATA_TYPE_SIZE(U16)
1907 IO_BUFFER_DATA_TYPE_SIZE(s16)
1908 IO_BUFFER_DATA_TYPE_SIZE(S16)
1909 IO_BUFFER_DATA_TYPE_SIZE(u32)
1910 IO_BUFFER_DATA_TYPE_SIZE(U32)
1911 IO_BUFFER_DATA_TYPE_SIZE(s32)
1912 IO_BUFFER_DATA_TYPE_SIZE(S32)
1913 IO_BUFFER_DATA_TYPE_SIZE(u64)
1914 IO_BUFFER_DATA_TYPE_SIZE(U64)
1915 IO_BUFFER_DATA_TYPE_SIZE(s64)
1916 IO_BUFFER_DATA_TYPE_SIZE(S64)
1917 IO_BUFFER_DATA_TYPE_SIZE(f32)
1918 IO_BUFFER_DATA_TYPE_SIZE(F32)
1919 IO_BUFFER_DATA_TYPE_SIZE(f64)
1920 IO_BUFFER_DATA_TYPE_SIZE(F64)
1921#undef IO_BUFFER_DATA_TYPE_SIZE
1923 rb_raise(rb_eArgError,
"Invalid type name!");
1937io_buffer_size_of(
VALUE klass,
VALUE buffer_type)
1941 for (
long i = 0; i <
RARRAY_LEN(buffer_type); i++) {
1952rb_io_buffer_get_value(
const void* base,
size_t size,
ID buffer_type,
size_t *offset)
1954#define IO_BUFFER_GET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) return io_buffer_read_##name(base, size, offset);
1955 IO_BUFFER_GET_VALUE(U8)
1956 IO_BUFFER_GET_VALUE(S8)
1958 IO_BUFFER_GET_VALUE(u16)
1959 IO_BUFFER_GET_VALUE(U16)
1960 IO_BUFFER_GET_VALUE(s16)
1961 IO_BUFFER_GET_VALUE(S16)
1963 IO_BUFFER_GET_VALUE(u32)
1964 IO_BUFFER_GET_VALUE(U32)
1965 IO_BUFFER_GET_VALUE(s32)
1966 IO_BUFFER_GET_VALUE(S32)
1968 IO_BUFFER_GET_VALUE(u64)
1969 IO_BUFFER_GET_VALUE(U64)
1970 IO_BUFFER_GET_VALUE(s64)
1971 IO_BUFFER_GET_VALUE(S64)
1973 IO_BUFFER_GET_VALUE(f32)
1974 IO_BUFFER_GET_VALUE(F32)
1975 IO_BUFFER_GET_VALUE(f64)
1976 IO_BUFFER_GET_VALUE(F64)
1977#undef IO_BUFFER_GET_VALUE
1979 rb_raise(rb_eArgError,
"Invalid type name!");
2021 size_t offset = io_buffer_extract_offset(_offset);
2023 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2025 return rb_io_buffer_get_value(base, size,
RB_SYM2ID(
type), &offset);
2041 size_t offset = io_buffer_extract_offset(_offset);
2045 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2048 rb_raise(rb_eArgError,
"Argument buffer_types should be an array!");
2053 for (
long i = 0; i <
RARRAY_LEN(buffer_types); i++) {
2054 VALUE type = rb_ary_entry(buffer_types, i);
2056 rb_ary_push(array, value);
2065io_buffer_extract_count(
VALUE argument)
2067 if (rb_int_negative_p(argument)) {
2068 rb_raise(rb_eArgError,
"Count can't be negative!");
2075io_buffer_extract_offset_count(
ID buffer_type,
size_t size,
int argc,
VALUE *argv,
size_t *offset,
size_t *count)
2078 *offset = io_buffer_extract_offset(argv[0]);
2085 *count = io_buffer_extract_count(argv[1]);
2088 if (*offset > size) {
2089 rb_raise(rb_eArgError,
"The given offset is bigger than the buffer size!");
2092 *count = (size - *offset) / io_buffer_buffer_type_size(buffer_type);
2113io_buffer_each(
int argc,
VALUE *argv,
VALUE self)
2120 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2127 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2130 size_t offset, count;
2131 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2133 for (
size_t i = 0; i < count; i++) {
2134 size_t current_offset = offset;
2135 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2153io_buffer_values(
int argc,
VALUE *argv,
VALUE self)
2158 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2165 buffer_type = RB_IO_BUFFER_DATA_TYPE_U8;
2168 size_t offset, count;
2169 io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count);
2171 VALUE array = rb_ary_new_capa(count);
2173 for (
size_t i = 0; i < count; i++) {
2174 VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset);
2175 rb_ary_push(array, value);
2197io_buffer_each_byte(
int argc,
VALUE *argv,
VALUE self)
2204 rb_io_buffer_get_bytes_for_reading(self, &base, &size);
2206 size_t offset, count;
2207 io_buffer_extract_offset_count(RB_IO_BUFFER_DATA_TYPE_U8, size, argc-1, argv+1, &offset, &count);
2209 for (
size_t i = 0; i < count; i++) {
2210 unsigned char *value = (
unsigned char *)base + i + offset;
2218rb_io_buffer_set_value(
const void* base,
size_t size,
ID buffer_type,
size_t *offset,
VALUE value)
2220#define IO_BUFFER_SET_VALUE(name) if (buffer_type == RB_IO_BUFFER_DATA_TYPE_##name) {io_buffer_write_##name(base, size, offset, value); return;}
2221 IO_BUFFER_SET_VALUE(U8);
2222 IO_BUFFER_SET_VALUE(S8);
2224 IO_BUFFER_SET_VALUE(u16);
2225 IO_BUFFER_SET_VALUE(U16);
2226 IO_BUFFER_SET_VALUE(s16);
2227 IO_BUFFER_SET_VALUE(S16);
2229 IO_BUFFER_SET_VALUE(u32);
2230 IO_BUFFER_SET_VALUE(U32);
2231 IO_BUFFER_SET_VALUE(s32);
2232 IO_BUFFER_SET_VALUE(S32);
2234 IO_BUFFER_SET_VALUE(u64);
2235 IO_BUFFER_SET_VALUE(U64);
2236 IO_BUFFER_SET_VALUE(s64);
2237 IO_BUFFER_SET_VALUE(S64);
2239 IO_BUFFER_SET_VALUE(f32);
2240 IO_BUFFER_SET_VALUE(F32);
2241 IO_BUFFER_SET_VALUE(f64);
2242 IO_BUFFER_SET_VALUE(F64);
2243#undef IO_BUFFER_SET_VALUE
2245 rb_raise(rb_eArgError,
"Invalid type name!");
2283 size_t offset = io_buffer_extract_offset(_offset);
2285 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2287 rb_io_buffer_set_value(base, size,
RB_SYM2ID(
type), &offset, value);
2310 rb_raise(rb_eArgError,
"Argument buffer_types should be an array!");
2314 rb_raise(rb_eArgError,
"Argument values should be an array!");
2318 rb_raise(rb_eArgError,
"Argument buffer_types and values should have the same length!");
2321 size_t offset = io_buffer_extract_offset(_offset);
2325 rb_io_buffer_get_bytes_for_writing(self, &base, &size);
2327 for (
long i = 0; i <
RARRAY_LEN(buffer_types); i++) {
2328 VALUE type = rb_ary_entry(buffer_types, i);
2329 VALUE value = rb_ary_entry(values, i);
2330 rb_io_buffer_set_value(base, size,
RB_SYM2ID(
type), &offset, value);
2336static size_t IO_BUFFER_BLOCKING_SIZE = 1024*1024;
2339 unsigned char * destination;
2340 const unsigned char * source;
2345io_buffer_memmove_blocking(
void *data)
2349 memmove(arguments->destination, arguments->source, arguments->length);
2355io_buffer_memmove_unblock(
void *data)
2361io_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)
2365 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2367 io_buffer_validate_range(buffer, offset, length);
2369 if (source_offset + length > source_size) {
2370 rb_raise(rb_eArgError,
"The computed source range exceeds the size of the source buffer!");
2374 .destination = (
unsigned char*)base+offset,
2375 .source = (
unsigned char*)source_base+source_offset,
2379 if (arguments.length >= IO_BUFFER_BLOCKING_SIZE) {
2381 }
else if (arguments.length != 0) {
2382 memmove(arguments.destination, arguments.source, arguments.length);
2388io_buffer_copy_from(
struct rb_io_buffer *buffer,
const void *source_base,
size_t source_size,
int argc,
VALUE *argv)
2392 size_t source_offset;
2396 offset = io_buffer_extract_offset(argv[0]);
2401 source_offset = io_buffer_extract_offset(argv[2]);
2403 if (source_offset > source_size) {
2404 rb_raise(rb_eArgError,
"The given source offset is bigger than the source itself!");
2412 if (argc >= 2 && !
RB_NIL_P(argv[1])) {
2413 length = io_buffer_extract_length(argv[1]);
2417 length = source_size - source_offset;
2420 io_buffer_memmove(buffer, offset, source_base, source_offset, source_size, length);
2443rb_io_buffer_initialize_copy(
VALUE self,
VALUE source)
2448 const void *source_base;
2451 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2453 io_buffer_initialize(self, buffer, NULL, source_size, io_flags_for_size(source_size),
Qnil);
2455 return io_buffer_copy_from(buffer, source_base, source_size, 0, NULL);
2527io_buffer_copy(
int argc,
VALUE *argv,
VALUE self)
2534 VALUE source = argv[0];
2535 const void *source_base;
2538 rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size);
2540 return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2558io_buffer_get_string(
int argc,
VALUE *argv,
VALUE self)
2562 size_t offset, length;
2563 struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
2567 io_buffer_get_bytes_for_reading(buffer, &base, &size);
2571 encoding = rb_find_encoding(argv[2]);
2574 encoding = rb_ascii8bit_encoding();
2577 io_buffer_validate_range(buffer, offset, length);
2579 return rb_enc_str_new((
const char*)base + offset, length, encoding);
2606io_buffer_set_string(
int argc,
VALUE *argv,
VALUE self)
2615 const void *source_base = RSTRING_PTR(
string);
2616 size_t source_size = RSTRING_LEN(
string);
2618 return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1);
2622rb_io_buffer_clear(
VALUE self, uint8_t value,
size_t offset,
size_t length)
2629 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2631 io_buffer_validate_range(buffer, offset, length);
2633 memset((
char*)base + offset, value, length);
2668io_buffer_clear(
int argc,
VALUE *argv,
VALUE self)
2677 size_t offset, length;
2678 io_buffer_extract_offset_length(self, argc-1, argv+1, &offset, &length);
2680 rb_io_buffer_clear(self, value, offset, length);
2686io_buffer_default_size(
size_t page_size)
2689 const size_t platform_agnostic_default_size = 64*1024;
2692 const char *default_size = getenv(
"RUBY_IO_BUFFER_DEFAULT_SIZE");
2695 int value = atoi(default_size);
2703 if (platform_agnostic_default_size < page_size) {
2707 return platform_agnostic_default_size;
2713 rb_blocking_function_t *function;
2718io_buffer_blocking_region_begin(
VALUE _argument)
2722 return rb_io_blocking_region(argument->io, argument->function, argument->data);
2726io_buffer_blocking_region_ensure(
VALUE _argument)
2730 io_buffer_unlock(argument->buffer);
2736io_buffer_blocking_region(
VALUE io,
struct rb_io_buffer *buffer, rb_blocking_function_t *function,
void *data)
2739 struct rb_io *ioptr;
2745 .function = function,
2750 if (buffer->flags & RB_IO_BUFFER_LOCKED) {
2751 return io_buffer_blocking_region_begin((
VALUE)&argument);
2755 io_buffer_lock(buffer);
2757 return rb_ensure(io_buffer_blocking_region_begin, (
VALUE)&argument, io_buffer_blocking_region_ensure, (
VALUE)&argument);
2773io_buffer_read_internal(
void *_argument)
2779 ssize_t result = read(argument->descriptor, argument->base, argument->size);
2784 else if (result == 0) {
2790 if (total >= argument->length) {
2794 argument->base = argument->base + result;
2795 argument->size = argument->size - result;
2801rb_io_buffer_read(
VALUE self,
VALUE io,
size_t length,
size_t offset)
2804 if (scheduler !=
Qnil) {
2807 if (!UNDEF_P(result)) {
2815 io_buffer_validate_range(buffer, offset, length);
2821 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2823 base = (
unsigned char*)base + offset;
2824 size = size - offset;
2827 .descriptor = descriptor,
2833 return io_buffer_blocking_region(io, buffer, io_buffer_read_internal, &argument);
2863io_buffer_read(
int argc,
VALUE *argv,
VALUE self)
2869 size_t length, offset;
2870 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
2872 return rb_io_buffer_read(self, io, length, offset);
2889io_buffer_pread_internal(
void *_argument)
2895 ssize_t result = pread(argument->descriptor, argument->base, argument->size, argument->offset);
2900 else if (result == 0) {
2906 if (total >= argument->length) {
2910 argument->base = argument->base + result;
2911 argument->size = argument->size - result;
2912 argument->offset = argument->offset + result;
2918rb_io_buffer_pread(
VALUE self,
VALUE io, rb_off_t from,
size_t length,
size_t offset)
2921 if (scheduler !=
Qnil) {
2924 if (!UNDEF_P(result)) {
2932 io_buffer_validate_range(buffer, offset, length);
2938 io_buffer_get_bytes_for_writing(buffer, &base, &size);
2940 base = (
unsigned char*)base + offset;
2941 size = size - offset;
2944 .descriptor = descriptor,
2951 return io_buffer_blocking_region(io, buffer, io_buffer_pread_internal, &argument);
2985io_buffer_pread(
int argc,
VALUE *argv,
VALUE self)
2992 size_t length, offset;
2993 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
2995 return rb_io_buffer_pread(self, io, from, length, offset);
3010io_buffer_write_internal(
void *_argument)
3016 ssize_t result = write(argument->descriptor, argument->base, argument->size);
3021 else if (result == 0) {
3027 if (total >= argument->length) {
3031 argument->base = argument->base + result;
3032 argument->size = argument->size - result;
3038rb_io_buffer_write(
VALUE self,
VALUE io,
size_t length,
size_t offset)
3041 if (scheduler !=
Qnil) {
3044 if (!UNDEF_P(result)) {
3052 io_buffer_validate_range(buffer, offset, length);
3058 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3060 base = (
unsigned char*)base + offset;
3061 size = size - offset;
3064 .descriptor = descriptor,
3070 return io_buffer_blocking_region(io, buffer, io_buffer_write_internal, &argument);
3093io_buffer_write(
int argc,
VALUE *argv,
VALUE self)
3099 size_t length, offset;
3100 io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
3102 return rb_io_buffer_write(self, io, length, offset);
3118io_buffer_pwrite_internal(
void *_argument)
3124 ssize_t result = pwrite(argument->descriptor, argument->base, argument->size, argument->offset);
3129 else if (result == 0) {
3135 if (total >= argument->length) {
3139 argument->base = argument->base + result;
3140 argument->size = argument->size - result;
3141 argument->offset = argument->offset + result;
3147rb_io_buffer_pwrite(
VALUE self,
VALUE io, rb_off_t from,
size_t length,
size_t offset)
3150 if (scheduler !=
Qnil) {
3153 if (!UNDEF_P(result)) {
3161 io_buffer_validate_range(buffer, offset, length);
3167 io_buffer_get_bytes_for_reading(buffer, &base, &size);
3169 base = (
unsigned char*)base + offset;
3170 size = size - offset;
3173 .descriptor = descriptor,
3188 return io_buffer_blocking_region(io, buffer, io_buffer_pwrite_internal, &argument);
3216io_buffer_pwrite(
int argc,
VALUE *argv,
VALUE self)
3223 size_t length, offset;
3224 io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset);
3226 return rb_io_buffer_pwrite(self, io, from, length, offset);
3232 if (buffer->size == 0)
3233 rb_raise(rb_eIOBufferMaskError,
"Zero-length mask given!");
3237memory_and(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3239 for (
size_t offset = 0; offset < size; offset += 1) {
3240 output[offset] = base[offset] & mask[offset % mask_size];
3265 io_buffer_check_mask(mask_buffer);
3267 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3271 memory_and(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3277memory_or(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3279 for (
size_t offset = 0; offset < size; offset += 1) {
3280 output[offset] = base[offset] | mask[offset % mask_size];
3305 io_buffer_check_mask(mask_buffer);
3307 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3311 memory_or(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3317memory_xor(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3319 for (
size_t offset = 0; offset < size; offset += 1) {
3320 output[offset] = base[offset] ^ mask[offset % mask_size];
3345 io_buffer_check_mask(mask_buffer);
3347 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3351 memory_xor(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size);
3357memory_not(
unsigned char * restrict output,
unsigned char * restrict base,
size_t size)
3359 for (
size_t offset = 0; offset < size; offset += 1) {
3360 output[offset] = ~base[offset];
3377io_buffer_not(
VALUE self)
3382 VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size));
3386 memory_not(output_buffer->base, buffer->base, buffer->size);
3394 if (a->base > b->base) {
3395 return io_buffer_overlaps(b, a);
3398 return (b->base >= a->base) && (b->base < (
void*)((
unsigned char *)a->base + a->size));
3404 if (io_buffer_overlaps(a, b))
3405 rb_raise(rb_eIOBufferMaskError,
"Mask overlaps source buffer!");
3409memory_and_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3411 for (
size_t offset = 0; offset < size; offset += 1) {
3412 base[offset] &= mask[offset % mask_size];
3442 io_buffer_check_mask(mask_buffer);
3443 io_buffer_check_overlaps(buffer, mask_buffer);
3447 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3449 memory_and_inplace(base, size, mask_buffer->base, mask_buffer->size);
3455memory_or_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3457 for (
size_t offset = 0; offset < size; offset += 1) {
3458 base[offset] |= mask[offset % mask_size];
3488 io_buffer_check_mask(mask_buffer);
3489 io_buffer_check_overlaps(buffer, mask_buffer);
3493 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3495 memory_or_inplace(base, size, mask_buffer->base, mask_buffer->size);
3501memory_xor_inplace(
unsigned char * restrict base,
size_t size,
unsigned char * restrict mask,
size_t mask_size)
3503 for (
size_t offset = 0; offset < size; offset += 1) {
3504 base[offset] ^= mask[offset % mask_size];
3534 io_buffer_check_mask(mask_buffer);
3535 io_buffer_check_overlaps(buffer, mask_buffer);
3539 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3541 memory_xor_inplace(base, size, mask_buffer->base, mask_buffer->size);
3547memory_not_inplace(
unsigned char * restrict base,
size_t size)
3549 for (
size_t offset = 0; offset < size; offset += 1) {
3550 base[offset] = ~base[offset];
3572io_buffer_not_inplace(
VALUE self)
3579 io_buffer_get_bytes_for_writing(buffer, &base, &size);
3581 memory_not_inplace(base, size);
3699 GetSystemInfo(&info);
3700 RUBY_IO_BUFFER_PAGE_SIZE = info.dwPageSize;
3702 RUBY_IO_BUFFER_PAGE_SIZE = sysconf(_SC_PAGESIZE);
3705 RUBY_IO_BUFFER_DEFAULT_SIZE = io_buffer_default_size(RUBY_IO_BUFFER_PAGE_SIZE);
3708 rb_define_const(rb_cIOBuffer,
"PAGE_SIZE",
SIZET2NUM(RUBY_IO_BUFFER_PAGE_SIZE));
3713 rb_define_const(rb_cIOBuffer,
"DEFAULT_SIZE",
SIZET2NUM(RUBY_IO_BUFFER_DEFAULT_SIZE));
3718 rb_define_method(rb_cIOBuffer,
"initialize_copy", rb_io_buffer_initialize_copy, 1);
3728 rb_define_const(rb_cIOBuffer,
"EXTERNAL",
RB_INT2NUM(RB_IO_BUFFER_EXTERNAL));
3731 rb_define_const(rb_cIOBuffer,
"INTERNAL",
RB_INT2NUM(RB_IO_BUFFER_INTERNAL));
3734 rb_define_const(rb_cIOBuffer,
"MAPPED",
RB_INT2NUM(RB_IO_BUFFER_MAPPED));
3737 rb_define_const(rb_cIOBuffer,
"SHARED",
RB_INT2NUM(RB_IO_BUFFER_SHARED));
3740 rb_define_const(rb_cIOBuffer,
"LOCKED",
RB_INT2NUM(RB_IO_BUFFER_LOCKED));
3743 rb_define_const(rb_cIOBuffer,
"PRIVATE",
RB_INT2NUM(RB_IO_BUFFER_PRIVATE));
3746 rb_define_const(rb_cIOBuffer,
"READONLY",
RB_INT2NUM(RB_IO_BUFFER_READONLY));
3749 rb_define_const(rb_cIOBuffer,
"LITTLE_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_LITTLE_ENDIAN));
3752 rb_define_const(rb_cIOBuffer,
"BIG_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_BIG_ENDIAN));
3755 rb_define_const(rb_cIOBuffer,
"HOST_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_HOST_ENDIAN));
3758 rb_define_const(rb_cIOBuffer,
"NETWORK_ENDIAN",
RB_INT2NUM(RB_IO_BUFFER_NETWORK_ENDIAN));
3784#define IO_BUFFER_DEFINE_DATA_TYPE(name) RB_IO_BUFFER_DATA_TYPE_##name = rb_intern_const(#name)
3785 IO_BUFFER_DEFINE_DATA_TYPE(U8);
3786 IO_BUFFER_DEFINE_DATA_TYPE(S8);
3788 IO_BUFFER_DEFINE_DATA_TYPE(u16);
3789 IO_BUFFER_DEFINE_DATA_TYPE(U16);
3790 IO_BUFFER_DEFINE_DATA_TYPE(s16);
3791 IO_BUFFER_DEFINE_DATA_TYPE(S16);
3793 IO_BUFFER_DEFINE_DATA_TYPE(u32);
3794 IO_BUFFER_DEFINE_DATA_TYPE(U32);
3795 IO_BUFFER_DEFINE_DATA_TYPE(s32);
3796 IO_BUFFER_DEFINE_DATA_TYPE(S32);
3798 IO_BUFFER_DEFINE_DATA_TYPE(u64);
3799 IO_BUFFER_DEFINE_DATA_TYPE(U64);
3800 IO_BUFFER_DEFINE_DATA_TYPE(s64);
3801 IO_BUFFER_DEFINE_DATA_TYPE(S64);
3803 IO_BUFFER_DEFINE_DATA_TYPE(f32);
3804 IO_BUFFER_DEFINE_DATA_TYPE(F32);
3805 IO_BUFFER_DEFINE_DATA_TYPE(f64);
3806 IO_BUFFER_DEFINE_DATA_TYPE(F64);
3807#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.