msgpack 1.4.2 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +89 -0
  3. data/README.md +73 -13
  4. data/ext/java/org/msgpack/jruby/Buffer.java +26 -19
  5. data/ext/java/org/msgpack/jruby/Decoder.java +29 -21
  6. data/ext/java/org/msgpack/jruby/Encoder.java +68 -30
  7. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +43 -64
  8. data/ext/java/org/msgpack/jruby/ExtensionValue.java +6 -9
  9. data/ext/java/org/msgpack/jruby/Factory.java +43 -42
  10. data/ext/java/org/msgpack/jruby/Packer.java +37 -40
  11. data/ext/java/org/msgpack/jruby/Unpacker.java +80 -73
  12. data/ext/msgpack/buffer.c +54 -74
  13. data/ext/msgpack/buffer.h +21 -18
  14. data/ext/msgpack/buffer_class.c +161 -52
  15. data/ext/msgpack/buffer_class.h +1 -0
  16. data/ext/msgpack/compat.h +0 -99
  17. data/ext/msgpack/extconf.rb +25 -46
  18. data/ext/msgpack/factory_class.c +143 -87
  19. data/ext/msgpack/packer.c +66 -43
  20. data/ext/msgpack/packer.h +25 -20
  21. data/ext/msgpack/packer_class.c +102 -130
  22. data/ext/msgpack/packer_class.h +11 -0
  23. data/ext/msgpack/packer_ext_registry.c +35 -40
  24. data/ext/msgpack/packer_ext_registry.h +41 -38
  25. data/ext/msgpack/rbinit.c +1 -1
  26. data/ext/msgpack/rmem.c +3 -4
  27. data/ext/msgpack/sysdep.h +5 -2
  28. data/ext/msgpack/unpacker.c +136 -111
  29. data/ext/msgpack/unpacker.h +16 -13
  30. data/ext/msgpack/unpacker_class.c +86 -126
  31. data/ext/msgpack/unpacker_class.h +11 -0
  32. data/ext/msgpack/unpacker_ext_registry.c +40 -28
  33. data/ext/msgpack/unpacker_ext_registry.h +21 -18
  34. data/lib/msgpack/bigint.rb +69 -0
  35. data/lib/msgpack/buffer.rb +9 -0
  36. data/lib/msgpack/factory.rb +140 -10
  37. data/lib/msgpack/packer.rb +10 -1
  38. data/lib/msgpack/symbol.rb +21 -4
  39. data/lib/msgpack/time.rb +1 -1
  40. data/lib/msgpack/unpacker.rb +14 -1
  41. data/lib/msgpack/version.rb +1 -1
  42. data/lib/msgpack.rb +6 -7
  43. data/msgpack.gemspec +8 -5
  44. metadata +37 -82
  45. data/.gitignore +0 -23
  46. data/.rubocop.yml +0 -36
  47. data/.travis.yml +0 -39
  48. data/Gemfile +0 -9
  49. data/Rakefile +0 -71
  50. data/appveyor.yml +0 -18
  51. data/bench/pack.rb +0 -23
  52. data/bench/pack_log.rb +0 -33
  53. data/bench/pack_log_long.rb +0 -65
  54. data/bench/pack_symbols.rb +0 -28
  55. data/bench/run.sh +0 -14
  56. data/bench/run_long.sh +0 -35
  57. data/bench/run_symbols.sh +0 -26
  58. data/bench/unpack.rb +0 -21
  59. data/bench/unpack_log.rb +0 -34
  60. data/bench/unpack_log_long.rb +0 -67
  61. data/doclib/msgpack/buffer.rb +0 -193
  62. data/doclib/msgpack/core_ext.rb +0 -101
  63. data/doclib/msgpack/error.rb +0 -19
  64. data/doclib/msgpack/extension_value.rb +0 -9
  65. data/doclib/msgpack/factory.rb +0 -101
  66. data/doclib/msgpack/packer.rb +0 -208
  67. data/doclib/msgpack/time.rb +0 -22
  68. data/doclib/msgpack/timestamp.rb +0 -44
  69. data/doclib/msgpack/unpacker.rb +0 -183
  70. data/doclib/msgpack.rb +0 -87
  71. data/msgpack.org.md +0 -46
  72. data/spec/cases.json +0 -1
  73. data/spec/cases.msg +0 -0
  74. data/spec/cases_compact.msg +0 -0
  75. data/spec/cases_spec.rb +0 -39
  76. data/spec/cruby/buffer_io_spec.rb +0 -255
  77. data/spec/cruby/buffer_packer.rb +0 -29
  78. data/spec/cruby/buffer_spec.rb +0 -575
  79. data/spec/cruby/buffer_unpacker.rb +0 -19
  80. data/spec/cruby/unpacker_spec.rb +0 -70
  81. data/spec/ext_value_spec.rb +0 -99
  82. data/spec/exttypes.rb +0 -51
  83. data/spec/factory_spec.rb +0 -367
  84. data/spec/format_spec.rb +0 -301
  85. data/spec/jruby/benchmarks/shootout_bm.rb +0 -73
  86. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +0 -25
  87. data/spec/jruby/unpacker_spec.rb +0 -186
  88. data/spec/msgpack_spec.rb +0 -214
  89. data/spec/pack_spec.rb +0 -61
  90. data/spec/packer_spec.rb +0 -557
  91. data/spec/random_compat.rb +0 -24
  92. data/spec/spec_helper.rb +0 -55
  93. data/spec/timestamp_spec.rb +0 -121
  94. data/spec/unpack_spec.rb +0 -57
  95. data/spec/unpacker_spec.rb +0 -819
