ed-precompiled_msgpack 1.8.0-arm64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
 - data/ChangeLog +368 -0
 - data/LICENSE +177 -0
 - data/README.md +302 -0
 - data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
 - data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
 - data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
 - data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
 - data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
 - data/ext/java/org/msgpack/jruby/Factory.java +130 -0
 - data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
 - data/ext/java/org/msgpack/jruby/Packer.java +266 -0
 - data/ext/java/org/msgpack/jruby/Types.java +37 -0
 - data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
 - data/ext/msgpack/buffer.c +669 -0
 - data/ext/msgpack/buffer.h +604 -0
 - data/ext/msgpack/buffer_class.c +616 -0
 - data/ext/msgpack/buffer_class.h +33 -0
 - data/ext/msgpack/compat.h +26 -0
 - data/ext/msgpack/extconf.rb +53 -0
 - data/ext/msgpack/extension_value_class.c +34 -0
 - data/ext/msgpack/extension_value_class.h +31 -0
 - data/ext/msgpack/factory_class.c +276 -0
 - data/ext/msgpack/factory_class.h +33 -0
 - data/ext/msgpack/packer.c +199 -0
 - data/ext/msgpack/packer.h +513 -0
 - data/ext/msgpack/packer_class.c +442 -0
 - data/ext/msgpack/packer_class.h +43 -0
 - data/ext/msgpack/packer_ext_registry.c +74 -0
 - data/ext/msgpack/packer_ext_registry.h +140 -0
 - data/ext/msgpack/rbinit.c +35 -0
 - data/ext/msgpack/rmem.c +93 -0
 - data/ext/msgpack/rmem.h +109 -0
 - data/ext/msgpack/sysdep.h +118 -0
 - data/ext/msgpack/sysdep_endian.h +50 -0
 - data/ext/msgpack/sysdep_types.h +46 -0
 - data/ext/msgpack/unpacker.c +986 -0
 - data/ext/msgpack/unpacker.h +152 -0
 - data/ext/msgpack/unpacker_class.c +447 -0
 - data/ext/msgpack/unpacker_class.h +43 -0
 - data/ext/msgpack/unpacker_ext_registry.c +74 -0
 - data/ext/msgpack/unpacker_ext_registry.h +62 -0
 - data/lib/msgpack/3.0/msgpack.bundle +0 -0
 - data/lib/msgpack/3.1/msgpack.bundle +0 -0
 - data/lib/msgpack/3.2/msgpack.bundle +0 -0
 - data/lib/msgpack/3.3/msgpack.bundle +0 -0
 - data/lib/msgpack/3.4/msgpack.bundle +0 -0
 - data/lib/msgpack/bigint.rb +69 -0
 - data/lib/msgpack/buffer.rb +9 -0
 - data/lib/msgpack/core_ext.rb +139 -0
 - data/lib/msgpack/factory.rb +211 -0
 - data/lib/msgpack/packer.rb +37 -0
 - data/lib/msgpack/symbol.rb +26 -0
 - data/lib/msgpack/time.rb +29 -0
 - data/lib/msgpack/timestamp.rb +76 -0
 - data/lib/msgpack/unpacker.rb +41 -0
 - data/lib/msgpack/version.rb +6 -0
 - data/lib/msgpack.rb +53 -0
 - data/msgpack.gemspec +41 -0
 - metadata +223 -0
 
| 
         @@ -0,0 +1,669 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            #include "buffer.h"
         
     | 
| 
      
 20 
     | 
    
         
            +
            #include "rmem.h"
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            int msgpack_rb_encindex_utf8;
         
     | 
| 
      
 23 
     | 
    
         
            +
            int msgpack_rb_encindex_usascii;
         
     | 
| 
      
 24 
     | 
    
         
            +
            int msgpack_rb_encindex_ascii8bit;
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            ID s_uminus;
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            static msgpack_rmem_t s_rmem;
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            void msgpack_buffer_static_init(void)
         
     | 
| 
      
 31 
     | 
    
         
            +
            {
         
     | 
| 
      
 32 
     | 
    
         
            +
                s_uminus = rb_intern("-@");
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                msgpack_rb_encindex_utf8 = rb_utf8_encindex();
         
     | 
| 
      
 35 
     | 
    
         
            +
                msgpack_rb_encindex_usascii = rb_usascii_encindex();
         
     | 
| 
      
 36 
     | 
    
         
            +
                msgpack_rb_encindex_ascii8bit = rb_ascii8bit_encindex();
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                msgpack_rmem_init(&s_rmem);
         
     | 
| 
      
 39 
     | 
    
         
            +
            }
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            void msgpack_buffer_static_destroy(void)
         
     | 
| 
      
 42 
     | 
    
         
            +
            {
         
     | 
| 
      
 43 
     | 
    
         
            +
                msgpack_rmem_destroy(&s_rmem);
         
     | 
| 
      
 44 
     | 
    
         
            +
            }
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            void msgpack_buffer_init(msgpack_buffer_t* b)
         
     | 
| 
      
 47 
     | 
    
         
            +
            {
         
     | 
| 
      
 48 
     | 
    
         
            +
                memset(b, 0, sizeof(msgpack_buffer_t));
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                b->head = &b->tail;
         
     | 
| 
      
 51 
     | 
    
         
            +
                b->write_reference_threshold = MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT;
         
     | 
| 
      
 52 
     | 
    
         
            +
                b->read_reference_threshold = MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT;
         
     | 
| 
      
 53 
     | 
    
         
            +
                b->io_buffer_size = MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT;
         
     | 
| 
      
 54 
     | 
    
         
            +
                b->io = Qnil;
         
     | 
| 
      
 55 
     | 
    
         
            +
                b->io_buffer = Qnil;
         
     | 
| 
      
 56 
     | 
    
         
            +
            }
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
            static void _msgpack_buffer_chunk_destroy(msgpack_buffer_chunk_t* c)
         
     | 
| 
      
 59 
     | 
    
         
            +
            {
         
     | 
| 
      
 60 
     | 
    
         
            +
                if(c->mem != NULL) {
         
     | 
| 
      
 61 
     | 
    
         
            +
                    if(c->rmem) {
         
     | 
| 
      
 62 
     | 
    
         
            +
                        if(!msgpack_rmem_free(&s_rmem, c->mem)) {
         
     | 
| 
      
 63 
     | 
    
         
            +
                            rb_bug("Failed to free an rmem pointer, memory leak?");
         
     | 
| 
      
 64 
     | 
    
         
            +
                        }
         
     | 
| 
      
 65 
     | 
    
         
            +
                    } else {
         
     | 
| 
      
 66 
     | 
    
         
            +
                        xfree(c->mem);
         
     | 
| 
      
 67 
     | 
    
         
            +
                    }
         
     | 
| 
      
 68 
     | 
    
         
            +
                    /* no needs to update rmem_owner because chunks will not be
         
     | 
| 
      
 69 
     | 
    
         
            +
                     * free()ed (left in free_list) and thus *rmem_owner is
         
     | 
| 
      
 70 
     | 
    
         
            +
                     * always valid. */
         
     | 
| 
      
 71 
     | 
    
         
            +
                }
         
     | 
| 
      
 72 
     | 
    
         
            +
                c->first = NULL;
         
     | 
| 
      
 73 
     | 
    
         
            +
                c->last = NULL;
         
     | 
| 
      
 74 
     | 
    
         
            +
                c->mem = NULL;
         
     | 
| 
      
 75 
     | 
    
         
            +
            }
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
            void msgpack_buffer_destroy(msgpack_buffer_t* b)
         
     | 
