nio4r 2.5.9 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1572a271d329a29e7168c04b8f3776a41f23f495227a0a41ab9f1cd80233f1d6
4
- data.tar.gz: 6c7e944abd93038272299db034c3c67829d093e2ce4a75292eba4c654cd2ae0f
3
+ metadata.gz: ae8b5e59a799c0161b454b1e4dcd97becc5bdb0b835ec6becf2a3a4488b648b5
4
+ data.tar.gz: e5b186044e74c4ad7c3b9846d6244dd21938b831eb415eccb63a781a4ee5dc9e
5
5
  SHA512:
6
- metadata.gz: 78e502f54f1eedeb948c412e9ecc5fa241c082b3ed292d9a50d1acbff76ee499904d70367eccb52db5c513f6bd91ac647846fa6cce6632ca9ba22c5d28a46682
7
- data.tar.gz: 992198b186b8186e37bc84da3d89b1ddbae76ae9e8aaa45973111a8a9cd94e80c7b23ed9558449c409079b2b9c6cb1ca9edefa78ef5f8010c43a7c34ee000fc4
6
+ metadata.gz: c22f5d7466ede134956ab01ea87c4554ce38a950019519f0a7c4a9f16cbe81c5a338de488d885761bd49393cfd72bf20a857204db13b051d6f59f8e5bc52bc1d
7
+ data.tar.gz: 27152f810d81a2f94b7428a3662e991441adb4756e24cb09753addd8d00b1a3a6ea80a2e89580eac76ab5d3328fa368cb43147ccaa9432d70e74a771ce22c863
data/.mailmap ADDED
@@ -0,0 +1,16 @@
1
+ Sadayuki Furuhashi <frsyuki@users.sourceforge.jp>
2
+ Shannon Skipper <shannonskipper@gmail.com>
3
+ Anatol Pomozov <anatol.pomozov@gmail.com>
4
+ Hiroshi Shibata <shibata.hiroshi@gmail.com>
5
+ John Thornton <ubergeek3141@gmail.com>
6
+ Upekshe Jayasekera <usmj000@gmail.com>
7
+ Upekshe Jayasekera <upekshej.11@cse.mrt.ac.lk>
8
+ Usaku Nakamura <usa@garbagecollect.jp>
9
+ Tomoya Ishida <tomoyapenguin@gmail.com>
10
+ Tiago Cardoso <cardoso_tiago@hotmail.com>
11
+ Ravil Bayramgalin <brainopia@evilmartians.com>
12
+ Gregory Longtin <Greg.mpls@gmail.com>
13
+ Gregory Longtin <Greg.mpls@gmail.com> <MSP-Greg@users.noreply.github.com>
14
+ Elad Eyal <elad.eyal@intel.com>
15
+ Boaz Segev <bo@bowild.com>
16
+ Tao Luo <luotao.ruby@gmail.com>
data/Gemfile CHANGED
@@ -6,8 +6,10 @@ gemspec
6
6
 
7
7
  gem "jruby-openssl" if defined? JRUBY_VERSION
8
8
 
9
- group :development do
10
- gem "pry", require: false
9
+ group :maintenance, optional: true do
10
+ gem "bake"
11
+ gem "bake-gem"
12
+ # gem "bake-modernize"
11
13
  end
12
14
 
13
15
  group :development, :test do
@@ -1,3 +1,21 @@
1
+ ## 2.6.2
2
+
3
+ * Convert NIO objects to TypedData API.
4
+
5
+ ## 2.6.1
6
+
7
+ * Don't update `io` which is subsequently stored. Retain the original.
8
+
9
+ ## 2.6.0
10
+
11
+ * Fix conversion loses int precision.
12
+ * Avoid direct access to IO internals.
13
+ * Resolve issue loading both nio and nio4r gems.
14
+
15
+ ## 2.5.9 (2023-04-02)
16
+
17
+ https://github.com/socketry/nio4r/compare/v2.5.8..v2.5.9
18
+
1
19
  ## 2.5.8 (2021-08-03)
2
20
 
