From 31659738110f7d73d8fd4fe2d6fc62991ac7912f Mon Sep 17 00:00:00 2001
From: frsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>
Date: Sun, 15 Feb 2009 09:09:58 +0000
Subject: [PATCH] lang/c/msgpack: fix compile optimization flag

git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@70 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
---
 c/configure.in          |  7 ++++++-
 cpp/configure.in        |  7 ++++++-
 cpp/test.mk             |  9 +++++++++
 cpp/zone.cpp            | 26 +++++++++---------------
 cpp/zone.hpp            | 16 +++++++++++++++
 msgpack/pack_template.h | 44 ++++++++++++++++++++++++-----------------
 ruby/extconf.rb         |  2 +-
 ruby/test_pack.rb       |  2 ++
 ruby/unpack.c           | 10 +++++-----
 9 files changed, 80 insertions(+), 43 deletions(-)
 create mode 100644 cpp/test.mk

diff --git a/c/configure.in b/c/configure.in
index 1cacbc46..0017917f 100644
--- a/c/configure.in
+++ b/c/configure.in
@@ -2,10 +2,15 @@ AC_INIT(pack.c)
 AM_INIT_AUTOMAKE(msgpackc, 0.1.0)
 AC_CONFIG_HEADER(config.h)
 
+AC_SUBST(CFLAGS)
+if test "" = "$CFLAGS"; then
+	CFLAGS="-g -O4"
+fi
+
 AC_PROG_CC
 AC_PROG_LIBTOOL
 
-CFLAGS="-O4 $CFLAGS -Wall -I.."
+CFLAGS="-O4 -Wall $CFLAGS -I.."
 
 AC_OUTPUT([Makefile])
 
diff --git a/cpp/configure.in b/cpp/configure.in
index 5126be46..a60a489a 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -2,13 +2,18 @@ AC_INIT(object.cpp)
 AM_INIT_AUTOMAKE(msgpack, 0.1.0)
 AC_CONFIG_HEADER(config.h)
 
+AC_SUBST(CXXFLAGS)
+if test "" = "$CXXFLAGS"; then
+	CXXFLAGS="-g -O4"
+fi
+
 AC_PROG_CXX
 AC_PROG_LIBTOOL
 AC_CHECK_PROG(ERB, erb, erb, [$PATH])
 
 AC_CHECK_LIB(stdc++, main)
 
-CXXFLAGS="-O4 $CXXFLAGS -Wall -I.."
+CXXFLAGS="-O4 -Wall $CXXFLAGS -I.."
 
 AC_OUTPUT([Makefile])
 
diff --git a/cpp/test.mk b/cpp/test.mk
new file mode 100644
index 00000000..f1beac55
--- /dev/null
+++ b/cpp/test.mk
@@ -0,0 +1,9 @@
+
+CXXFLAGS  += -Wall -g -I. -I.. -O4
+LDFLAGS +=
+
+all: test
+
+test: test.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp
+	$(CXX) test.o unpack.o zone.o object.o $(CXXFLAGS) $(LDFLAGS) -o $@
+
diff --git a/cpp/zone.cpp b/cpp/zone.cpp
index 490bc25c..527cc9ca 100644
--- a/cpp/zone.cpp
+++ b/cpp/zone.cpp
@@ -20,10 +20,6 @@
 namespace msgpack {
 
 
-zone::zone() { }
-
-zone::~zone() { clear(); }
-
 void zone::clear()
 {
 	for(std::vector<char*>::iterator it(m_ptrs.begin()), it_end(m_ptrs.end());
@@ -33,22 +29,18 @@ void zone::clear()
 	m_ptrs.clear();
 }
 
-char* zone::realloc(char* ptr, size_t count)
+char* zone::realloc_real(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;
-			}
+	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;
 		}
-		throw std::bad_alloc();
 	}
+	throw std::bad_alloc();
 }
 
 
diff --git a/cpp/zone.hpp b/cpp/zone.hpp
index f8c9cba8..e7e73e18 100644
--- a/cpp/zone.hpp
+++ b/cpp/zone.hpp
@@ -40,11 +40,18 @@ public:
 private:
 	std::vector<char*> m_ptrs;
 
+private:
+	char* realloc_real(char* ptr, size_t count);
+
 private:
 	zone(const zone&);
 };
 
 
+inline zone::zone() { }
+
+inline zone::~zone() { clear(); }
+
 inline char* zone::malloc(size_t count)
 {
 	char* ptr = (char*)::malloc(count);
@@ -58,6 +65,15 @@ inline char* zone::malloc(size_t count)
 	return ptr;
 }
 
