msgpack 0.4.1-mswin32 → 0.4.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/unpack.c CHANGED
@@ -386,90 +386,70 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val)
386
386
  }
387
387
 
388
388
 
389
- #ifdef RUBY_VM
390
- # ifndef STR_SHARED
391
- # define STR_SHARED FL_USER2
392
- # endif
393
- # ifndef STR_NOEMBED
394
- # define STR_NOEMBED FL_USER1
395
- # endif
396
- # ifndef STR_ASSOC
397
- # define STR_ASSOC FL_USER3
398
- # endif
399
- # ifndef STR_NOCAPA_P
400
- # define STR_NOCAPA_P(s) (FL_TEST(s,STR_NOEMBED) && FL_ANY(s,STR_SHARED|STR_ASSOC))
401
- # endif
402
- # define NEED_MORE_CAPA(s,size) (!STR_NOCAPA_P(s) && RSTRING(s)->as.heap.aux.capa < size)
403
- #else
404
- # ifndef STR_NOCAPA
405
- # ifndef STR_ASSOC
406
- # define STR_ASSOC FL_USER3
407
- # endif
408
- # ifndef ELTS_SHARED
409
- # define ELTS_SHARED FL_USER2
410
- # endif
411
- # define STR_NOCAPA (ELTS_SHARED|STR_ASSOC)
412
- # endif
413
- # define NEED_MORE_CAPA(s,size) (!FL_TEST(s,STR_NOCAPA) && RSTRING(s)->aux.capa < size)
414
- #endif
415
-
416
- static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
389
+ static void reserve_buffer(msgpack_unpack_t* mp, size_t require)
417
390
  {
418
391
  struct unpack_buffer* buffer = &mp->user.buffer;
419
392
 
420
393
  if(buffer->size == 0) {
421
- char* tmp = ALLOC_N(char, MSGPACK_UNPACKER_BUFFER_INIT_SIZE);
394
+ size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
395
+ while(nsize < require) {
396
+ nsize *= 2;
397
+ }
398
+ char* tmp = ALLOC_N(char, nsize);
422
399
  buffer->ptr = tmp;
423
- buffer->free = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
400
+ buffer->free = nsize;
424
401
  buffer->size = 0;
402
+ return;
403
+ }
425
404
 
426
- } else if(buffer->size <= mp->user.offset) {
405
+ if(buffer->size <= mp->user.offset) {
427
406
  /* clear buffer and rewind offset */
428
407
  buffer->free += buffer->size;
429
408
  buffer->size = 0;
430
409
  mp->user.offset = 0;
431
410
  }
432
411
 
433
- if(len <= buffer->free) {
434
- /* enough free space: just copy */
435
- memcpy(buffer->ptr+buffer->size, ptr, len);
436
- buffer->size += len;
437
- buffer->free -= len;
412
+ if(require <= buffer->free) {
413
+ /* enough free space */
438
414
  return;
439
415
  }
440
416
 
441
- size_t csize = buffer->size + buffer->free;
417
+ size_t nsize = (buffer->size + buffer->free) * 2;
442
418
 
443
419
  if(mp->user.offset <= buffer->size / 2) {
444
- /* parsed less than half: realloc and copy */
445
- csize *= 2;
446
- while(csize < buffer->size + len) {
447
- csize *= 2;
420
+ /* parsed less than half: realloc only */
421
+ while(nsize < buffer->size + require) {
422
+ nsize *= 2;
448
423
  }
449
- char* tmp = REALLOC_N(buffer->ptr, char, csize);
450
- memcpy(tmp + buffer->size, ptr, len);
424
+ char* tmp = REALLOC_N(buffer->ptr, char, nsize);
425
+ buffer->free = nsize - buffer->size;
451
426
  buffer->ptr = tmp;
452
- buffer->free = csize - buffer->size;
453
- return;
454
- }
455
427
 
456
- size_t not_parsed = buffer->size - mp->user.offset;
457
-
458
- if(csize < not_parsed + len) {
459
- /* more buffer size */
460
- csize *= 2;
461
- while(csize < not_parsed + len) {
462
- csize *= 2;
428
+ } else {
429
+ /* parsed more than half: realloc and move */
430
+ size_t not_parsed = buffer->size - mp->user.offset;
431
+ while(nsize < not_parsed + require) {
432
+ nsize *= 2;
463
433
  }
464
- char* tmp = REALLOC_N(buffer->ptr, char, csize);
434
+ char* tmp = REALLOC_N(buffer->ptr, char, nsize);
435
+ memcpy(tmp, tmp + mp->user.offset, not_parsed);
436
+ buffer->free = nsize - buffer->size;
437
+ buffer->size = not_parsed;
465
438
  buffer->ptr = tmp;
439
+ mp->user.offset = 0;
466
440
  }
441
+ }
467
442
 
468
- memcpy(buffer->ptr+not_parsed, ptr, not_parsed);
469
- buffer->size = not_parsed;
470
- buffer->free = csize - buffer->size;
471
- buffer->ptr = buffer->ptr;
472
- mp->user.offset = 0;
443
+ static inline void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
444
+ {
445
+ struct unpack_buffer* buffer = &mp->user.buffer;
446
+
447
+ if(buffer->free < len) {
448
+ reserve_buffer(mp, len);
449
+ }
450
+ memcpy(buffer->ptr + buffer->size, ptr, len);
451
+ buffer->size += len;
452
+ buffer->free -= len;
473
453
  }
474
454
 
475
455
  /**
data/lib/1.8/msgpack.so CHANGED
Binary file
data/lib/1.9/msgpack.so CHANGED
Binary file
data/test/cases.json ADDED
@@ -0,0 +1 @@
1
+ [false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]]
data/test/cases.mpac ADDED
Binary file
Binary file
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ here = File.dirname(__FILE__)
3
+ require "#{here}/test_helper"
4
+
5
+ begin
6
+ require 'json'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'json'
10
+ end
11
+
12
+ CASES_PATH = "#{here}/cases.mpac"
13
+ CASES_COMPACT_PATH = "#{here}/cases_compact.mpac"
14
+ CASES_JSON_PATH = "#{here}/cases.json"
15
+
16
+ class MessagePackTestCases < Test::Unit::TestCase
17
+ def feed_file(path)
18
+ pac = MessagePack::Unpacker.new
19
+ pac.feed File.read(path)
20
+ pac
21
+ end
22
+
23
+ def test_compare_compact
24
+ pac = feed_file(CASES_PATH)
25
+ cpac = feed_file(CASES_COMPACT_PATH)
26
+
27
+ objs = []; pac.each {| obj| objs << obj }
28
+ cobjs = []; cpac.each {|cobj| cobjs << cobj }
29
+
30
+ objs.zip(cobjs).each {|obj, cobj|
31
+ assert_equal(obj, cobj)
32
+ }
33
+ end
34
+
35
+ def test_compare_json
36
+ pac = feed_file(CASES_PATH)
37
+
38
+ objs = []; pac.each {|obj| objs << obj }
39
+ jobjs = JSON.load File.read(CASES_JSON_PATH)
40
+
41
+ objs.zip(jobjs) {|obj, jobj|
42
+ assert_equal(obj, jobj)
43
+ }
44
+ end
45
+ end
46
+
data/test/test_helper.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  require 'test/unit'
2
+ begin
3
+ require File.dirname(__FILE__) + '/../msgpack'
4
+ rescue LoadError
2
5
  require File.dirname(__FILE__) + '/../lib/msgpack'
6
+ end
3
7
 
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + '/test_helper.rb'
3
- require 'test/unit'
2
+ require File.dirname(__FILE__)+'/test_helper'
4
3
 
5
- class MessagePackTestFormat < Test::Unit::TestCase
4
+ class MessagePackTestPackUnpack < Test::Unit::TestCase
6
5
  def self.it(name, &block)
7
6
  define_method("test_#{name}", &block)
8
7
  end
@@ -177,16 +176,18 @@ class MessagePackTestFormat < Test::Unit::TestCase
177
176
  match ({}), "\x80"
178
177
  end
179
178
 
180
- it "{0=>0, 1=>1, ..., 14=>14}" do
181
- a = (0..14).to_a;
182
- match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
183
- end
184
-
185
- it "{0=>0, 1=>1, ..., 15=>15}" do
186
- a = (0..15).to_a;
187
- match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
188
- end
179
+ ## FIXME
180
+ # it "{0=>0, 1=>1, ..., 14=>14}" do
181
+ # a = (0..14).to_a;
182
+ # match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
183
+ # end
184
+ #
185
+ # it "{0=>0, 1=>1, ..., 15=>15}" do
186
+ # a = (0..15).to_a;
187
+ # match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
188
+ # end
189
189
 
190
+ ## FIXME
190
191
  # it "fixmap" do
191
192
  # check_map 1, 0
192
193
  # check_map 1, (1<<4)-1
@@ -202,6 +203,37 @@ class MessagePackTestFormat < Test::Unit::TestCase
202
203
  # #check_map 5, (1<<32)-1 # memory error
203
204
  # end
204
205
 
206
+ it "buffer" do
207
+ str = "a"*32*1024*4
208
+ raw = str.to_msgpack
209
+ pac = MessagePack::Unpacker.new
210
+
211
+ len = 0
212
+ parsed = false
213
+
214
+ n = 655
215
+ time = raw.size / n
216
+ time += 1 unless raw.size % n == 0
217
+ off = 0
218
+
219
+ time.times do
220
+ assert(!parsed)
221
+
222
+ fe = raw[off, n]
223
+ assert(fe.length > 0)
224
+ off += fe.length
225
+
226
+ pac.feed fe
227
+ pac.each {|obj|
228
+ assert(!parsed)
229
+ assert_equal(obj, str)
230
+ parsed = true
231
+ }
232
+ end
233
+
234
+ assert(parsed)
235
+ end
236
+
205
237
  it "gc mark" do
206
238
  obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"]
207
239
  num = 4
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  segments:
5
5
  - 0
6
6
  - 4
7
- - 1
8
- version: 0.4.1
7
+ - 2
8
+ version: 0.4.2
9
9
  platform: mswin32
10
10
  authors:
11
11
  - FURUHASHI Sadayuki
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-05-27 00:00:00 +09:00
16
+ date: 2010-06-03 00:00:00 +09:00
17
17
  default_executable:
18
18
  dependencies: []
19
19
 
@@ -40,8 +40,12 @@ files:
40
40
  - msgpack/sysdep.h
41
41
  - msgpack/unpack_define.h
42
42
  - msgpack/unpack_template.h
43
- - test/msgpack_test.rb
43
+ - test/cases.json
44
+ - test/cases.mpac
45
+ - test/cases_compact.mpac
46
+ - test/test_cases.rb
44
47
  - test/test_helper.rb
48
+ - test/test_pack_unpack.rb
45
49
  has_rdoc: true
46
50
  homepage: http://msgpack.sourceforge.net/
47
51
  licenses: []
@@ -75,4 +79,6 @@ signing_key:
75
79
  specification_version: 3
76
80
  summary: MessagePack, a binary-based efficient data interchange format.
77
81
  test_files:
82
+ - test/test_cases.rb
78
83
  - test/test_helper.rb
84
+ - test/test_pack_unpack.rb