| 
      
 78 
     | 
    
         
            +
            {
         
     | 
| 
      
 79 
     | 
    
         
            +
                /* head is always available */
         
     | 
| 
      
 80 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head;
         
     | 
| 
      
 81 
     | 
    
         
            +
                while(c != &b->tail) {
         
     | 
| 
      
 82 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* n = c->next;
         
     | 
| 
      
 83 
     | 
    
         
            +
                    _msgpack_buffer_chunk_destroy(c);
         
     | 
| 
      
 84 
     | 
    
         
            +
                    xfree(c);
         
     | 
| 
      
 85 
     | 
    
         
            +
                    c = n;
         
     | 
| 
      
 86 
     | 
    
         
            +
                }
         
     | 
| 
      
 87 
     | 
    
         
            +
                _msgpack_buffer_chunk_destroy(c);
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                c = b->free_list;
         
     | 
| 
      
 90 
     | 
    
         
            +
                while(c != NULL) {
         
     | 
| 
      
 91 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* n = c->next;
         
     | 
| 
      
 92 
     | 
    
         
            +
                    xfree(c);
         
     | 
| 
      
 93 
     | 
    
         
            +
                    c = n;
         
     | 
| 
      
 94 
     | 
    
         
            +
                }
         
     | 
| 
      
 95 
     | 
    
         
            +
            }
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
            size_t msgpack_buffer_memsize(const msgpack_buffer_t* b)
         
     | 
| 
      
 98 
     | 
    
         
            +
            {
         
     | 
| 
      
 99 
     | 
    
         
            +
                size_t memsize = 0;
         
     | 
| 
      
 100 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head;
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                while(c) {
         
     | 
| 
      
 103 
     | 
    
         
            +
                    memsize += sizeof(msgpack_buffer_chunk_t);
         
     | 
| 
      
 104 
     | 
    
         
            +
                    if(c->mapped_string != NO_MAPPED_STRING) {
         
     | 
| 
      
 105 
     | 
    
         
            +
                        memsize += (c->last - c->first);
         
     | 
| 
      
 106 
     | 
    
         
            +
                    }
         
     | 
| 
      
 107 
     | 
    
         
            +
                    c = c->next;
         
     | 
| 
      
 108 
     | 
    
         
            +
                }
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                return memsize;
         
     | 
| 
      
 111 
     | 
    
         
            +
            }
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
            void msgpack_buffer_mark(void *ptr)
         
     | 
| 
      
 114 
     | 
    
         
            +
            {
         
     | 
| 
      
 115 
     | 
    
         
            +
                msgpack_buffer_t* b = ptr;
         
     | 
| 
      
 116 
     | 
    
         
            +
                /* head is always available */
         
     | 
| 
      
 117 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head;
         
     | 
| 
      
 118 
     | 
    
         
            +
                while(c != &b->tail) {
         
     | 
| 
      
 119 
     | 
    
         
            +
                    rb_gc_mark(c->mapped_string);
         
     | 
| 
      
 120 
     | 
    
         
            +
                    c = c->next;
         
     | 
| 
      
 121 
     | 
    
         
            +
                }
         
     | 
| 
      
 122 
     | 
    
         
            +
                rb_gc_mark(c->mapped_string);
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
                rb_gc_mark(b->io);
         
     | 
| 
      
 125 
     | 
    
         
            +
                rb_gc_mark(b->io_buffer);
         
     | 
| 
      
 126 
     | 
    
         
            +
            }
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
      
 128 
     | 
    
         
            +
            bool _msgpack_buffer_shift_chunk(msgpack_buffer_t* b)
         
     | 
| 
      
 129 
     | 
    
         
            +
            {
         
     | 
| 
      
 130 
     | 
    
         
            +
                _msgpack_buffer_chunk_destroy(b->head);
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 133 
     | 
    
         
            +
                    /* list becomes empty. don't add head to free_list
         
     | 
| 
      
 134 
     | 
    
         
            +
                     * because head should be always available */
         
     | 
| 
      
 135 
     | 
    
         
            +
                    b->tail_buffer_end = NULL;
         
     | 
| 
      
 136 
     | 
    
         
            +
                    b->read_buffer = NULL;
         
     | 
| 
      
 137 
     | 
    
         
            +
                    return false;
         
     | 
| 
      
 138 
     | 
    
         
            +
                }
         
     | 
| 
      
 139 
     | 
    
         
            +
             
     | 
| 
      
 140 
     | 
    
         
            +
                /* add head to free_list */
         
     | 
| 
      
 141 
     | 
    
         
            +
                msgpack_buffer_chunk_t* next_head = b->head->next;
         
     | 
| 
      
 142 
     | 
    
         
            +
                b->head->next = b->free_list;
         
     | 
| 
      
 143 
     | 
    
         
            +
                b->free_list = b->head;
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                b->head = next_head;
         
     | 
| 
      
 146 
     | 
    
         
            +
                b->read_buffer = next_head->first;
         
     | 
| 
      
 147 
     | 
    
         
            +
             
     | 
| 
      
 148 
     | 
    
         
            +
                return true;
         
     | 
| 
      
 149 
     | 
    
         
            +
            }
         
     | 
| 
      
 150 
     | 
    
         
            +
             
     | 
| 
      
 151 
     | 
    
         
            +
            void msgpack_buffer_clear(msgpack_buffer_t* b)
         
     | 
| 
      
 152 
     | 
    
         
            +
            {
         
     | 
| 
      
 153 
     | 
    
         
            +
                while(_msgpack_buffer_shift_chunk(b)) {
         
     | 
| 
      
 154 
     | 
    
         
            +
                    ;
         
     | 
| 
      
 155 
     | 
    
         
            +
                }
         
     | 
| 
      
 156 
     | 
    
         
            +
            }
         
     | 
| 
      
 157 
     | 
    
         
            +
             
     | 
| 
      
 158 
     | 
    
         
            +
            size_t msgpack_buffer_read_to_string_nonblock(msgpack_buffer_t* b, VALUE string, size_t length)
         
     | 
| 
      
 159 
     | 
    
         
            +
            {
         
     | 
| 
      
 160 
     | 
    
         
            +
                size_t avail = msgpack_buffer_top_readable_size(b);
         
     | 
| 
      
 161 
     | 
    
         
            +
             
     | 
| 
      
 162 
     | 
    
         
            +
                /* optimize */
         
     | 
| 
      
 163 
     | 
    
         
            +
                if(length <= avail && RSTRING_LEN(string) == 0 &&
         
     | 
| 
      
 164 
     | 
    
         
            +
                        b->head->mapped_string != NO_MAPPED_STRING &&
         
     | 
| 
      
 165 
     | 
    
         
            +
                        length >= b->read_reference_threshold) {
         
     | 
| 
      
 166 
     | 
    
         
            +
                    VALUE s = _msgpack_buffer_refer_head_mapped_string(b, length);
         
     | 
| 
      
 167 
     | 
    
         
            +
                    rb_str_replace(string, s);
         
     | 
| 
      
 168 
     | 
    
         
            +
                    /* here doesn't have to call ENCODING_SET because
         
     | 
| 
      
 169 
     | 
    
         
            +
                     * encoding of s is always ASCII-8BIT */
         
     | 
| 
      
 170 
     | 
    
         
            +
                    _msgpack_buffer_consumed(b, length);
         
     | 
| 
      
 171 
     | 
    
         
            +
                    return length;
         
     | 
| 
      
 172 
     | 
    
         
            +
                }
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
      
 174 
     | 
    
         
            +
                size_t const length_orig = length;
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
                while(true) {
         
     | 
| 
      
 177 
     | 
    
         
            +
                    if(length <= avail) {
         
     | 
| 
      
 178 
     | 
    
         
            +
                        rb_str_buf_cat(string, b->read_buffer, length);
         
     | 
| 
      
 179 
     | 
    
         
            +
                        _msgpack_buffer_consumed(b, length);
         
     | 
| 
      
 180 
     | 
    
         
            +
                        return length_orig;
         
     | 
| 
      
 181 
     | 
    
         
            +
                    }
         
     | 
| 
      
 182 
     | 
    
         
            +
             
     | 
| 
      
 183 
     | 
    
         
            +
                    rb_str_buf_cat(string, b->read_buffer, avail);
         
     | 
| 
      
 184 
     | 
    
         
            +
                    length -= avail;
         
     | 
| 
      
 185 
     | 
    
         
            +
             
     | 
| 
      
 186 
     | 
    
         
            +
                    if(!_msgpack_buffer_shift_chunk(b)) {
         
     | 
| 
      
 187 
     | 
    
         
            +
                        return length_orig - length;
         
     | 
| 
      
 188 
     | 
    
         
            +
                    }
         
     | 
| 
      
 189 
     | 
    
         
            +
             
     | 
| 
      
 190 
     | 
    
         
            +
                    avail = msgpack_buffer_top_readable_size(b);
         
     | 
| 
      
 191 
     | 
    
         
            +
                }
         
     | 
| 
      
 192 
     | 
    
         
            +
            }
         
     | 
