mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 06:55:50 +02:00
lang/c/msgpack: C++ binding: enlarged chunk size of zone
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@58 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
@@ -78,7 +78,7 @@ void unpacker::expand_buffer(size_t len)
|
|||||||
if(m_off == 0) {
|
if(m_off == 0) {
|
||||||
size_t next_size;
|
size_t next_size;
|
||||||
if(m_free != 0) { next_size = m_free * 2; }
|
if(m_free != 0) { next_size = m_free * 2; }
|
||||||
else { next_size = MSGPACK_UNPACKER_INITIAL_BUFFER_SIZE; }
|
else { next_size = UNPACKER_INITIAL_BUFFER_SIZE; }
|
||||||
while(next_size < len + m_used) { next_size *= 2; }
|
while(next_size < len + m_used) { next_size *= 2; }
|
||||||
|
|
||||||
// FIXME realloc?
|
// FIXME realloc?
|
||||||
@@ -91,7 +91,7 @@ void unpacker::expand_buffer(size_t len)
|
|||||||
m_free = next_size - m_used;
|
m_free = next_size - m_used;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
size_t next_size = MSGPACK_UNPACKER_INITIAL_BUFFER_SIZE;
|
size_t next_size = UNPACKER_INITIAL_BUFFER_SIZE;
|
||||||
while(next_size < len + m_used - m_off) { next_size *= 2; }
|
while(next_size < len + m_used - m_off) { next_size *= 2; }
|
||||||
|
|
||||||
void* tmp = malloc(next_size);
|
void* tmp = malloc(next_size);
|
||||||
|
@@ -12,6 +12,9 @@
|
|||||||
namespace msgpack {
|
namespace msgpack {
|
||||||
|
|
||||||
|
|
||||||
|
static const size_t UNPACKER_INITIAL_BUFFER_SIZE = MSGPACK_UNPACKER_INITIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
|
||||||
struct unpack_error : public std::runtime_error {
|
struct unpack_error : public std::runtime_error {
|
||||||
unpack_error(const std::string& msg) :
|
unpack_error(const std::string& msg) :
|
||||||
std::runtime_error(msg) { }
|
std::runtime_error(msg) { }
|
||||||
|
25
cpp/zone.cpp
25
cpp/zone.cpp
@@ -5,10 +5,17 @@ namespace msgpack {
|
|||||||
|
|
||||||
void* zone::alloc()
|
void* zone::alloc()
|
||||||
{
|
{
|
||||||
if(m_used >= m_pool.size()*MSGPACK_ZONE_CHUNK_SIZE) {
|
if(m_pool.size()*ZONE_CHUNK_SIZE <= m_used) {
|
||||||
m_pool.push_back(new chunk_t());
|
cell_t* chunk = (cell_t*)malloc(sizeof(cell_t)*ZONE_CHUNK_SIZE);
|
||||||
|
if(!chunk) { throw std::bad_alloc(); }
|
||||||
|
try {
|
||||||
|
m_pool.push_back(chunk);
|
||||||
|
} catch (...) {
|
||||||
|
free(chunk);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void* data = m_pool[m_used/MSGPACK_ZONE_CHUNK_SIZE]->cells[m_used%MSGPACK_ZONE_CHUNK_SIZE].data;
|
void* data = m_pool[m_used/ZONE_CHUNK_SIZE][m_used%ZONE_CHUNK_SIZE].data;
|
||||||
++m_used;
|
++m_used;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -16,21 +23,21 @@ void* zone::alloc()
|
|||||||
void zone::clear()
|
void zone::clear()
|
||||||
{
|
{
|
||||||
if(!m_pool.empty()) {
|
if(!m_pool.empty()) {
|
||||||
for(size_t b=0; b < m_used/MSGPACK_ZONE_CHUNK_SIZE; ++b) {
|
for(size_t b=0; b < m_used/ZONE_CHUNK_SIZE; ++b) {
|
||||||
cell_t* c(m_pool[b]->cells);
|
cell_t* c(m_pool[b]);
|
||||||
for(size_t e=0; e < MSGPACK_ZONE_CHUNK_SIZE; ++e) {
|
for(size_t e=0; e < ZONE_CHUNK_SIZE; ++e) {
|
||||||
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cell_t* c(m_pool.back()->cells);
|
cell_t* c(m_pool.back());
|
||||||
for(size_t e=0; e < m_used%MSGPACK_ZONE_CHUNK_SIZE; ++e) {
|
for(size_t e=0; e < m_used%ZONE_CHUNK_SIZE; ++e) {
|
||||||
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
||||||
}
|
}
|
||||||
|
|
||||||
for(pool_t::iterator it(m_pool.begin()), it_end(m_pool.end());
|
for(pool_t::iterator it(m_pool.begin()), it_end(m_pool.end());
|
||||||
it != it_end;
|
it != it_end;
|
||||||
++it) {
|
++it) {
|
||||||
delete *it;
|
free(*it);
|
||||||
}
|
}
|
||||||
m_pool.clear();
|
m_pool.clear();
|
||||||
}
|
}
|
||||||
|
@@ -8,12 +8,15 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#ifndef MSGPACK_ZONE_CHUNK_SIZE
|
#ifndef MSGPACK_ZONE_CHUNK_SIZE
|
||||||
#define MSGPACK_ZONE_CHUNK_SIZE 64
|
#define MSGPACK_ZONE_CHUNK_SIZE 8*1024
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace msgpack {
|
namespace msgpack {
|
||||||
|
|
||||||
|
|
||||||
|
static const size_t ZONE_CHUNK_SIZE = MSGPACK_ZONE_CHUNK_SIZE;
|
||||||
|
|
||||||
|
|
||||||
class zone {
|
class zone {
|
||||||
public:
|
public:
|
||||||
zone() : m_used(0) { }
|
zone() : m_used(0) { }
|
||||||
@@ -110,11 +113,7 @@ private:
|
|||||||
char data[MAX_OBJECT_SIZE];
|
char data[MAX_OBJECT_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct chunk_t {
|
typedef std::vector<cell_t*> pool_t;
|
||||||
cell_t cells[MSGPACK_ZONE_CHUNK_SIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::vector<chunk_t*> pool_t;
|
|
||||||
pool_t m_pool;
|
pool_t m_pool;
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user