Do not interleave code and declarations in C files

Avoid C99 style to interleave code and declarations in order to compile msgpackc with Visual Studion < 2013
This commit is contained in:
tbeu 2015-03-26 21:48:03 +01:00
parent 7bee573a72
commit 8921f9dcfc
9 changed files with 514 additions and 478 deletions

View File

@ -52,6 +52,7 @@ void unpack(receiver* r) {
msgpack_unpack_return ret; msgpack_unpack_return ret;
char* buf; char* buf;
size_t recv_len; size_t recv_len;
int recv_count = 0;
msgpack_unpacked_init(&result); msgpack_unpacked_init(&result);
if (msgpack_unpacker_buffer_capacity(unp) < EACH_RECV_SIZE) { if (msgpack_unpacker_buffer_capacity(unp) < EACH_RECV_SIZE) {
@ -64,7 +65,6 @@ void unpack(receiver* r) {
msgpack_unpacker_buffer_consumed(unp, recv_len); msgpack_unpacker_buffer_consumed(unp, recv_len);
int recv_count = 0;
while (recv_len > 0) { while (recv_len > 0) {
int i = 0; int i = 0;
printf("receive count: %d %zd bytes received.:\n", recv_count++, recv_len); printf("receive count: %d %zd bytes received.:\n", recv_count++, recv_len);

View File

@ -3,12 +3,15 @@
int main(void) int main(void)
{ {
/* msgpack::sbuffer is a simple buffer implementation. */
msgpack_sbuffer sbuf; msgpack_sbuffer sbuf;
msgpack_packer pk;
msgpack_zone mempool;
msgpack_object deserialized;
/* msgpack::sbuffer is a simple buffer implementation. */
msgpack_sbuffer_init(&sbuf); msgpack_sbuffer_init(&sbuf);
/* serialize values into the buffer using msgpack_sbuffer_write callback function. */ /* serialize values into the buffer using msgpack_sbuffer_write callback function. */
msgpack_packer pk;
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
msgpack_pack_array(&pk, 3); msgpack_pack_array(&pk, 3);
@ -19,10 +22,8 @@ int main(void)
/* deserialize the buffer into msgpack_object instance. */ /* deserialize the buffer into msgpack_object instance. */
/* deserialized object is valid during the msgpack_zone instance alive. */ /* deserialized object is valid during the msgpack_zone instance alive. */
msgpack_zone mempool;
msgpack_zone_init(&mempool, 2048); msgpack_zone_init(&mempool, 2048);
msgpack_object deserialized;
msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);
/* print the deserialized object. */ /* print the deserialized object. */

View File

@ -1,13 +1,16 @@
#include <msgpack.h> #include <msgpack.h>
#include <assert.h>
void test() void test()
{ {
size_t size = 10000000; size_t size = 10000000;
msgpack_sbuffer buf; msgpack_sbuffer buf;
msgpack_packer * pk;
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_sbuffer_init(&buf); msgpack_sbuffer_init(&buf);
msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write); pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
msgpack_pack_array(pk, size); msgpack_pack_array(pk, size);
{ {
@ -17,9 +20,6 @@ void test()
} }
msgpack_packer_free(pk); msgpack_packer_free(pk);
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg); msgpack_unpacked_init(&msg);
while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) { while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {

View File

@ -1,14 +1,17 @@
#include <msgpack.h> #include <msgpack.h>
#include <assert.h>
void test() void test()
{ {
uint64_t test_u64 = 0xFFF0000000000001LL; uint64_t test_u64 = 0xFFF0000000000001LL;
size_t size = 10000000; size_t size = 10000000;
msgpack_sbuffer buf; msgpack_sbuffer buf;
msgpack_packer * pk;
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_sbuffer_init(&buf); msgpack_sbuffer_init(&buf);
msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write); pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
msgpack_pack_array(pk, size); msgpack_pack_array(pk, size);
{ {
@ -18,9 +21,6 @@ void test()
} }
msgpack_packer_free(pk); msgpack_packer_free(pk);
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg); msgpack_unpacked_init(&msg);
while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) { while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {

View File

@ -97,78 +97,78 @@ msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off) msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
{ {
assert(len >= *off); assert(len >= *off);
{
const unsigned char* p = (unsigned char*)data + *off;
const unsigned char* const pe = (unsigned char*)data + len;
const void* n = NULL;
const unsigned char* p = (unsigned char*)data + *off; unsigned int trail = ctx->trail;
const unsigned char* const pe = (unsigned char*)data + len; unsigned int cs = ctx->cs;
const void* n = NULL; unsigned int top = ctx->top;
msgpack_unpack_struct(_stack)* stack = ctx->stack;
/*
unsigned int stack_size = ctx->stack_size;
*/
msgpack_unpack_user* user = &ctx->user;
unsigned int trail = ctx->trail; msgpack_unpack_object obj;
unsigned int cs = ctx->cs; msgpack_unpack_struct(_stack)* c = NULL;
unsigned int top = ctx->top;
msgpack_unpack_struct(_stack)* stack = ctx->stack;
/*
unsigned int stack_size = ctx->stack_size;
*/
msgpack_unpack_user* user = &ctx->user;
msgpack_unpack_object obj; int ret;
msgpack_unpack_struct(_stack)* c = NULL;
int ret;
#define push_simple_value(func) \ #define push_simple_value(func) \
if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \ if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \
goto _push goto _push
#define push_fixed_value(func, arg) \ #define push_fixed_value(func, arg) \
if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \ if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \
goto _push goto _push
#define push_variable_value(func, base, pos, len) \ #define push_variable_value(func, base, pos, len) \
if(msgpack_unpack_callback(func)(user, \ if(msgpack_unpack_callback(func)(user, \
(const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \ (const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
goto _push goto _push
#define again_fixed_trail(_cs, trail_len) \ #define again_fixed_trail(_cs, trail_len) \
trail = trail_len; \ trail = trail_len; \
cs = _cs; \ cs = _cs; \
goto _fixed_trail_again goto _fixed_trail_again
#define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \ #define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
trail = trail_len; \ trail = trail_len; \
if(trail == 0) { goto ifzero; } \ if(trail == 0) { goto ifzero; } \
cs = _cs; \ cs = _cs; \
goto _fixed_trail_again goto _fixed_trail_again
#define start_container(func, count_, ct_) \ #define start_container(func, count_, ct_) \
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \
if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \ if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
if((count_) == 0) { obj = stack[top].obj; goto _push; } \ if((count_) == 0) { obj = stack[top].obj; goto _push; } \
stack[top].ct = ct_; \ stack[top].ct = ct_; \
stack[top].count = count_; \ stack[top].count = count_; \
++top; \ ++top; \
/*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \ /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
/*printf("stack push %d\n", top);*/ \ /*printf("stack push %d\n", top);*/ \
/* FIXME \ /* FIXME \
if(top >= stack_size) { \ if(top >= stack_size) { \
if(stack_size == MSGPACK_EMBED_STACK_SIZE) { \ if(stack_size == MSGPACK_EMBED_STACK_SIZE) { \
size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \ size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \
size_t nsize = csize * 2; \ size_t nsize = csize * 2; \
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \ msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \
if(tmp == NULL) { goto _failed; } \ if(tmp == NULL) { goto _failed; } \
memcpy(tmp, ctx->stack, csize); \ memcpy(tmp, ctx->stack, csize); \
ctx->stack = stack = tmp; \ ctx->stack = stack = tmp; \
ctx->stack_size = stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \ ctx->stack_size = stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \
} else { \ } else { \
size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \ size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \ msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \
if(tmp == NULL) { goto _failed; } \ if(tmp == NULL) { goto _failed; } \
ctx->stack = stack = tmp; \ ctx->stack = stack = tmp; \
ctx->stack_size = stack_size = stack_size * 2; \ ctx->stack_size = stack_size = stack_size * 2; \
} \
} \ } \
} \ */ \
*/ \ goto _header_again
goto _header_again
#define NEXT_CS(p) \ #define NEXT_CS(p) \
((unsigned int)*p & 0x1f) ((unsigned int)*p & 0x1f)
#ifdef USE_CASE_RANGE #ifdef USE_CASE_RANGE
#define SWITCH_RANGE_BEGIN switch(*p) { #define SWITCH_RANGE_BEGIN switch(*p) {
@ -182,283 +182,283 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
#define SWITCH_RANGE_END } } #define SWITCH_RANGE_END } }
#endif #endif
if(p == pe) { goto _out; } if(p == pe) { goto _out; }
do { do {
switch(cs) { switch(cs) {
case MSGPACK_CS_HEADER: case MSGPACK_CS_HEADER:
SWITCH_RANGE_BEGIN SWITCH_RANGE_BEGIN
SWITCH_RANGE(0x00, 0x7f) // Positive Fixnum SWITCH_RANGE(0x00, 0x7f) // Positive Fixnum
push_fixed_value(_uint8, *(uint8_t*)p); push_fixed_value(_uint8, *(uint8_t*)p);
SWITCH_RANGE(0xe0, 0xff) // Negative Fixnum SWITCH_RANGE(0xe0, 0xff) // Negative Fixnum
push_fixed_value(_int8, *(int8_t*)p); push_fixed_value(_int8, *(int8_t*)p);
SWITCH_RANGE(0xc0, 0xdf) // Variable SWITCH_RANGE(0xc0, 0xdf) // Variable
switch(*p) { switch(*p) {
case 0xc0: // nil case 0xc0: // nil
push_simple_value(_nil); push_simple_value(_nil);
//case 0xc1: // string //case 0xc1: // string
// again_terminal_trail(NEXT_CS(p), p+1); // again_terminal_trail(NEXT_CS(p), p+1);
case 0xc2: // false case 0xc2: // false
push_simple_value(_false); push_simple_value(_false);
case 0xc3: // true case 0xc3: // true
push_simple_value(_true); push_simple_value(_true);
case 0xc4: // bin 8 case 0xc4: // bin 8
case 0xc5: // bin 16 case 0xc5: // bin 16
case 0xc6: // bin 32 case 0xc6: // bin 32
again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03)); again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
case 0xc7: // ext 8 case 0xc7: // ext 8
case 0xc8: // ext 16 case 0xc8: // ext 16
case 0xc9: // ext 32 case 0xc9: // ext 32
again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) + 1) & 0x03)); again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) + 1) & 0x03));
case 0xca: // float case 0xca: // float
case 0xcb: // double case 0xcb: // double
case 0xcc: // unsigned int 8 case 0xcc: // unsigned int 8
case 0xcd: // unsigned int 16 case 0xcd: // unsigned int 16
case 0xce: // unsigned int 32 case 0xce: // unsigned int 32
case 0xcf: // unsigned int 64 case 0xcf: // unsigned int 64
case 0xd0: // signed int 8 case 0xd0: // signed int 8
case 0xd1: // signed int 16 case 0xd1: // signed int 16
case 0xd2: // signed int 32 case 0xd2: // signed int 32
case 0xd3: // signed int 64 case 0xd3: // signed int 64
again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03)); again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
case 0xd4: // fixext 1 case 0xd4: // fixext 1
case 0xd5: // fixext 2 case 0xd5: // fixext 2
case 0xd6: // fixext 4 case 0xd6: // fixext 4
case 0xd7: // fixext 8 case 0xd7: // fixext 8
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE,
(1 << (((unsigned int)*p) & 0x03)) + 1, _ext_zero); (1 << (((unsigned int)*p) & 0x03)) + 1, _ext_zero);
case 0xd8: // fixext 16 case 0xd8: // fixext 16
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero); again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero);
case 0xd9: // str 8
case 0xda: // str 16
case 0xdb: // str 32
again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) & 0x03) - 1));
case 0xdc: // array 16
case 0xdd: // array 32
case 0xde: // map 16
case 0xdf: // map 32
again_fixed_trail(NEXT_CS(p), 2u << (((unsigned int)*p) & 0x01));
default:
goto _failed;
}
SWITCH_RANGE(0xa0, 0xbf) // FixStr
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, ((unsigned int)*p & 0x1f), _str_zero);
SWITCH_RANGE(0x90, 0x9f) // FixArray
start_container(_array, ((unsigned int)*p) & 0x0f, MSGPACK_CT_ARRAY_ITEM);
SWITCH_RANGE(0x80, 0x8f) // FixMap
start_container(_map, ((unsigned int)*p) & 0x0f, MSGPACK_CT_MAP_KEY);
SWITCH_RANGE_DEFAULT
goto _failed;
SWITCH_RANGE_END
// end MSGPACK_CS_HEADER
_fixed_trail_again:
++p;
default:
if((size_t)(pe - p) < trail) { goto _out; }
n = p; p += trail - 1;
switch(cs) {
//case MSGPACK_CS_
//case MSGPACK_CS_
case MSGPACK_CS_FLOAT: {
union { uint32_t i; float f; } mem;
_msgpack_load32(uint32_t, n, &mem.i);
push_fixed_value(_float, mem.f); }
case MSGPACK_CS_DOUBLE: {
union { uint64_t i; double f; } mem;
_msgpack_load64(uint64_t, n, &mem.i);
#if defined(__arm__) && !(__ARM_EABI__) // arm-oabi
// https://github.com/msgpack/msgpack-perl/pull/1
mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
#endif
push_fixed_value(_double, mem.f); }
case MSGPACK_CS_UINT_8:
push_fixed_value(_uint8, *(uint8_t*)n);
case MSGPACK_CS_UINT_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
push_fixed_value(_uint16, tmp);
}
case MSGPACK_CS_UINT_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
push_fixed_value(_uint32, tmp);
}
case MSGPACK_CS_UINT_64:{
uint64_t tmp;
_msgpack_load64(uint64_t,n,&tmp);
push_fixed_value(_uint64, tmp);
}
case MSGPACK_CS_INT_8:
push_fixed_value(_int8, *(int8_t*)n);
case MSGPACK_CS_INT_16:{
int16_t tmp;
_msgpack_load16(int16_t,n,&tmp);
push_fixed_value(_int16, tmp);
}
case MSGPACK_CS_INT_32:{
int32_t tmp;
_msgpack_load32(int32_t,n,&tmp);
push_fixed_value(_int32, tmp);
}
case MSGPACK_CS_INT_64:{
int64_t tmp;
_msgpack_load64(int64_t,n,&tmp);
push_fixed_value(_int64, tmp);
}
case MSGPACK_CS_FIXEXT_1:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 1+1, _ext_zero);
case MSGPACK_CS_FIXEXT_2:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 2+1, _ext_zero);
case MSGPACK_CS_FIXEXT_4:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 4+1, _ext_zero);
case MSGPACK_CS_FIXEXT_8:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 8+1, _ext_zero);
case MSGPACK_CS_FIXEXT_16:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero);
case MSGPACK_CS_STR_8:
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, *(uint8_t*)n, _str_zero);
case MSGPACK_CS_BIN_8:
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, *(uint8_t*)n, _bin_zero);
case MSGPACK_CS_EXT_8:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, (*(uint8_t*)n) + 1, _ext_zero);
case MSGPACK_CS_STR_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
}
case MSGPACK_CS_BIN_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
}
case MSGPACK_CS_EXT_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
}
case MSGPACK_CS_STR_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
}
case MSGPACK_CS_BIN_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
}
case MSGPACK_CS_EXT_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
}
case MSGPACK_ACS_STR_VALUE:
_str_zero:
push_variable_value(_str, data, n, trail);
case MSGPACK_ACS_BIN_VALUE:
_bin_zero:
push_variable_value(_bin, data, n, trail);
case MSGPACK_ACS_EXT_VALUE:
_ext_zero:
push_variable_value(_ext, data, n, trail);
case MSGPACK_CS_ARRAY_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
}
case MSGPACK_CS_ARRAY_32:{
/* FIXME security guard */
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
}
case MSGPACK_CS_MAP_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
}
case MSGPACK_CS_MAP_32:{
/* FIXME security guard */
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
}
case 0xd9: // str 8
case 0xda: // str 16
case 0xdb: // str 32
again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) & 0x03) - 1));
case 0xdc: // array 16
case 0xdd: // array 32
case 0xde: // map 16
case 0xdf: // map 32
again_fixed_trail(NEXT_CS(p), 2u << (((unsigned int)*p) & 0x01));
default: default:
goto _failed; goto _failed;
} }
SWITCH_RANGE(0xa0, 0xbf) // FixStr }
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, ((unsigned int)*p & 0x1f), _str_zero);
SWITCH_RANGE(0x90, 0x9f) // FixArray
start_container(_array, ((unsigned int)*p) & 0x0f, MSGPACK_CT_ARRAY_ITEM);
SWITCH_RANGE(0x80, 0x8f) // FixMap
start_container(_map, ((unsigned int)*p) & 0x0f, MSGPACK_CT_MAP_KEY);
SWITCH_RANGE_DEFAULT _push:
goto _failed; if(top == 0) { goto _finish; }
SWITCH_RANGE_END c = &stack[top-1];
// end MSGPACK_CS_HEADER switch(c->ct) {
case MSGPACK_CT_ARRAY_ITEM:
if(msgpack_unpack_callback(_array_item)(user, &c->obj, obj) < 0) { goto _failed; }
_fixed_trail_again: if(--c->count == 0) {
++p; obj = c->obj;
--top;
/*printf("stack pop %d\n", top);*/
goto _push;
}
goto _header_again;
case MSGPACK_CT_MAP_KEY:
c->map_key = obj;
c->ct = MSGPACK_CT_MAP_VALUE;
goto _header_again;
case MSGPACK_CT_MAP_VALUE:
if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
if(--c->count == 0) {
obj = c->obj;
--top;
/*printf("stack pop %d\n", top);*/
goto _push;
}
c->ct = MSGPACK_CT_MAP_KEY;
goto _header_again;
default: default:
if((size_t)(pe - p) < trail) { goto _out; } goto _failed;
n = p; p += trail - 1;
switch(cs) {
//case MSGPACK_CS_
//case MSGPACK_CS_
case MSGPACK_CS_FLOAT: {
union { uint32_t i; float f; } mem;
_msgpack_load32(uint32_t, n, &mem.i);
push_fixed_value(_float, mem.f); }
case MSGPACK_CS_DOUBLE: {
union { uint64_t i; double f; } mem;
_msgpack_load64(uint64_t, n, &mem.i);
#if defined(__arm__) && !(__ARM_EABI__) // arm-oabi
// https://github.com/msgpack/msgpack-perl/pull/1
mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
#endif
push_fixed_value(_double, mem.f); }
case MSGPACK_CS_UINT_8:
push_fixed_value(_uint8, *(uint8_t*)n);
case MSGPACK_CS_UINT_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
push_fixed_value(_uint16, tmp);
}
case MSGPACK_CS_UINT_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
push_fixed_value(_uint32, tmp);
}
case MSGPACK_CS_UINT_64:{
uint64_t tmp;
_msgpack_load64(uint64_t,n,&tmp);
push_fixed_value(_uint64, tmp);
}
case MSGPACK_CS_INT_8:
push_fixed_value(_int8, *(int8_t*)n);
case MSGPACK_CS_INT_16:{
int16_t tmp;
_msgpack_load16(int16_t,n,&tmp);
push_fixed_value(_int16, tmp);
}
case MSGPACK_CS_INT_32:{
int32_t tmp;
_msgpack_load32(int32_t,n,&tmp);
push_fixed_value(_int32, tmp);
}
case MSGPACK_CS_INT_64:{
int64_t tmp;
_msgpack_load64(int64_t,n,&tmp);
push_fixed_value(_int64, tmp);
}
case MSGPACK_CS_FIXEXT_1:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 1+1, _ext_zero);
case MSGPACK_CS_FIXEXT_2:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 2+1, _ext_zero);
case MSGPACK_CS_FIXEXT_4:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 4+1, _ext_zero);
case MSGPACK_CS_FIXEXT_8:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 8+1, _ext_zero);
case MSGPACK_CS_FIXEXT_16:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero);
case MSGPACK_CS_STR_8:
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, *(uint8_t*)n, _str_zero);
case MSGPACK_CS_BIN_8:
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, *(uint8_t*)n, _bin_zero);
case MSGPACK_CS_EXT_8:
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, (*(uint8_t*)n) + 1, _ext_zero);
case MSGPACK_CS_STR_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
}
case MSGPACK_CS_BIN_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
}
case MSGPACK_CS_EXT_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
}
case MSGPACK_CS_STR_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
}
case MSGPACK_CS_BIN_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
}
case MSGPACK_CS_EXT_32:{
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
}
case MSGPACK_ACS_STR_VALUE:
_str_zero:
push_variable_value(_str, data, n, trail);
case MSGPACK_ACS_BIN_VALUE:
_bin_zero:
push_variable_value(_bin, data, n, trail);
case MSGPACK_ACS_EXT_VALUE:
_ext_zero:
push_variable_value(_ext, data, n, trail);
case MSGPACK_CS_ARRAY_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
}
case MSGPACK_CS_ARRAY_32:{
/* FIXME security guard */
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
}
case MSGPACK_CS_MAP_16:{
uint16_t tmp;
_msgpack_load16(uint16_t,n,&tmp);
start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
}
case MSGPACK_CS_MAP_32:{
/* FIXME security guard */
uint32_t tmp;
_msgpack_load32(uint32_t,n,&tmp);
start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
}
default:
goto _failed;
}
} }
_push: _header_again:
if(top == 0) { goto _finish; } cs = MSGPACK_CS_HEADER;
c = &stack[top-1]; ++p;
switch(c->ct) { } while(p != pe);
case MSGPACK_CT_ARRAY_ITEM: goto _out;
if(msgpack_unpack_callback(_array_item)(user, &c->obj, obj) < 0) { goto _failed; }
if(--c->count == 0) {
obj = c->obj;
--top;
/*printf("stack pop %d\n", top);*/
goto _push;
}
goto _header_again;
case MSGPACK_CT_MAP_KEY:
c->map_key = obj;
c->ct = MSGPACK_CT_MAP_VALUE;
goto _header_again;
case MSGPACK_CT_MAP_VALUE:
if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
if(--c->count == 0) {
obj = c->obj;
--top;
/*printf("stack pop %d\n", top);*/
goto _push;
}
c->ct = MSGPACK_CT_MAP_KEY;
goto _header_again;
default:
goto _failed;
}
_header_again: _finish:
cs = MSGPACK_CS_HEADER; stack[0].obj = obj;
++p; ++p;
} while(p != pe); ret = 1;
goto _out; /*printf("-- finish --\n"); */
goto _end;
_failed:
/*printf("** FAILED **\n"); */
ret = -1;
goto _end;
_finish: _out:
stack[0].obj = obj; ret = 0;
++p; goto _end;
ret = 1;
/*printf("-- finish --\n"); */
goto _end;
_failed: _end:
/*printf("** FAILED **\n"); */ ctx->cs = cs;
ret = -1; ctx->trail = trail;
goto _end; ctx->top = top;
*off = (size_t)(p - (const unsigned char*)data);
_out: return ret;
ret = 0; }
goto _end;
_end:
ctx->cs = cs;
ctx->trail = trail;
ctx->top = top;
*off = (size_t)(p - (const unsigned char*)data);
return ret;
} }
#undef msgpack_unpack_func #undef msgpack_unpack_func
#undef msgpack_unpack_callback #undef msgpack_unpack_callback
#undef msgpack_unpack_struct #undef msgpack_unpack_struct

