mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 02:40:09 +00:00
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
This commit is contained in:
parent
1222466a1c
commit
9923cf4daf
26 changed files with 1338 additions and 1116 deletions
61
cpp/zone.cpp
61
cpp/zone.cpp
|
|
@ -20,52 +20,35 @@
|
|||
namespace msgpack {
|
||||
|
||||
|
||||
// FIXME custom allocator?
|
||||
zone::zone() { }
|
||||
|
||||
void zone::expand_chunk()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
zone::~zone() { clear(); }
|
||||
|
||||
void zone::clear()
|
||||
{
|
||||
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();
|
||||
for(std::vector<char*>::iterator it(m_ptrs.begin()), it_end(m_ptrs.end());
|
||||
it != it_end; ++it) {
|
||||
free(*it);
|
||||
}
|
||||
m_ptrs.clear();
|
||||
}
|
||||
|
||||
char* zone::realloc(char* ptr, size_t count)
|
||||
{
|
||||
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();
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
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();
|
||||
}
|
||||
m_user_finalizer.clear();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue