diff --git a/ext/msgpack/buffer.c b/ext/msgpack/buffer.c index 9b7fd4e8..63c38f33 100644 --- a/ext/msgpack/buffer.c +++ b/ext/msgpack/buffer.c @@ -257,7 +257,6 @@ static inline msgpack_buffer_chunk_t* _msgpack_buffer_alloc_new_chunk(msgpack_bu } else { chunk = xmalloc(sizeof(msgpack_buffer_chunk_t)); } - memset(chunk, 0, sizeof(msgpack_buffer_chunk_t)); return chunk; } @@ -265,7 +264,8 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b) { if(b->head == &b->tail) { if(b->tail.first == NULL) { - /* empty buffer */ + /* The buffer is empty, we can just use the embeded tail directly */ + memset(&b->tail, 0, sizeof(msgpack_buffer_chunk_t)); return; } @@ -295,6 +295,7 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b) before_tail->next = nc; nc->next = &b->tail; } + memset(&b->tail, 0, sizeof(msgpack_buffer_chunk_t)); } static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE string) @@ -315,7 +316,6 @@ static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE s b->tail.first = (char*) data; b->tail.last = (char*) data + length; b->tail.mapped_string = mapped_string; - b->tail.mem = NULL; /* msgpack_buffer_writable_size should return 0 for mapped chunk */ b->tail_buffer_end = b->tail.last; @@ -344,6 +344,8 @@ static inline void* _msgpack_buffer_chunk_malloc( msgpack_buffer_t* b, msgpack_buffer_chunk_t* c, size_t required_size, size_t* allocated_size) { + c->mapped_string = NO_MAPPED_STRING; + if(required_size <= MSGPACK_RMEM_PAGE_SIZE) { c->rmem = true;