View File

@ -71,33 +71,39 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
case MSGPACK_OBJECT_ARRAY: case MSGPACK_OBJECT_ARRAY:
{ {
int ret = msgpack_pack_array(pk, d.via.array.size); int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; } if(ret < 0) {
return ret;
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
} }
else {
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}
return 0; return 0;
}
} }
case MSGPACK_OBJECT_MAP: case MSGPACK_OBJECT_MAP:
{ {
int ret = msgpack_pack_map(pk, d.via.map.size); int ret = msgpack_pack_map(pk, d.via.map.size);
if(ret < 0) { return ret; } if(ret < 0) {
return ret;
msgpack_object_kv* kv = d.via.map.ptr;
msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
for(; kv != kvend; ++kv) {
ret = msgpack_pack_object(pk, kv->key);
if(ret < 0) { return ret; }
ret = msgpack_pack_object(pk, kv->val);
if(ret < 0) { return ret; }
} }
else {
msgpack_object_kv* kv = d.via.map.ptr;
msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
for(; kv != kvend; ++kv) {
ret = msgpack_pack_object(pk, kv->key);
if(ret < 0) { return ret; }
ret = msgpack_pack_object(pk, kv->val);
if(ret < 0) { return ret; }
}
return 0; return 0;
}
} }
default: default:
@ -172,9 +178,9 @@ void msgpack_object_print(FILE* out, msgpack_object o)
fprintf(out, "["); fprintf(out, "[");
if(o.via.array.size != 0) { if(o.via.array.size != 0) {
msgpack_object* p = o.via.array.ptr; msgpack_object* p = o.via.array.ptr;
msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
msgpack_object_print(out, *p); msgpack_object_print(out, *p);
++p; ++p;
msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
for(; p < pend; ++p) { for(; p < pend; ++p) {
fprintf(out, ", "); fprintf(out, ", ");
msgpack_object_print(out, *p); msgpack_object_print(out, *p);
@ -187,11 +193,11 @@ void msgpack_object_print(FILE* out, msgpack_object o)
fprintf(out, "{"); fprintf(out, "{");
if(o.via.map.size != 0) { if(o.via.map.size != 0) {
msgpack_object_kv* p = o.via.map.ptr; msgpack_object_kv* p = o.via.map.ptr;
msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
msgpack_object_print(out, p->key); msgpack_object_print(out, p->key);
fprintf(out, "=>"); fprintf(out, "=>");
msgpack_object_print(out, p->val); msgpack_object_print(out, p->val);
++p; ++p;
msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
for(; p < pend; ++p) { for(; p < pend; ++p) {
fprintf(out, ", "); fprintf(out, ", ");
msgpack_object_print(out, p->key); msgpack_object_print(out, p->key);

View File

@ -58,8 +58,8 @@ static int template_execute(
static inline msgpack_object template_callback_root(unpack_user* u) static inline msgpack_object template_callback_root(unpack_user* u)
{ {
MSGPACK_UNUSED(u);
msgpack_object o; msgpack_object o;
MSGPACK_UNUSED(u);
o.type = MSGPACK_OBJECT_NIL; o.type = MSGPACK_OBJECT_NIL;
return o; return o;
} }
@ -306,26 +306,28 @@ static inline _msgpack_atomic_counter_t get_count(void* buffer)
return *(volatile _msgpack_atomic_counter_t*)buffer; return *(volatile _msgpack_atomic_counter_t*)buffer;
} }
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size) bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
{ {
char* buffer;
void* ctx;
msgpack_zone* z;
if(initial_buffer_size < COUNTER_SIZE) { if(initial_buffer_size < COUNTER_SIZE) {
initial_buffer_size = COUNTER_SIZE; initial_buffer_size = COUNTER_SIZE;
} }
char* buffer = (char*)malloc(initial_buffer_size); buffer = (char*)malloc(initial_buffer_size);
if(buffer == NULL) { if(buffer == NULL) {
return false; return false;
} }
void* ctx = malloc(sizeof(template_context)); ctx = malloc(sizeof(template_context));
if(ctx == NULL) { if(ctx == NULL) {
free(buffer); free(buffer);
return false; return false;
} }
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(z == NULL) { if(z == NULL) {
free(ctx); free(ctx);
free(buffer); free(buffer);
@ -357,7 +359,6 @@ void msgpack_unpacker_destroy(msgpack_unpacker* mpac)
decr_count(mpac->buffer); decr_count(mpac->buffer);
} }
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size) msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size)
{ {
msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker)); msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker));
@ -394,6 +395,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
} }
if(mpac->off == COUNTER_SIZE) { if(mpac->off == COUNTER_SIZE) {
char* tmp;
size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE
while(next_size < size + mpac->used) { while(next_size < size + mpac->used) {
size_t tmp_next_size = next_size * 2; size_t tmp_next_size = next_size * 2;
@ -404,7 +406,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
next_size = tmp_next_size; next_size = tmp_next_size;
} }
char* tmp = (char*)realloc(mpac->buffer, next_size); tmp = (char*)realloc(mpac->buffer, next_size);
if(tmp == NULL) { if(tmp == NULL) {
return false; return false;
} }
@ -413,6 +415,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
mpac->free = next_size - mpac->used; mpac->free = next_size - mpac->used;
} else { } else {
char* tmp;
size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE
size_t not_parsed = mpac->used - mpac->off; size_t not_parsed = mpac->used - mpac->off;
while(next_size < size + not_parsed + COUNTER_SIZE) { while(next_size < size + not_parsed + COUNTER_SIZE) {
@ -424,7 +427,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
next_size = tmp_next_size; next_size = tmp_next_size;
} }
char* tmp = (char*)malloc(next_size); tmp = (char*)malloc(next_size);
if(tmp == NULL) { if(tmp == NULL) {
return false; return false;
} }
@ -470,16 +473,19 @@ msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
{ {
msgpack_zone* r;
msgpack_zone* old;
if(!msgpack_unpacker_flush_zone(mpac)) { if(!msgpack_unpacker_flush_zone(mpac)) {
return NULL; return NULL;
} }
msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(r == NULL) { if(r == NULL) {
return NULL; return NULL;
} }
msgpack_zone* old = mpac->z; old = mpac->z;
mpac->z = r; mpac->z = r;
CTX_CAST(mpac->ctx)->user.z = mpac->z; CTX_CAST(mpac->ctx)->user.z = mpac->z;
@ -514,9 +520,11 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac)
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result) msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result)
{ {
int ret;
msgpack_unpacked_destroy(result); msgpack_unpacked_destroy(result);
int ret = msgpack_unpacker_execute(mpac); ret = msgpack_unpacker_execute(mpac);
if(ret < 0) { if(ret < 0) {
result->zone = NULL; result->zone = NULL;
@ -546,40 +554,42 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
// FIXME // FIXME
return MSGPACK_UNPACK_CONTINUE; return MSGPACK_UNPACK_CONTINUE;
} }
else {
int e;
template_context ctx;
template_init(&ctx);
template_context ctx; ctx.user.z = result_zone;
template_init(&ctx); ctx.user.referenced = false;
ctx.user.z = result_zone; e = template_execute(&ctx, data, len, &noff);
ctx.user.referenced = false; if(e < 0) {
return MSGPACK_UNPACK_PARSE_ERROR;
}
int e = template_execute(&ctx, data, len, &noff); if(off != NULL) { *off = noff; }
if(e < 0) {
return MSGPACK_UNPACK_PARSE_ERROR; if(e == 0) {
return MSGPACK_UNPACK_CONTINUE;
}
*result = template_data(&ctx);
if(noff < len) {
return MSGPACK_UNPACK_EXTRA_BYTES;
}
return MSGPACK_UNPACK_SUCCESS;
} }
if(off != NULL) { *off = noff; }
if(e == 0) {
return MSGPACK_UNPACK_CONTINUE;
}
*result = template_data(&ctx);
if(noff < len) {
return MSGPACK_UNPACK_EXTRA_BYTES;
}
return MSGPACK_UNPACK_SUCCESS;
} }
msgpack_unpack_return msgpack_unpack_return
msgpack_unpack_next(msgpack_unpacked* result, msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off) const char* data, size_t len, size_t* off)
{ {
size_t noff = 0;
msgpack_unpacked_destroy(result); msgpack_unpacked_destroy(result);
size_t noff = 0;
if(off != NULL) { noff = *off; } if(off != NULL) { noff = *off; }
if(len <= noff) { if(len <= noff) {
@ -593,29 +603,31 @@ msgpack_unpack_next(msgpack_unpacked* result,
if (!result->zone) { if (!result->zone) {
return MSGPACK_UNPACK_NOMEM_ERROR; return MSGPACK_UNPACK_NOMEM_ERROR;
} }
else {
int e;
template_context ctx;
template_init(&ctx);
template_context ctx; ctx.user.z = result->zone;
template_init(&ctx); ctx.user.referenced = false;
ctx.user.z = result->zone; e = template_execute(&ctx, data, len, &noff);
ctx.user.referenced = false; if(e < 0) {
msgpack_zone_free(result->zone);
result->zone = NULL;
return MSGPACK_UNPACK_PARSE_ERROR;
}
int e = template_execute(&ctx, data, len, &noff); if(off != NULL) { *off = noff; }
if(e < 0) {
msgpack_zone_free(result->zone); if(e == 0) {
result->zone = NULL; return MSGPACK_UNPACK_CONTINUE;
return MSGPACK_UNPACK_PARSE_ERROR; }
result->data = template_data(&ctx);
return MSGPACK_UNPACK_SUCCESS;
} }
if(off != NULL) { *off = noff; }
if(e == 0) {
return MSGPACK_UNPACK_CONTINUE;
}
result->data = template_data(&ctx);
return MSGPACK_UNPACK_SUCCESS;
} }
#if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND) #if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND)

View File

@ -29,15 +29,19 @@ struct msgpack_vrefbuffer_chunk {
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
size_t ref_size, size_t chunk_size) size_t ref_size, size_t chunk_size)
{ {
size_t nfirst;
struct iovec* array;
msgpack_vrefbuffer_chunk* chunk;
vbuf->chunk_size = chunk_size; vbuf->chunk_size = chunk_size;
vbuf->ref_size = vbuf->ref_size =
ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ? ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ?
ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ; ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ;
size_t nfirst = (sizeof(struct iovec) < 72/2) ? nfirst = (sizeof(struct iovec) < 72/2) ?
72 / sizeof(struct iovec) : 8; 72 / sizeof(struct iovec) : 8;
struct iovec* array = (struct iovec*)malloc( array = (struct iovec*)malloc(
sizeof(struct iovec) * nfirst); sizeof(struct iovec) * nfirst);
if(array == NULL) { if(array == NULL) {
return false; return false;
@ -47,21 +51,22 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
vbuf->end = array + nfirst; vbuf->end = array + nfirst;
vbuf->array = array; vbuf->array = array;
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc( chunk = (msgpack_vrefbuffer_chunk*)malloc(
sizeof(msgpack_vrefbuffer_chunk) + chunk_size); sizeof(msgpack_vrefbuffer_chunk) + chunk_size);
if(chunk == NULL) { if(chunk == NULL) {
free(array); free(array);
return false; return false;
} }
else {
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; ib->free = chunk_size;
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
ib->head = chunk;
chunk->next = NULL;
ib->free = chunk_size; return true;
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); }
ib->head = chunk;
chunk->next = NULL;
return true;
} }
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf) void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
@ -89,13 +94,15 @@ void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
c = n; c = n;
} }
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; {
msgpack_vrefbuffer_chunk* chunk = ib->head; msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
chunk->next = NULL; msgpack_vrefbuffer_chunk* chunk = ib->head;
ib->free = vbuf->chunk_size; chunk->next = NULL;
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); ib->free = vbuf->chunk_size;
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
vbuf->tail = vbuf->array; vbuf->tail = vbuf->array;
}
} }
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
@ -127,14 +134,16 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
const char* buf, size_t len) const char* buf, size_t len)
{ {
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
char* m;
if(ib->free < len) { if(ib->free < len) {
msgpack_vrefbuffer_chunk* chunk;
size_t sz = vbuf->chunk_size; size_t sz = vbuf->chunk_size;
if(sz < len) { if(sz < len) {
sz = len; sz = len;
} }
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc( chunk = (msgpack_vrefbuffer_chunk*)malloc(
sizeof(msgpack_vrefbuffer_chunk) + sz); sizeof(msgpack_vrefbuffer_chunk) + sz);
if(chunk == NULL) { if(chunk == NULL) {
return -1; return -1;
@ -146,7 +155,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
} }
char* m = ib->ptr; m = ib->ptr;
memcpy(m, buf, len); memcpy(m, buf, len);
ib->free -= len; ib->free -= len;
ib->ptr += len; ib->ptr += len;
@ -172,57 +181,60 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
empty->next = NULL; empty->next = NULL;
{
const size_t nused = (size_t)(vbuf->tail - vbuf->array); const size_t nused = (size_t)(vbuf->tail - vbuf->array);
if(to->tail + nused < vbuf->end) { if(to->tail + nused < vbuf->end) {
const size_t tosize = (size_t)(to->tail - to->array); struct iovec* nvec;
const size_t reqsize = nused + tosize; const size_t tosize = (size_t)(to->tail - to->array);
size_t nnext = (size_t)(to->end - to->array) * 2; const size_t reqsize = nused + tosize;
while(nnext < reqsize) { size_t nnext = (size_t)(to->end - to->array) * 2;
size_t tmp_nnext = nnext * 2; while(nnext < reqsize) {
if (tmp_nnext <= nnext) { size_t tmp_nnext = nnext * 2;
nnext = reqsize; if (tmp_nnext <= nnext) {
break; nnext = reqsize;
break;
}
nnext = tmp_nnext;
} }
nnext = tmp_nnext;
nvec = (struct iovec*)realloc(
to->array, sizeof(struct iovec)*nnext);
if(nvec == NULL) {
free(empty);
return -1;
}
to->array = nvec;
to->end = nvec + nnext;
to->tail = nvec + tosize;
} }
struct iovec* nvec = (struct iovec*)realloc( memcpy(to->tail, vbuf->array, sizeof(struct iovec)*nused);
to->array, sizeof(struct iovec)*nnext);
if(nvec == NULL) { to->tail += nused;
free(empty); vbuf->tail = vbuf->array;
return -1;
{
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer;
msgpack_vrefbuffer_chunk* last = ib->head;
while(last->next != NULL) {
last = last->next;
}
last->next = toib->head;
toib->head = ib->head;
if(toib->free < ib->free) {
toib->free = ib->free;
toib->ptr = ib->ptr;
}
ib->head = empty;
ib->free = sz;
ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk);
} }
to->array = nvec;
to->end = nvec + nnext;
to->tail = nvec + tosize;
} }
memcpy(to->tail, vbuf->array, sizeof(struct iovec)*nused);
to->tail += nused;
vbuf->tail = vbuf->array;
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer;
msgpack_vrefbuffer_chunk* last = ib->head;
while(last->next != NULL) {
last = last->next;
}
last->next = toib->head;
toib->head = ib->head;
if(toib->free < ib->free) {
toib->free = ib->free;
toib->ptr = ib->ptr;
}
ib->head = empty;
ib->free = sz;
ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk);
return 0; return 0;
} }

