summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/unpack.cpp4
-rw-r--r--cpp/unpack.hpp3
-rw-r--r--cpp/zone.cpp25
-rw-r--r--cpp/zone.hpp.erb11
4 files changed, 26 insertions, 17 deletions
diff --git a/cpp/unpack.cpp b/cpp/unpack.cpp
index 807ded9..6a6a4e8 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 ae536c9..9bda544 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 de2de22..ea4c7e4 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<object_class*>(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<object_class*>(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 40ce694..5c0e0d8 100644
--- a/cpp/zone.hpp.erb
+++ b/cpp/zone.hpp.erb
@@ -8,12 +8,15 @@
#include <stdexcept>
#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<chunk_t*> pool_t;
+ typedef std::vector<cell_t*> pool_t;
pool_t m_pool;