| 
      
 193 
     | 
    
         
            +
             
     | 
| 
      
 194 
     | 
    
         
            +
            size_t msgpack_buffer_read_nonblock(msgpack_buffer_t* b, char* buffer, size_t length)
         
     | 
| 
      
 195 
     | 
    
         
            +
            {
         
     | 
| 
      
 196 
     | 
    
         
            +
                /* buffer == NULL means skip */
         
     | 
| 
      
 197 
     | 
    
         
            +
                size_t const length_orig = length;
         
     | 
| 
      
 198 
     | 
    
         
            +
             
     | 
| 
      
 199 
     | 
    
         
            +
                while(true) {
         
     | 
| 
      
 200 
     | 
    
         
            +
                    size_t avail = msgpack_buffer_top_readable_size(b);
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
                    if(length <= avail) {
         
     | 
| 
      
 203 
     | 
    
         
            +
                        if(buffer != NULL) {
         
     | 
| 
      
 204 
     | 
    
         
            +
                            memcpy(buffer, b->read_buffer, length);
         
     | 
| 
      
 205 
     | 
    
         
            +
                        }
         
     | 
| 
      
 206 
     | 
    
         
            +
                        _msgpack_buffer_consumed(b, length);
         
     | 
| 
      
 207 
     | 
    
         
            +
                        return length_orig;
         
     | 
| 
      
 208 
     | 
    
         
            +
                    }
         
     | 
| 
      
 209 
     | 
    
         
            +
             
     | 
| 
      
 210 
     | 
    
         
            +
                    if(buffer != NULL) {
         
     | 
| 
      
 211 
     | 
    
         
            +
                        memcpy(buffer, b->read_buffer, avail);
         
     | 
| 
      
 212 
     | 
    
         
            +
                        buffer += avail;
         
     | 
| 
      
 213 
     | 
    
         
            +
                    }
         
     | 
| 
      
 214 
     | 
    
         
            +
                    length -= avail;
         
     | 
| 
      
 215 
     | 
    
         
            +
             
     | 
| 
      
 216 
     | 
    
         
            +
                    if(!_msgpack_buffer_shift_chunk(b)) {
         
     | 
| 
      
 217 
     | 
    
         
            +
                        return length_orig - length;
         
     | 
| 
      
 218 
     | 
    
         
            +
                    }
         
     | 
| 
      
 219 
     | 
    
         
            +
                }
         
     | 
| 
      
 220 
     | 
    
         
            +
            }
         
     | 
| 
      
 221 
     | 
    
         
            +
             
     | 
| 
      
 222 
     | 
    
         
            +
            size_t msgpack_buffer_all_readable_size(const msgpack_buffer_t* b)
         
     | 
| 
      
 223 
     | 
    
         
            +
            {
         
     | 
| 
      
 224 
     | 
    
         
            +
                size_t sz = msgpack_buffer_top_readable_size(b);
         
     | 
| 
      
 225 
     | 
    
         
            +
             
     | 
| 
      
 226 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 227 
     | 
    
         
            +
                    return sz;
         
     | 
| 
      
 228 
     | 
    
         
            +
                }
         
     | 
| 
      
 229 
     | 
    
         
            +
             
     | 
| 
      
 230 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head->next;
         
     | 
| 
      
 231 
     | 
    
         
            +
             
     | 
| 
      
 232 
     | 
    
         
            +
                while(true) {
         
     | 
| 
      
 233 
     | 
    
         
            +
                    sz += c->last - c->first;
         
     | 
| 
      
 234 
     | 
    
         
            +
                    if(c == &b->tail) {
         
     | 
| 
      
 235 
     | 
    
         
            +
                        return sz;
         
     | 
| 
      
 236 
     | 
    
         
            +
                    }
         
     | 
| 
      
 237 
     | 
    
         
            +
                    c = c->next;
         
     | 
| 
      
 238 
     | 
    
         
            +
                }
         
     | 
| 
      
 239 
     | 
    
         
            +
            }
         
     | 
| 
      
 240 
     | 
    
         
            +
             
     | 
| 
      
 241 
     | 
    
         
            +
            bool _msgpack_buffer_read_all2(msgpack_buffer_t* b, char* buffer, size_t length)
         
     | 
| 
      
 242 
     | 
    
         
            +
            {
         
     | 
| 
      
 243 
     | 
    
         
            +
                if(!msgpack_buffer_ensure_readable(b, length)) {
         
     | 
| 
      
 244 
     | 
    
         
            +
                    return false;
         
     | 
| 
      
 245 
     | 
    
         
            +
                }
         
     | 
| 
      
 246 
     | 
    
         
            +
             
     | 
| 
      
 247 
     | 
    
         
            +
                msgpack_buffer_read_nonblock(b, buffer, length);
         
     | 
| 
      
 248 
     | 
    
         
            +
                return true;
         
     | 
| 
      
 249 
     | 
    
         
            +
            }
         
     | 
| 
      
 250 
     | 
    
         
            +
             
     | 
| 
      
 251 
     | 
    
         
            +
             
     | 
| 
      
 252 
     | 
    
         
            +
            static inline msgpack_buffer_chunk_t* _msgpack_buffer_alloc_new_chunk(msgpack_buffer_t* b)
         
     | 
| 
      
 253 
     | 
    
         
            +
            {
         
     | 
| 
      
 254 
     | 
    
         
            +
                msgpack_buffer_chunk_t* chunk = b->free_list;
         
     | 
| 
      
 255 
     | 
    
         
            +
                if (chunk) {
         
     | 
| 
      
 256 
     | 
    
         
            +
                    b->free_list = b->free_list->next;
         
     | 
| 
      
 257 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 258 
     | 
    
         
            +
                    chunk = xmalloc(sizeof(msgpack_buffer_chunk_t));
         
     | 
| 
      
 259 
     | 
    
         
            +
                }
         
     | 
| 
      
 260 
     | 
    
         
            +
                memset(chunk, 0, sizeof(msgpack_buffer_chunk_t));
         
     | 
| 
      
 261 
     | 
    
         
            +
                return chunk;
         
     | 
| 
      
 262 
     | 
    
         
            +
            }
         
     | 
| 
      
 263 
     | 
    
         
            +
             
     | 
| 
      
 264 
     | 
    
         
            +
            static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
         
     | 