3
21
  * [#276](https://github.com/socketry/nio4r/pull/276)
@@ -1,6 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ # Released under the MIT License.
5
+ # Copyright, 2012-2016, by Tony Arcieri.
6
+ # Copyright, 2016, by Jun Aruga.
7
+ # Copyright, 2019, by Zhang Kang.
8
+ # Copyright, 2020, by Thomas Dziedzic.
9
+ # Copyright, 2023, by Samuel Williams.
10
+
4
11
  $LOAD_PATH.push File.expand_path("../lib", __dir__)
5
12
  require "nio"
6
13
  require "socket"
@@ -8,8 +8,8 @@ static VALUE cNIO_ByteBuffer_MarkUnsetError = Qnil;
8
8
 
9
9
  /* Allocator/deallocator */
10
10
  static VALUE NIO_ByteBuffer_allocate(VALUE klass);
11
- static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *byteBuffer);
12
- static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *byteBuffer);
11
+ static void NIO_ByteBuffer_free(void *data);
12
+ static size_t NIO_ByteBuffer_memsize(const void *data);
13
13
 
14
14
  /* Methods */
15
15
  static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity);
@@ -36,6 +36,26 @@ static VALUE NIO_ByteBuffer_inspect(VALUE self);
36
36
 
37
37
  #define MARK_UNSET -1
38
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
+
39
59
  void Init_NIO_ByteBuffer()
40
60
  {
41
61
  mNIO = rb_define_module("NIO");
@@ -72,28 +92,46 @@ void Init_NIO_ByteBuffer()
72
92
  rb_define_method(cNIO_ByteBuffer, "inspect", NIO_ByteBuffer_inspect, 0);
73
93
  }
74
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
+
75
107
  static VALUE NIO_ByteBuffer_allocate(VALUE klass)
76
108
  {
77
109
  struct NIO_ByteBuffer *bytebuffer = (struct NIO_ByteBuffer *)xmalloc(sizeof(struct NIO_ByteBuffer));
78
110
  bytebuffer->buffer = NULL;
79
- return Data_Wrap_Struct(klass, NIO_ByteBuffer_gc_mark, NIO_ByteBuffer_free, bytebuffer);
111
+ return TypedData_Wrap_Struct(klass, &NIO_ByteBuffer_type, bytebuffer);
80
112
  }
81
113
 
82
- static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)
114
+ static void NIO_ByteBuffer_free(void *data)
83
115
  {
116
+ struct NIO_ByteBuffer *buffer = (struct NIO_ByteBuffer *)data;
117
+ if (buffer->buffer)
118
+ xfree(buffer->buffer);
119
+ xfree(buffer);
84
120
  }
85
121
 
86
- static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
122
+ static size_t NIO_ByteBuffer_memsize(const void *data)
87
123
  {
124
+ const struct NIO_ByteBuffer *buffer = (const struct NIO_ByteBuffer *)data;
125
+ size_t memsize = sizeof(struct NIO_ByteBuffer);
88
126
  if (buffer->buffer)
89
- xfree(buffer->buffer);
90
- xfree(buffer);
127
+ memsize += buffer->capacity;
128
+ return memsize;
91
129
  }
92
130
 
93
131
  static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
