From 5697b9a15d89a397a53174b9eca2d96bc7ebaef0 Mon Sep 17 00:00:00 2001
From: frsyuki <frsyuki@vcore.(none)>
Date: Thu, 26 Feb 2009 01:15:14 +0900
Subject: [PATCH] remove msgpack_unpacker_buffered_size, add
 msgpack_unpacker_parsed_size

---
 c/unpack.c                | 12 ++++++++----
 c/unpack.h                | 14 ++++++++------
 cpp/unpack.hpp            | 16 ++++++++--------
 msgpack/unpack_template.h |  1 -
 ruby/unpack.c             |  2 ++
 5 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/c/unpack.c b/c/unpack.c
index e06d97ce..865e0d40 100644
--- a/c/unpack.c
+++ b/c/unpack.c
@@ -197,6 +197,7 @@ bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
 	mpac->used = COUNTER_SIZE;
 	mpac->free = initial_buffer_size - mpac->used;
 	mpac->off = COUNTER_SIZE;
+	mpac->parsed = 0;
 	mpac->initial_buffer_size = initial_buffer_size;
 	mpac->z = z;
 	mpac->referenced = false;
@@ -305,8 +306,13 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
 
 int msgpack_unpacker_execute(msgpack_unpacker* mpac)
 {
-	return template_execute(CTX_CAST(mpac->ctx),
+	size_t off = mpac->off;
+	int ret = template_execute(CTX_CAST(mpac->ctx),
 			mpac->buf, mpac->used, &mpac->off);
+	if(mpac->off > off) {
+		mpac->parsed += mpac->off - off;
+	}
+	return ret;
 }
 
 msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
@@ -347,10 +353,8 @@ bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac)
 
 void msgpack_unpacker_reset(msgpack_unpacker* mpac)
 {
-	msgpack_zone* z = mpac->z;
 	template_init(CTX_CAST(mpac->ctx));
-	CTX_CAST(mpac->ctx)->user.z = z;
-	CTX_CAST(mpac->ctx)->user.referenced = &mpac->referenced;
+	mpac->parsed = 0;
 }
 
 
diff --git a/c/unpack.h b/c/unpack.h
index 46777b9c..77fbd54d 100644
--- a/c/unpack.h
+++ b/c/unpack.h
@@ -33,6 +33,7 @@ typedef struct msgpack_unpacker {
 	size_t used;
 	size_t free;
 	size_t off;
+	size_t parsed;
 	msgpack_zone* z;
 	bool referenced;
 	size_t initial_buffer_size;
@@ -46,7 +47,6 @@ void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
 msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
 void msgpack_unpacker_free(msgpack_unpacker* mpac);
 
-static inline size_t msgpack_unpacker_buffered_size(const msgpack_unpacker* mpac);
 static inline bool   msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
 static inline char*  msgpack_unpacker_buffer(msgpack_unpacker* mpac);
 static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
@@ -61,6 +61,8 @@ msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
 
 void msgpack_unpacker_reset(msgpack_unpacker* mpac);
 
+static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
+
 
 typedef enum {
 	MSGPACK_UNPACK_SUCCESS				=  2,
@@ -78,11 +80,6 @@ bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
 
 bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
 
-size_t msgpack_unpacker_buffered_size(const msgpack_unpacker* mpac)
-{
-	return mpac->used;
-}
-
 bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
 {
 	if(mpac->free >= size) { return true; }
@@ -105,6 +102,11 @@ void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
 	mpac->free -= size;
 }
 
+size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
+{
+	return mpac->parsed;
+}
+
 
 #ifdef __cplusplus
 }
diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp
index 6e9b6410..0b7fe574 100644
--- a/cpp/unpack.hpp
+++ b/cpp/unpack.hpp
@@ -43,9 +43,6 @@ public:
 	~unpacker();
 
 public:
-	/*! 0. check if the buffered size is not exceed the assumption. */
-	size_t buffered_size() const;
-
 	/*! 1. reserve buffer. at least `size' bytes of capacity will be ready */
 	void reserve_buffer(size_t size);
 
@@ -70,6 +67,9 @@ public:
 	/*! 5.3. after release_zone(), re-initialize unpacker */
 	void reset();
 
+	/*! 6. check if the parsed message size doesn't exceed assumption. */
+	size_t parsed_size() const;
+
 
 	// Basic usage of the unpacker is as following:
 	//
@@ -158,11 +158,6 @@ inline unpacker::~unpacker()
 }
 
 
-inline size_t unpacker::buffered_size() const
-{
-	return msgpack_unpacker_buffered_size(this);
-}
-
 inline void unpacker::reserve_buffer(size_t size)
 {
 	if(!msgpack_unpacker_reserve_buffer(this, size)) {
@@ -223,6 +218,11 @@ inline void unpacker::reset()
 	msgpack_unpacker_reset(this);
 }
 
+inline size_t unpacker::parsed_size() const
+{
+	return msgpack_unpacker_parsed_size(this);
+}
+
 
 inline char* unpacker::nonparsed_buffer()
 {
diff --git a/msgpack/unpack_template.h b/msgpack/unpack_template.h
index ff30955b..d67fd1ef 100644
--- a/msgpack/unpack_template.h
+++ b/msgpack/unpack_template.h
@@ -59,7 +59,6 @@ msgpack_unpack_struct_decl(_context) {
 
 msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx)
 {
-	/*memset(ctx, 0, sizeof( msgpack_unpack_struct(_context) ));   FIXME needed? */
 	ctx->cs = CS_HEADER;
 	ctx->trail = 0;
 	ctx->top = 0;
diff --git a/ruby/unpack.c b/ruby/unpack.c
index 4de49557..411a94d2 100644
--- a/ruby/unpack.c
+++ b/ruby/unpack.c
@@ -278,6 +278,7 @@ static VALUE MessagePack_unpack_rescue(VALUE args)
 static VALUE MessagePack_unpack_limit(VALUE self, VALUE data, VALUE limit)
 {
 	CHECK_STRING_TYPE(data);
+
 	msgpack_unpack_t mp;
 	template_init(&mp);
 	unpack_user u = {0, Qnil};
@@ -288,6 +289,7 @@ static VALUE MessagePack_unpack_limit(VALUE self, VALUE data, VALUE limit)
 	VALUE ret = rb_rescue(MessagePack_unpack_impl, (VALUE)args,
 			MessagePack_unpack_rescue, Qnil);
 	rb_gc_enable();
+
 	return ret;
 }