msgpack 0.6.0pre1-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.travis.yml +26 -0
  4. data/ChangeLog +117 -0
  5. data/Dockerfile +30 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +177 -0
  8. data/README.rdoc +129 -0
  9. data/Rakefile +114 -0
  10. data/bench/pack.rb +23 -0
  11. data/bench/pack_log.rb +33 -0
  12. data/bench/pack_log_long.rb +65 -0
  13. data/bench/run.sh +14 -0
  14. data/bench/run_long.sh +35 -0
  15. data/bench/unpack.rb +21 -0
  16. data/bench/unpack_log.rb +34 -0
  17. data/bench/unpack_log_long.rb +67 -0
  18. data/cross-build.sh +9 -0
  19. data/doclib/msgpack/buffer.rb +193 -0
  20. data/doclib/msgpack/core_ext.rb +101 -0
  21. data/doclib/msgpack/error.rb +14 -0
  22. data/doclib/msgpack/packer.rb +134 -0
  23. data/doclib/msgpack/unpacker.rb +146 -0
  24. data/doclib/msgpack.rb +77 -0
  25. data/ext/java/org/msgpack/jruby/Buffer.java +221 -0
  26. data/ext/java/org/msgpack/jruby/Decoder.java +201 -0
  27. data/ext/java/org/msgpack/jruby/Encoder.java +308 -0
  28. data/ext/java/org/msgpack/jruby/ExtensionValue.java +136 -0
  29. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +107 -0
  30. data/ext/java/org/msgpack/jruby/Packer.java +78 -0
  31. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  32. data/ext/java/org/msgpack/jruby/Unpacker.java +170 -0
  33. data/ext/msgpack/buffer.c +695 -0
  34. data/ext/msgpack/buffer.h +447 -0
  35. data/ext/msgpack/buffer_class.c +507 -0
  36. data/ext/msgpack/buffer_class.h +32 -0
  37. data/ext/msgpack/compat.h +113 -0
  38. data/ext/msgpack/core_ext.c +129 -0
  39. data/ext/msgpack/core_ext.h +26 -0
  40. data/ext/msgpack/extconf.rb +28 -0
  41. data/ext/msgpack/packer.c +168 -0
  42. data/ext/msgpack/packer.h +441 -0
  43. data/ext/msgpack/packer_class.c +302 -0
  44. data/ext/msgpack/packer_class.h +30 -0
  45. data/ext/msgpack/rbinit.c +33 -0
  46. data/ext/msgpack/rmem.c +94 -0
  47. data/ext/msgpack/rmem.h +109 -0
  48. data/ext/msgpack/sysdep.h +115 -0
  49. data/ext/msgpack/sysdep_endian.h +50 -0
  50. data/ext/msgpack/sysdep_types.h +46 -0
  51. data/ext/msgpack/unpacker.c +771 -0
  52. data/ext/msgpack/unpacker.h +122 -0
  53. data/ext/msgpack/unpacker_class.c +405 -0
  54. data/ext/msgpack/unpacker_class.h +32 -0
  55. data/lib/msgpack/msgpack.so +0 -0
  56. data/lib/msgpack/version.rb +3 -0
  57. data/lib/msgpack.rb +13 -0
  58. data/msgpack.gemspec +31 -0
  59. data/msgpack.org.md +46 -0
  60. data/spec/cases.json +1 -0
  61. data/spec/cases.msg +0 -0
  62. data/spec/cases_compact.msg +0 -0
  63. data/spec/cases_spec.rb +39 -0
  64. data/spec/cruby/buffer_io_spec.rb +256 -0
  65. data/spec/cruby/buffer_packer.rb +29 -0
  66. data/spec/cruby/buffer_spec.rb +572 -0
  67. data/spec/cruby/buffer_unpacker.rb +19 -0
  68. data/spec/cruby/packer_spec.rb +120 -0
  69. data/spec/cruby/unpacker_spec.rb +305 -0
  70. data/spec/format_spec.rb +282 -0
  71. data/spec/jruby/benchmarks/shootout_bm.rb +73 -0
  72. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +25 -0
  73. data/spec/jruby/msgpack/unpacker_spec.rb +290 -0
  74. data/spec/jruby/msgpack_spec.rb +142 -0
  75. data/spec/pack_spec.rb +67 -0
  76. data/spec/random_compat.rb +24 -0
  77. data/spec/spec_helper.rb +27 -0
  78. data/spec/unpack_spec.rb +60 -0
  79. metadata +209 -0