+inline char* zone::realloc(char* ptr, size_t count)
+{
+	if(ptr == NULL) {
+		return zone::malloc(count);
+	} else {
+		return realloc_real(ptr, count);
+	}
+}
+
 inline object* zone::malloc_container(size_t count)
 {
 	return (object*)zone::malloc(sizeof(object)*count);
diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h
index 928b581b..69c7345d 100644
--- a/msgpack/pack_template.h
+++ b/msgpack/pack_template.h
@@ -72,17 +72,21 @@
 msgpack_pack_inline_func(int)(msgpack_pack_user x, int d)
 {
 	if(d < -32) {
-		if(d < -32768) { // signed 32
+		if(d < -32768) {
+			// signed 32
 			const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
 			msgpack_pack_append_buffer(x, buf, 5);
-		} else if(d < -128) { // signed 16
+		} else if(d < -128) {
+			// signed 16
 			const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
 			msgpack_pack_append_buffer(x, buf, 3);
-		} else { // signed 8
+		} else {
+			// signed 8
 			const unsigned char buf[2] = {0xd0, (uint8_t)d};
 			msgpack_pack_append_buffer(x, buf, 2);
 		}
-	} else if(d < 128) { // fixnum
+	} else if(d < 128) {
+		// fixnum
 		msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
 	} else {
 		if(d < 256) {
@@ -104,21 +108,25 @@ msgpack_pack_inline_func(int)(msgpack_pack_user x, int d)
 // wrapper
 msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d)
 {
-	if(d < 128) {
-		// fixnum
-		msgpack_pack_append_buffer(x, (unsigned char*)&d, 1);
-	} else if(d < 256) {
-		// unsigned 8
-		const unsigned char buf[2] = {0xcc, (uint8_t)d};
-		msgpack_pack_append_buffer(x, buf, 2);
-	} else if(d < 65536) {
-		// unsigned 16
-		const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
-		msgpack_pack_append_buffer(x, buf, 3);
+	if(d < 256) {
+		if(d < 128) {
+			// fixnum
+			msgpack_pack_append_buffer(x, (unsigned char*)&d, 1);
+		} else {
+			// unsigned 8
+			const unsigned char buf[2] = {0xcc, (uint8_t)d};
+			msgpack_pack_append_buffer(x, buf, 2);
+		}
 	} else {
-		// unsigned 32
-		const unsigned char buf[5] = {0xce, STORE_BE32(d)};
-		msgpack_pack_append_buffer(x, buf, 5);
+		if(d < 65536) {
+			// unsigned 16
+			const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
+			msgpack_pack_append_buffer(x, buf, 3);
+		} else {
+			// unsigned 32
+			const unsigned char buf[5] = {0xce, STORE_BE32(d)};
+			msgpack_pack_append_buffer(x, buf, 5);
+		}
 	}
 }
 
diff --git a/ruby/extconf.rb b/ruby/extconf.rb
index 88abb55d..e6d4bd6d 100644
--- a/ruby/extconf.rb
+++ b/ruby/extconf.rb
@@ -1,4 +1,4 @@
 require 'mkmf'
-$CFLAGS << " -I.. -Wall -O9"
+$CFLAGS << " -I.. -Wall -O4"
 create_makefile('msgpack')
 
diff --git a/ruby/test_pack.rb b/ruby/test_pack.rb
index 16a8ccf7..6873d574 100644
--- a/ruby/test_pack.rb
+++ b/ruby/test_pack.rb
@@ -21,6 +21,8 @@ check 256
 check 65535
 check 65536
 check -1
+check -32
+check -33
 check -128
 check -129
 check -32768
diff --git a/ruby/unpack.c b/ruby/unpack.c
index b948aa67..8ab425c6 100644
--- a/ruby/unpack.c
+++ b/ruby/unpack.c
@@ -176,13 +176,13 @@ static VALUE MessagePack_Unpacker_execute_impl(VALUE args)
 	int ret;
 
 	if(from >= dlen) {
-		rb_raise(eUnpackError, "Requested start is after data buffer end.");
+		rb_raise(eUnpackError, "offset is bigger than data buffer size.");
 	}
 
 	ret = msgpack_unpacker_execute(mp, dptr, (size_t)dlen, &from);
 
 	if(ret < 0) {
-		rb_raise(eUnpackError, "Parse error.");
+		rb_raise(eUnpackError, "parse error.");
 	} else if(ret > 0) {
 		mp->user.finished = 1;
 		return ULONG2NUM(from);
@@ -242,12 +242,12 @@ static VALUE MessagePack_unpack_impl(VALUE args)
 	ret = msgpack_unpacker_execute(mp, dptr, (size_t)dlen, &from);
 
 	if(ret < 0) {
-		rb_raise(eUnpackError, "Parse error.");
+		rb_raise(eUnpackError, "parse error.");
 	} else if(ret == 0) {
-		rb_raise(eUnpackError, "Insufficient bytes.");
+		rb_raise(eUnpackError, "insufficient bytes.");
 	} else {
 		if(from < dlen) {
-			rb_raise(eUnpackError, "Extra bytes.");
+			rb_raise(eUnpackError, "extra bytes.");
 		}
 		return msgpack_unpacker_data(mp);
 	}