| 
      
 265 
     | 
    
         
            +
            {
         
     | 
| 
      
 266 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 267 
     | 
    
         
            +
                    if(b->tail.first == NULL) {
         
     | 
| 
      
 268 
     | 
    
         
            +
                        /* empty buffer */
         
     | 
| 
      
 269 
     | 
    
         
            +
                        return;
         
     | 
| 
      
 270 
     | 
    
         
            +
                    }
         
     | 
| 
      
 271 
     | 
    
         
            +
             
     | 
| 
      
 272 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* nc = _msgpack_buffer_alloc_new_chunk(b);
         
     | 
| 
      
 273 
     | 
    
         
            +
             
     | 
| 
      
 274 
     | 
    
         
            +
                    *nc = b->tail;
         
     | 
| 
      
 275 
     | 
    
         
            +
                    b->head = nc;
         
     | 
| 
      
 276 
     | 
    
         
            +
                    nc->next = &b->tail;
         
     | 
| 
      
 277 
     | 
    
         
            +
             
     | 
| 
      
 278 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 279 
     | 
    
         
            +
                    /* search node before tail */
         
     | 
| 
      
 280 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* before_tail = b->head;
         
     | 
| 
      
 281 
     | 
    
         
            +
                    while(before_tail->next != &b->tail) {
         
     | 
| 
      
 282 
     | 
    
         
            +
                        before_tail = before_tail->next;
         
     | 
| 
      
 283 
     | 
    
         
            +
                    }
         
     | 
| 
      
 284 
     | 
    
         
            +
             
     | 
| 
      
 285 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* nc = _msgpack_buffer_alloc_new_chunk(b);
         
     | 
| 
      
 286 
     | 
    
         
            +
             
     | 
| 
      
 287 
     | 
    
         
            +
                    if(b->rmem_last == b->tail_buffer_end) {
         
     | 
| 
      
 288 
     | 
    
         
            +
                        /* reuse unused rmem space */
         
     | 
| 
      
 289 
     | 
    
         
            +
                        size_t unused = b->tail_buffer_end - b->tail.last;
         
     | 
| 
      
 290 
     | 
    
         
            +
                        b->rmem_last -= unused;
         
     | 
| 
      
 291 
     | 
    
         
            +
                    }
         
     | 
| 
      
 292 
     | 
    
         
            +
             
     | 
| 
      
 293 
     | 
    
         
            +
                    /* rebuild tail */
         
     | 
| 
      
 294 
     | 
    
         
            +
                    *nc = b->tail;
         
     | 
| 
      
 295 
     | 
    
         
            +
                    before_tail->next = nc;
         
     | 
| 
      
 296 
     | 
    
         
            +
                    nc->next = &b->tail;
         
     | 
| 
      
 297 
     | 
    
         
            +
                }
         
     | 
| 
      
 298 
     | 
    
         
            +
            }
         
     | 
| 
      
 299 
     | 
    
         
            +
             
     | 
| 
      
 300 
     | 
    
         
            +
            static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE string)
         
     | 
| 
      
 301 
     | 
    
         
            +
            {
         
     | 
| 
      
 302 
     | 
    
         
            +
                VALUE mapped_string;
         
     | 
| 
      
 303 
     | 
    
         
            +
                if(ENCODING_GET_INLINED(string) == msgpack_rb_encindex_ascii8bit && RB_OBJ_FROZEN_RAW(string)) {
         
     | 
| 
      
 304 
     | 
    
         
            +
                    mapped_string = string;
         
     | 
| 
      
 305 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 306 
     | 
    
         
            +
                    mapped_string = rb_str_dup(string);
         
     | 
| 
      
 307 
     | 
    
         
            +
                    ENCODING_SET(mapped_string, msgpack_rb_encindex_ascii8bit);
         
     | 
| 
      
 308 
     | 
    
         
            +
                }
         
     | 
| 
      
 309 
     | 
    
         
            +
             
     | 
| 
      
 310 
     | 
    
         
            +
                _msgpack_buffer_add_new_chunk(b);
         
     | 
| 
      
 311 
     | 
    
         
            +
             
     | 
| 
      
 312 
     | 
    
         
            +
                char* data;
         
     | 
| 
      
 313 
     | 
    
         
            +
                size_t length;
         
     | 
| 
      
 314 
     | 
    
         
            +
                RSTRING_GETMEM(mapped_string, data, length);
         
     | 
| 
      
 315 
     | 
    
         
            +
             
     | 
| 
      
 316 
     | 
    
         
            +
                b->tail.first = (char*) data;
         
     | 
| 
      
 317 
     | 
    
         
            +
                b->tail.last = (char*) data + length;
         
     | 
| 
      
 318 
     | 
    
         
            +
                b->tail.mapped_string = mapped_string;
         
     | 
| 
      
 319 
     | 
    
         
            +
                b->tail.mem = NULL;
         
     | 
| 
      
 320 
     | 
    
         
            +
             
     | 
| 
      
 321 
     | 
    
         
            +
                /* msgpack_buffer_writable_size should return 0 for mapped chunk */
         
     | 
| 
      
 322 
     | 
    
         
            +
                b->tail_buffer_end = b->tail.last;
         
     | 
| 
      
 323 
     | 
    
         
            +
             
     | 
| 
      
 324 
     | 
    
         
            +
                /* consider read_buffer */
         
     | 
| 
      
 325 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 326 
     | 
    
         
            +
                    b->read_buffer = b->tail.first;
         
     | 
| 
      
 327 
     | 
    
         
            +
                }
         
     | 
| 
      
 328 
     | 
    
         
            +
            }
         
     | 
| 
      
 329 
     | 
    
         
            +
             
     | 
| 
      
 330 
     | 
    
         
            +
            void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string)
         
     | 
| 
      
 331 
     | 
    
         
            +
            {
         
     | 
| 
      
 332 
     | 
    
         
            +
                if(b->io != Qnil) {
         
     | 
| 
      
 333 
     | 
    
         
            +
                    msgpack_buffer_flush(b);
         
     | 
| 
      
 334 
     | 
    
         
            +
                    if (ENCODING_GET_INLINED(string) == msgpack_rb_encindex_ascii8bit) {
         
     | 
| 
      
 335 
     | 
    
         
            +
                        rb_funcall(b->io, b->io_write_all_method, 1, string);
         
     | 
| 
      
 336 
     | 
    
         
            +
                    } else {
         
     | 
| 
      
 337 
     | 
    
         
            +
                        msgpack_buffer_append(b, RSTRING_PTR(string), RSTRING_LEN(string));
         
     | 
| 
      
 338 
     | 
    
         
            +
                    }
         
     | 
| 
      
 339 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 340 
     | 
    
         
            +
                   _msgpack_buffer_append_reference(b, string);
         
     | 
| 
      
 341 
     | 
    
         
            +
                }
         
     | 
| 
      
 342 
     | 
    
         
            +
            }
         
     | 
| 
      
 343 
     | 
    
         
            +
             
     | 
| 
      
 344 
     | 
    
         
            +
            static inline void* _msgpack_buffer_chunk_malloc(
         
     | 
| 
      
 345 
     | 
    
         
            +
                    msgpack_buffer_t* b, msgpack_buffer_chunk_t* c,
         
     | 
| 
      
 346 
     | 
    
         
            +
                    size_t required_size, size_t* allocated_size)
         
     | 