@@ -21,20 +21,87 @@
21
21
  #include "buffer.h"
22
22
  #include "buffer_class.h"
23
23
 
24
- VALUE cMessagePack_Buffer;
24
+ VALUE cMessagePack_Buffer = Qnil;
25
+ VALUE cMessagePack_HeldBuffer = Qnil;
25
26
 
26
27
  static ID s_read;
27
28
  static ID s_readpartial;
28
29
  static ID s_write;
29
30
  static ID s_append;
30
31
  static ID s_close;
32
+ static ID s_at_owner;
31
33
 
32
- #define BUFFER(from, name) \
33
- msgpack_buffer_t *name = NULL; \
34
- Data_Get_Struct(from, msgpack_buffer_t, name); \
35
- if(name == NULL) { \
36
- rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
34
+ static VALUE sym_read_reference_threshold;
35
+ static VALUE sym_write_reference_threshold;
36
+ static VALUE sym_io_buffer_size;
37
+
38
+ typedef struct msgpack_held_buffer_t msgpack_held_buffer_t;
39
+ struct msgpack_held_buffer_t {
40
+ size_t size;
41
+ VALUE mapped_strings[];
42
+ };
43
+
44
+ static void HeldBuffer_mark(void *data)
45
+ {
46
+ msgpack_held_buffer_t* held_buffer = (msgpack_held_buffer_t*)data;
47
+ for (size_t index = 0; index < held_buffer->size; index++) {
48
+ rb_gc_mark(held_buffer->mapped_strings[index]);
49
+ }
50
+ }
51
+
52
+ static size_t HeldBuffer_memsize(const void *data)
53
+ {
54
+ const msgpack_held_buffer_t* held_buffer = (msgpack_held_buffer_t*)data;
55
+ return sizeof(size_t) + sizeof(VALUE) * held_buffer->size;
56
+ }
57
+
58
+ static const rb_data_type_t held_buffer_data_type = {
59
+ .wrap_struct_name = "msgpack:held_buffer",
60
+ .function = {
61
+ .dmark = HeldBuffer_mark,
62
+ .dfree = RUBY_TYPED_DEFAULT_FREE,
63
+ .dsize = HeldBuffer_memsize,
64
+ },
65
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
66
+ };
67
+
68
+ VALUE MessagePack_Buffer_hold(msgpack_buffer_t* buffer)
69
+ {
70
+ size_t mapped_strings_count = 0;
71
+ msgpack_buffer_chunk_t* c = buffer->head;
72
+ while (c != &buffer->tail) {
73
+ if (c->mapped_string != NO_MAPPED_STRING) {
74
+ mapped_strings_count++;
75
+ }
76
+ c = c->next;
77
+ }
78
+ if (c->mapped_string != NO_MAPPED_STRING) {
79
+ mapped_strings_count++;
80
+ }
81
+
82
+ if (mapped_strings_count == 0) {
83
+ return Qnil;
84
+ }
85
+
86
+ msgpack_held_buffer_t* held_buffer = xmalloc(sizeof(msgpack_held_buffer_t) + mapped_strings_count * sizeof(VALUE));
87
+
88
+ c = buffer->head;
89
+ mapped_strings_count = 0;
90
+ while (c != &buffer->tail) {
91
+ if (c->mapped_string != NO_MAPPED_STRING) {
92
+ held_buffer->mapped_strings[mapped_strings_count] = c->mapped_string;
93
+ mapped_strings_count++;
94
+ }
95
+ c = c->next;
96
+ }
97
+ if (c->mapped_string != NO_MAPPED_STRING) {
98
+ held_buffer->mapped_strings[mapped_strings_count] = c->mapped_string;
99
+ mapped_strings_count++;
37
100
  }
101
+ held_buffer->size = mapped_strings_count;
102
+ return TypedData_Wrap_Struct(cMessagePack_HeldBuffer, &held_buffer_data_type, held_buffer);
103
+ }
104
+
38
105
 
39
106
  #define CHECK_STRING_TYPE(value) \
40
107
  value = rb_check_string_type(value); \
@@ -52,34 +119,69 @@ static void Buffer_free(void* data)
52
119
  xfree(b);
53
120
  }
