fix unpacker

This commit is contained in:
frsyuki 2009-03-01 02:27:04 +09:00
parent 6fc38d1669
commit bf13ba72b5
4 changed files with 76 additions and 81 deletions

View File

@ -22,7 +22,7 @@
typedef struct { typedef struct {
msgpack_zone* z; msgpack_zone* z;
bool* referenced; bool referenced;
} unpack_user; } unpack_user;
@ -131,7 +131,7 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
o->type = MSGPACK_OBJECT_RAW; o->type = MSGPACK_OBJECT_RAW;
o->via.raw.ptr = p; o->via.raw.ptr = p;
o->via.raw.size = l; o->via.raw.size = l;
*u->referenced = true; u->referenced = true;
return 0; return 0;
} }
@ -139,32 +139,33 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
#define CTX_CAST(m) ((template_context*)(m)) #define CTX_CAST(m) ((template_context*)(m))
#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced
static const size_t COUNTER_SIZE = sizeof(unsigned int); static const size_t COUNTER_SIZE = sizeof(unsigned int);
static inline void init_count(void* buf) static inline void init_count(void* buffer)
{ {
*(volatile unsigned int*)buf = 1; *(volatile unsigned int*)buffer = 1;
} }
static inline void decl_count(void* buf) static inline void decl_count(void* buffer)
{ {
//if(--*(unsigned int*)buf == 0) { //if(--*(unsigned int*)buffer == 0) {
if(__sync_sub_and_fetch((unsigned int*)buf, 1) == 0) { if(__sync_sub_and_fetch((unsigned int*)buffer, 1) == 0) {
free(buf); free(buffer);
} }
} }
static inline void incr_count(void* buf) static inline void incr_count(void* buffer)
{ {
//++*(unsigned int*)buf; //++*(unsigned int*)buffer;
__sync_add_and_fetch((unsigned int*)buf, 1); __sync_add_and_fetch((unsigned int*)buffer, 1);
} }
static inline unsigned int get_count(void* buf) static inline unsigned int get_count(void* buffer)
{ {
return *(volatile unsigned int*)buf; return *(volatile unsigned int*)buffer;
} }
@ -175,39 +176,38 @@ bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
initial_buffer_size = COUNTER_SIZE; initial_buffer_size = COUNTER_SIZE;
} }
char* buf = (char*)malloc(initial_buffer_size); char* buffer = (char*)malloc(initial_buffer_size);
if(buf == NULL) { if(buffer == NULL) {
return false; return false;
} }
void* ctx = malloc(sizeof(template_context)); void* ctx = malloc(sizeof(template_context));
if(ctx == NULL) { if(ctx == NULL) {
free(buf); free(buffer);
return false; return false;
} }
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(z == NULL) { if(z == NULL) {
free(ctx); free(ctx);
free(buf); free(buffer);
return false; return false;
} }
mpac->buf = buf; mpac->buffer = buffer;
mpac->used = COUNTER_SIZE; mpac->used = COUNTER_SIZE;
mpac->free = initial_buffer_size - mpac->used; mpac->free = initial_buffer_size - mpac->used;
mpac->off = COUNTER_SIZE; mpac->off = COUNTER_SIZE;
mpac->parsed = 0; mpac->parsed = 0;
mpac->initial_buffer_size = initial_buffer_size; mpac->initial_buffer_size = initial_buffer_size;
mpac->z = z; mpac->z = z;
mpac->referenced = false;
mpac->ctx = ctx; mpac->ctx = ctx;
init_count(mpac->buf); init_count(mpac->buffer);
template_init(CTX_CAST(mpac->ctx)); template_init(CTX_CAST(mpac->ctx));
CTX_CAST(mpac->ctx)->user.z = mpac->z; CTX_CAST(mpac->ctx)->user.z = mpac->z;
CTX_CAST(mpac->ctx)->user.referenced = &mpac->referenced; CTX_CAST(mpac->ctx)->user.referenced = false;
return true; return true;
} }
@ -216,7 +216,7 @@ void msgpack_unpacker_destroy(msgpack_unpacker* mpac)
{ {
msgpack_zone_free(mpac->z); msgpack_zone_free(mpac->z);
free(mpac->ctx); free(mpac->ctx);
decl_count(mpac->buf); decl_count(mpac->buffer);
} }
@ -241,10 +241,10 @@ void msgpack_unpacker_free(msgpack_unpacker* mpac)
free(mpac); free(mpac);
} }
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size) bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
{ {
if(mpac->used == mpac->off && get_count(mpac->buf) == 1 && !mpac->referenced) { if(mpac->used == mpac->off && get_count(mpac->buffer) == 1
&& !CTX_REFERENCED(mpac)) {
// rewind buffer // rewind buffer
mpac->free += mpac->used - COUNTER_SIZE; mpac->free += mpac->used - COUNTER_SIZE;
mpac->used = COUNTER_SIZE; mpac->used = COUNTER_SIZE;
@ -261,12 +261,12 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
next_size *= 2; next_size *= 2;
} }
char* tmp = (char*)realloc(mpac->buf, next_size); char* tmp = (char*)realloc(mpac->buffer, next_size);
if(tmp == NULL) { if(tmp == NULL) {
return false; return false;
} }
mpac->buf = tmp; mpac->buffer = tmp;
mpac->free = next_size - mpac->used; mpac->free = next_size - mpac->used;
} else { } else {
@ -283,19 +283,19 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
init_count(tmp); init_count(tmp);
if(mpac->referenced) { if(CTX_REFERENCED(mpac)) {
if(!msgpack_zone_push_finalizer(mpac->z, decl_count, mpac->buf)) { if(!msgpack_zone_push_finalizer(mpac->z, decl_count, mpac->buffer)) {
free(tmp); free(tmp);
return false; return false;
} }
mpac->referenced = false; CTX_REFERENCED(mpac) = false;
} else { } else {
decl_count(mpac->buf); decl_count(mpac->buffer);
} }
memcpy(tmp+COUNTER_SIZE, mpac->buf+mpac->off, not_parsed); memcpy(tmp+COUNTER_SIZE, mpac->buffer+mpac->off, not_parsed);
mpac->buf = tmp; mpac->buffer = tmp;
mpac->used = not_parsed + COUNTER_SIZE; mpac->used = not_parsed + COUNTER_SIZE;
mpac->free = next_size - mpac->used; mpac->free = next_size - mpac->used;
mpac->off = COUNTER_SIZE; mpac->off = COUNTER_SIZE;
@ -308,7 +308,7 @@ int msgpack_unpacker_execute(msgpack_unpacker* mpac)
{ {
size_t off = mpac->off; size_t off = mpac->off;
int ret = template_execute(CTX_CAST(mpac->ctx), int ret = template_execute(CTX_CAST(mpac->ctx),
mpac->buf, mpac->used, &mpac->off); mpac->buffer, mpac->used, &mpac->off);
if(mpac->off > off) { if(mpac->off > off) {
mpac->parsed += mpac->off - off; mpac->parsed += mpac->off - off;
} }
@ -326,26 +326,26 @@ msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
return false; return false;
} }
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(z == NULL) { if(r == NULL) {
return NULL; return NULL;
} }
msgpack_zone* old = mpac->z; msgpack_zone* old = mpac->z;
mpac->z = z; mpac->z = r;
return old; return old;
} }
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac) bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac)
{ {
if(mpac->referenced) { if(CTX_REFERENCED(mpac)) {
if(!msgpack_zone_push_finalizer(mpac->z, decl_count, mpac->buf)) { if(!msgpack_zone_push_finalizer(mpac->z, decl_count, mpac->buffer)) {
return false; return false;
} }
mpac->referenced = false; CTX_REFERENCED(mpac) = false;
incr_count(mpac->buf); incr_count(mpac->buffer);
} }
return true; return true;
@ -354,6 +354,7 @@ bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac)
void msgpack_unpacker_reset(msgpack_unpacker* mpac) void msgpack_unpacker_reset(msgpack_unpacker* mpac)
{ {
template_init(CTX_CAST(mpac->ctx)); template_init(CTX_CAST(mpac->ctx));
// don't reset referenced flag
mpac->parsed = 0; mpac->parsed = 0;
} }
@ -365,9 +366,8 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
template_context ctx; template_context ctx;
template_init(&ctx); template_init(&ctx);
bool referenced = false;
ctx.user.z = z; ctx.user.z = z;
ctx.user.referenced = &referenced; ctx.user.referenced = false;
size_t noff = 0; size_t noff = 0;
if(off != NULL) { noff = *off; } if(off != NULL) { noff = *off; }

View File

@ -29,13 +29,12 @@ extern "C" {
typedef struct msgpack_unpacker { typedef struct msgpack_unpacker {
char* buf; char* buffer;
size_t used; size_t used;
size_t free; size_t free;
size_t off; size_t off;
size_t parsed; size_t parsed;
msgpack_zone* z; msgpack_zone* z;
bool referenced;
size_t initial_buffer_size; size_t initial_buffer_size;
void* ctx; void* ctx;
} msgpack_unpacker; } msgpack_unpacker;
@ -91,7 +90,7 @@ bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
char* msgpack_unpacker_buffer(msgpack_unpacker* mpac) char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
{ {
return mpac->buf + mpac->used; return mpac->buffer + mpac->used;
} }
size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac) size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)