94
132
  {
95
133
  struct NIO_ByteBuffer *buffer;
96
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
134
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
97
135
 
98
136
  buffer->capacity = NUM2INT(capacity);
99
137
  buffer->buffer = xmalloc(buffer->capacity);
@@ -106,7 +144,7 @@ static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
106
144
  static VALUE NIO_ByteBuffer_clear(VALUE self)
107
145
  {
108
146
  struct NIO_ByteBuffer *buffer;
109
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
147
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
110
148
 
111
149
  memset(buffer->buffer, 0, buffer->capacity);
112
150
 
@@ -120,7 +158,7 @@ static VALUE NIO_ByteBuffer_clear(VALUE self)
120
158
  static VALUE NIO_ByteBuffer_get_position(VALUE self)
121
159
  {
122
160
  struct NIO_ByteBuffer *buffer;
123
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
161
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
124
162
 
125
163
  return INT2NUM(buffer->position);
126
164
  }
@@ -129,7 +167,7 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
129
167
  {
130
168
  int pos;
131
169
  struct NIO_ByteBuffer *buffer;
132
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
170
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
133
171
 
134
172
  pos = NUM2INT(new_position);
135
173
 
@@ -153,7 +191,7 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
153
191
  static VALUE NIO_ByteBuffer_get_limit(VALUE self)
154
192
  {
155
193
  struct NIO_ByteBuffer *buffer;
156
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
194
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
157
195
 
158
196
  return INT2NUM(buffer->limit);
159
197
  }
@@ -162,7 +200,7 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
162
200
  {
163
201
  int lim;
164
202
  struct NIO_ByteBuffer *buffer;
165
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
203
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
166
204
 
167
205
  lim = NUM2INT(new_limit);
168
206
 
@@ -190,7 +228,7 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
190
228
  static VALUE NIO_ByteBuffer_capacity(VALUE self)
191
229
  {
192
230
  struct NIO_ByteBuffer *buffer;
193
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
231
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
194
232
 
195
233
  return INT2NUM(buffer->capacity);
196
234
  }
@@ -198,7 +236,7 @@ static VALUE NIO_ByteBuffer_capacity(VALUE self)
198
236
  static VALUE NIO_ByteBuffer_remaining(VALUE self)
199
237
  {
200
238
  struct NIO_ByteBuffer *buffer;
201
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
239
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
202
240
 
203
241
  return INT2NUM(buffer->limit - buffer->position);
204
242
  }
@@ -206,7 +244,7 @@ static VALUE NIO_ByteBuffer_remaining(VALUE self)
206
244
  static VALUE NIO_ByteBuffer_full(VALUE self)
207
245
  {
208
246
  struct NIO_ByteBuffer *buffer;
209
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
247
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
210
248
 
211
249
  return buffer->position == buffer->limit ? Qtrue : Qfalse;
212
250
  }
@@ -216,7 +254,7 @@ static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self)
216
254
  int len;
217
255
  VALUE length, result;
218
256
  struct NIO_ByteBuffer *buffer;
219
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
257
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
220
258
 
221
259
  rb_scan_args(argc, argv, "01", &length);
222
260
 
@@ -244,7 +282,7 @@ static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index)
244
282
  {
245
283
  int i;
246
284
  struct NIO_ByteBuffer *buffer;
247
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
285
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
248
286
 
249
287
  i = NUM2INT(index);
250
288
 
@@ -263,7 +301,7 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
263
301
  {
264
302
  long length;
265
303
  struct NIO_ByteBuffer *buffer;
266
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
304
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
267
305
 
268
306
  StringValue(string);
269
307
  length = RSTRING_LEN(string);
@@ -281,19 +319,19 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
281
319
  static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
282
320
  {
283
321
  struct NIO_ByteBuffer *buffer;
284
- rb_io_t *fptr;
285
322
  ssize_t nbytes, bytes_read;
286
323
 
287
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
288
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
289
- rb_io_set_nonblock(fptr);
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);
290
328
 
291
329
  nbytes = buffer->limit - buffer->position;
292
330
  if (nbytes == 0) {
293
331
  rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
294
332
  }
295
333
 
296
- bytes_read = read(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
334
+ bytes_read = read(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
297
335
 
298
336
  if (bytes_read < 0) {
299
337
  if (errno == EAGAIN) {
@@ -305,25 +343,24 @@ static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
305
343
 
306
344
  buffer->position += bytes_read;
307
345
 
308
- return INT2NUM(bytes_read);
346
+ return SIZET2NUM(bytes_read);
309
347
  }
310
348
 
311
349
  static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
312
350
  {
313
351
  struct NIO_ByteBuffer *buffer;
314
- rb_io_t *fptr;
315
352
  ssize_t nbytes, bytes_written;
316
353
 
317
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
318
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
319
- rb_io_set_nonblock(fptr);
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);
320
357
 
321
358
  nbytes = buffer->limit - buffer->position;
322
359
  if (nbytes == 0) {
323
360
  rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer");
324
361
  }
325
362
 
326
- bytes_written = write(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
363
+ bytes_written = write(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
327
364
 
328
365
  if (bytes_written < 0) {
329
366
  if (errno == EAGAIN) {
@@ -335,13 +372,13 @@ static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
335
372
 
336
373
  buffer->position += bytes_written;
337
374
 
338
- return INT2NUM(bytes_written);
375
+ return SIZET2NUM(bytes_written);
339
376
  }
340
377
 
341
378
  static VALUE NIO_ByteBuffer_flip(VALUE self)
342
379
  {
343
380
  struct NIO_ByteBuffer *buffer;
344
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
381
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
345
382
 
346
383
  buffer->limit = buffer->position;
347
384
  buffer->position = 0;
@@ -353,7 +390,7 @@ static VALUE NIO_ByteBuffer_flip(VALUE self)
353
390
  static VALUE NIO_ByteBuffer_rewind(VALUE self)
354
391
  {
355
392
  struct NIO_ByteBuffer *buffer;
356
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
393
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
357
394
 
358
395
  buffer->position = 0;
359
396
  buffer->mark = MARK_UNSET;
@@ -364,7 +401,7 @@ static VALUE NIO_ByteBuffer_rewind(VALUE self)
364
401
  static VALUE NIO_ByteBuffer_mark(VALUE self)
365
402
  {
366
403
  struct NIO_ByteBuffer *buffer;
367
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
404
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
368
405
 
369
406
  buffer->mark = buffer->position;
370
407
  return self;
@@ -373,7 +410,7 @@ static VALUE NIO_ByteBuffer_mark(VALUE self)
373
410
  static VALUE NIO_ByteBuffer_reset(VALUE self)
374
411
  {
375
412
  struct NIO_ByteBuffer *buffer;
376
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
413
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
377
414
 
378
415
  if (buffer->mark < 0) {
379
416
  rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set");
@@ -387,7 +424,7 @@ static VALUE NIO_ByteBuffer_reset(VALUE self)
387
424
  static VALUE NIO_ByteBuffer_compact(VALUE self)
388
425
  {
389
426
  struct NIO_ByteBuffer *buffer;
390
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
427
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
391
428
 
392
429
  memmove(buffer->buffer, buffer->buffer + buffer->position, buffer->limit - buffer->position);
393
430
  buffer->position = buffer->limit - buffer->position;
@@ -400,7 +437,7 @@ static VALUE NIO_ByteBuffer_each(VALUE self)
400
437
  {
401
438
  int i;
402
439
  struct NIO_ByteBuffer *buffer;
403
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
440
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
404
441
 
405
442
  if (rb_block_given_p()) {
406
443
  for (i = 0; i < buffer->limit; i++) {
@@ -416,7 +453,7 @@ static VALUE NIO_ByteBuffer_each(VALUE self)
416
453
  static VALUE NIO_ByteBuffer_inspect(VALUE self)
417
454
  {
418
455
  struct NIO_ByteBuffer *buffer;
419
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
456
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
420
457
 
421
458
  return rb_sprintf(
422
459
  "#<%s:%p @position=%d @limit=%d @capacity=%d>",
data/ext/nio4r/extconf.rb CHANGED
@@ -1,5 +1,19 @@
1
1
  # frozen_string_literal: true
2
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
+
3
17
  require "rubygems"
4
18
 
5
19
  # Write a dummy Makefile on Windows because we use the pure Ruby implementation there
@@ -16,6 +30,7 @@ end
16
30
  require "mkmf"
17
31
 
18
32
  have_header("unistd.h")
33
+ have_func("rb_io_descriptor")
19
34
 
20
35
  $defs << "-DEV_USE_LINUXAIO" if have_header("linux/aio_abi.h")
21
36
  $defs << "-DEV_USE_IOURING" if have_header("linux/io_uring.h")
data/ext/nio4r/monitor.c CHANGED
@@ -11,8 +11,8 @@ static VALUE cNIO_Monitor = Qnil;
11
11
 
12
12
  /* Allocator/deallocator */
13
13
  static VALUE NIO_Monitor_allocate(VALUE klass);
14
- static void NIO_Monitor_mark(struct NIO_Monitor *monitor);
15
- static void NIO_Monitor_free(struct NIO_Monitor *monitor);
14
+ static void NIO_Monitor_mark(void *data);
15
+ static size_t NIO_Monitor_memsize(const void *data);
16
16
 
17
17
  /* Methods */
18
18
  static VALUE NIO_Monitor_initialize(VALUE self, VALUE selector, VALUE io, VALUE interests);
@@ -34,6 +34,18 @@ static VALUE NIO_Monitor_readiness(VALUE self);
34
34
  static int NIO_Monitor_symbol2interest(VALUE interests);
35
35
  static void NIO_Monitor_update_interests(VALUE self, int interests);
36
36
 
37
+ /* Compatibility for Ruby <= 3.1 */
38
+ #ifndef HAVE_RB_IO_DESCRIPTOR
39
+ static int
40
+ io_descriptor_fallback(VALUE io)
41
+ {
42
+ rb_io_t *fptr;
43
+ GetOpenFile(io, fptr);
44
+ return fptr->fd;
45
+ }
46
+ #define rb_io_descriptor io_descriptor_fallback
47
+ #endif
48
+
37
49
  /* Monitor control how a channel is being waited for by a monitor */
38
50
  void Init_NIO_Monitor()
39
51
  {
@@ -58,22 +70,36 @@ void Init_NIO_Monitor()
58
70
  rb_define_method(cNIO_Monitor, "writeable?", NIO_Monitor_is_writable, 0);
59
71
  }
60
72
 
73
+ static const rb_data_type_t NIO_Monitor_type = {
74
+ "NIO::Monitor",
75
+ {
76
+ NIO_Monitor_mark,
77
+ RUBY_TYPED_DEFAULT_FREE,
78
+ NIO_Monitor_memsize,
79
+ },
80
+ 0,
81
+ 0,
82
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
83
+ };
84
+
61
85
  static VALUE NIO_Monitor_allocate(VALUE klass)
62
86
  {
63
87
  struct NIO_Monitor *monitor = (struct NIO_Monitor *)xmalloc(sizeof(struct NIO_Monitor));
64
88
  assert(monitor);
65
89
  *monitor = (struct NIO_Monitor){.self = Qnil};
66
- return Data_Wrap_Struct(klass, NIO_Monitor_mark, NIO_Monitor_free, monitor);
90
+ return TypedData_Wrap_Struct(klass, &NIO_Monitor_type, monitor);
67
91
  }
68
92
 
69
- static void NIO_Monitor_mark(struct NIO_Monitor *monitor)
93
+ static void NIO_Monitor_mark(void *data)
70
94
  {
95
+ struct NIO_Monitor *monitor = (struct NIO_Monitor *)data;
71
96
  rb_gc_mark(monitor->self);
72
97
  }
73
98
 
74
- static void NIO_Monitor_free(struct NIO_Monitor *monitor)
99
+ static size_t NIO_Monitor_memsize(const void *data)
75
100
  {
76
- xfree(monitor);
101
+ const struct NIO_Monitor *monitor = (const struct NIO_Monitor *)data;
102
+ return sizeof(*monitor);
77
103
  }
78
104
 
79
105
  static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE selector_obj)
@@ -81,11 +107,10 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
81
107
  struct NIO_Monitor *monitor;
82
108
  struct NIO_Selector *selector;
83
109
  ID interests_id;
84
- rb_io_t *fptr;
85
110
 
86
111
  interests_id = SYM2ID(interests);
87
112
 
88
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
113
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
89
114
 
90
115
  if (interests_id == rb_intern("r")) {
91
116
  monitor->interests = EV_READ;
@@ -97,16 +122,16 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
97
122
  rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
98
123
  }
99
124
 
100
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
101
- ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, FPTR_TO_FD(fptr), monitor->interests);
125
+ int descriptor = rb_io_descriptor(rb_convert_type(io, T_FILE, "IO", "to_io"));
126
+ ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, descriptor, monitor->interests);
102
127
 
103
128
  rb_ivar_set(self, rb_intern("io"), io);
104
129
  rb_ivar_set(self, rb_intern("interests"), interests);
105
130
  rb_ivar_set(self, rb_intern("selector"), selector_obj);
106
131
 
107
- Data_Get_Struct(selector_obj, struct NIO_Selector, selector);
132
+ selector = NIO_Selector_unwrap(selector_obj);
108
133
 
109
- monitor->self = self;
134
+ RB_OBJ_WRITE(self, &monitor->self, self);
110
135
  monitor->ev_io.data = (void *)monitor;
111
136
 
112
137
  /* We can safely hang onto this as we also hang onto a reference to the
@@ -124,7 +149,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
124
149
  {
125
150
  VALUE deregister, selector;
126
151
  struct NIO_Monitor *monitor;
127
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
152
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
128
153
 
129
154
  rb_scan_args(argc, argv, "01", &deregister);
130
155
  selector = rb_ivar_get(self, rb_intern("selector"));
@@ -150,7 +175,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
150
175
  static VALUE NIO_Monitor_is_closed(VALUE self)
151
176
  {
152
177
  struct NIO_Monitor *monitor;
153
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
178
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
154
179
 
155
180
  return monitor->selector == 0 ? Qtrue : Qfalse;
156
181
  }
@@ -179,10 +204,10 @@ static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
179
204
  static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
180
205
  {
181
206
  struct NIO_Monitor *monitor;
182
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
207
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
183
208
 
184
209
  interest = monitor->interests | NIO_Monitor_symbol2interest(interest);
185
- NIO_Monitor_update_interests(self, interest);
210
+ NIO_Monitor_update_interests(self, (int)interest);
186
211
 
187
212
  return rb_ivar_get(self, rb_intern("interests"));
188
213
  }
@@ -190,10 +215,10 @@ static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
190
215
  static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
191
216
  {
192
217
  struct NIO_Monitor *monitor;
193
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
218
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
194
219
 
195
220
  interest = monitor->interests & ~NIO_Monitor_symbol2interest(interest);
196
- NIO_Monitor_update_interests(self, interest);
221
+ NIO_Monitor_update_interests(self, (int)interest);
197
222
 
198
223
  return rb_ivar_get(self, rb_intern("interests"));
199
224
  }
@@ -216,7 +241,7 @@ static VALUE NIO_Monitor_set_value(VALUE self, VALUE obj)
216
241
  static VALUE NIO_Monitor_readiness(VALUE self)
217
242
  {
218
243
  struct NIO_Monitor *monitor;
219
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
244
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
220
245
 
221
246
  if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
222
247
  return ID2SYM(rb_intern("rw"));
@@ -232,7 +257,7 @@ static VALUE NIO_Monitor_readiness(VALUE self)
232
257
  static VALUE NIO_Monitor_is_readable(VALUE self)
233
258
  {
234
259
  struct NIO_Monitor *monitor;
235
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
260
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
236
261
 
237
262
  if (monitor->revents & EV_READ) {
238
263
  return Qtrue;
@@ -244,7 +269,7 @@ static VALUE NIO_Monitor_is_readable(VALUE self)
244
269
  static VALUE NIO_Monitor_is_writable(VALUE self)
245
270
  {
246
271
  struct NIO_Monitor *monitor;
247
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
272
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
248
273
 
249
274
  if (monitor->revents & EV_WRITE) {
250
275
  return Qtrue;
@@ -275,7 +300,7 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
275
300
  {
276
301
  ID interests_id;
277
302
  struct NIO_Monitor *monitor;
278
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
303
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
279
304
 
280
305
  if (NIO_Monitor_is_closed(self) == Qtrue) {
281
306
  rb_raise(rb_eEOFError, "monitor is closed");
data/ext/nio4r/nio4r.h CHANGED
@@ -40,11 +40,7 @@ struct NIO_ByteBuffer {
40
40
  int position, limit, capacity, mark;
41
41
  };
42
42
 
43
- #ifdef GetReadFile
44
- #define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
45
- #else
46
- #define FPTR_TO_FD(fptr) fptr->fd
47
- #endif /* GetReadFile */
43
+ struct NIO_Selector *NIO_Selector_unwrap(VALUE selector);
48
44
 
49
45
  /* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
50
46
  void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);