msgpack 1.5.1 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +55 -0
  3. data/README.md +30 -1
  4. data/ext/java/org/msgpack/jruby/Buffer.java +3 -3
  5. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +11 -20
  6. data/ext/java/org/msgpack/jruby/ExtensionValue.java +1 -1
  7. data/ext/java/org/msgpack/jruby/Factory.java +11 -50
  8. data/ext/java/org/msgpack/jruby/Packer.java +9 -24
  9. data/ext/java/org/msgpack/jruby/Unpacker.java +15 -32
  10. data/ext/msgpack/buffer.c +54 -69
  11. data/ext/msgpack/buffer.h +16 -18
  12. data/ext/msgpack/buffer_class.c +138 -37
  13. data/ext/msgpack/buffer_class.h +1 -0
  14. data/ext/msgpack/compat.h +0 -99
  15. data/ext/msgpack/extconf.rb +25 -39
  16. data/ext/msgpack/factory_class.c +75 -86
  17. data/ext/msgpack/packer.c +12 -39
  18. data/ext/msgpack/packer.h +1 -11
  19. data/ext/msgpack/packer_class.c +73 -99
  20. data/ext/msgpack/packer_class.h +11 -0
  21. data/ext/msgpack/packer_ext_registry.c +31 -28
  22. data/ext/msgpack/packer_ext_registry.h +10 -14
  23. data/ext/msgpack/rbinit.c +1 -1
  24. data/ext/msgpack/rmem.c +3 -4
  25. data/ext/msgpack/sysdep.h +5 -2
  26. data/ext/msgpack/unpacker.c +66 -94
  27. data/ext/msgpack/unpacker.h +13 -12
  28. data/ext/msgpack/unpacker_class.c +64 -80
  29. data/ext/msgpack/unpacker_class.h +11 -0
  30. data/ext/msgpack/unpacker_ext_registry.c +4 -16
  31. data/ext/msgpack/unpacker_ext_registry.h +3 -7
  32. data/lib/msgpack/buffer.rb +9 -0
  33. data/lib/msgpack/factory.rb +90 -63
  34. data/lib/msgpack/packer.rb +10 -1
  35. data/lib/msgpack/unpacker.rb +14 -1
  36. data/lib/msgpack/version.rb +1 -1
  37. data/lib/msgpack.rb +1 -0
  38. data/msgpack.gemspec +7 -3
  39. metadata +33 -56
  40. data/.github/workflows/ci.yaml +0 -57
  41. data/.gitignore +0 -23
  42. data/.rubocop.yml +0 -36
  43. data/Gemfile +0 -9
  44. data/Rakefile +0 -70
  45. data/appveyor.yml +0 -18
  46. data/bench/pack.rb +0 -23
  47. data/bench/pack_log.rb +0 -33
  48. data/bench/pack_log_long.rb +0 -65
  49. data/bench/pack_symbols.rb +0 -28
  50. data/bench/run.sh +0 -14
  51. data/bench/run_long.sh +0 -35
  52. data/bench/run_symbols.sh +0 -26
  53. data/bench/unpack.rb +0 -21
  54. data/bench/unpack_log.rb +0 -34
  55. data/bench/unpack_log_long.rb +0 -67
  56. data/doclib/msgpack/buffer.rb +0 -193
  57. data/doclib/msgpack/core_ext.rb +0 -101
  58. data/doclib/msgpack/error.rb +0 -19
  59. data/doclib/msgpack/extension_value.rb +0 -9
  60. data/doclib/msgpack/factory.rb +0 -145
  61. data/doclib/msgpack/packer.rb +0 -209
  62. data/doclib/msgpack/time.rb +0 -22
  63. data/doclib/msgpack/timestamp.rb +0 -44
  64. data/doclib/msgpack/unpacker.rb +0 -183
  65. data/doclib/msgpack.rb +0 -87
  66. data/msgpack.org.md +0 -46
  67. data/spec/bigint_spec.rb +0 -26
  68. data/spec/cases.json +0 -1
  69. data/spec/cases.msg +0 -0
  70. data/spec/cases_compact.msg +0 -0
  71. data/spec/cases_spec.rb +0 -39
  72. data/spec/cruby/buffer_io_spec.rb +0 -255
  73. data/spec/cruby/buffer_packer.rb +0 -29
  74. data/spec/cruby/buffer_spec.rb +0 -575
  75. data/spec/cruby/buffer_unpacker.rb +0 -19
  76. data/spec/cruby/unpacker_spec.rb +0 -70
  77. data/spec/ext_value_spec.rb +0 -99
  78. data/spec/exttypes.rb +0 -51
  79. data/spec/factory_spec.rb +0 -654
  80. data/spec/format_spec.rb +0 -301
  81. data/spec/jruby/benchmarks/shootout_bm.rb +0 -73
  82. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +0 -25
  83. data/spec/jruby/unpacker_spec.rb +0 -186
  84. data/spec/msgpack_spec.rb +0 -214
  85. data/spec/pack_spec.rb +0 -61
  86. data/spec/packer_spec.rb +0 -575
  87. data/spec/random_compat.rb +0 -24
  88. data/spec/spec_helper.rb +0 -65
  89. data/spec/timestamp_spec.rb +0 -159
  90. data/spec/unpack_spec.rb +0 -57
  91. data/spec/unpacker_spec.rb +0 -847