| 
      
 347 
     | 
    
         
            +
            {
         
     | 
| 
      
 348 
     | 
    
         
            +
                if(required_size <= MSGPACK_RMEM_PAGE_SIZE) {
         
     | 
| 
      
 349 
     | 
    
         
            +
                    c->rmem = true;
         
     | 
| 
      
 350 
     | 
    
         
            +
             
     | 
| 
      
 351 
     | 
    
         
            +
                    if((size_t)(b->rmem_end - b->rmem_last) < required_size) {
         
     | 
| 
      
 352 
     | 
    
         
            +
                        /* alloc new rmem page */
         
     | 
| 
      
 353 
     | 
    
         
            +
                        *allocated_size = MSGPACK_RMEM_PAGE_SIZE;
         
     | 
| 
      
 354 
     | 
    
         
            +
                        char* buffer = msgpack_rmem_alloc(&s_rmem);
         
     | 
| 
      
 355 
     | 
    
         
            +
                        c->mem = buffer;
         
     | 
| 
      
 356 
     | 
    
         
            +
             
     | 
| 
      
 357 
     | 
    
         
            +
                        /* update rmem owner */
         
     | 
| 
      
 358 
     | 
    
         
            +
                        b->rmem_owner = &c->mem;
         
     | 
| 
      
 359 
     | 
    
         
            +
                        b->rmem_last = b->rmem_end = buffer + MSGPACK_RMEM_PAGE_SIZE;
         
     | 
| 
      
 360 
     | 
    
         
            +
             
     | 
| 
      
 361 
     | 
    
         
            +
                        return buffer;
         
     | 
| 
      
 362 
     | 
    
         
            +
                    } else {
         
     | 
| 
      
 363 
     | 
    
         
            +
                        /* reuse unused rmem */
         
     | 
| 
      
 364 
     | 
    
         
            +
                        *allocated_size = (size_t)(b->rmem_end - b->rmem_last);
         
     | 
| 
      
 365 
     | 
    
         
            +
                        char* buffer = b->rmem_last;
         
     | 
| 
      
 366 
     | 
    
         
            +
                        b->rmem_last = b->rmem_end;
         
     | 
| 
      
 367 
     | 
    
         
            +
             
     | 
| 
      
 368 
     | 
    
         
            +
                        /* update rmem owner */
         
     | 
| 
      
 369 
     | 
    
         
            +
                        c->mem = *b->rmem_owner;
         
     | 
| 
      
 370 
     | 
    
         
            +
                        *b->rmem_owner = NULL;
         
     | 
| 
      
 371 
     | 
    
         
            +
                        b->rmem_owner = &c->mem;
         
     | 
| 
      
 372 
     | 
    
         
            +
             
     | 
| 
      
 373 
     | 
    
         
            +
                        return buffer;
         
     | 
| 
      
 374 
     | 
    
         
            +
                    }
         
     | 
| 
      
 375 
     | 
    
         
            +
                }
         
     | 
| 
      
 376 
     | 
    
         
            +
             
     | 
| 
      
 377 
     | 
    
         
            +
                // TODO alignment?
         
     | 
| 
      
 378 
     | 
    
         
            +
                *allocated_size = required_size;
         
     | 
| 
      
 379 
     | 
    
         
            +
                void* mem = xmalloc(required_size);
         
     | 
| 
      
 380 
     | 
    
         
            +
                c->mem = mem;
         
     | 
| 
      
 381 
     | 
    
         
            +
                c->rmem = false;
         
     | 
| 
      
 382 
     | 
    
         
            +
                return mem;
         
     | 
| 
      
 383 
     | 
    
         
            +
            }
         
     | 
| 
      
 384 
     | 
    
         
            +
             
     | 
| 
      
 385 
     | 
    
         
            +
            static inline void* _msgpack_buffer_chunk_realloc(
         
     | 
| 
      
 386 
     | 
    
         
            +
                    msgpack_buffer_t* b, msgpack_buffer_chunk_t* c,
         
     | 
| 
      
 387 
     | 
    
         
            +
                    void* mem, size_t required_size, size_t* current_size)
         
     | 
| 
      
 388 
     | 
    
         
            +
            {
         
     | 
| 
      
 389 
     | 
    
         
            +
                if(mem == NULL) {
         
     | 
| 
      
 390 
     | 
    
         
            +
                    return _msgpack_buffer_chunk_malloc(b, c, required_size, current_size);
         
     | 
| 
      
 391 
     | 
    
         
            +
                }
         
     | 
| 
      
 392 
     | 
    
         
            +
             
     | 
| 
      
 393 
     | 
    
         
            +
                size_t next_size = *current_size * 2;
         
     | 
| 
      
 394 
     | 
    
         
            +
                while(next_size < required_size) {
         
     | 
| 
      
 395 
     | 
    
         
            +
                    next_size *= 2;
         
     | 
| 
      
 396 
     | 
    
         
            +
                }
         
     | 
| 
      
 397 
     | 
    
         
            +
                *current_size = next_size;
         
     | 
| 
      
 398 
     | 
    
         
            +
                mem = xrealloc(mem, next_size);
         
     | 
| 
      
 399 
     | 
    
         
            +
             
     | 
| 
      
 400 
     | 
    
         
            +
                c->mem = mem;
         
     | 
| 
      
 401 
     | 
    
         
            +
                return mem;
         
     | 
| 
      
 402 
     | 
    
         
            +
            }
         
     | 
| 
      
 403 
     | 
    
         
            +
             
     | 
| 
      
 404 
     | 
    
         
            +
            void _msgpack_buffer_expand(msgpack_buffer_t* b, const char* data, size_t length, bool flush_to_io)
         
     | 