View File

@ -28,77 +28,70 @@ class sbuffer : public msgpack_sbuffer {
public: public:
sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE) sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE)
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); base::data = (char*)::malloc(initsz);
if(!base::data) {
sbuf->data = (char*)::malloc(initsz);
if(!sbuf->data) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
sbuf->size = 0; base::size = 0;
sbuf->alloc = initsz; base::alloc = initsz;
} }
~sbuffer() ~sbuffer()
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); ::free(base::data);
::free(sbuf->data);
} }
public: public:
void write(const char* buf, unsigned int len) void write(const char* buf, unsigned int len)
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); if(base::alloc - base::size < len) {
if(sbuf->alloc - sbuf->size < len) {
expand_buffer(len); expand_buffer(len);
} }
memcpy(sbuf->data + sbuf->size, buf, len); memcpy(base::data + base::size, buf, len);
sbuf->size += len; base::size += len;
} }
char* data() char* data()
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); return base::data;
return sbuf->data;
} }
const char* data() const const char* data() const
{ {
const msgpack_sbuffer* sbuf = static_cast<const msgpack_sbuffer*>(this); return base::data;
return sbuf->data;
} }
size_t size() const size_t size() const
{ {
const msgpack_sbuffer* sbuf = static_cast<const msgpack_sbuffer*>(this); return base::size;
return sbuf->size;
} }
char* release() char* release()
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); return msgpack_sbuffer_release(this);
return msgpack_sbuffer_release(sbuf);
} }
private: private:
void expand_buffer(size_t len) void expand_buffer(size_t len)
{ {
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this); size_t nsize = (base::alloc) ?
base::alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
size_t nsize = (sbuf->alloc) ?
sbuf->alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
while(nsize < sbuf->size + len) { nsize *= 2; } while(nsize < base::size + len) { nsize *= 2; }
void* tmp = realloc(sbuf->data, nsize); void* tmp = realloc(base::data, nsize);
if(!tmp) { if(!tmp) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
sbuf->data = (char*)tmp; base::data = (char*)tmp;
sbuf->alloc = nsize; base::alloc = nsize;
} }
private:
typedef msgpack_sbuffer base;
private: private:
sbuffer(const sbuffer&); sbuffer(const sbuffer&);
}; };