@@ -0,0 +1,447 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_RUBY_BUFFER_H__
19
+ #define MSGPACK_RUBY_BUFFER_H__
20
+
21
+ #include "compat.h"
22
+ #include "sysdep.h"
23
+
24
+ #ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT
25
+ #define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT (512*1024)
26
+ #endif
27
+
28
+ /* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
29
+ #ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM
30
+ #define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM 256
31
+ #endif
32
+
33
+ #ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT
34
+ #define MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT 256
35
+ #endif
36
+
37
+ /* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
38
+ #ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM
39
+ #define MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM 256
40
+ #endif
41
+
42
+ #ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT
43
+ #define MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT (32*1024)
44
+ #endif
45
+
46
+ #ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM
47
+ #define MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM (1024)
48
+ #endif
49
+
50
+ #define NO_MAPPED_STRING ((VALUE)0)
51
+
52
+ #ifdef COMPAT_HAVE_ENCODING /* see compat.h*/
53
+ extern int msgpack_rb_encindex_utf8;
54
+ extern int msgpack_rb_encindex_usascii;
55
+ extern int msgpack_rb_encindex_ascii8bit;
56
+ #endif
57
+
58
+ struct msgpack_buffer_chunk_t;
59
+ typedef struct msgpack_buffer_chunk_t msgpack_buffer_chunk_t;
60
+
61
+ struct msgpack_buffer_t;
62
+ typedef struct msgpack_buffer_t msgpack_buffer_t;
63
+
64
+ /*
65
+ * msgpack_buffer_chunk_t
66
+ * +----------------+
67
+ * | filled | free |
68
+ * +---------+------+
69
+ * ^ first ^ last
70
+ */
71
+ struct msgpack_buffer_chunk_t {
72
+ char* first;
73
+ char* last;
74
+ void* mem;
75
+ msgpack_buffer_chunk_t* next;
76
+ VALUE mapped_string; /* RBString or NO_MAPPED_STRING */
77
+ };
78
+
79
+ union msgpack_buffer_cast_block_t {
80
+ char buffer[8];
81
+ uint8_t u8;
82
+ uint16_t u16;
83
+ uint32_t u32;
84
+ uint64_t u64;
85
+ int8_t i8;
86
+ int16_t i16;
87
+ int32_t i32;
88
+ int64_t i64;
89
+ float f;
90
+ double d;
91
+ };
92
+
93
+ struct msgpack_buffer_t {
94
+ char* read_buffer;
95
+ char* tail_buffer_end;
96
+
97
+ msgpack_buffer_chunk_t tail;
98
+ msgpack_buffer_chunk_t* head;
99
+ msgpack_buffer_chunk_t* free_list;
100
+
101
+ #ifndef DISABLE_RMEM
102
+ char* rmem_last;
103
+ char* rmem_end;
104
+ void** rmem_owner;
105
+ #endif
106
+
107
+ union msgpack_buffer_cast_block_t cast_block;
108
+
109
+ VALUE io;
110
+ VALUE io_buffer;
111
+ ID io_write_all_method;
112
+ ID io_partial_read_method;
113
+
114
+ size_t write_reference_threshold;
115
+ size_t read_reference_threshold;
116
+ size_t io_buffer_size;
117
+
118
+ VALUE owner;
119
+ };
120
+
121
+ /*
122
+ * initialization functions
123
+ */
124
+ void msgpack_buffer_static_init();
125
+
126
+ void msgpack_buffer_static_destroy();
127
+
128
+ void msgpack_buffer_init(msgpack_buffer_t* b);
129
+
130
+ void msgpack_buffer_destroy(msgpack_buffer_t* b);
131
+
132
+ void msgpack_buffer_mark(msgpack_buffer_t* b);
133
+
134
+ void msgpack_buffer_clear(msgpack_buffer_t* b);
135
+
136
+ static inline void msgpack_buffer_set_write_reference_threshold(msgpack_buffer_t* b, size_t length)
137
+ {
138
+ if(length < MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM) {
139
+ length = MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM;
140
+ }
141
+ b->write_reference_threshold = length;
142
+ }
143
+
144
+ static inline void msgpack_buffer_set_read_reference_threshold(msgpack_buffer_t* b, size_t length)
145
+ {
146
+ if(length < MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM) {
147
+ length = MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM;
148
+ }
149
+ b->read_reference_threshold = length;
150
+ }
151
+
152
+ static inline void msgpack_buffer_set_io_buffer_size(msgpack_buffer_t* b, size_t length)
153
+ {
154
+ if(length < MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM) {
155
+ length = MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM;
156
+ }
157
+ b->io_buffer_size = length;
158
+ }
159
+
160
+ static inline void msgpack_buffer_reset_io(msgpack_buffer_t* b)
161
+ {
162
+ b->io = Qnil;
163
+ }
164
+
165
+ static inline bool msgpack_buffer_has_io(msgpack_buffer_t* b)
166
+ {
167
+ return b->io != Qnil;
168
+ }
169
+
170
+ static inline void msgpack_buffer_reset(msgpack_buffer_t* b)
171
+ {
172
+ msgpack_buffer_clear(b);
173
+ msgpack_buffer_reset_io(b);
174
+ }
175
+
176
+
177
+ /*
178
+ * writer functions
179
+ */
180
+
181
+ static inline size_t msgpack_buffer_writable_size(const msgpack_buffer_t* b)
182
+ {
183
+ return b->tail_buffer_end - b->tail.last;
184
+ }
185
+
186
+ static inline void msgpack_buffer_write_1(msgpack_buffer_t* b, int byte)
187
+ {
188
+ (*b->tail.last++) = (char) byte;
189
+ }
190
+
191
+ static inline void msgpack_buffer_write_2(msgpack_buffer_t* b, int byte1, unsigned char byte2)
192
+ {
193
+ *(b->tail.last++) = (char) byte1;
194
+ *(b->tail.last++) = (char) byte2;
195
+ }
196
+
197
+ static inline void msgpack_buffer_write_byte_and_data(msgpack_buffer_t* b, int byte, const void* data, size_t length)
198
+ {
199
+ (*b->tail.last++) = (char) byte;
200
+
201
+ memcpy(b->tail.last, data, length);
202
+ b->tail.last += length;
203
+ }
204
+
205
+ void _msgpack_buffer_expand(msgpack_buffer_t* b, const char* data, size_t length, bool use_flush);
206
+
207
+ size_t msgpack_buffer_flush_to_io(msgpack_buffer_t* b, VALUE io, ID write_method, bool consume);
208
+
209
+ static inline size_t msgpack_buffer_flush(msgpack_buffer_t* b)
210
+ {
211
+ if(b->io == Qnil) {
212
+ return 0;
213
+ }
214
+ return msgpack_buffer_flush_to_io(b, b->io, b->io_write_all_method, true);
215
+ }
216
+
217
+ static inline void msgpack_buffer_ensure_writable(msgpack_buffer_t* b, size_t require)
218
+ {
219
+ if(msgpack_buffer_writable_size(b) < require) {
220
+ _msgpack_buffer_expand(b, NULL, require, true);
221
+ }
222
+ }
223
+
224
+ static inline void _msgpack_buffer_append_impl(msgpack_buffer_t* b, const char* data, size_t length, bool flush_to_io)
225
+ {
226
+ if(length == 0) {
227
+ return;
228
+ }
229
+
230
+ if(length <= msgpack_buffer_writable_size(b)) {
231
+ memcpy(b->tail.last, data, length);
232
+ b->tail.last += length;
233
+ return;
234
+ }
235
+
236
+ _msgpack_buffer_expand(b, data, length, flush_to_io);
237
+ }
238
+
239
+ static inline void msgpack_buffer_append(msgpack_buffer_t* b, const char* data, size_t length)
240
+ {
241
+ _msgpack_buffer_append_impl(b, data, length, true);
242
+ }
243
+
244
+ static inline void msgpack_buffer_append_nonblock(msgpack_buffer_t* b, const char* data, size_t length)
245
+ {
246
+ _msgpack_buffer_append_impl(b, data, length, false);
247
+ }
248
+
249
+ void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string);
250
+
251
+ static inline size_t msgpack_buffer_append_string(msgpack_buffer_t* b, VALUE string)
252
+ {
253
+ size_t length = RSTRING_LEN(string);
254
+
255
+ if(length > b->write_reference_threshold) {
256
+ _msgpack_buffer_append_long_string(b, string);
257
+
258
+ } else {
259
+ msgpack_buffer_append(b, RSTRING_PTR(string), length);
260
+ }
261
+
262
+ return length;
263
+ }
264
+
265
+
266
+ /*
267
+ * IO functions
268
+ */
269
+ size_t _msgpack_buffer_feed_from_io(msgpack_buffer_t* b);
270
+
271
+ size_t _msgpack_buffer_read_from_io_to_string(msgpack_buffer_t* b, VALUE string, size_t length);
272
+
273
+ size_t _msgpack_buffer_skip_from_io(msgpack_buffer_t* b, size_t length);
274
+
275
+
276
+ /*
277
+ * reader functions
278
+ */
279
+
280
+ static inline size_t msgpack_buffer_top_readable_size(const msgpack_buffer_t* b)
281
+ {
282
+ return b->head->last - b->read_buffer;
283
+ }
284
+
285
+ size_t msgpack_buffer_all_readable_size(const msgpack_buffer_t* b);
286
+
287
+ bool _msgpack_buffer_shift_chunk(msgpack_buffer_t* b);
288
+
289
+ static inline void _msgpack_buffer_consumed(msgpack_buffer_t* b, size_t length)
290
+ {
291
+ b->read_buffer += length;
292
+ if(b->read_buffer >= b->head->last) {
293
+ _msgpack_buffer_shift_chunk(b);
294
+ }
295
+ }
296
+
297
+ static inline int msgpack_buffer_peek_top_1(msgpack_buffer_t* b)
298
+ {
299
+ return (int) (unsigned char) b->read_buffer[0];
300
+ }
301
+
302
+ static inline int msgpack_buffer_read_top_1(msgpack_buffer_t* b)
303
+ {
304
+ int r = (int) (unsigned char) b->read_buffer[0];
305
+
306
+ _msgpack_buffer_consumed(b, 1);
307
+
308
+ return r;
309
+ }
310
+
311
+ static inline int msgpack_buffer_read_1(msgpack_buffer_t* b)
312
+ {
313
+ if(msgpack_buffer_top_readable_size(b) <= 0) {
314
+ if(b->io == Qnil) {
315
+ return -1;
316
+ }
317
+ _msgpack_buffer_feed_from_io(b);
318
+ }
319
+
320
+ int r = (int) (unsigned char) b->read_buffer[0];
321
+ _msgpack_buffer_consumed(b, 1);
322
+
323
+ return r;
324
+ }
325
+
326
+
327
+ /*
328
+ * bulk read / skip functions
329
+ */
330
+
331
+ size_t msgpack_buffer_read_nonblock(msgpack_buffer_t* b, char* buffer, size_t length);
332
+
333
+ static inline bool msgpack_buffer_ensure_readable(msgpack_buffer_t* b, size_t require)
334
+ {
335
+ if(msgpack_buffer_top_readable_size(b) < require) {
336
+ size_t sz = msgpack_buffer_all_readable_size(b);
337
+ if(sz < require) {
338
+ if(b->io == Qnil) {
339
+ return false;
340
+ }
341
+ do {
342
+ size_t rl = _msgpack_buffer_feed_from_io(b);
343
+ sz += rl;
344
+ } while(sz < require);
345
+ }
346
+ }
347
+ return true;
348
+ }
349
+
350
+ bool _msgpack_buffer_read_all2(msgpack_buffer_t* b, char* buffer, size_t length);
351
+
352
+ static inline bool msgpack_buffer_read_all(msgpack_buffer_t* b, char* buffer, size_t length)
353
+ {
354
+ size_t avail = msgpack_buffer_top_readable_size(b);
355
+ if(avail < length) {
356
+ return _msgpack_buffer_read_all2(b, buffer, length);
357
+ }
358
+
359
+ memcpy(buffer, b->read_buffer, length);
360
+ _msgpack_buffer_consumed(b, length);
361
+ return true;
362
+ }
363
+
364
+ static inline size_t msgpack_buffer_skip_nonblock(msgpack_buffer_t* b, size_t length)
365
+ {
366
+ size_t avail = msgpack_buffer_top_readable_size(b);
367
+ if(avail < length) {
368
+ return msgpack_buffer_read_nonblock(b, NULL, length);
369
+ }
370
+ _msgpack_buffer_consumed(b, length);
371
+ return length;
372
+ }
373
+
374
+ static inline union msgpack_buffer_cast_block_t* msgpack_buffer_read_cast_block(msgpack_buffer_t* b, size_t n)
375
+ {
376
+ if(!msgpack_buffer_read_all(b, b->cast_block.buffer, n)) {
377
+ return NULL;
378
+ }
379
+ return &b->cast_block;
380
+ }
381
+
382
+ size_t msgpack_buffer_read_to_string_nonblock(msgpack_buffer_t* b, VALUE string, size_t length);
383
+
384
+ static inline size_t msgpack_buffer_read_to_string(msgpack_buffer_t* b, VALUE string, size_t length)
385
+ {
386
+ if(length == 0) {
387
+ return 0;
388
+ }
389
+
390
+ size_t avail = msgpack_buffer_top_readable_size(b);
391
+ if(avail > 0) {
392
+ return msgpack_buffer_read_to_string_nonblock(b, string, length);
393
+ } else if(b->io != Qnil) {
394
+ return _msgpack_buffer_read_from_io_to_string(b, string, length);
395
+ } else {
396
+ return 0;
397
+ }
398
+ }
399
+
400
+ static inline size_t msgpack_buffer_skip(msgpack_buffer_t* b, size_t length)
401
+ {
402
+ if(length == 0) {
403
+ return 0;
404
+ }
405
+
406
+ size_t avail = msgpack_buffer_top_readable_size(b);
407
+ if(avail > 0) {
408
+ return msgpack_buffer_skip_nonblock(b, length);
409
+ } else if(b->io != Qnil) {
410
+ return _msgpack_buffer_skip_from_io(b, length);
411
+ } else {
412
+ return 0;
413
+ }
414
+ }
415
+
416
+
417
+ VALUE msgpack_buffer_all_as_string(msgpack_buffer_t* b);
418
+
419
+ VALUE msgpack_buffer_all_as_string_array(msgpack_buffer_t* b);
420
+
421
+ static inline VALUE _msgpack_buffer_refer_head_mapped_string(msgpack_buffer_t* b, size_t length)
422
+ {
423
+ size_t offset = b->read_buffer - b->head->first;
424
+ return rb_str_substr(b->head->mapped_string, offset, length);
425
+ }
426
+
427
+ static inline VALUE msgpack_buffer_read_top_as_string(msgpack_buffer_t* b, size_t length, bool will_be_frozen)
428
+ {
429
+ #ifndef DISABLE_BUFFER_READ_REFERENCE_OPTIMIZE
430
+ /* optimize */
431
+ if(!will_be_frozen &&
432
+ b->head->mapped_string != NO_MAPPED_STRING &&
433
+ length >= b->read_reference_threshold) {
434
+ VALUE result = _msgpack_buffer_refer_head_mapped_string(b, length);
435
+ _msgpack_buffer_consumed(b, length);
436
+ return result;
437
+ }
438
+ #endif
439
+
440
+ VALUE result = rb_str_new(b->read_buffer, length);
441
+ _msgpack_buffer_consumed(b, length);
442
+ return result;
443
+ }
444
+
445
+
446
+ #endif
447
+