@@ -33,15 +33,9 @@ static VALUE sym_compatibility_mode;
33
33
  //static VALUE s_packer_value;
34
34
  //static msgpack_packer_t* s_packer;
35
35
 
36
- #define PACKER(from, name) \
37
- msgpack_packer_t* name; \
38
- Data_Get_Struct(from, msgpack_packer_t, name); \
39
- if(name == NULL) { \
40
- rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
41
- }
42
-
43
- static void Packer_free(msgpack_packer_t* pk)
36
+ static void Packer_free(void *ptr)
44
37
  {
38
+ msgpack_packer_t* pk = ptr;
45
39
  if(pk == NULL) {
46
40
  return;
47
41
  }
@@ -50,21 +44,37 @@ static void Packer_free(msgpack_packer_t* pk)
50
44
  xfree(pk);
51
45
  }
52
46
 
53
- static void Packer_mark(msgpack_packer_t* pk)
47
+ static void Packer_mark(void *ptr)
54
48
  {
49
+ msgpack_packer_t* pk = ptr;
50
+ msgpack_buffer_mark(pk);
55
51
  msgpack_packer_mark(pk);
56
52
  msgpack_packer_ext_registry_mark(&pk->ext_registry);
57
53
  }
58
54
 
59
- VALUE MessagePack_Packer_alloc(VALUE klass)
55
+ static size_t Packer_memsize(const void *ptr)
60
56
  {
61
- msgpack_packer_t* pk = ZALLOC_N(msgpack_packer_t, 1);
62
- msgpack_packer_init(pk);
57
+ const msgpack_packer_t* pk = ptr;
58
+ return sizeof(msgpack_packer_t) + msgpack_buffer_memsize(&pk->buffer);
59
+ }
63
60
 
64
- VALUE self = Data_Wrap_Struct(klass, Packer_mark, Packer_free, pk);
61
+ const rb_data_type_t packer_data_type = {
62
+ .wrap_struct_name = "msgpack:packer",
63
+ .function = {
64
+ .dmark = Packer_mark,
65
+ .dfree = Packer_free,
66
+ .dsize = Packer_memsize,
67
+ },
68
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
69
+ };
65
70
 
66
- msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
67
71
 
72
+ VALUE MessagePack_Packer_alloc(VALUE klass)
73
+ {
74
+ msgpack_packer_t* pk;
75
+ VALUE self = TypedData_Make_Struct(klass, msgpack_packer_t, &packer_data_type, pk);
76
+ msgpack_packer_init(pk);
77
+ msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
68
78
  return self;
69
79
  }
70
80
 
@@ -94,9 +104,9 @@ VALUE MessagePack_Packer_initialize(int argc, VALUE* argv, VALUE self)
94
104
  Check_Type(options, T_HASH);
95
105
  }
96
106
 
97
- PACKER(self, pk);
107
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
98
108
 
99
- msgpack_packer_ext_registry_init(&pk->ext_registry);
109
+ msgpack_packer_ext_registry_init(self, &pk->ext_registry);
100
110
  pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
101
111
 