| 
      
 405 
     | 
    
         
            +
            {
         
     | 
| 
      
 406 
     | 
    
         
            +
                if(flush_to_io && b->io != Qnil) {
         
     | 
| 
      
 407 
     | 
    
         
            +
                    msgpack_buffer_flush(b);
         
     | 
| 
      
 408 
     | 
    
         
            +
                    if(msgpack_buffer_writable_size(b) >= length) {
         
     | 
| 
      
 409 
     | 
    
         
            +
                        /* data == NULL means ensure_writable */
         
     | 
| 
      
 410 
     | 
    
         
            +
                        if(data != NULL) {
         
     | 
| 
      
 411 
     | 
    
         
            +
                            size_t tail_avail = msgpack_buffer_writable_size(b);
         
     | 
| 
      
 412 
     | 
    
         
            +
                            memcpy(b->tail.last, data, length);
         
     | 
| 
      
 413 
     | 
    
         
            +
                            b->tail.last += tail_avail;
         
     | 
| 
      
 414 
     | 
    
         
            +
                        }
         
     | 
| 
      
 415 
     | 
    
         
            +
                        return;
         
     | 
| 
      
 416 
     | 
    
         
            +
                    }
         
     | 
| 
      
 417 
     | 
    
         
            +
                }
         
     | 
| 
      
 418 
     | 
    
         
            +
             
     | 
| 
      
 419 
     | 
    
         
            +
                /* data == NULL means ensure_writable */
         
     | 
| 
      
 420 
     | 
    
         
            +
                if(data != NULL) {
         
     | 
| 
      
 421 
     | 
    
         
            +
                    size_t tail_avail = msgpack_buffer_writable_size(b);
         
     | 
| 
      
 422 
     | 
    
         
            +
                    memcpy(b->tail.last, data, tail_avail);
         
     | 
| 
      
 423 
     | 
    
         
            +
                    b->tail.last += tail_avail;
         
     | 
| 
      
 424 
     | 
    
         
            +
                    data += tail_avail;
         
     | 
| 
      
 425 
     | 
    
         
            +
                    length -= tail_avail;
         
     | 
| 
      
 426 
     | 
    
         
            +
                }
         
     | 
| 
      
 427 
     | 
    
         
            +
             
     | 
| 
      
 428 
     | 
    
         
            +
                size_t capacity = b->tail.last - b->tail.first;
         
     | 
| 
      
 429 
     | 
    
         
            +
             
     | 
| 
      
 430 
     | 
    
         
            +
                /* can't realloc mapped chunk or rmem page */
         
     | 
| 
      
 431 
     | 
    
         
            +
                if(b->tail.mapped_string != NO_MAPPED_STRING || capacity <= MSGPACK_RMEM_PAGE_SIZE) {
         
     | 
| 
      
 432 
     | 
    
         
            +
                    /* allocate new chunk */
         
     | 
| 
      
 433 
     | 
    
         
            +
                    _msgpack_buffer_add_new_chunk(b);
         
     | 
| 
      
 434 
     | 
    
         
            +
             
     | 
| 
      
 435 
     | 
    
         
            +
                    char* mem = _msgpack_buffer_chunk_malloc(b, &b->tail, length, &capacity);
         
     | 
| 
      
 436 
     | 
    
         
            +
             
     | 
| 
      
 437 
     | 
    
         
            +
                    char* last = mem;
         
     | 
| 
      
 438 
     | 
    
         
            +
                    if(data != NULL) {
         
     | 
| 
      
 439 
     | 
    
         
            +
                        memcpy(mem, data, length);
         
     | 
| 
      
 440 
     | 
    
         
            +
                        last += length;
         
     | 
| 
      
 441 
     | 
    
         
            +
                    }
         
     | 
| 
      
 442 
     | 
    
         
            +
             
     | 
| 
      
 443 
     | 
    
         
            +
                    /* rebuild tail chunk */
         
     | 
| 
      
 444 
     | 
    
         
            +
                    b->tail.first = mem;
         
     | 
| 
      
 445 
     | 
    
         
            +
                    b->tail.last = last;
         
     | 
| 
      
 446 
     | 
    
         
            +
                    b->tail.mapped_string = NO_MAPPED_STRING;
         
     | 
| 
      
 447 
     | 
    
         
            +
                    b->tail_buffer_end = mem + capacity;
         
     | 
| 
      
 448 
     | 
    
         
            +
             
     | 
| 
      
 449 
     | 
    
         
            +
                    /* consider read_buffer */
         
     | 
| 
      
 450 
     | 
    
         
            +
                    if(b->head == &b->tail) {
         
     | 
| 
      
 451 
     | 
    
         
            +
                        b->read_buffer = b->tail.first;
         
     | 
| 
      
 452 
     | 
    
         
            +
                    }
         
     | 
| 
      
 453 
     | 
    
         
            +
             
     | 
| 
      
 454 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 455 
     | 
    
         
            +
                    /* realloc malloc()ed chunk or NULL */
         
     | 
| 
      
 456 
     | 
    
         
            +
                    size_t tail_filled = b->tail.last - b->tail.first;
         
     | 
| 
      
 457 
     | 
    
         
            +
                    char* mem = _msgpack_buffer_chunk_realloc(b, &b->tail,
         
     | 
| 
      
 458 
     | 
    
         
            +
                            b->tail.first, tail_filled+length, &capacity);
         
     | 
| 
      
 459 
     | 
    
         
            +
             
     | 
| 
      
 460 
     | 
    
         
            +
                    char* last = mem + tail_filled;
         
     | 
| 
      
 461 
     | 
    
         
            +
                    if(data != NULL) {
         
     | 
| 
      
 462 
     | 
    
         
            +
                        memcpy(last, data, length);
         
     | 
| 
      
 463 
     | 
    
         
            +
                        last += length;
         
     | 
| 
      
 464 
     | 
    
         
            +
                    }
         
     | 
| 
      
 465 
     | 
    
         
            +
             
     | 
| 
      
 466 
     | 
    
         
            +
                    /* consider read_buffer */
         
     | 
| 
      
 467 
     | 
    
         
            +
                    if(b->head == &b->tail) {
         
     | 
| 
      
 468 
     | 
    
         
            +
                        size_t read_offset = b->read_buffer - b->head->first;
         
     | 
| 
      
 469 
     | 
    
         
            +
                        b->read_buffer = mem + read_offset;
         
     | 
| 
      
 470 
     | 
    
         
            +
                    }
         
     | 
| 
      
 471 
     | 
    
         
            +
             
     | 
| 
      
 472 
     | 
    
         
            +
                    /* rebuild tail chunk */
         
     | 
| 
      
 473 
     | 
    
         
            +
                    b->tail.first = mem;
         
     | 
| 
      
 474 
     | 
    
         
            +
                    b->tail.last = last;
         
     | 
| 
      
 475 
     | 
    
         
            +
                    b->tail_buffer_end = mem + capacity;
         
     | 
| 
      
 476 
     | 
    
         
            +
                }
         
     | 
| 
      
 477 
     | 
    
         
            +
            }
         
     | 
| 
      
 478 
     | 
    
         
            +
             
     | 
| 
      
 479 
     | 
    
         
            +
            static inline VALUE _msgpack_buffer_head_chunk_as_string(msgpack_buffer_t* b)
         
     | 
| 
      
 480 
     | 
    
         
            +
            {
         
     | 
| 
      
 481 
     | 
    
         
            +
                size_t length = b->head->last - b->read_buffer;
         
     | 
| 
      
 482 
     | 
    
         
            +
                if(length == 0) {
         
     | 
| 
      
 483 
     | 
    
         
            +
                    return rb_str_buf_new(0);
         
     | 
| 
      
 484 
     | 
    
         
            +
                }
         
     | 
| 
      
 485 
     | 
    
         
            +
             
     | 
| 
      
 486 
     | 
    
         
            +
                if(b->head->mapped_string != NO_MAPPED_STRING) {
         
     | 
| 
      
 487 
     | 
    
         
            +
                    return _msgpack_buffer_refer_head_mapped_string(b, length);
         
     | 
| 
      
 488 
     | 
    
         
            +
                }
         
     | 
| 
      
 489 
     | 
    
         
            +
             
     | 
| 
      
 490 
     | 
    
         
            +
                return rb_str_new(b->read_buffer, length);
         
     | 
| 
      
 491 
     | 
    
         
            +
            }
         
     | 
| 
      
 492 
     | 
    
         
            +
             
     | 
| 
      
 493 
     | 
    
         
            +
            static inline VALUE _msgpack_buffer_chunk_as_string(msgpack_buffer_chunk_t* c)
         
     | 
| 
      
 494 
     | 
    
         
            +
            {
         
     | 
| 
      
 495 
     | 
    
         
            +
                size_t chunk_size = c->last - c->first;
         
     | 
| 
      
 496 
     | 
    
         
            +
                if(chunk_size == 0) {
         
     | 
| 
      
 497 
     | 
    
         
            +
                    return rb_str_buf_new(0);
         
     | 
| 
      
 498 
     | 
    
         
            +
                }
         
     | 
| 
      
 499 
     | 
    
         
            +
             
     | 
| 
      
 500 
     | 
    
         
            +
                if(c->mapped_string != NO_MAPPED_STRING) {
         
     | 
| 
      
 501 
     | 
    
         
            +
                    return rb_str_dup(c->mapped_string);
         
     | 
| 
      
 502 
     | 
    
         
            +
                }
         
     | 
| 
      
 503 
     | 
    
         
            +
             
     | 
| 
      
 504 
     | 
    
         
            +
                return rb_str_new(c->first, chunk_size);
         
     | 
| 
      
 505 
     | 
    
         
            +
            }
         
     | 
| 
      
 506 
     | 
    
         
            +
             
     | 
| 
      
 507 
     | 
    
         
            +
            VALUE msgpack_buffer_all_as_string(msgpack_buffer_t* b)
         
     | 
| 
      
 508 
     | 
    
         
            +
            {
         
     | 
| 
      
 509 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 510 
     | 
    
         
            +
                    return _msgpack_buffer_head_chunk_as_string(b);
         
     | 
| 
      
 511 
     | 
    
         
            +
                }
         
     | 
