summaryrefslogtreecommitdiff
path: root/cpp/zone.cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:58 +0000
committerfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:58 +0000
commit9923cf4daf631432e389dd0694042b6b405c1288 (patch)
tree0219d0365d4f83b48001fbb9f413e7f43a4654fd /cpp/zone.cpp
parent1222466a1c52161a3da3c3c5ce552d4b90e32bf6 (diff)
downloadmsgpack-python-9923cf4daf631432e389dd0694042b6b405c1288.tar.gz
lang/c/msgpack: reimplemented C++ binding with template-based static resolution design
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@67 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
Diffstat (limited to 'cpp/zone.cpp')
-rw-r--r--cpp/zone.cpp57
1 files changed, 20 insertions, 37 deletions
diff --git a/cpp/zone.cpp b/cpp/zone.cpp
index 4cc50d7..490bc25 100644
--- a/cpp/zone.cpp
+++ b/cpp/zone.cpp
@@ -20,52 +20,35 @@
namespace msgpack {
-// FIXME custom allocator?
+zone::zone() { }
-void zone::expand_chunk()
+zone::~zone() { clear(); }
+
+void zone::clear()
{
- 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;
+ for(std::vector<char*>::iterator it(m_ptrs.begin()), it_end(m_ptrs.end());
+ it != it_end; ++it) {
+ free(*it);
}
+ m_ptrs.clear();
}
-void zone::clear()
+char* zone::realloc(char* ptr, size_t count)
{
- if(!m_pool.empty()) {
- size_t base_size = m_used / ZONE_CHUNK_SIZE;
- size_t extend_size = m_used % ZONE_CHUNK_SIZE;
- for(size_t b=0; b < base_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();
+ if(ptr == NULL) {
+ return zone::malloc(count);
+ } else {
+ for(std::vector<char*>::reverse_iterator it(m_ptrs.rbegin()), it_end(m_ptrs.rend());
+ it != it_end; ++it) {
+ if(*it == ptr) {
+ char* tmp = (char*)::realloc(ptr, count);
+ if(!tmp) { throw std::bad_alloc(); }
+ *it = tmp;
+ return tmp;
}
}
- cell_t* c(m_pool.back());
- for(size_t e=0; e < extend_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) {
- free(*it);
- }
- m_pool.clear();
- }
- m_used = 0;
-
- for(user_finalizer_t::reverse_iterator it(m_user_finalizer.rbegin()),
- it_end(m_user_finalizer.rend());
- it != it_end;
- ++it) {
- it->call();
+ throw std::bad_alloc();
}
- m_user_finalizer.clear();
}