102
112
  MessagePack_Buffer_set_options(PACKER_BUFFER_(pk), io, options);
@@ -113,54 +123,57 @@ VALUE MessagePack_Packer_initialize(int argc, VALUE* argv, VALUE self)
113
123
 
114
124
  static VALUE Packer_compatibility_mode_p(VALUE self)
115
125
  {
116
- PACKER(self, pk);
126
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
117
127
  return pk->compatibility_mode ? Qtrue : Qfalse;
118
128
  }
119
129
 
120
130
  static VALUE Packer_buffer(VALUE self)
121
131
  {
122
- PACKER(self, pk);
132
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
133
+ if (!RTEST(pk->buffer_ref)) {
134
+ pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
135
+ }
123
136
  return pk->buffer_ref;
124
137
  }
125
138
 
126
139
  static VALUE Packer_write(VALUE self, VALUE v)
127
140
  {
128
- PACKER(self, pk);
141
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
129
142
  msgpack_packer_write_value(pk, v);
130
143
  return self;
131
144
  }
132
145
 
133
146
  static VALUE Packer_write_nil(VALUE self)
134
147
  {
135
- PACKER(self, pk);
148
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
136
149
  msgpack_packer_write_nil(pk);
137
150
  return self;
138
151
  }
139
152
 
140
153
  static VALUE Packer_write_true(VALUE self)
141
154
  {
142
- PACKER(self, pk);
155
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
143
156
  msgpack_packer_write_true(pk);
144
157
  return self;
145
158
  }
146
159
 
147
160
  static VALUE Packer_write_false(VALUE self)
148
161
  {
149
- PACKER(self, pk);
162
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
150
163
  msgpack_packer_write_false(pk);
151
164
  return self;
152
165
  }
153
166
 
154
167
  static VALUE Packer_write_float(VALUE self, VALUE obj)
155
168
  {
156
- PACKER(self, pk);
169
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
157
170
  msgpack_packer_write_float_value(pk, obj);
158
171
  return self;
159
172
  }
160
173
 
161
174
  static VALUE Packer_write_string(VALUE self, VALUE obj)