54
121
 
122
+ static size_t Buffer_memsize(const void *data)
123
+ {
124
+ return sizeof(msgpack_buffer_t) + msgpack_buffer_memsize(data);
125
+ }
126
+
127
+ static const rb_data_type_t buffer_data_type = {
128
+ .wrap_struct_name = "msgpack:buffer",
129
+ .function = {
130
+ .dmark = msgpack_buffer_mark,
131
+ .dfree = Buffer_free,
132
+ .dsize = Buffer_memsize,
133
+ },
134
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
135
+ };
136
+
137
+ static const rb_data_type_t buffer_view_data_type = {
138
+ .wrap_struct_name = "msgpack:buffer_view",
139
+ .function = {
140
+ .dmark = NULL,
141
+ .dfree = NULL,
142
+ .dsize = NULL,
143
+ },
144
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
145
+ };
146
+
147
+ static inline msgpack_buffer_t *MessagePack_Buffer_get(VALUE object)
148
+ {
149
+ msgpack_buffer_t *buffer;
150
+ bool view = RTEST(rb_ivar_get(object, s_at_owner));
151
+ TypedData_Get_Struct(object, msgpack_buffer_t, view ? &buffer_view_data_type : &buffer_data_type, buffer);
152
+ if (!buffer) {
153
+ rb_raise(rb_eArgError, "Uninitialized Buffer object");
154
+ }
155
+ return buffer;
156
+ }
157
+
55
158
  static VALUE Buffer_alloc(VALUE klass)
56
159
  {
57
- msgpack_buffer_t* b = ZALLOC_N(msgpack_buffer_t, 1);
160
+ msgpack_buffer_t* b;
161
+ VALUE buffer = TypedData_Make_Struct(klass, msgpack_buffer_t, &buffer_data_type, b);
58
162
  msgpack_buffer_init(b);
59
-
60
- return Data_Wrap_Struct(klass, msgpack_buffer_mark, Buffer_free, b);
163
+ rb_ivar_set(buffer, s_at_owner, Qnil);
164
+ return buffer;
61
165
  }
62
166
 
63
167
  static ID get_partial_read_method(VALUE io)
