Replaced m_stack legacy array with std::vector.

This commit is contained in:
Takatoshi Kondo 2015-02-03 23:36:56 +09:00
parent d6a7bd1995
commit 46e2398e02

View File

@ -410,16 +410,17 @@ inline void load(T& dst, const char* n, typename msgpack::enable_if<sizeof(T) ==
class context { class context {
public: public:
context(unpack_reference_func f, void* user_data, unpack_limit const& limit) context(unpack_reference_func f, void* user_data, unpack_limit const& limit)
:m_trail(0), m_user(f, user_data, limit), m_cs(CS_HEADER), m_top(0) :m_trail(0), m_user(f, user_data, limit), m_cs(CS_HEADER)
{ {
m_stack[0].set_obj(object()); m_stack.reserve(MSGPACK_EMBED_STACK_SIZE);
m_stack.push_back(unpack_stack());
} }
void init() void init()
{ {
m_cs = CS_HEADER; m_cs = CS_HEADER;
m_trail = 0; m_trail = 0;
m_top = 0; m_stack.resize(1);
m_stack[0].set_obj(object()); m_stack[0].set_obj(object());
} }
@ -454,64 +455,55 @@ private:
object& obj, object& obj,
const char* load_pos, const char* load_pos,
std::size_t& off) { std::size_t& off) {
if(m_top < MSGPACK_EMBED_STACK_SIZE /* FIXME */) {
typename value<T>::type tmp; typename value<T>::type tmp;
load<T>(tmp, load_pos); load<T>(tmp, load_pos);
f(m_user, tmp, m_stack[m_top].obj()); f(m_user, tmp, m_stack.back().obj());
if(tmp == 0) { if(tmp == 0) {
obj = m_stack[m_top].obj(); obj = m_stack.back().obj();
int ret = push_proc(obj, off); int ret = push_proc(obj, off);
if (ret != 0) return ret; if (ret != 0) return ret;
} }
else { else {
m_stack[m_top].set_container_type(container_type); m_stack.back().set_container_type(container_type);
m_stack[m_top].set_count(tmp); m_stack.back().set_count(tmp);
++m_top; m_stack.push_back(unpack_stack());
m_cs = CS_HEADER; m_cs = CS_HEADER;
++m_current; ++m_current;
} }
}
else {
off = m_current - m_start;
return -1;
}
return 0; return 0;
} }
int push_item(object& obj) { int push_item(object& obj) {
bool finish = false; bool finish = false;
while (!finish) { while (!finish) {
if(m_top == 0) { if(m_stack.size() == 1) {
return 1; return 1;
} }
m_stack_idx = m_top - 1; unpack_stack& sp = *(m_stack.end() - 2);
unpack_stack* sp = &m_stack[m_stack_idx]; switch(sp.container_type()) {
switch(sp->container_type()) {
case CT_ARRAY_ITEM: case CT_ARRAY_ITEM:
unpack_array_item(sp->obj(), obj); unpack_array_item(sp.obj(), obj);
if(sp->decl_count() == 0) { if(sp.decl_count() == 0) {
obj = sp->obj(); obj = sp.obj();
--m_top; m_stack.pop_back();
/*printf("stack pop %d\n", m_top);*/
} }
else { else {
finish = true; finish = true;
} }
break; break;
case CT_MAP_KEY: case CT_MAP_KEY:
sp->set_map_key(obj); sp.set_map_key(obj);
sp->set_container_type(CT_MAP_VALUE); sp.set_container_type(CT_MAP_VALUE);
finish = true; finish = true;
break; break;
case CT_MAP_VALUE: case CT_MAP_VALUE:
unpack_map_item(sp->obj(), sp->map_key(), obj); unpack_map_item(sp.obj(), sp.map_key(), obj);
if(sp->decl_count() == 0) { if(sp.decl_count() == 0) {
obj = sp->obj(); obj = sp.obj();
--m_top; m_stack.pop_back();
/*printf("stack pop %d\n", m_top);*/
} }
else { else {
sp->set_container_type(CT_MAP_KEY); sp.set_container_type(CT_MAP_KEY);
finish = true; finish = true;
} }
break; break;
@ -551,9 +543,7 @@ private:
std::size_t m_trail; std::size_t m_trail;
unpack_user m_user; unpack_user m_user;
uint32_t m_cs; uint32_t m_cs;
uint32_t m_top; std::vector<unpack_stack> m_stack;
uint32_t m_stack_idx;
unpack_stack m_stack[MSGPACK_EMBED_STACK_SIZE];
}; };
template <> template <>
@ -567,7 +557,6 @@ inline int context::execute(const char* data, std::size_t len, std::size_t& off)
m_start = data; m_start = data;
m_current = data + off; m_current = data + off;
m_stack_idx = 0;
const char* const pe = data + len; const char* const pe = data + len;
const char* n = nullptr; const char* n = nullptr;