| 
      
 512 
     | 
    
         
            +
             
     | 
| 
      
 513 
     | 
    
         
            +
                size_t length = msgpack_buffer_all_readable_size(b);
         
     | 
| 
      
 514 
     | 
    
         
            +
                VALUE string = rb_str_new(NULL, length);
         
     | 
| 
      
 515 
     | 
    
         
            +
                char* buffer = RSTRING_PTR(string);
         
     | 
| 
      
 516 
     | 
    
         
            +
             
     | 
| 
      
 517 
     | 
    
         
            +
                size_t avail = msgpack_buffer_top_readable_size(b);
         
     | 
| 
      
 518 
     | 
    
         
            +
                memcpy(buffer, b->read_buffer, avail);
         
     | 
| 
      
 519 
     | 
    
         
            +
                buffer += avail;
         
     | 
| 
      
 520 
     | 
    
         
            +
                length -= avail;
         
     | 
| 
      
 521 
     | 
    
         
            +
             
     | 
| 
      
 522 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head->next;
         
     | 
| 
      
 523 
     | 
    
         
            +
             
     | 
| 
      
 524 
     | 
    
         
            +
                while(true) {
         
     | 
| 
      
 525 
     | 
    
         
            +
                    avail = c->last - c->first;
         
     | 
| 
      
 526 
     | 
    
         
            +
                    memcpy(buffer, c->first, avail);
         
     | 
| 
      
 527 
     | 
    
         
            +
             
     | 
| 
      
 528 
     | 
    
         
            +
                    if(length <= avail) {
         
     | 
| 
      
 529 
     | 
    
         
            +
                        return string;
         
     | 
| 
      
 530 
     | 
    
         
            +
                    }
         
     | 
| 
      
 531 
     | 
    
         
            +
                    buffer += avail;
         
     | 
| 
      
 532 
     | 
    
         
            +
                    length -= avail;
         
     | 
| 
      
 533 
     | 
    
         
            +
             
     | 
| 
      
 534 
     | 
    
         
            +
                    c = c->next;
         
     | 
| 
      
 535 
     | 
    
         
            +
                }
         
     | 
| 
      
 536 
     | 
    
         
            +
            }
         
     | 
| 
      
 537 
     | 
    
         
            +
             
     | 
| 
      
 538 
     | 
    
         
            +
            VALUE msgpack_buffer_all_as_string_array(msgpack_buffer_t* b)
         
     | 
| 
      
 539 
     | 
    
         
            +
            {
         
     | 
| 
      
 540 
     | 
    
         
            +
                if(b->head == &b->tail) {
         
     | 
| 
      
 541 
     | 
    
         
            +
                    VALUE s = msgpack_buffer_all_as_string(b);
         
     | 
| 
      
 542 
     | 
    
         
            +
                    VALUE ary = rb_ary_new3(1, s);
         
     | 
| 
      
 543 
     | 
    
         
            +
                    return ary;
         
     | 
| 
      
 544 
     | 
    
         
            +
                }
         
     | 
| 
      
 545 
     | 
    
         
            +
             
     | 
| 
      
 546 
     | 
    
         
            +
                /* TODO optimize ary construction */
         
     | 
| 
      
 547 
     | 
    
         
            +
                VALUE ary = rb_ary_new();
         
     | 
| 
      
 548 
     | 
    
         
            +
             
     | 
| 
      
 549 
     | 
    
         
            +
                VALUE s = _msgpack_buffer_head_chunk_as_string(b);
         
     | 
| 
      
 550 
     | 
    
         
            +
                rb_ary_push(ary, s);
         
     | 
| 
      
 551 
     | 
    
         
            +
             
     | 
| 
      
 552 
     | 
    
         
            +
                msgpack_buffer_chunk_t* c = b->head->next;
         
     | 
| 
      
 553 
     | 
    
         
            +
             
     | 
| 
      
 554 
     | 
    
         
            +
                while(true) {
         
     | 
| 
      
 555 
     | 
    
         
            +
                    s = _msgpack_buffer_chunk_as_string(c);
         
     | 
| 
      
 556 
     | 
    
         
            +
                    rb_ary_push(ary, s);
         
     | 
| 
      
 557 
     | 
    
         
            +
                    if(c == &b->tail) {
         
     | 
| 
      
 558 
     | 
    
         
            +
                        return ary;
         
     | 
| 
      
 559 
     | 
    
         
            +
                    }
         
     | 
| 
      
 560 
     | 
    
         
            +
                    c = c->next;
         
     | 
| 
      
 561 
     | 
    
         
            +
                }
         
     | 
| 
      
 562 
     | 
    
         
            +
             
     | 
| 
      
 563 
     | 
    
         
            +
                return ary;
         
     | 
| 
      
 564 
     | 
    
         
            +
            }
         
     | 
| 
      
 565 
     | 
    
         
            +
             
     | 
| 
      
 566 
     | 
    
         
            +
            size_t msgpack_buffer_flush_to_io(msgpack_buffer_t* b, VALUE io, ID write_method, bool consume)
         
     | 
| 
      
 567 
     | 
    
         
            +
            {
         
     | 
| 
      
 568 
     | 
    
         
            +
                if(msgpack_buffer_top_readable_size(b) == 0) {
         
     | 
| 
      
 569 
     | 
    
         
            +
                    return 0;
         
     | 
| 
      
 570 
     | 
    
         
            +
                }
         
     | 
| 
      
 571 
     | 
    
         
            +
             
     | 
| 
      
 572 
     | 
    
         
            +
                VALUE s = _msgpack_buffer_head_chunk_as_string(b);
         
     | 
| 
      
 573 
     | 
    
         
            +
                rb_funcall(io, write_method, 1, s);
         
     | 
| 
      
 574 
     | 
    
         
            +
                size_t sz = RSTRING_LEN(s);
         
     | 
| 
      
 575 
     | 
    
         
            +
             
     | 
| 
      
 576 
     | 
    
         
            +
                if(consume) {
         
     | 
| 
      
 577 
     | 
    
         
            +
                    while(_msgpack_buffer_shift_chunk(b)) {
         
     | 
| 
      
 578 
     | 
    
         
            +
                        s = _msgpack_buffer_chunk_as_string(b->head);
         
     | 
| 
      
 579 
     | 
    
         
            +
                        rb_funcall(io, write_method, 1, s);
         
     | 
| 
      
 580 
     | 
    
         
            +
                        sz += RSTRING_LEN(s);
         
     | 
| 
      
 581 
     | 
    
         
            +
                    }
         
     | 
| 
      
 582 
     | 
    
         
            +
                    return sz;
         
     | 
| 
      
 583 
     | 
    
         
            +
             
     | 
| 
      
 584 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 585 
     | 
    
         
            +
                    if(b->head == &b->tail) {
         
     | 
| 
      
 586 
     | 
    
         
            +
                        return sz;
         
     | 
| 
      
 587 
     | 
    
         
            +
                    }
         
     | 
| 
      
 588 
     | 
    
         
            +
                    msgpack_buffer_chunk_t* c = b->head->next;
         
     | 
| 
      
 589 
     | 
    
         
            +
                    while(true) {
         
     | 
| 
      
 590 
     | 
    
         
            +
                        s = _msgpack_buffer_chunk_as_string(c);
         
     | 
| 
      
 591 
     | 
    
         
            +
                        rb_funcall(io, write_method, 1, s);
         
     | 
| 
      
 592 
     | 
    
         
            +
                        sz += RSTRING_LEN(s);
         
     | 
| 
      
 593 
     | 
    
         
            +
                        if(c == &b->tail) {
         
     | 
| 
      
 594 
     | 
    
         
            +
                            return sz;
         
     | 
| 
      
 595 
     | 
    
         
            +
                        }
         
     | 
| 
      
 596 
     | 
    
         
            +
                        c = c->next;
         
     | 
| 
      
 597 
     | 
    
         
            +
                    }
         
     | 
| 
      
 598 
     | 
    
         
            +
                }
         
     | 
| 
      
 599 
     | 
    
         
            +
            }
         
     | 