64
168
  {
65
- if(rb_respond_to(io, s_readpartial)) {
169
+ if(io != Qnil && rb_respond_to(io, s_readpartial)) {
66
170
  return s_readpartial;
67
- } else if(rb_respond_to(io, s_read)) {
68
- return s_read;
69
- } else {
70
- return s_read;
71
171
  }
172
+ return s_read;
72
173
  }
73
174
 
74
175
  static ID get_write_all_method(VALUE io)
75
176
  {
76
- if(rb_respond_to(io, s_write)) {
77
- return s_write;
78
- } else if(rb_respond_to(io, s_append)) {
79
- return s_append;
80
- } else {
81
- return s_write;
177
+ if(io != Qnil) {
178
+ if(rb_respond_to(io, s_write)) {
179
+ return s_write;
180
+ } else if(rb_respond_to(io, s_append)) {
181
+ return s_append;
182
+ }
82
183
  }
184
+ return s_write;
83
185
  }
84
186
 
85
187
  void MessagePack_Buffer_set_options(msgpack_buffer_t* b, VALUE io, VALUE options)
@@ -91,27 +193,28 @@ void MessagePack_Buffer_set_options(msgpack_buffer_t* b, VALUE io, VALUE options
91
193
  if(options != Qnil) {
92
194
  VALUE v;
93
195
 
94
- v = rb_hash_aref(options, ID2SYM(rb_intern("read_reference_threshold")));
196
+ v = rb_hash_aref(options, sym_read_reference_threshold);
95
197
  if(v != Qnil) {
96
- msgpack_buffer_set_read_reference_threshold(b, NUM2ULONG(v));
198
+ msgpack_buffer_set_read_reference_threshold(b, NUM2SIZET(v));
97
199
  }
98
200
 
99
- v = rb_hash_aref(options, ID2SYM(rb_intern("write_reference_threshold")));
201
+ v = rb_hash_aref(options, sym_write_reference_threshold);
100
202
  if(v != Qnil) {
101
- msgpack_buffer_set_write_reference_threshold(b, NUM2ULONG(v));
203
+ msgpack_buffer_set_write_reference_threshold(b, NUM2SIZET(v));
102
204
  }
103
205
 
104
- v = rb_hash_aref(options, ID2SYM(rb_intern("io_buffer_size")));
206
+ v = rb_hash_aref(options, sym_io_buffer_size);
105
207
  if(v != Qnil) {
106
- msgpack_buffer_set_io_buffer_size(b, NUM2ULONG(v));
208
+ msgpack_buffer_set_io_buffer_size(b, NUM2SIZET(v));
107
209
  }
108
210
  }
109
211
  }
110
212
 
111
213
  VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner)
112
214
  {
113
- b->owner = owner;
114
- return Data_Wrap_Struct(cMessagePack_Buffer, msgpack_buffer_mark, NULL, b);
215
+ VALUE buffer = TypedData_Wrap_Struct(cMessagePack_Buffer, &buffer_view_data_type, b);
216
+ rb_ivar_set(buffer, s_at_owner, owner);
217
+ return buffer;
115
218
  }
116
219
 
117
220
  static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
@@ -141,7 +244,7 @@ static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
141
244
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
142
245
  }
143
246
 
144
- BUFFER(self, b);
247
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
145
248
 
146
249
  MessagePack_Buffer_set_options(b, io, options);
147
250
 
@@ -150,21 +253,21 @@ static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
150
253
 
151
254
  static VALUE Buffer_clear(VALUE self)
152
255
  {
153
- BUFFER(self, b);
256
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
154
257
  msgpack_buffer_clear(b);
155
258
  return Qnil;
156
259
  }
157
260
 
158
261
  static VALUE Buffer_size(VALUE self)
159
262
  {
160
- BUFFER(self, b);
263
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
161
264
  size_t size = msgpack_buffer_all_readable_size(b);
162
265
  return SIZET2NUM(size);
163
266
  }
164
267
 
165
268
  static VALUE Buffer_empty_p(VALUE self)
