Fixed zone allocating functions always throw std::bad_alloc() if allocation is failed.

This commit is contained in:
Takatoshi Kondo 2014-08-06 12:16:28 +09:00
parent 5af6d36341
commit ce21ab0ebf
4 changed files with 18 additions and 25 deletions

View File

@ -202,7 +202,7 @@ inline zone* zone::create(size_t chunk_size)
{ {
zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size)); zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size));
if (!z) { if (!z) {
return nullptr; throw std::bad_alloc();
} }
new (z) zone(chunk_size); new (z) zone(chunk_size);
return z; return z;
@ -248,6 +248,7 @@ inline void* zone::allocate_expand(size_t size)
} }
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz)); chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz));
if (!c) throw std::bad_alloc();
char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk); char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk);

View File

@ -247,7 +247,7 @@ inline zone* zone::create(size_t chunk_size)
{ {
zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size)); zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size));
if (!z) { if (!z) {
return nullptr; throw std::bad_alloc();
} }
new (z) zone(chunk_size); new (z) zone(chunk_size);
return z; return z;
@ -293,6 +293,7 @@ inline void* zone::allocate_expand(size_t size)
} }
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz)); chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz));
if (!c) throw std::bad_alloc();
char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk); char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk);

View File

@ -202,7 +202,7 @@ inline zone* zone::create(size_t chunk_size)
{ {
zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size)); zone* z = static_cast<zone*>(::malloc(sizeof(zone) + chunk_size));
if (!z) { if (!z) {
return nullptr; throw std::bad_alloc();
} }
new (z) zone(chunk_size); new (z) zone(chunk_size);
return z; return z;
@ -248,7 +248,7 @@ inline void* zone::allocate_expand(size_t size)
} }
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz)); chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz));
if (!c) return nullptr; if (!c) throw std::bad_alloc();
char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk); char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk);

View File

@ -106,12 +106,10 @@ inline void unpack_false(object& o)
{ o.type = type::BOOLEAN; o.via.boolean = false; } { o.type = type::BOOLEAN; o.via.boolean = false; }
struct unpack_array { struct unpack_array {
bool operator()(unpack_user&u, unsigned int n, object& o) const { void operator()(unpack_user&u, unsigned int n, object& o) const {
o.type = type::ARRAY; o.type = type::ARRAY;
o.via.array.size = 0; o.via.array.size = 0;
o.via.array.ptr = static_cast<object*>(u.zone().allocate_align(n*sizeof(object))); o.via.array.ptr = static_cast<object*>(u.zone().allocate_align(n*sizeof(object)));
if(o.via.array.ptr == nullptr) { return false; }
return true;
} }
}; };
@ -125,12 +123,10 @@ inline void unpack_array_item(object& c, object const& o)
} }
struct unpack_map { struct unpack_map {
bool operator()(unpack_user& u, unsigned int n, object& o) const { void operator()(unpack_user& u, unsigned int n, object& o) const {
o.type = type::MAP; o.type = type::MAP;
o.via.map.size = 0; o.via.map.size = 0;
o.via.map.ptr = static_cast<object_kv*>(u.zone().allocate_align(n*sizeof(object_kv))); o.via.map.ptr = static_cast<object_kv*>(u.zone().allocate_align(n*sizeof(object_kv)));
if(o.via.map.ptr == nullptr) { return false; }
return true;
} }
}; };
@ -623,23 +619,18 @@ private:
if(m_top < MSGPACK_EMBED_STACK_SIZE /* FIXME */) { 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);
if (f(m_user, tmp, m_stack[m_top].obj())) { f(m_user, tmp, m_stack[m_top].obj());
if(tmp == 0) { if(tmp == 0) {
obj = m_stack[m_top].obj(); obj = m_stack[m_top].obj();
int ret = push_proc(obj, off); int ret = push_proc(obj, off);
if (ret != 0) return ret; if (ret != 0) return ret;
}
else {
m_stack[m_top].set_container_type(container_type);
m_stack[m_top].set_count(tmp);
++m_top;
m_cs = CS_HEADER;
++m_current;
}
} }
else { else {
off = m_current - m_start; m_stack[m_top].set_container_type(container_type);
return -1; m_stack[m_top].set_count(tmp);
++m_top;
m_cs = CS_HEADER;
++m_current;
} }
} }
else { else {