View File

@ -75,6 +75,7 @@ static inline void clear_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_si
void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size) void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
{ {
msgpack_zone_chunk_list* const cl = &zone->chunk_list; msgpack_zone_chunk_list* const cl = &zone->chunk_list;
msgpack_zone_chunk* chunk;
size_t sz = zone->chunk_size; size_t sz = zone->chunk_size;
@ -87,16 +88,20 @@ void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
sz = tmp_sz; sz = tmp_sz;
} }
msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc( chunk = (msgpack_zone_chunk*)malloc(
sizeof(msgpack_zone_chunk) + sz); sizeof(msgpack_zone_chunk) + sz);
if (chunk == NULL) return NULL; if (chunk == NULL) {
char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk); return NULL;
chunk->next = cl->head; }
cl->head = chunk; else {
cl->free = sz - size; char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
cl->ptr = ptr + size; chunk->next = cl->head;
cl->head = chunk;
cl->free = sz - size;
cl->ptr = ptr + size;
return ptr; return ptr;
}
} }
@ -131,6 +136,7 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
void (*func)(void* data), void* data) void (*func)(void* data), void* data)
{ {
msgpack_zone_finalizer_array* const fa = &zone->finalizer_array; msgpack_zone_finalizer_array* const fa = &zone->finalizer_array;
msgpack_zone_finalizer* tmp;
const size_t nused = (size_t)(fa->end - fa->array); const size_t nused = (size_t)(fa->end - fa->array);
@ -143,8 +149,7 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
nnext = nused * 2; nnext = nused * 2;
} }
msgpack_zone_finalizer* tmp = tmp = (msgpack_zone_finalizer*)realloc(fa->array,
(msgpack_zone_finalizer*)realloc(fa->array,
sizeof(msgpack_zone_finalizer) * nnext); sizeof(msgpack_zone_finalizer) * nnext);
if(tmp == NULL) { if(tmp == NULL) {
return false; return false;