| 
      
 600 
     | 
    
         
            +
             
     | 
| 
      
 601 
     | 
    
         
            +
            size_t _msgpack_buffer_feed_from_io(msgpack_buffer_t* b)
         
     | 
| 
      
 602 
     | 
    
         
            +
            {
         
     | 
| 
      
 603 
     | 
    
         
            +
                if(b->io_buffer == Qnil) {
         
     | 
| 
      
 604 
     | 
    
         
            +
                    b->io_buffer = rb_funcall(b->io, b->io_partial_read_method, 1, SIZET2NUM(b->io_buffer_size));
         
     | 
| 
      
 605 
     | 
    
         
            +
                    if(b->io_buffer == Qnil) {
         
     | 
| 
      
 606 
     | 
    
         
            +
                        rb_raise(rb_eEOFError, "IO reached end of file");
         
     | 
| 
      
 607 
     | 
    
         
            +
                    }
         
     | 
| 
      
 608 
     | 
    
         
            +
                    StringValue(b->io_buffer);
         
     | 
| 
      
 609 
     | 
    
         
            +
                } else {
         
     | 
| 
      
 610 
     | 
    
         
            +
                    VALUE ret = rb_funcall(b->io, b->io_partial_read_method, 2, SIZET2NUM(b->io_buffer_size), b->io_buffer);
         
     | 
| 
      
 611 
     | 
    
         
            +
                    if(ret == Qnil) {
         
     | 
| 
      
 612 
     | 
    
         
            +
                        rb_raise(rb_eEOFError, "IO reached end of file");
         
     | 
| 
      
 613 
     | 
    
         
            +
                    }
         
     | 
| 
      
 614 
     | 
    
         
            +
                }
         
     | 
| 
      
 615 
     | 
    
         
            +
             
     | 
| 
      
 616 
     | 
    
         
            +
                size_t len = RSTRING_LEN(b->io_buffer);
         
     | 
| 
      
 617 
     | 
    
         
            +
                if(len == 0) {
         
     | 
| 
      
 618 
     | 
    
         
            +
                    rb_raise(rb_eEOFError, "IO reached end of file");
         
     | 
| 
      
 619 
     | 
    
         
            +
                }
         
     | 
| 
      
 620 
     | 
    
         
            +
             
     | 
| 
      
 621 
     | 
    
         
            +
                /* TODO zero-copy optimize? */
         
     | 
| 
      
 622 
     | 
    
         
            +
                msgpack_buffer_append_nonblock(b, RSTRING_PTR(b->io_buffer), len);
         
     | 
| 
      
 623 
     | 
    
         
            +
             
     | 
| 
      
 624 
     | 
    
         
            +
                return len;
         
     | 
| 
      
 625 
     | 
    
         
            +
            }
         
     | 
| 
      
 626 
     | 
    
         
            +
             
     | 
| 
      
 627 
     | 
    
         
            +
            size_t _msgpack_buffer_read_from_io_to_string(msgpack_buffer_t* b, VALUE string, size_t length)
         
     | 
| 
      
 628 
     | 
    
         
            +
            {
         
     | 
| 
      
 629 
     | 
    
         
            +
            #define MIN(x, y) (((x) < (y)) ? (x) : (y))
         
     | 
| 
      
 630 
     | 
    
         
            +
             
     | 
| 
      
 631 
     | 
    
         
            +
                if(RSTRING_LEN(string) == 0) {
         
     | 
| 
      
 632 
     | 
    
         
            +
                    /* direct read */
         
     | 
| 
      
 633 
     | 
    
         
            +
                    VALUE ret = rb_funcall(b->io, b->io_partial_read_method, 2, SIZET2NUM(MIN(b->io_buffer_size, length)), string);
         
     | 
| 
      
 634 
     | 
    
         
            +
                    if(ret == Qnil) {
         
     | 
| 
      
 635 
     | 
    
         
            +
                        return 0;
         
     | 
| 
      
 636 
     | 
    
         
            +
                    }
         
     | 
| 
      
 637 
     | 
    
         
            +
                    return RSTRING_LEN(string);
         
     | 
| 
      
 638 
     | 
    
         
            +
                }
         
     | 
| 
      
 639 
     | 
    
         
            +
             
     | 
| 
      
 640 
     | 
    
         
            +
                /* copy via io_buffer */
         
     | 
| 
      
 641 
     | 
    
         
            +
                if(b->io_buffer == Qnil) {
         
     | 
| 
      
 642 
     | 
    
         
            +
                    b->io_buffer = rb_str_buf_new(0);
         
     | 
| 
      
 643 
     | 
    
         
            +
                }
         
     | 
| 
      
 644 
     | 
    
         
            +
             
     | 
| 
      
 645 
     | 
    
         
            +
                VALUE ret = rb_funcall(b->io, b->io_partial_read_method, 2, SIZET2NUM(MIN(b->io_buffer_size, length)), b->io_buffer);
         
     | 
| 
      
 646 
     | 
    
         
            +
                if(ret == Qnil) {
         
     | 
| 
      
 647 
     | 
    
         
            +
                    return 0;
         
     | 
| 
      
 648 
     | 
    
         
            +
                }
         
     | 
| 
      
 649 
     | 
    
         
            +
                size_t rl = RSTRING_LEN(b->io_buffer);
         
     | 
| 
      
 650 
     | 
    
         
            +
             
     | 
| 
      
 651 
     | 
    
         
            +
                rb_str_buf_cat(string, (const void*)RSTRING_PTR(b->io_buffer), rl);
         
     | 
| 
      
 652 
     | 
    
         
            +
                return rl;
         
     | 
| 
      
 653 
     | 
    
         
            +
             
     | 
| 
      
 654 
     | 
    
         
            +
            #undef MIN
         
     | 
| 
      
 655 
     | 
    
         
            +
            }
         
     | 
| 
      
 656 
     | 
    
         
            +
             
     | 
| 
      
 657 
     | 
    
         
            +
            size_t _msgpack_buffer_skip_from_io(msgpack_buffer_t* b, size_t length)
         
     | 
| 
      
 658 
     | 
    
         
            +
            {
         
     | 
| 
      
 659 
     | 
    
         
            +
                if(b->io_buffer == Qnil) {
         
     | 
| 
      
 660 
     | 
    
         
            +
                    b->io_buffer = rb_str_buf_new(0);
         
     | 
| 
      
 661 
     | 
    
         
            +
                }
         
     | 
| 
      
 662 
     | 
    
         
            +
             
     | 
| 
      
 663 
     | 
    
         
            +
                VALUE ret = rb_funcall(b->io, b->io_partial_read_method, 2, SIZET2NUM(length), b->io_buffer);
         
     | 
| 
      
 664 
     | 
    
         
            +
                if(ret == Qnil) {
         
     | 
| 
      
 665 
     | 
    
         
            +
                    return 0;
         
     | 
| 
      
 666 
     | 
    
         
            +
                }
         
     | 
| 
      
 667 
     | 
    
         
            +
                return RSTRING_LEN(b->io_buffer);
         
     | 
| 
      
 668 
     | 
    
         
            +
            }
         
     | 
| 
      
 669 
     | 
    
         
            +
             
     |