Modified unpack functions to class member functions.

This commit is contained in:
Takatoshi Kondo 2013-08-27 11:24:48 +09:00
parent ec5c1194fc
commit 11afd4820f

View File

@ -47,12 +47,17 @@ namespace msgpack {
namespace detail { namespace detail {
struct unpack_user { class unpack_user {
zone* z; public:
bool referenced; zone* z() const { return z_; }
void set_z(zone* z) { z_ = z; }
bool referenced() const { return referenced_; }
void set_referenced(bool referenced) { referenced_ = referenced; }
private:
zone* z_;
bool referenced_;
}; };
static inline object template_callback_root(unpack_user* u) static inline object template_callback_root(unpack_user* u)
{ object o = {}; return o; } { object o = {}; return o; }
@ -103,7 +108,7 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, object
{ {
o->type = type::ARRAY; o->type = type::ARRAY;
o->via.array.size = 0; o->via.array.size = 0;
o->via.array.ptr = (object*)u->z->malloc(n*sizeof(object)); o->via.array.ptr = (object*)u->z()->malloc(n*sizeof(object));
if(o->via.array.ptr == NULL) { return -1; } if(o->via.array.ptr == NULL) { return -1; }
return 0; return 0;
} }
@ -115,7 +120,7 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, object*
{ {
o->type = type::MAP; o->type = type::MAP;
o->via.map.size = 0; o->via.map.size = 0;
o->via.map.ptr = (object_kv*)u->z->malloc(n*sizeof(object_kv)); o->via.map.ptr = (object_kv*)u->z()->malloc(n*sizeof(object_kv));
if(o->via.map.ptr == NULL) { return -1; } if(o->via.map.ptr == NULL) { return -1; }
return 0; return 0;
} }
@ -133,27 +138,30 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
o->type = type::RAW; o->type = type::RAW;
o->via.raw.ptr = p; o->via.raw.ptr = p;
o->via.raw.size = l; o->via.raw.size = l;
u->referenced = true; u->set_referenced(true);
return 0; return 0;
} }
struct template_unpack_stack { class template_unpack_stack {
object obj; public:
size_t count; object obj() const { return obj_; }
unsigned int ct; object& obj() { return obj_; }
object map_key; void setObj(object obj) { obj_ = obj; }
size_t count() const { return count_; }
void set_count(size_t count) { count_ = count; }
size_t decl_count() { return --count_; }
unsigned int ct() const { return ct_; }
void set_ct(unsigned int ct) { ct_ = ct; }
object map_key() const { return map_key_; }
void set_map_key(object map_key) { map_key_ = map_key; }
private:
object obj_;
size_t count_;
unsigned int ct_;
object map_key_;
}; };
struct template_context {
unpack_user user;
unsigned int cs;
unsigned int trail;
unsigned int top;
template_unpack_stack stack[MSGPACK_EMBED_STACK_SIZE];
};
inline void init_count(void* buffer) inline void init_count(void* buffer)
{ {
*(volatile _msgpack_atomic_counter_t*)buffer = 1; *(volatile _msgpack_atomic_counter_t*)buffer = 1;
@ -178,26 +186,37 @@ inline _msgpack_atomic_counter_t get_count(void* buffer)
return *(volatile _msgpack_atomic_counter_t*)buffer; return *(volatile _msgpack_atomic_counter_t*)buffer;
} }
inline void template_init(template_context& ctx) class template_context {
public:
template_context():cs_(CS_HEADER), trail_(0), top_(0)
{ {
ctx.cs = CS_HEADER; stack_[0].setObj(template_callback_root(&user_));
ctx.trail = 0;
ctx.top = 0;
ctx.stack[0].obj = template_callback_root(&ctx.user);
} }
object template_data(template_context const& ctx) void init()
{ {
return ctx.stack[0].obj; cs_ = CS_HEADER;
trail_ = 0;
top_ = 0;
stack_[0].setObj(template_callback_root(&user_));
} }
template <typename T> object data() const
inline unsigned int next_cs(T p)
{ {
return (unsigned int)*p & 0x1f; return stack_[0].obj();
} }
int template_execute(template_context& ctx, const char* data, size_t len, size_t* off) unpack_user& user()
{
return user_;
}
unpack_user const& user() const
{
return user_;
}
int execute(const char* data, size_t len, size_t* off)
{ {
assert(len >= *off); assert(len >= *off);
@ -205,14 +224,11 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
const unsigned char* const pe = (unsigned char*)data + len; const unsigned char* const pe = (unsigned char*)data + len;
const void* n = nullptr; const void* n = nullptr;
unsigned int trail = ctx.trail; unsigned int trail = trail_;
unsigned int cs = ctx.cs; unsigned int cs = cs_;
unsigned int top = ctx.top; unsigned int top = top_;
template_unpack_stack* stack = ctx.stack; template_unpack_stack* stack = stack_;
/* unpack_user* user = &user_;
unsigned int stack_size = ctx.stack_size;
*/
unpack_user* user = &ctx.user;
object obj; object obj;
template_unpack_stack* c = nullptr; template_unpack_stack* c = nullptr;
@ -296,19 +312,19 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
} else if(0x90 <= *p && *p <= 0x9f) { // FixArray } else if(0x90 <= *p && *p <= 0x9f) { // FixArray
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_array(user, ((unsigned int)*p) & 0x0f, &stack[top].obj) < 0) { goto _failed; } if(template_callback_array(user, ((unsigned int)*p) & 0x0f, &stack[top].obj()) < 0) { goto _failed; }
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj; goto _push; } if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_ARRAY_ITEM; stack[top].set_ct(CT_ARRAY_ITEM);
stack[top].count = ((unsigned int)*p) & 0x0f; stack[top].set_count(((unsigned int)*p) & 0x0f);
++top; ++top;
goto _header_again; goto _header_again;
} else if(0x80 <= *p && *p <= 0x8f) { // FixMap } else if(0x80 <= *p && *p <= 0x8f) { // FixMap
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_map(user, ((unsigned int)*p) & 0x0f, &stack[top].obj) < 0) { goto _failed; } if(template_callback_map(user, ((unsigned int)*p) & 0x0f, &stack[top].obj()) < 0) { goto _failed; }
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj; goto _push; } if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_MAP_KEY; stack[top].set_ct(CT_MAP_KEY);
stack[top].count = ((unsigned int)*p) & 0x0f; stack[top].set_count(((unsigned int)*p) & 0x0f);
++top; ++top;
goto _header_again; goto _header_again;
@ -391,37 +407,37 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
goto _push; goto _push;
case CS_ARRAY_16: case CS_ARRAY_16:
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_array(user, _msgpack_load16(uint16_t, n), &stack[top].obj) < 0) { goto _failed; } if(template_callback_array(user, _msgpack_load16(uint16_t, n), &stack[top].obj()) < 0) { goto _failed; }
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj; goto _push; } if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_ARRAY_ITEM; stack[top].set_ct(CT_ARRAY_ITEM);
stack[top].count = _msgpack_load16(uint16_t, n); stack[top].set_count(_msgpack_load16(uint16_t, n));
++top; ++top;
goto _header_again; goto _header_again;
case CS_ARRAY_32: case CS_ARRAY_32:
/* FIXME security guard */ /* FIXME security guard */
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_array(user, _msgpack_load32(uint32_t, n), &stack[top].obj) < 0) { goto _failed; } if(template_callback_array(user, _msgpack_load32(uint32_t, n), &stack[top].obj()) < 0) { goto _failed; }
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj; goto _push; } if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_ARRAY_ITEM; stack[top].set_ct(CT_ARRAY_ITEM);
stack[top].count = _msgpack_load32(uint32_t, n); stack[top].set_count(_msgpack_load32(uint32_t, n));
++top; ++top;
goto _header_again; goto _header_again;
case CS_MAP_16: case CS_MAP_16:
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_map(user, _msgpack_load16(uint16_t, n), &stack[top].obj) < 0) { goto _failed; } if(template_callback_map(user, _msgpack_load16(uint16_t, n), &stack[top].obj()) < 0) { goto _failed; }
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj; goto _push; } if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_MAP_KEY; stack[top].set_ct(CT_MAP_KEY);
stack[top].count = _msgpack_load16(uint16_t, n); stack[top].set_count(_msgpack_load16(uint16_t, n));
++top; ++top;
goto _header_again; goto _header_again;
case CS_MAP_32: case CS_MAP_32:
/* FIXME security guard */ /* FIXME security guard */
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
if(template_callback_map(user, _msgpack_load32(uint32_t, n), &stack[top].obj) < 0) { goto _failed; } if(template_callback_map(user, _msgpack_load32(uint32_t, n), &stack[top].obj()) < 0) { goto _failed; }
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj; goto _push; } if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj(); goto _push; }
stack[top].ct = CT_MAP_KEY; stack[top].set_ct(CT_MAP_KEY);
stack[top].count = _msgpack_load32(uint32_t, n); stack[top].set_count(_msgpack_load32(uint32_t, n));
++top; ++top;
goto _header_again; goto _header_again;
@ -433,29 +449,29 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
_push: _push:
if(top == 0) { goto _finish; } if(top == 0) { goto _finish; }
c = &stack[top-1]; c = &stack[top-1];
switch(c->ct) { switch(c->ct()) {
case CT_ARRAY_ITEM: case CT_ARRAY_ITEM:
if(template_callback_array_item(user, &c->obj, obj) < 0) { goto _failed; } if(template_callback_array_item(user, &c->obj(), obj) < 0) { goto _failed; }
if(--c->count == 0) { if(c->decl_count() == 0) {
obj = c->obj; obj = c->obj();
--top; --top;
/*printf("stack pop %d\n", top);*/ /*printf("stack pop %d\n", top);*/
goto _push; goto _push;
} }
goto _header_again; goto _header_again;
case CT_MAP_KEY: case CT_MAP_KEY:
c->map_key = obj; c->set_map_key(obj);
c->ct = CT_MAP_VALUE; c->set_ct(CT_MAP_VALUE);
goto _header_again; goto _header_again;
case CT_MAP_VALUE: case CT_MAP_VALUE:
if(template_callback_map_item(user, &c->obj, c->map_key, obj) < 0) { goto _failed; } if(template_callback_map_item(user, &c->obj(), c->map_key(), obj) < 0) { goto _failed; }
if(--c->count == 0) { if(c->decl_count() == 0) {
obj = c->obj; obj = c->obj();
--top; --top;
/*printf("stack pop %d\n", top);*/ /*printf("stack pop %d\n", top);*/
goto _push; goto _push;
} }
c->ct = CT_MAP_KEY; c->set_ct(CT_MAP_KEY);
goto _header_again; goto _header_again;
default: default:
@ -470,7 +486,7 @@ _header_again:
_finish: _finish:
stack[0].obj = obj; stack[0].setObj(obj);
++p; ++p;
ret = 1; ret = 1;
/*printf("-- finish --\n"); */ /*printf("-- finish --\n"); */
@ -486,16 +502,28 @@ _out:
goto _end; goto _end;
_end: _end:
ctx.cs = cs; cs_ = cs;
ctx.trail = trail; trail_ = trail;
ctx.top = top; top_ = top;
*off = p - (const unsigned char*)data; *off = p - (const unsigned char*)data;
return ret; return ret;
} }
private:
template <typename T>
static unsigned int next_cs(T p)
{
return (unsigned int)*p & 0x1f;
}
private:
unpack_user user_;
unsigned int cs_;
unsigned int trail_;
unsigned int top_;
template_unpack_stack stack_[MSGPACK_EMBED_STACK_SIZE];
};
} // detail } // detail
@ -689,9 +717,9 @@ inline unpacker::unpacker(size_t initial_buffer_size)
detail::init_count(buffer_); detail::init_count(buffer_);
detail::template_init(ctx_); ctx_.init();
ctx_.user.z = z_; ctx_.user().set_z(z_);
ctx_.user.referenced = false; ctx_.user().set_referenced(false);
} }
inline unpacker::~unpacker() inline unpacker::~unpacker()
@ -710,7 +738,7 @@ inline void unpacker::reserve_buffer(size_t size)
inline void unpacker::expand_buffer(size_t size) inline void unpacker::expand_buffer(size_t size)
{ {
if(used_ == off_ && detail::get_count(buffer_) == 1 if(used_ == off_ && detail::get_count(buffer_) == 1
&& !ctx_.user.referenced) { && !ctx_.user().referenced()) {
// rewind buffer // rewind buffer
free_ += used_ - COUNTER_SIZE; free_ += used_ - COUNTER_SIZE;
used_ = COUNTER_SIZE; used_ = COUNTER_SIZE;
@ -749,7 +777,7 @@ inline void unpacker::expand_buffer(size_t size)
::memcpy(tmp+COUNTER_SIZE, buffer_ + off_, not_parsed); ::memcpy(tmp+COUNTER_SIZE, buffer_ + off_, not_parsed);
if(ctx_.user.referenced) { if(ctx_.user().referenced()) {
try { try {
z_->push_finalizer(&detail::decl_count, buffer_); z_->push_finalizer(&detail::decl_count, buffer_);
} }
@ -757,7 +785,7 @@ inline void unpacker::expand_buffer(size_t size)
::free(tmp); ::free(tmp);
throw; throw;
} }
ctx_.user.referenced = false; ctx_.user().set_referenced(false);
} else { } else {
detail::decl_count(buffer_); detail::decl_count(buffer_);
} }
@ -822,8 +850,7 @@ inline bool unpacker::execute()
inline int unpacker::execute_imp() inline int unpacker::execute_imp()
{ {
size_t off = off_; size_t off = off_;
int ret = detail::template_execute(ctx_, int ret = ctx_.execute(buffer_, used_, &off_);
buffer_, used_, &off_);
if(off_ > off) { if(off_ > off) {
parsed_ += off_ - off; parsed_ += off_ - off;
} }
@ -832,7 +859,7 @@ inline int unpacker::execute_imp()
inline object unpacker::data() inline object unpacker::data()
{ {
return template_data(ctx_); return ctx_.data();
} }
inline zone* unpacker::release_zone() inline zone* unpacker::release_zone()
@ -848,7 +875,7 @@ inline zone* unpacker::release_zone()
zone* old = z_; zone* old = z_;
z_ = r; z_ = r;
ctx_.user.z = z_; ctx_.user().set_z(z_);
return old; return old;
} }
@ -860,13 +887,13 @@ inline void unpacker::reset_zone()
inline bool unpacker::flush_zone() inline bool unpacker::flush_zone()
{ {
if(ctx_.user.referenced) { if(ctx_.user().referenced()) {
try { try {
z_->push_finalizer(&detail::decl_count, buffer_); z_->push_finalizer(&detail::decl_count, buffer_);
} catch (...) { } catch (...) {
return false; return false;
} }
ctx_.user.referenced = false; ctx_.user().set_referenced(false);
detail::incr_count(buffer_); detail::incr_count(buffer_);
} }
@ -876,7 +903,7 @@ inline bool unpacker::flush_zone()
inline void unpacker::reset() inline void unpacker::reset()
{ {
detail::template_init(ctx_); ctx_.init();
// don't reset referenced flag // don't reset referenced flag
parsed_ = 0; parsed_ = 0;
} }
@ -926,12 +953,12 @@ unpack_imp(const char* data, size_t len, size_t* off,
} }
detail::template_context ctx; detail::template_context ctx;
detail::template_init(ctx); ctx.init();
ctx.user.z = result_zone; ctx.user().set_z(result_zone);
ctx.user.referenced = false; ctx.user().set_referenced(false);
int e = detail::template_execute(ctx, data, len, &noff); int e = ctx.execute(data, len, &noff);
if(e < 0) { if(e < 0) {
return UNPACK_PARSE_ERROR; return UNPACK_PARSE_ERROR;
} }
@ -942,7 +969,7 @@ unpack_imp(const char* data, size_t len, size_t* off,
return UNPACK_CONTINUE; return UNPACK_CONTINUE;
} }
*result = detail::template_data(ctx); *result = ctx.data();
if(noff < len) { if(noff < len) {
return UNPACK_EXTRA_BYTES; return UNPACK_EXTRA_BYTES;