From 97bc0441b1204b3eb38f9cfc03f90e5ee675ab9a Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:09:56 +0000 Subject: [PATCH] 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 --- cpp/unpack.cpp | 4 ++-- cpp/unpack.hpp | 3 +++ cpp/zone.cpp | 25 ++++++++++++++++--------- cpp/zone.hpp.erb | 11 +++++------ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/cpp/unpack.cpp b/cpp/unpack.cpp index 807ded9f..6a6a4e85 100644 --- a/cpp/unpack.cpp +++ b/cpp/unpack.cpp @@ -78,7 +78,7 @@ void unpacker::expand_buffer(size_t len) if(m_off == 0) { size_t next_size; 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; } // FIXME realloc? @@ -91,7 +91,7 @@ void unpacker::expand_buffer(size_t len) m_free = next_size - m_used; } 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; } void* tmp = malloc(next_size); diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp index ae536c97..9bda5445 100644 --- a/cpp/unpack.hpp +++ b/cpp/unpack.hpp @@ -12,6 +12,9 @@ namespace msgpack { +static const size_t UNPACKER_INITIAL_BUFFER_SIZE = MSGPACK_UNPACKER_INITIAL_BUFFER_SIZE; + + struct unpack_error : public std::runtime_error { unpack_error(const std::string& msg) : std::runtime_error(msg) { } diff --git a/cpp/zone.cpp b/cpp/zone.cpp index de2de22b..ea4c7e4e 100644 --- a/cpp/zone.cpp +++ b/cpp/zone.cpp @@ -5,10 +5,17 @@ namespace msgpack { void* zone::alloc() { - if(m_used >= m_pool.size()*MSGPACK_ZONE_CHUNK_SIZE) { - m_pool.push_back(new chunk_t()); + if(m_pool.size()*ZONE_CHUNK_SIZE <= m_used) { + 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; return data; } @@ -16,21 +23,21 @@ void* zone::alloc() void zone::clear() { if(!m_pool.empty()) { - for(size_t b=0; b < m_used/MSGPACK_ZONE_CHUNK_SIZE; ++b) { - cell_t* c(m_pool[b]->cells); - for(size_t e=0; e < MSGPACK_ZONE_CHUNK_SIZE; ++e) { + for(size_t b=0; b < m_used/ZONE_CHUNK_SIZE; ++b) { + cell_t* c(m_pool[b]); + for(size_t e=0; e < ZONE_CHUNK_SIZE; ++e) { reinterpret_cast(c[e].data)->~object_class(); } } - cell_t* c(m_pool.back()->cells); - for(size_t e=0; e < m_used%MSGPACK_ZONE_CHUNK_SIZE; ++e) { + cell_t* c(m_pool.back()); + for(size_t e=0; e < m_used%ZONE_CHUNK_SIZE; ++e) { reinterpret_cast(c[e].data)->~object_class(); } for(pool_t::iterator it(m_pool.begin()), it_end(m_pool.end()); it != it_end; ++it) { - delete *it; + free(*it); } m_pool.clear(); } diff --git a/cpp/zone.hpp.erb b/cpp/zone.hpp.erb index 40ce694e..5c0e0d8d 100644 --- a/cpp/zone.hpp.erb +++ b/cpp/zone.hpp.erb @@ -8,12 +8,15 @@ #include #ifndef MSGPACK_ZONE_CHUNK_SIZE -#define MSGPACK_ZONE_CHUNK_SIZE 64 +#define MSGPACK_ZONE_CHUNK_SIZE 8*1024 #endif namespace msgpack { +static const size_t ZONE_CHUNK_SIZE = MSGPACK_ZONE_CHUNK_SIZE; + + class zone { public: zone() : m_used(0) { } @@ -110,11 +113,7 @@ private: char data[MAX_OBJECT_SIZE]; }; - struct chunk_t { - cell_t cells[MSGPACK_ZONE_CHUNK_SIZE]; - }; - - typedef std::vector pool_t; + typedef std::vector pool_t; pool_t m_pool;