166
269
  {
167
- BUFFER(self, b);
270
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
168
271
  if(msgpack_buffer_top_readable_size(b) == 0) {
169
272
  return Qtrue;
170
273
  } else {
@@ -174,7 +277,7 @@ static VALUE Buffer_empty_p(VALUE self)
174
277
 
175
278
  static VALUE Buffer_write(VALUE self, VALUE string_or_buffer)
176
279
  {
177
- BUFFER(self, b);
280
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
178
281
 
179
282
  VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
180
283
  StringValue(string);
@@ -186,7 +289,7 @@ static VALUE Buffer_write(VALUE self, VALUE string_or_buffer)
186
289
 
187
290
  static VALUE Buffer_append(VALUE self, VALUE string_or_buffer)
188
291
  {
189
- BUFFER(self, b);
292
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
190
293
 
191
294
  VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
192
295
  StringValue(string);
@@ -245,10 +348,11 @@ static VALUE read_until_eof_rescue(VALUE args)
245
348
  return Qnil;
246
349
  }
247
350
 
248
- static VALUE read_until_eof_error(VALUE args)
351
+ static VALUE read_until_eof_error(VALUE args, VALUE error)
249
352
  {
250
353
  /* ignore EOFError */
251
354
  UNUSED(args);
355
+ UNUSED(error);
252
356
  return Qnil;
253
357
  }
254
358
 
@@ -276,14 +380,13 @@ static inline size_t read_until_eof(msgpack_buffer_t* b, VALUE out, unsigned lon
276
380
 
277
381
  static inline VALUE read_all(msgpack_buffer_t* b, VALUE out)
278
382
  {
279
- #ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
280
383
  if(out == Qnil && !msgpack_buffer_has_io(b)) {
281
384
  /* same as to_s && clear; optimize */
282
385
  VALUE str = msgpack_buffer_all_as_string(b);
283
386
  msgpack_buffer_clear(b);
284
387
  return str;
285
388
  }
286
- #endif
389
+
287
390
  MAKE_EMPTY_STRING(out);
288
391
  read_until_eof(b, out, 0);
289
392
  return out;
@@ -291,22 +394,22 @@ static inline VALUE read_all(msgpack_buffer_t* b, VALUE out)
291
394
 
292
395
  static VALUE Buffer_skip(VALUE self, VALUE sn)
293
396
  {
294
- BUFFER(self, b);
397
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
295
398
 
296
399
  unsigned long n = FIX2ULONG(sn);
297
400
 
298
401
  /* do nothing */
299
402
  if(n == 0) {
300
- return ULONG2NUM(0);
403
+ return INT2NUM(0);
301
404
  }
302
405
 
303
406
  size_t sz = read_until_eof(b, Qnil, n);
304
- return ULONG2NUM(sz);
407
+ return SIZET2NUM(sz);
305
408
  }
306
409
 
307
410
  static VALUE Buffer_skip_all(VALUE self, VALUE sn)
308
411
  {
309
- BUFFER(self, b);
412
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
310
413
 
311
414
  unsigned long n = FIX2ULONG(sn);
312
415
 
@@ -344,7 +447,7 @@ static VALUE Buffer_read_all(int argc, VALUE* argv, VALUE self)
344
447
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
345
448
  }
346
449
 
347
- BUFFER(self, b);
450
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
348
451
 
349
452
  if(out != Qnil) {
350
453
  CHECK_STRING_TYPE(out);
@@ -390,7 +493,7 @@ static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
390
493
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
391
494
  }
392
495
 
393
- BUFFER(self, b);
496
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
394
497
 
395
498
  if(out != Qnil) {
396
499
  CHECK_STRING_TYPE(out);
@@ -406,7 +509,6 @@ static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
406
509
  return out;
407
510
  }
408
511
 
409
- #ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
410
512
  if(!msgpack_buffer_has_io(b) && out == Qnil &&
411
513
  msgpack_buffer_all_readable_size(b) <= n) {
412
514
  /* same as to_s && clear; optimize */
@@ -419,7 +521,6 @@ static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
419
521
  return str;
420
522
  }
421
523
  }
422
- #endif
423
524
 
424
525
  MAKE_EMPTY_STRING(out);
425
526
  read_until_eof(b, out, n);
@@ -433,32 +534,32 @@ static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
433
534
 
434
535
  static VALUE Buffer_to_str(VALUE self)
435
536
  {
436
- BUFFER(self, b);
537
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
437
538
  return msgpack_buffer_all_as_string(b);
438
539
  }
439
540
 
440
541
  static VALUE Buffer_to_a(VALUE self)
441
542
  {
442
- BUFFER(self, b);
543
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
443
544
  return msgpack_buffer_all_as_string_array(b);
444
545
  }
445
546
 
446
547
  static VALUE Buffer_flush(VALUE self)
447
548
  {
448
- BUFFER(self, b);
549
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
449
550
  msgpack_buffer_flush(b);
450
551
  return self;
451
552
  }
452
553
 
453
554
  static VALUE Buffer_io(VALUE self)
454
555
  {
455
- BUFFER(self, b);
556
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
456
557
  return b->io;
457
558
  }
458
559
 
459
560
  static VALUE Buffer_close(VALUE self)
460
561
  {
461
- BUFFER(self, b);
562
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
462
563
  if(b->io != Qnil) {
463
564
  return rb_funcall(b->io, s_close, 0);
464
565
  }
@@ -467,9 +568,9 @@ static VALUE Buffer_close(VALUE self)
467
568
 
468
569
  static VALUE Buffer_write_to(VALUE self, VALUE io)
469
570
  {
470
- BUFFER(self, b);
571
+ msgpack_buffer_t *b = MessagePack_Buffer_get(self);
471
572
  size_t sz = msgpack_buffer_flush_to_io(b, io, s_write, true);
472
- return ULONG2NUM(sz);
573
+ return SIZET2NUM(sz);
473
574
  }
474
575
 
475
576
  void MessagePack_Buffer_module_init(VALUE mMessagePack)
@@ -479,9 +580,17 @@ void MessagePack_Buffer_module_init(VALUE mMessagePack)
479
580
  s_write = rb_intern("write");
480
581
  s_append = rb_intern("<<");
481
582
  s_close = rb_intern("close");
583
+ s_at_owner = rb_intern("@owner");
584
+
585
+ sym_read_reference_threshold = ID2SYM(rb_intern("read_reference_threshold"));
586
+ sym_write_reference_threshold = ID2SYM(rb_intern("write_reference_threshold"));
587
+ sym_io_buffer_size = ID2SYM(rb_intern("io_buffer_size"));
482
588
 
483
589
  msgpack_buffer_static_init();
484
590
 
591
+ cMessagePack_HeldBuffer = rb_define_class_under(mMessagePack, "HeldBuffer", rb_cBasicObject);
592
+ rb_undef_alloc_func(cMessagePack_HeldBuffer);
593
+
485
594
  cMessagePack_Buffer = rb_define_class_under(mMessagePack, "Buffer", rb_cObject);
486
595
 
487
596
  rb_define_alloc_func(cMessagePack_Buffer, Buffer_alloc);
@@ -25,6 +25,7 @@ extern VALUE cMessagePack_Buffer;
25
25
  void MessagePack_Buffer_module_init(VALUE mMessagePack);
26
26
 
27
27
  VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner);
28
+ VALUE MessagePack_Buffer_hold(msgpack_buffer_t* b);
28
29
 
29
30
  void MessagePack_Buffer_set_options(msgpack_buffer_t* b, VALUE io, VALUE options);
30
31
 
data/ext/msgpack/compat.h CHANGED
@@ -22,104 +22,5 @@
22
22
  #include "ruby.h"
23
23
  #include "ruby/encoding.h"
24
24
 
25
- #if defined(HAVE_RUBY_ST_H)
26
- # include "ruby/st.h" /* ruby hash on Ruby 1.9 */
27
- #elif defined(HAVE_ST_H)
28
- # include "st.h" /* ruby hash on Ruby 1.8 */
29
- #endif
30
-
31
-
32
- /*
33
- * ZALLOC_N (ruby 2.2 or later)
34
- */
35
- #ifndef RB_ZALLOC_N
36
- # define RB_ZALLOC_N(type,n) ((type*)ruby_xcalloc((size_t)(n),sizeof(type)))
37
- #endif
38
- #ifndef ZALLOC_N
39
- # define ZALLOC_N(type,n) RB_ZALLOC_N(type,n)
40
- #endif
41
-
42
-
43
- /*
44
- * define STR_DUP_LIKELY_DOES_COPY
45
- * check rb_str_dup actually copies the string or not
46
- */
47
- #if defined(RUBY_VM) && defined(FL_ALL) && defined(FL_USER1) && defined(FL_USER3) /* MRI 1.9 */
48
- # define STR_DUP_LIKELY_DOES_COPY(str) FL_ALL(str, FL_USER1|FL_USER3) /* same as STR_ASSOC_P(str) */
49
-
50
- #elif defined(FL_TEST) && defined(ELTS_SHARED) /* MRI 1.8 */
51
- # define STR_DUP_LIKELY_DOES_COPY(str) (!FL_TEST(str, ELTS_SHARED))
52
-
53
- //#elif defined(RUBINIUS) || defined(JRUBY) /* Rubinius and JRuby */
54
- #else
55
- # define STR_DUP_LIKELY_DOES_COPY(str) (1)
56
-
57
- #endif
58
-
59
-
60
- /*
61
- * SIZET2NUM
62
- */
63
- #ifndef SIZET2NUM /* MRI 1.8 */
64
- # define SIZET2NUM(v) ULL2NUM(v)
65
- #endif
66
-
67
-
68
- /*
69
- * rb_errinfo()
70
- */
71
- #if defined(RUBY_VM) /* MRI 1.9 */
72
- # define COMPAT_RERAISE rb_exc_raise(rb_errinfo())
73
-
74
- #elif defined(JRUBY) /* JRuby */
75
- # define COMPAT_RERAISE rb_exc_raise(rb_gv_get("$!"))
76
-
77
- #else /* MRI 1.8 and Rubinius */
78
- # define COMPAT_RERAISE rb_exc_raise(ruby_errinfo)
79
- #endif
80
-
81
-
82
- /*
83
- * RBIGNUM_POSITIVE_P
84
- */
85
- #ifndef RBIGNUM_POSITIVE_P
86
- # if defined(RUBINIUS) /* Rubinius <= v1.2.3 */
87
- # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
88
-
89
- # elif defined(JRUBY) /* JRuby */
90
- # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
91
- # define rb_big2ull(b) rb_num2ull(b)
92
- /*#define rb_big2ll(b) rb_num2ll(b)*/
93
-
94
- # else /* MRI 1.8 */
95
- # define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign)
96
- # endif
97
- #endif
98
-
99
-
100
- /*
101
- * RSTRING_PTR, RSTRING_LEN
102
- */
103
- #ifndef RSTRING_PTR /* MRI 1.8.5 */
104
- # define RSTRING_PTR(s) (RSTRING(s)->ptr)
105
- #endif
106
-
107
- #ifndef RSTRING_LEN /* MRI 1.8.5 */
108
- # define RSTRING_LEN(s) (RSTRING(s)->len)
109
- #endif
110
-
111
-
112
- /*
113
- * RSTRUCT_GET
114
- */
115
- #ifndef RSTRUCT_GET
116
- # ifdef RSTRUCT_PTR /* MRI <= 2.0.0 */
117
- # define RSTRUCT_GET(st, idx) (RSTRUCT_PTR(st)[idx])
118
- # else /* Rubinius */
119
- # define RSTRUCT_GET(st, idx) (rb_struct_aref(st, INT2FIX(idx)))
120
- # endif
121
- #endif
122
-
123
-
124
25
  #endif
125
26
 
@@ -1,67 +1,47 @@
1
1
  require 'mkmf'
2
2
 
3
- have_header("ruby/st.h")
4
- have_header("st.h")
5
- have_func("rb_str_replace", ["ruby.h"])
6
- have_func("rb_intern_str", ["ruby.h"])
7
- have_func("rb_enc_interned_str", "ruby.h")
8
- have_func("rb_sym2str", ["ruby.h"])
9
- have_func("rb_str_intern", ["ruby.h"])
10
- have_func("rb_block_lambda", ["ruby.h"])
11
- have_func("rb_hash_dup", ["ruby.h"])
12
- have_func("rb_hash_clear", ["ruby.h"])
13
-
14
- unless RUBY_PLATFORM.include? 'mswin'
15
- $CFLAGS << %[ -I.. -Wall -O3 -g -std=gnu99]
16
- end
17
- #$CFLAGS << %[ -DDISABLE_RMEM]
18
- #$CFLAGS << %[ -DDISABLE_RMEM_REUSE_INTERNAL_FRAGMENT]
19
- #$CFLAGS << %[ -DDISABLE_BUFFER_READ_REFERENCE_OPTIMIZE]
20
- #$CFLAGS << %[ -DDISABLE_BUFFER_READ_TO_S_OPTIMIZE]
21
-
22
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
23
- # msgpack-ruby doesn't modify data came from RSTRING_PTR(str)
24
- $CFLAGS << %[ -DRSTRING_NOT_MODIFIED]
25
- # Rubinius C extensions don't grab GVL while rmem is not thread safe
26
- $CFLAGS << %[ -DDISABLE_RMEM]
3
+ have_func("rb_enc_interned_str", "ruby.h") # Ruby 3.0+
4
+ have_func("rb_hash_new_capa", "ruby.h") # Ruby 3.2+
5
+ have_func("rb_proc_call_with_block", "ruby.h") # CRuby (TruffleRuby doesn't have it)
6
+
7
+ append_cflags([
8
+ "-fvisibility=hidden",
9
+ "-I..",
10
+ "-Wall",
11
+ "-O3",
12
+ "-std=gnu99"
13
+ ])
14
+ append_cflags(RbConfig::CONFIG["debugflags"]) if RbConfig::CONFIG["debugflags"]
15
+
16
+ append_cflags("-DRUBY_DEBUG=1") if ENV["MSGPACK_DEBUG"]
17
+
18
+ if RUBY_VERSION.start_with?('3.0.') && RUBY_VERSION <= '3.0.5'
19
+ # https://bugs.ruby-lang.org/issues/18772
20
+ append_cflags("-DRB_ENC_INTERNED_STR_NULL_CHECK=1")
27
21
  end
28
22
 
29
- # checking if Hash#[]= (rb_hash_aset) dedupes string keys
23
+ # checking if Hash#[]= (rb_hash_aset) dedupes string keys (Ruby 2.6+)
30
24
  h = {}
31
25
  x = {}
32
26
  r = rand.to_s
33
27
  h[%W(#{r}).join('')] = :foo
34
28
  x[%W(#{r}).join('')] = :foo
35
29
  if x.keys[0].equal?(h.keys[0])
36
- $CFLAGS << ' -DHASH_ASET_DEDUPE=1 '
30
+ append_cflags("-DHASH_ASET_DEDUPE=1")
37
31
  else
38
- $CFLAGS << ' -DHASH_ASET_DEDUPE=0 '
32
+ append_cflags("-DHASH_ASET_DEDUPE=0")
39
33
  end
40
34
 
41
-
42
- # checking if String#-@ (str_uminus) dedupes... '
43
- begin
44
- a = -(%w(t e s t).join)
45
- b = -(%w(t e s t).join)
46
- if a.equal?(b)
47
- $CFLAGS << ' -DSTR_UMINUS_DEDUPE=1 '
48
- else
49
- $CFLAGS += ' -DSTR_UMINUS_DEDUPE=0 '
50
- end
51
- rescue NoMethodError
52
- $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 '
53
- end
54
-
55
- # checking if String#-@ (str_uminus) directly interns frozen strings... '
35
+ # checking if String#-@ (str_uminus) directly interns frozen strings... ' (Ruby 3.0+)
56
36
  begin
57
37
  s = rand.to_s.freeze
58
38
  if (-s).equal?(s) && (-s.dup).equal?(s)
59
- $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=1 '
39
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=1")
60
40
  else
61
- $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
41
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=0")
62
42
  end
63
43
  rescue NoMethodError
64
- $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
44
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=0")
65
45
  end
66
46
 
67
47
  if warnflags = CONFIG['warnflags']
@@ -69,4 +49,3 @@ if warnflags = CONFIG['warnflags']
69
49
  end
70
50
 
71
51
  create_makefile('msgpack/msgpack')
72
-