View File

@ -39,7 +39,7 @@ struct unpack_error : public std::runtime_error {
class unpacker : public msgpack_unpacker { class unpacker : public msgpack_unpacker {
public: public:
unpacker(size_t initial_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE); unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE);
~unpacker(); ~unpacker();
public: public:
@ -126,6 +126,9 @@ public:
// Note that reset() leaves non-parsed buffer. // Note that reset() leaves non-parsed buffer.
void remove_nonparsed_buffer(); void remove_nonparsed_buffer();
private:
typedef msgpack_unpacker base;
private: private:
unpacker(const unpacker&); unpacker(const unpacker&);
}; };
@ -207,9 +210,9 @@ inline zone* unpacker::release_zone()
zone* r = new zone(); zone* r = new zone();
msgpack_zone old = *this->z; msgpack_zone old = *base::z;
*this->z = *z; *base::z = *r;
*z = old; *static_cast<msgpack_zone*>(r) = old;
return r; return r;
} }
@ -232,22 +235,22 @@ inline size_t unpacker::parsed_size() const
inline char* unpacker::nonparsed_buffer() inline char* unpacker::nonparsed_buffer()
{ {
return buf + off; return base::buffer + base::off;
} }
inline size_t unpacker::nonparsed_size() const inline size_t unpacker::nonparsed_size() const
{ {
return used - off; return base::used - base::off;
} }
inline void unpacker::skip_nonparsed_buffer(size_t size) inline void unpacker::skip_nonparsed_buffer(size_t size)
{ {
off += size; base::off += size;
} }
inline void unpacker::remove_nonparsed_buffer() inline void unpacker::remove_nonparsed_buffer()
{ {
used = off; base::used = base::off;
} }