162
175
  {
163
- PACKER(self, pk);
176
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
164
177
  Check_Type(obj, T_STRING);
165
178
  msgpack_packer_write_string_value(pk, obj);
166
179
  return self;
@@ -168,7 +181,7 @@ static VALUE Packer_write_string(VALUE self, VALUE obj)
168
181
 
169
182
  static VALUE Packer_write_bin(VALUE self, VALUE obj)
170
183
  {
171
- PACKER(self, pk);
184
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
172
185
  Check_Type(obj, T_STRING);
173
186
 
174
187
  VALUE enc = rb_enc_from_encoding(rb_ascii8bit_encoding());
@@ -180,7 +193,7 @@ static VALUE Packer_write_bin(VALUE self, VALUE obj)
180
193
 
181
194
  static VALUE Packer_write_array(VALUE self, VALUE obj)
182
195
  {
183
- PACKER(self, pk);
196
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
184
197
  Check_Type(obj, T_ARRAY);
185
198
  msgpack_packer_write_array_value(pk, obj);
186
199
  return self;
@@ -188,7 +201,7 @@ static VALUE Packer_write_array(VALUE self, VALUE obj)
188
201
 
189
202
  static VALUE Packer_write_hash(VALUE self, VALUE obj)
190
203
  {
191
- PACKER(self, pk);
204
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
192
205
  Check_Type(obj, T_HASH);
193
206
  msgpack_packer_write_hash_value(pk, obj);
194
207
  return self;
@@ -196,7 +209,7 @@ static VALUE Packer_write_hash(VALUE self, VALUE obj)
196
209
 
197
210
  static VALUE Packer_write_symbol(VALUE self, VALUE obj)
198
211
  {
199
- PACKER(self, pk);
212
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
200
213
  Check_Type(obj, T_SYMBOL);
201
214
  msgpack_packer_write_symbol_value(pk, obj);
202
215
  return self;
@@ -204,7 +217,7 @@ static VALUE Packer_write_symbol(VALUE self, VALUE obj)
204
217
 
205
218
  static VALUE Packer_write_int(VALUE self, VALUE obj)
206
219
  {
207
- PACKER(self, pk);
220
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
208
221
 
209
222
  if (FIXNUM_P(obj)) {
210
223
  msgpack_packer_write_fixnum_value(pk, obj);
@@ -217,10 +230,15 @@ static VALUE Packer_write_int(VALUE self, VALUE obj)
217
230
 
218
231
  static VALUE Packer_write_extension(VALUE self, VALUE obj)
219
232
  {
220
- PACKER(self, pk);
233
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
221
234
  Check_Type(obj, T_STRUCT);
222
235
 
223
- int ext_type = FIX2INT(RSTRUCT_GET(obj, 0));
236
+ VALUE rb_ext_type = RSTRUCT_GET(obj, 0);
237
+ if(!RB_TYPE_P(rb_ext_type, T_FIXNUM)) {
238
+ rb_raise(rb_eRangeError, "integer %s too big to convert to `signed char'", RSTRING_PTR(rb_String(rb_ext_type)));
239
+ }
240
+
241
+ int ext_type = FIX2INT(rb_ext_type);
224
242
  if(ext_type < -128 || ext_type > 127) {
225
243
  rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
226
244
  }
@@ -233,21 +251,21 @@ static VALUE Packer_write_extension(VALUE self, VALUE obj)
233
251
 
234
252
  static VALUE Packer_write_array_header(VALUE self, VALUE n)
235
253
  {
236
- PACKER(self, pk);
254
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
237
255
  msgpack_packer_write_array_header(pk, NUM2UINT(n));
238
256
  return self;
239
257
  }
240
258
 
241
259
  static VALUE Packer_write_map_header(VALUE self, VALUE n)
242
260
  {
243
- PACKER(self, pk);
261
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
244
262
  msgpack_packer_write_map_header(pk, NUM2UINT(n));
245
263
  return self;
246
264
  }
247
265
 
248
266
  static VALUE Packer_write_bin_header(VALUE self, VALUE n)
249
267
  {
250
- PACKER(self, pk);
268
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
251
269
  msgpack_packer_write_bin_header(pk, NUM2UINT(n));
252
270
  return self;
253
271
  }
@@ -258,14 +276,14 @@ static VALUE Packer_write_float32(VALUE self, VALUE numeric)
258
276
  rb_raise(rb_eArgError, "Expected numeric");
259
277
  }
260
278
 
261
- PACKER(self, pk);
279
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
262
280
  msgpack_packer_write_float(pk, (float)rb_num2dbl(numeric));
263
281
  return self;
264
282
  }
265
283
 
266
284
  static VALUE Packer_write_ext(VALUE self, VALUE type, VALUE data)
267
285
  {
268
- PACKER(self, pk);
286
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
269
287
  int ext_type = NUM2INT(type);
270
288
  if(ext_type < -128 || ext_type > 127) {
271
289
  rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
@@ -277,28 +295,28 @@ static VALUE Packer_write_ext(VALUE self, VALUE type, VALUE data)
277
295
 
278
296
  static VALUE Packer_flush(VALUE self)
279
297
  {
280
- PACKER(self, pk);
298
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
281
299
  msgpack_buffer_flush(PACKER_BUFFER_(pk));
282
300
  return self;
283
301
  }
284
302
 
285
303
  static VALUE Packer_reset(VALUE self)
286
304
  {
287
- PACKER(self, pk);
305
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
288
306
  msgpack_buffer_clear(PACKER_BUFFER_(pk));
289
307
  return Qnil;
290
308
  }
291
309
 
292
310
  static VALUE Packer_size(VALUE self)
293
311
  {
294
- PACKER(self, pk);
312
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
295
313
  size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
296
314
  return SIZET2NUM(size);
297
315
  }
298
316
 
299
317
  static VALUE Packer_empty_p(VALUE self)
300
318
  {
301
- PACKER(self, pk);
319
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
302
320
  if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
303
321
  return Qtrue;
304
322
  } else {
@@ -308,80 +326,46 @@ static VALUE Packer_empty_p(VALUE self)
308
326
 
309
327
  static VALUE Packer_to_str(VALUE self)
310
328
  {
311
- PACKER(self, pk);
329
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
312
330
  return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
313
331
  }
314
332
 
315
333
  static VALUE Packer_to_a(VALUE self)
316
334
  {
317
- PACKER(self, pk);
335
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
318
336
  return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
319
337
  }
320
338
 
321
339
  static VALUE Packer_write_to(VALUE self, VALUE io)
322
340
  {
323
- PACKER(self, pk);
341
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
324
342
  size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
325
- return ULONG2NUM(sz);
343
+ return SIZET2NUM(sz);
326
344
  }
327
345
 
328
- //static VALUE Packer_append(VALUE self, VALUE string_or_buffer)
329
- //{
330
- // PACKER(self, pk);
331
- //
332
- // // TODO if string_or_buffer is a Buffer
333
- // VALUE string = string_or_buffer;
334
- //
335
- // msgpack_buffer_append_string(PACKER_BUFFER_(pk), string);
336
- //
337
- // return self;
338
- //}
339
-
340
346
  static VALUE Packer_registered_types_internal(VALUE self)
341
347
  {
342
- PACKER(self, pk);
348
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
343
349
  if (RTEST(pk->ext_registry.hash)) {
344
350
  return rb_hash_dup(pk->ext_registry.hash);
345
351
  }
346
352
  return rb_hash_new();
347
353
  }
348
354
 
349
- static VALUE Packer_register_type(int argc, VALUE* argv, VALUE self)
350
- {
351
- PACKER(self, pk);
352
-
353
- int ext_type;
354
- VALUE ext_module;
355
- VALUE proc;
356
- VALUE arg;
357
-
358
- switch (argc) {
359
- case 2:
360
- /* register_type(0x7f, Time) {|obj| block... } */
361
- rb_need_block();
362
- proc = rb_block_lambda();
363
- arg = proc;
364
- break;
365
- case 3:
366
- /* register_type(0x7f, Time, :to_msgpack_ext) */
367
- arg = argv[2];
368
- proc = rb_funcall(arg, rb_intern("to_proc"), 0);
369
- break;
370
- default:
371
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
355
+ static VALUE Packer_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE ext_module, VALUE proc)
356
+ {
357
+ if (OBJ_FROZEN(self)) {
358
+ rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Packer");
372
359
  }
373
360
 
374
- ext_type = NUM2INT(argv[0]);
361
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
362
+
363
+ int ext_type = NUM2INT(rb_ext_type);
375
364
  if(ext_type < -128 || ext_type > 127) {
376
365
  rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
377
366
  }
378
367
 
379
- ext_module = argv[1];
380
- if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
381
- rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
382
- }
383
-
384
- msgpack_packer_ext_registry_put(&pk->ext_registry, ext_module, ext_type, 0, proc, arg);
368
+ msgpack_packer_ext_registry_put(self, &pk->ext_registry, ext_module, ext_type, 0, proc);
385
369
 
386
370
  if (ext_module == rb_cSymbol) {
387
371
  pk->has_symbol_ext_type = true;
@@ -394,7 +378,7 @@ VALUE Packer_full_pack(VALUE self)
394
378
  {
395
379
  VALUE retval;
396
380
 
397
- PACKER(self, pk);
381
+ msgpack_packer_t *pk = MessagePack_Packer_get(self);
398
382
 
399
383
  if(msgpack_buffer_has_io(PACKER_BUFFER_(pk))) {
400
384
  msgpack_buffer_flush(PACKER_BUFFER_(pk));
@@ -414,10 +398,6 @@ void MessagePack_Packer_module_init(VALUE mMessagePack)
414
398
  s_write = rb_intern("write");
415
399
 
416
400
  sym_compatibility_mode = ID2SYM(rb_intern("compatibility_mode"));
417
-
418
- msgpack_packer_static_init();
419
- msgpack_packer_ext_registry_static_init();
420
-
421
401
  cMessagePack_Packer = rb_define_class_under(mMessagePack, "Packer", rb_cObject);
422
402
 
423
403
  rb_define_alloc_func(cMessagePack_Packer, MessagePack_Packer_alloc);
@@ -454,15 +434,9 @@ void MessagePack_Packer_module_init(VALUE mMessagePack)
454
434
  rb_define_method(cMessagePack_Packer, "to_str", Packer_to_str, 0);
455
435
  rb_define_alias(cMessagePack_Packer, "to_s", "to_str");
456
436
  rb_define_method(cMessagePack_Packer, "to_a", Packer_to_a, 0);
457
- //rb_define_method(cMessagePack_Packer, "append", Packer_append, 1);
458
- //rb_define_alias(cMessagePack_Packer, "<<", "append");
459
437
 
460
438
  rb_define_private_method(cMessagePack_Packer, "registered_types_internal", Packer_registered_types_internal, 0);
461
- rb_define_method(cMessagePack_Packer, "register_type", Packer_register_type, -1);
462
-
463
- //s_packer_value = MessagePack_Packer_alloc(cMessagePack_Packer);
464
- //rb_gc_register_address(&s_packer_value);
465
- //Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
439
+ rb_define_method(cMessagePack_Packer, "register_type_internal", Packer_register_type_internal, 3);
466
440
 
467
441
  rb_define_method(cMessagePack_Packer, "full_pack", Packer_full_pack, 0);
468
442
  }
@@ -22,6 +22,17 @@
22
22
 
23
23
  extern VALUE cMessagePack_Packer;
24
24
 
25
+ extern const rb_data_type_t packer_data_type;
26
+
27
+ static inline msgpack_packer_t *MessagePack_Packer_get(VALUE object) {
28
+ msgpack_packer_t *packer;
29
+ TypedData_Get_Struct(object, msgpack_packer_t, &packer_data_type, packer);
30
+ if (!packer) {
31
+ rb_raise(rb_eArgError, "Uninitialized Packer object");
32
+ }
33
+ return packer;
34
+ }
35
+
25
36
  void MessagePack_Packer_module_init(VALUE mMessagePack);
26
37
 
27
38
  VALUE MessagePack_Packer_alloc(VALUE klass);
@@ -18,20 +18,10 @@
18
18
 
19
19
  #include "packer_ext_registry.h"
20
20
 
21
- static ID s_call;
22
-
23
- void msgpack_packer_ext_registry_static_init()
24
- {
25
- s_call = rb_intern("call");
26
- }
27
-
28
- void msgpack_packer_ext_registry_static_destroy()
29
- { }
30
-
31
- void msgpack_packer_ext_registry_init(msgpack_packer_ext_registry_t* pkrg)
21
+ void msgpack_packer_ext_registry_init(VALUE owner, msgpack_packer_ext_registry_t* pkrg)
32
22
  {
33
- pkrg->hash = Qnil;
34
- pkrg->cache = Qnil;
23
+ RB_OBJ_WRITE(owner, &pkrg->hash, Qnil);
24
+ RB_OBJ_WRITE(owner, &pkrg->cache, Qnil);
35
25
  }
36
26
 
37
27
  void msgpack_packer_ext_registry_mark(msgpack_packer_ext_registry_t* pkrg)
@@ -40,32 +30,45 @@ void msgpack_packer_ext_registry_mark(msgpack_packer_ext_registry_t* pkrg)
40
30
  rb_gc_mark(pkrg->cache);
41
31
  }
42
32
 
43
- void msgpack_packer_ext_registry_dup(msgpack_packer_ext_registry_t* src,
33
+ void msgpack_packer_ext_registry_borrow(VALUE owner, msgpack_packer_ext_registry_t* src,
44
34
  msgpack_packer_ext_registry_t* dst)
45
35
  {
46
- if(RTEST(src->hash) && !rb_obj_frozen_p(src->hash)) {
47
- dst->hash = rb_hash_dup(src->hash);
48
- dst->cache = RTEST(src->cache) ? rb_hash_dup(src->cache) : Qnil;
36
+ if(RTEST(src->hash)) {
37
+ if(rb_obj_frozen_p(src->hash)) {
38
+ // If the type registry is frozen we can safely share it, and share the cache as well.
39
+ RB_OBJ_WRITE(owner, &dst->hash, src->hash);
40
+ RB_OBJ_WRITE(owner, &dst->cache, src->cache);
41
+ } else {
42
+ RB_OBJ_WRITE(owner, &dst->hash, rb_hash_dup(src->hash));
43
+ RB_OBJ_WRITE(owner, &dst->cache, NIL_P(src->cache) ? Qnil : rb_hash_dup(src->cache));
44
+ }
49
45
  } else {
50
- // If the type registry is frozen we can safely share it, and share the cache as well.
51
- dst->hash = src->hash;
52
- dst->cache = src->cache;
46
+ RB_OBJ_WRITE(owner, &dst->hash, Qnil);
47
+ RB_OBJ_WRITE(owner, &dst->cache, Qnil);
53
48
  }
54
49
  }
55
50
 
56
- VALUE msgpack_packer_ext_registry_put(msgpack_packer_ext_registry_t* pkrg,
57
- VALUE ext_module, int ext_type, int flags, VALUE proc, VALUE arg)
51
+ void msgpack_packer_ext_registry_dup(VALUE owner, msgpack_packer_ext_registry_t* src,
52
+ msgpack_packer_ext_registry_t* dst)
58
53
  {
59
- if (!RTEST(pkrg->hash)) {
60
- pkrg->hash = rb_hash_new();
54
+ RB_OBJ_WRITE(owner, &dst->hash, NIL_P(src->hash) ? Qnil : rb_hash_dup(src->hash));
55
+ RB_OBJ_WRITE(owner, &dst->cache, NIL_P(src->cache) ? Qnil : rb_hash_dup(src->cache));
56
+ }
57
+
58
+ void msgpack_packer_ext_registry_put(VALUE owner, msgpack_packer_ext_registry_t* pkrg,
59
+ VALUE ext_module, int ext_type, int flags, VALUE proc)
60
+ {
61
+ if(NIL_P(pkrg->hash)) {
62
+ RB_OBJ_WRITE(owner, &pkrg->hash, rb_hash_new());
61
63
  }
62
64
 
63
- if (RTEST(pkrg->cache)) {
65
+ if(NIL_P(pkrg->cache)) {
66
+ RB_OBJ_WRITE(owner, &pkrg->cache, rb_hash_new());
67
+ } else {
64
68
  /* clear lookup cache not to miss added type */
65
69
  rb_hash_clear(pkrg->cache);
66
70
  }
67
71
 
68
- // TODO: Ruby embeded array limit is 3, merging `proc` and `arg` would be good.
69
- VALUE entry = rb_ary_new3(4, INT2FIX(ext_type), proc, arg, INT2FIX(flags));
70
- return rb_hash_aset(pkrg->hash, ext_module, entry);
72
+ VALUE entry = rb_ary_new3(3, INT2FIX(ext_type), proc, INT2FIX(flags));
73
+ rb_hash_aset(pkrg->hash, ext_module, entry);
71
74
  }
@@ -31,22 +31,21 @@ struct msgpack_packer_ext_registry_t {
31
31
  VALUE cache; // lookup cache for ext types inherited from a super class
32
32
  };
33
33
 
34
- void msgpack_packer_ext_registry_static_init();
35
-
36
- void msgpack_packer_ext_registry_static_destroy();
37
-
38
- void msgpack_packer_ext_registry_init(msgpack_packer_ext_registry_t* pkrg);
34
+ void msgpack_packer_ext_registry_init(VALUE owner, msgpack_packer_ext_registry_t* pkrg);
39
35
 
40
36
  static inline void msgpack_packer_ext_registry_destroy(msgpack_packer_ext_registry_t* pkrg)
41
37
  { }
42
38
 
43
39
  void msgpack_packer_ext_registry_mark(msgpack_packer_ext_registry_t* pkrg);
44
40
 
45
- void msgpack_packer_ext_registry_dup(msgpack_packer_ext_registry_t* src,
41
+ void msgpack_packer_ext_registry_borrow(VALUE owner, msgpack_packer_ext_registry_t* src,
46
42
  msgpack_packer_ext_registry_t* dst);
47
43
 
48
- VALUE msgpack_packer_ext_registry_put(msgpack_packer_ext_registry_t* pkrg,
49
- VALUE ext_module, int ext_type, int flags, VALUE proc, VALUE arg);
44
+ void msgpack_packer_ext_registry_dup(VALUE owner, msgpack_packer_ext_registry_t* src,
45
+ msgpack_packer_ext_registry_t* dst);
46
+
47
+ void msgpack_packer_ext_registry_put(VALUE owner, msgpack_packer_ext_registry_t* pkrg,
48
+ VALUE ext_module, int ext_type, int flags, VALUE proc);
50
49
 
51
50
  static int msgpack_packer_ext_find_superclass(VALUE key, VALUE value, VALUE arg)
52
51
  {
@@ -68,7 +67,7 @@ static inline VALUE msgpack_packer_ext_registry_fetch(msgpack_packer_ext_registr
68
67
  VALUE type = rb_hash_lookup(pkrg->hash, lookup_class);
69
68
  if(type != Qnil) {
70
69
  *ext_type_result = FIX2INT(rb_ary_entry(type, 0));
71
- *ext_flags_result = FIX2INT(rb_ary_entry(type, 3));
70
+ *ext_flags_result = FIX2INT(rb_ary_entry(type, 2));
72
71
  return rb_ary_entry(type, 1);
73
72
  }
74
73
 
@@ -77,7 +76,7 @@ static inline VALUE msgpack_packer_ext_registry_fetch(msgpack_packer_ext_registr
77
76
  VALUE type_inht = rb_hash_lookup(pkrg->cache, lookup_class);
78
77
  if(type_inht != Qnil) {
79
78
  *ext_type_result = FIX2INT(rb_ary_entry(type_inht, 0));
80
- *ext_flags_result = FIX2INT(rb_ary_entry(type_inht, 3));
79
+ *ext_flags_result = FIX2INT(rb_ary_entry(type_inht, 2));
81
80
  return rb_ary_entry(type_inht, 1);
82
81
  }
83
82
  }
@@ -129,12 +128,9 @@ static inline VALUE msgpack_packer_ext_registry_lookup(msgpack_packer_ext_regist
129
128
  VALUE superclass = args[1];
130
129
  if(superclass != Qnil) {
131
130
  VALUE superclass_type = rb_hash_lookup(pkrg->hash, superclass);
132
- if (!RTEST(pkrg->cache)) {
133
- pkrg->cache = rb_hash_new();
134
- }
135
131
  rb_hash_aset(pkrg->cache, lookup_class, superclass_type);
136
132
  *ext_type_result = FIX2INT(rb_ary_entry(superclass_type, 0));
137
- *ext_flags_result = FIX2INT(rb_ary_entry(superclass_type, 3));
133
+ *ext_flags_result = FIX2INT(rb_ary_entry(superclass_type, 2));
138
134
  return rb_ary_entry(superclass_type, 1);
139
135
  }
140
136
 
data/ext/msgpack/rbinit.c CHANGED
@@ -22,7 +22,7 @@
22
22
  #include "factory_class.h"
23
23
  #include "extension_value_class.h"
24
24
 
25
- void Init_msgpack(void)
25
+ RUBY_FUNC_EXPORTED void Init_msgpack(void)
26
26
  {
27
27
  VALUE mMessagePack = rb_define_module("MessagePack");
28
28
 
data/ext/msgpack/rmem.c CHANGED
@@ -65,11 +65,10 @@ void* _msgpack_rmem_alloc2(msgpack_rmem_t* pm)
65
65
  /* allocate new chunk */
66
66
  c = pm->array_last++;
67
67
 
68
- /* move to head */
69
- msgpack_rmem_chunk_t tmp = pm->head;
70
- pm->head = *c;
71
- *c = tmp;
68
+ /* move head to array */
69
+ *c = pm->head;
72
70
 
71
+ pm->head.pages = NULL; /* make sure we don't point to another chunk's pages in case xmalloc triggers GC */
73
72
  pm->head.mask = 0xffffffff & (~1); /* "& (~1)" means first chunk is already allocated */
74
73
  pm->head.pages = xmalloc(MSGPACK_RMEM_PAGE_SIZE * 32);
75
74
 
data/ext/msgpack/sysdep.h CHANGED
@@ -35,8 +35,11 @@
35
35
  # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
36
36
  # else
37
37
  # define _msgpack_be16(x) ( \
38
- ((((uint16_t)x) << 8) ) | \
39
- ((((uint16_t)x) >> 8) ) )
38
+ ( \
39
+ ((((uint16_t)x) << 8) ) | \
40
+ ((((uint16_t)x) >> 8) ) \
41
+ ) \
42
+ & 0x0000FFFF )
40
43
  # endif
41
44
  #else
42
45
  # define _msgpack_be16(x) ntohs(x)