ed-precompiled_nio4r 2.7.4-x86_64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/libev/Changes +617 -0
- data/ext/libev/LICENSE +37 -0
- data/ext/libev/README +59 -0
- data/ext/libev/ev.c +5689 -0
- data/ext/libev/ev.h +859 -0
- data/ext/libev/ev_epoll.c +298 -0
- data/ext/libev/ev_iouring.c +694 -0
- data/ext/libev/ev_kqueue.c +224 -0
- data/ext/libev/ev_linuxaio.c +620 -0
- data/ext/libev/ev_poll.c +156 -0
- data/ext/libev/ev_port.c +192 -0
- data/ext/libev/ev_select.c +316 -0
- data/ext/libev/ev_vars.h +249 -0
- data/ext/libev/ev_win32.c +162 -0
- data/ext/libev/ev_wrap.h +272 -0
- data/ext/nio4r/.clang-format +16 -0
- data/ext/nio4r/bytebuffer.c +465 -0
- data/ext/nio4r/extconf.rb +53 -0
- data/ext/nio4r/libev.h +7 -0
- data/ext/nio4r/monitor.c +344 -0
- data/ext/nio4r/nio4r.h +48 -0
- data/ext/nio4r/nio4r_ext.c +24 -0
- data/ext/nio4r/org/nio4r/ByteBuffer.java +295 -0
- data/ext/nio4r/org/nio4r/Monitor.java +176 -0
- data/ext/nio4r/org/nio4r/Nio4r.java +104 -0
- data/ext/nio4r/org/nio4r/Selector.java +297 -0
- data/ext/nio4r/selector.c +606 -0
- data/lib/3.0/nio4r_ext.so +0 -0
- data/lib/3.1/nio4r_ext.so +0 -0
- data/lib/3.2/nio4r_ext.so +0 -0
- data/lib/3.3/nio4r_ext.so +0 -0
- data/lib/3.4/nio4r_ext.so +0 -0
- data/lib/nio/bytebuffer.rb +235 -0
- data/lib/nio/monitor.rb +124 -0
- data/lib/nio/selector.rb +188 -0
- data/lib/nio/version.rb +10 -0
- data/lib/nio.rb +66 -0
- data/lib/nio4r.rb +7 -0
- data/lib/nio4r_ext.so +0 -0
- data/license.md +80 -0
- data/readme.md +91 -0
- data/releases.md +343 -0
- metadata +139 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
#include "nio4r.h"
|
|
2
|
+
|
|
3
|
+
static VALUE mNIO = Qnil;
|
|
4
|
+
static VALUE cNIO_ByteBuffer = Qnil;
|
|
5
|
+
static VALUE cNIO_ByteBuffer_OverflowError = Qnil;
|
|
6
|
+
static VALUE cNIO_ByteBuffer_UnderflowError = Qnil;
|
|
7
|
+
static VALUE cNIO_ByteBuffer_MarkUnsetError = Qnil;
|
|
8
|
+
|
|
9
|
+
/* Allocator/deallocator */
|
|
10
|
+
static VALUE NIO_ByteBuffer_allocate(VALUE klass);
|
|
11
|
+
static void NIO_ByteBuffer_free(void *data);
|
|
12
|
+
static size_t NIO_ByteBuffer_memsize(const void *data);
|
|
13
|
+
|
|
14
|
+
/* Methods */
|
|
15
|
+
static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity);
|
|
16
|
+
static VALUE NIO_ByteBuffer_clear(VALUE self);
|
|
17
|
+
static VALUE NIO_ByteBuffer_get_position(VALUE self);
|
|
18
|
+
static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position);
|
|
19
|
+
static VALUE NIO_ByteBuffer_get_limit(VALUE self);
|
|
20
|
+
static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit);
|
|
21
|
+
static VALUE NIO_ByteBuffer_capacity(VALUE self);
|
|
22
|
+
static VALUE NIO_ByteBuffer_remaining(VALUE self);
|
|
23
|
+
static VALUE NIO_ByteBuffer_full(VALUE self);
|
|
24
|
+
static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self);
|
|
25
|
+
static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index);
|
|
26
|
+
static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string);
|
|
27
|
+
static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE file);
|
|
28
|
+
static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE file);
|
|
29
|
+
static VALUE NIO_ByteBuffer_flip(VALUE self);
|
|
30
|
+
static VALUE NIO_ByteBuffer_rewind(VALUE self);
|
|
31
|
+
static VALUE NIO_ByteBuffer_mark(VALUE self);
|
|
32
|
+
static VALUE NIO_ByteBuffer_reset(VALUE self);
|
|
33
|
+
static VALUE NIO_ByteBuffer_compact(VALUE self);
|
|
34
|
+
static VALUE NIO_ByteBuffer_each(VALUE self);
|
|
35
|
+
static VALUE NIO_ByteBuffer_inspect(VALUE self);
|
|
36
|
+
|
|
37
|
+
#define MARK_UNSET -1
|
|
38
|
+
|
|
39
|
+
/* Compatibility for Ruby <= 3.1 */
|
|
40
|
+
#ifndef HAVE_RB_IO_DESCRIPTOR
|
|
41
|
+
static int
|
|
42
|
+
io_descriptor_fallback(VALUE io)
|
|
43
|
+
{
|
|
44
|
+
rb_io_t *fptr;
|
|
45
|
+
GetOpenFile(io, fptr);
|
|
46
|
+
return fptr->fd;
|
|
47
|
+
}
|
|
48
|
+
#define rb_io_descriptor io_descriptor_fallback
|
|
49
|
+
#endif
|
|
50
|
+
|
|
51
|
+
static void
|
|
52
|
+
io_set_nonblock(VALUE io)
|
|
53
|
+
{
|
|
54
|
+
rb_io_t *fptr;
|
|
55
|
+
GetOpenFile(io, fptr);
|
|
56
|
+
rb_io_set_nonblock(fptr);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void Init_NIO_ByteBuffer()
|
|
60
|
+
{
|
|
61
|
+
mNIO = rb_define_module("NIO");
|
|
62
|
+
cNIO_ByteBuffer = rb_define_class_under(mNIO, "ByteBuffer", rb_cObject);
|
|
63
|
+
rb_define_alloc_func(cNIO_ByteBuffer, NIO_ByteBuffer_allocate);
|
|
64
|
+
|
|
65
|
+
cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
|
|
66
|
+
cNIO_ByteBuffer_UnderflowError = rb_define_class_under(cNIO_ByteBuffer, "UnderflowError", rb_eIOError);
|
|
67
|
+
cNIO_ByteBuffer_MarkUnsetError = rb_define_class_under(cNIO_ByteBuffer, "MarkUnsetError", rb_eIOError);
|
|
68
|
+
|
|
69
|
+
rb_include_module(cNIO_ByteBuffer, rb_mEnumerable);
|
|
70
|
+
|
|
71
|
+
rb_define_method(cNIO_ByteBuffer, "initialize", NIO_ByteBuffer_initialize, 1);
|
|
72
|
+
rb_define_method(cNIO_ByteBuffer, "clear", NIO_ByteBuffer_clear, 0);
|
|
73
|
+
rb_define_method(cNIO_ByteBuffer, "position", NIO_ByteBuffer_get_position, 0);
|
|
74
|
+
rb_define_method(cNIO_ByteBuffer, "position=", NIO_ByteBuffer_set_position, 1);
|
|
75
|
+
rb_define_method(cNIO_ByteBuffer, "limit", NIO_ByteBuffer_get_limit, 0);
|
|
76
|
+
rb_define_method(cNIO_ByteBuffer, "limit=", NIO_ByteBuffer_set_limit, 1);
|
|
77
|
+
rb_define_method(cNIO_ByteBuffer, "capacity", NIO_ByteBuffer_capacity, 0);
|
|
78
|
+
rb_define_method(cNIO_ByteBuffer, "size", NIO_ByteBuffer_capacity, 0);
|
|
79
|
+
rb_define_method(cNIO_ByteBuffer, "remaining", NIO_ByteBuffer_remaining, 0);
|
|
80
|
+
rb_define_method(cNIO_ByteBuffer, "full?", NIO_ByteBuffer_full, 0);
|
|
81
|
+
rb_define_method(cNIO_ByteBuffer, "get", NIO_ByteBuffer_get, -1);
|
|
82
|
+
rb_define_method(cNIO_ByteBuffer, "[]", NIO_ByteBuffer_fetch, 1);
|
|
83
|
+
rb_define_method(cNIO_ByteBuffer, "<<", NIO_ByteBuffer_put, 1);
|
|
84
|
+
rb_define_method(cNIO_ByteBuffer, "read_from", NIO_ByteBuffer_read_from, 1);
|
|
85
|
+
rb_define_method(cNIO_ByteBuffer, "write_to", NIO_ByteBuffer_write_to, 1);
|
|
86
|
+
rb_define_method(cNIO_ByteBuffer, "flip", NIO_ByteBuffer_flip, 0);
|
|
87
|
+
rb_define_method(cNIO_ByteBuffer, "rewind", NIO_ByteBuffer_rewind, 0);
|
|
88
|
+
rb_define_method(cNIO_ByteBuffer, "mark", NIO_ByteBuffer_mark, 0);
|
|
89
|
+
rb_define_method(cNIO_ByteBuffer, "reset", NIO_ByteBuffer_reset, 0);
|
|
90
|
+
rb_define_method(cNIO_ByteBuffer, "compact", NIO_ByteBuffer_compact, 0);
|
|
91
|
+
rb_define_method(cNIO_ByteBuffer, "each", NIO_ByteBuffer_each, 0);
|
|
92
|
+
rb_define_method(cNIO_ByteBuffer, "inspect", NIO_ByteBuffer_inspect, 0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static const rb_data_type_t NIO_ByteBuffer_type = {
|
|
96
|
+
"NIO::ByteBuffer",
|
|
97
|
+
{
|
|
98
|
+
NULL, // Nothing to mark
|
|
99
|
+
NIO_ByteBuffer_free,
|
|
100
|
+
NIO_ByteBuffer_memsize,
|
|
101
|
+
},
|
|
102
|
+
0,
|
|
103
|
+
0,
|
|
104
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
static VALUE NIO_ByteBuffer_allocate(VALUE klass)
|
|
108
|
+
{
|
|
109
|
+
struct NIO_ByteBuffer *bytebuffer = (struct NIO_ByteBuffer *)xmalloc(sizeof(struct NIO_ByteBuffer));
|
|
110
|
+
bytebuffer->buffer = NULL;
|
|
111
|
+
return TypedData_Wrap_Struct(klass, &NIO_ByteBuffer_type, bytebuffer);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static void NIO_ByteBuffer_free(void *data)
|
|
115
|
+
{
|
|
116
|
+
struct NIO_ByteBuffer *buffer = (struct NIO_ByteBuffer *)data;
|
|
117
|
+
if (buffer->buffer)
|
|
118
|
+
xfree(buffer->buffer);
|
|
119
|
+
xfree(buffer);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static size_t NIO_ByteBuffer_memsize(const void *data)
|
|
123
|
+
{
|
|
124
|
+
const struct NIO_ByteBuffer *buffer = (const struct NIO_ByteBuffer *)data;
|
|
125
|
+
size_t memsize = sizeof(struct NIO_ByteBuffer);
|
|
126
|
+
if (buffer->buffer)
|
|
127
|
+
memsize += buffer->capacity;
|
|
128
|
+
return memsize;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
|
|
132
|
+
{
|
|
133
|
+
struct NIO_ByteBuffer *buffer;
|
|
134
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
135
|
+
|
|
136
|
+
buffer->capacity = NUM2INT(capacity);
|
|
137
|
+
buffer->buffer = xmalloc(buffer->capacity);
|
|
138
|
+
|
|
139
|
+
NIO_ByteBuffer_clear(self);
|
|
140
|
+
|
|
141
|
+
return self;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static VALUE NIO_ByteBuffer_clear(VALUE self)
|
|
145
|
+
{
|
|
146
|
+
struct NIO_ByteBuffer *buffer;
|
|
147
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
148
|
+
|
|
149
|
+
memset(buffer->buffer, 0, buffer->capacity);
|
|
150
|
+
|
|
151
|
+
buffer->position = 0;
|
|
152
|
+
buffer->limit = buffer->capacity;
|
|
153
|
+
buffer->mark = MARK_UNSET;
|
|
154
|
+
|
|
155
|
+
return self;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static VALUE NIO_ByteBuffer_get_position(VALUE self)
|
|
159
|
+
{
|
|
160
|
+
struct NIO_ByteBuffer *buffer;
|
|
161
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
162
|
+
|
|
163
|
+
return INT2NUM(buffer->position);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
|
|
167
|
+
{
|
|
168
|
+
int pos;
|
|
169
|
+
struct NIO_ByteBuffer *buffer;
|
|
170
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
171
|
+
|
|
172
|
+
pos = NUM2INT(new_position);
|
|
173
|
+
|
|
174
|
+
if (pos < 0) {
|
|
175
|
+
rb_raise(rb_eArgError, "negative position given");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (pos > buffer->limit) {
|
|
179
|
+
rb_raise(rb_eArgError, "specified position exceeds limit");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
buffer->position = pos;
|
|
183
|
+
|
|
184
|
+
if (buffer->mark > buffer->position) {
|
|
185
|
+
buffer->mark = MARK_UNSET;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return new_position;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static VALUE NIO_ByteBuffer_get_limit(VALUE self)
|
|
192
|
+
{
|
|
193
|
+
struct NIO_ByteBuffer *buffer;
|
|
194
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
195
|
+
|
|
196
|
+
return INT2NUM(buffer->limit);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
|
|
200
|
+
{
|
|
201
|
+
int lim;
|
|
202
|
+
struct NIO_ByteBuffer *buffer;
|
|
203
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
204
|
+
|
|
205
|
+
lim = NUM2INT(new_limit);
|
|
206
|
+
|
|
207
|
+
if (lim < 0) {
|
|
208
|
+
rb_raise(rb_eArgError, "negative limit given");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (lim > buffer->capacity) {
|
|
212
|
+
rb_raise(rb_eArgError, "specified limit exceeds capacity");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
buffer->limit = lim;
|
|
216
|
+
|
|
217
|
+
if (buffer->position > lim) {
|
|
218
|
+
buffer->position = lim;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (buffer->mark > lim) {
|
|
222
|
+
buffer->mark = MARK_UNSET;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return new_limit;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static VALUE NIO_ByteBuffer_capacity(VALUE self)
|
|
229
|
+
{
|
|
230
|
+
struct NIO_ByteBuffer *buffer;
|
|
231
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
232
|
+
|
|
233
|
+
return INT2NUM(buffer->capacity);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
static VALUE NIO_ByteBuffer_remaining(VALUE self)
|
|
237
|
+
{
|
|
238
|
+
struct NIO_ByteBuffer *buffer;
|
|
239
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
240
|
+
|
|
241
|
+
return INT2NUM(buffer->limit - buffer->position);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
static VALUE NIO_ByteBuffer_full(VALUE self)
|
|
245
|
+
{
|
|
246
|
+
struct NIO_ByteBuffer *buffer;
|
|
247
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
248
|
+
|
|
249
|
+
return buffer->position == buffer->limit ? Qtrue : Qfalse;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self)
|
|
253
|
+
{
|
|
254
|
+
int len;
|
|
255
|
+
VALUE length, result;
|
|
256
|
+
struct NIO_ByteBuffer *buffer;
|
|
257
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
258
|
+
|
|
259
|
+
rb_scan_args(argc, argv, "01", &length);
|
|
260
|
+
|
|
261
|
+
if (length == Qnil) {
|
|
262
|
+
len = buffer->limit - buffer->position;
|
|
263
|
+
} else {
|
|
264
|
+
len = NUM2INT(length);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (len < 0) {
|
|
268
|
+
rb_raise(rb_eArgError, "negative length given");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (len > buffer->limit - buffer->position) {
|
|
272
|
+
rb_raise(cNIO_ByteBuffer_UnderflowError, "not enough data in buffer");
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
result = rb_str_new(buffer->buffer + buffer->position, len);
|
|
276
|
+
buffer->position += len;
|
|
277
|
+
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index)
|
|
282
|
+
{
|
|
283
|
+
int i;
|
|
284
|
+
struct NIO_ByteBuffer *buffer;
|
|
285
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
286
|
+
|
|
287
|
+
i = NUM2INT(index);
|
|
288
|
+
|
|
289
|
+
if (i < 0) {
|
|
290
|
+
rb_raise(rb_eArgError, "negative index given");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (i >= buffer->limit) {
|
|
294
|
+
rb_raise(rb_eArgError, "specified index exceeds limit");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return INT2NUM(buffer->buffer[i]);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
|
|
301
|
+
{
|
|
302
|
+
long length;
|
|
303
|
+
struct NIO_ByteBuffer *buffer;
|
|
304
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
305
|
+
|
|
306
|
+
StringValue(string);
|
|
307
|
+
length = RSTRING_LEN(string);
|
|
308
|
+
|
|
309
|
+
if (length > buffer->limit - buffer->position) {
|
|
310
|
+
rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
memcpy(buffer->buffer + buffer->position, StringValuePtr(string), length);
|
|
314
|
+
buffer->position += length;
|
|
315
|
+
|
|
316
|
+
return self;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
|
|
320
|
+
{
|
|
321
|
+
struct NIO_ByteBuffer *buffer;
|
|
322
|
+
ssize_t nbytes, bytes_read;
|
|
323
|
+
|
|
324
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
325
|
+
|
|
326
|
+
io = rb_convert_type(io, T_FILE, "IO", "to_io");
|
|
327
|
+
io_set_nonblock(io);
|
|
328
|
+
|
|
329
|
+
nbytes = buffer->limit - buffer->position;
|
|
330
|
+
if (nbytes == 0) {
|
|
331
|
+
rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
bytes_read = read(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
|
|
335
|
+
|
|
336
|
+
if (bytes_read < 0) {
|
|
337
|
+
if (errno == EAGAIN) {
|
|
338
|
+
return INT2NUM(0);
|
|
339
|
+
} else {
|
|
340
|
+
rb_sys_fail("write");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
buffer->position += bytes_read;
|
|
345
|
+
|
|
346
|
+
return SIZET2NUM(bytes_read);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
|
|
350
|
+
{
|
|
351
|
+
struct NIO_ByteBuffer *buffer;
|
|
352
|
+
ssize_t nbytes, bytes_written;
|
|
353
|
+
|
|
354
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
355
|
+
io = rb_convert_type(io, T_FILE, "IO", "to_io");
|
|
356
|
+
io_set_nonblock(io);
|
|
357
|
+
|
|
358
|
+
nbytes = buffer->limit - buffer->position;
|
|
359
|
+
if (nbytes == 0) {
|
|
360
|
+
rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer");
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
bytes_written = write(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
|
|
364
|
+
|
|
365
|
+
if (bytes_written < 0) {
|
|
366
|
+
if (errno == EAGAIN) {
|
|
367
|
+
return INT2NUM(0);
|
|
368
|
+
} else {
|
|
369
|
+
rb_sys_fail("write");
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
buffer->position += bytes_written;
|
|
374
|
+
|
|
375
|
+
return SIZET2NUM(bytes_written);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
static VALUE NIO_ByteBuffer_flip(VALUE self)
|
|
379
|
+
{
|
|
380
|
+
struct NIO_ByteBuffer *buffer;
|
|
381
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
382
|
+
|
|
383
|
+
buffer->limit = buffer->position;
|
|
384
|
+
buffer->position = 0;
|
|
385
|
+
buffer->mark = MARK_UNSET;
|
|
386
|
+
|
|
387
|
+
return self;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
static VALUE NIO_ByteBuffer_rewind(VALUE self)
|
|
391
|
+
{
|
|
392
|
+
struct NIO_ByteBuffer *buffer;
|
|
393
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
394
|
+
|
|
395
|
+
buffer->position = 0;
|
|
396
|
+
buffer->mark = MARK_UNSET;
|
|
397
|
+
|
|
398
|
+
return self;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
static VALUE NIO_ByteBuffer_mark(VALUE self)
|
|
402
|
+
{
|
|
403
|
+
struct NIO_ByteBuffer *buffer;
|
|
404
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
405
|
+
|
|
406
|
+
buffer->mark = buffer->position;
|
|
407
|
+
return self;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
static VALUE NIO_ByteBuffer_reset(VALUE self)
|
|
411
|
+
{
|
|
412
|
+
struct NIO_ByteBuffer *buffer;
|
|
413
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
414
|
+
|
|
415
|
+
if (buffer->mark < 0) {
|
|
416
|
+
rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set");
|
|
417
|
+
} else {
|
|
418
|
+
buffer->position = buffer->mark;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return self;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
static VALUE NIO_ByteBuffer_compact(VALUE self)
|
|
425
|
+
{
|
|
426
|
+
struct NIO_ByteBuffer *buffer;
|
|
427
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
428
|
+
|
|
429
|
+
memmove(buffer->buffer, buffer->buffer + buffer->position, buffer->limit - buffer->position);
|
|
430
|
+
buffer->position = buffer->limit - buffer->position;
|
|
431
|
+
buffer->limit = buffer->capacity;
|
|
432
|
+
|
|
433
|
+
return self;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static VALUE NIO_ByteBuffer_each(VALUE self)
|
|
437
|
+
{
|
|
438
|
+
int i;
|
|
439
|
+
struct NIO_ByteBuffer *buffer;
|
|
440
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
441
|
+
|
|
442
|
+
if (rb_block_given_p()) {
|
|
443
|
+
for (i = 0; i < buffer->limit; i++) {
|
|
444
|
+
rb_yield(INT2NUM(buffer->buffer[i]));
|
|
445
|
+
}
|
|
446
|
+
} else {
|
|
447
|
+
rb_raise(rb_eArgError, "no block given");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return self;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
static VALUE NIO_ByteBuffer_inspect(VALUE self)
|
|
454
|
+
{
|
|
455
|
+
struct NIO_ByteBuffer *buffer;
|
|
456
|
+
TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
|
|
457
|
+
|
|
458
|
+
return rb_sprintf(
|
|
459
|
+
"#<%s:%p @position=%d @limit=%d @capacity=%d>",
|
|
460
|
+
rb_class2name(CLASS_OF(self)),
|
|
461
|
+
(void *)self,
|
|
462
|
+
buffer->position,
|
|
463
|
+
buffer->limit,
|
|
464
|
+
buffer->capacity);
|
|
465
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2011-2020, by Tony Arcieri.
|
|
5
|
+
# Copyright, 2014, by Hiroshi Shibata.
|
|
6
|
+
# Copyright, 2014, by Sergey Avseyev.
|
|
7
|
+
# Copyright, 2015, by Daniel Berger.
|
|
8
|
+
# Copyright, 2017, by Jun Aruga.
|
|
9
|
+
# Copyright, 2017, by Usaku Nakamura.
|
|
10
|
+
# Copyright, 2017, by Lars Kanis.
|
|
11
|
+
# Copyright, 2019-2023, by Samuel Williams.
|
|
12
|
+
# Copyright, 2020, by Gregory Longtin.
|
|
13
|
+
# Copyright, 2020, by Boaz Segev.
|
|
14
|
+
# Copyright, 2020, by Joao Fernandes.
|
|
15
|
+
# Copyright, 2021, by Jeffrey Martin.
|
|
16
|
+
|
|
17
|
+
require "rubygems"
|
|
18
|
+
|
|
19
|
+
# Write a dummy Makefile on Windows because we use the pure Ruby implementation there
|
|
20
|
+
if Gem.win_platform?
|
|
21
|
+
begin
|
|
22
|
+
require "devkit" if RUBY_PLATFORM.include?("mingw")
|
|
23
|
+
rescue LoadError => e
|
|
24
|
+
end
|
|
25
|
+
File.write("Makefile", "all install::\n")
|
|
26
|
+
File.write("nio4r_ext.so", "")
|
|
27
|
+
exit
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
require "mkmf"
|
|
31
|
+
|
|
32
|
+
have_header("unistd.h")
|
|
33
|
+
have_func("rb_io_descriptor")
|
|
34
|
+
|
|
35
|
+
$defs << "-DEV_USE_LINUXAIO" if have_header("linux/aio_abi.h")
|
|
36
|
+
$defs << "-DEV_USE_IOURING" if have_header("linux/io_uring.h")
|
|
37
|
+
$defs << "-DEV_USE_SELECT" if have_header("sys/select.h")
|
|
38
|
+
$defs << "-DEV_USE_POLL" if have_type("port_event_t", "poll.h")
|
|
39
|
+
$defs << "-DEV_USE_EPOLL" if have_header("sys/epoll.h")
|
|
40
|
+
$defs << "-DEV_USE_KQUEUE" if have_header("sys/event.h") && have_header("sys/queue.h")
|
|
41
|
+
$defs << "-DEV_USE_PORT" if have_type("port_event_t", "port.h")
|
|
42
|
+
$defs << "-DHAVE_SYS_RESOURCE_H" if have_header("sys/resource.h")
|
|
43
|
+
|
|
44
|
+
$defs << "-DEV_STANDALONE" # prevent libev from assuming "config.h" exists
|
|
45
|
+
|
|
46
|
+
CONFIG["optflags"] << " -fno-strict-aliasing" unless RUBY_PLATFORM =~ /mswin/
|
|
47
|
+
|
|
48
|
+
if RUBY_PLATFORM =~ /darwin/
|
|
49
|
+
$DLDFLAGS.gsub!(/\-arch\s+[^\s]+/, "")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
dir_config "nio4r_ext"
|
|
53
|
+
create_makefile "nio4r_ext"
|