summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2009-12-19 22:09:29 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2009-12-19 22:09:29 +0900
commitc2dd22ec102d12374b97f4ae32dccdaaca8630cd (patch)
tree0ac27ac3133898b319cf56d3dcd6910a21f7826c
parent232aced926635a7054ac4081d472529cdf96f749 (diff)
downloadmsgpack-python-c2dd22ec102d12374b97f4ae32dccdaaca8630cd.tar.gz
c,cpp: add msgpack_vrefbuffer_migrate, msgpack::vrefbuffer::migrate
-rw-r--r--c/vrefbuffer.c29
-rw-r--r--c/vrefbuffer.h1
-rw-r--r--cpp/vrefbuffer.hpp7
3 files changed, 36 insertions, 1 deletions
diff --git a/c/vrefbuffer.c b/c/vrefbuffer.c
index bbaf61d..2bf97af 100644
--- a/c/vrefbuffer.c
+++ b/c/vrefbuffer.c
@@ -77,7 +77,7 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
const char* buf, unsigned int len)
{
if(vbuf->tail == vbuf->end) {
- const size_t nused = vbuf->end - vbuf->array;
+ const size_t nused = vbuf->tail - vbuf->array;
const size_t nnext = nused * 2;
struct iovec* nvec = (struct iovec*)realloc(
@@ -133,3 +133,30 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
}
}
+int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
+{
+ const size_t tosize = to->tail - to->array;
+ if(vbuf->tail + tosize < vbuf->end) {
+ const size_t nused = vbuf->tail - vbuf->array;
+ const size_t nsize = vbuf->end - vbuf->array;
+ const size_t reqsize = nused + tosize;
+ size_t nnext = nsize * 2;
+ while(nnext < reqsize) {
+ nnext *= 2;
+ }
+
+ struct iovec* nvec = (struct iovec*)realloc(
+ vbuf->array, sizeof(struct iovec)*nnext);
+ if(nvec == NULL) {
+ return -1;
+ }
+
+ vbuf->array = nvec;
+ vbuf->end = nvec + nnext;
+ vbuf->tail = nvec + nused;
+ }
+
+ memcpy(vbuf->tail, vbuf->array, sizeof(struct iovec)*tosize);
+ return 0;
+}
+
diff --git a/c/vrefbuffer.h b/c/vrefbuffer.h
index 063075f..9f24f14 100644
--- a/c/vrefbuffer.h
+++ b/c/vrefbuffer.h
@@ -76,6 +76,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
const char* buf, unsigned int len);
+int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
{
diff --git a/cpp/vrefbuffer.hpp b/cpp/vrefbuffer.hpp
index 549d77f..c8eca7b 100644
--- a/cpp/vrefbuffer.hpp
+++ b/cpp/vrefbuffer.hpp
@@ -71,6 +71,13 @@ public:
return msgpack_vrefbuffer_veclen(this);
}
+ void migrate(vrefbuffer* to)
+ {
+ if(msgpack_vrefbuffer_migrate(this, to) < 0) {
+ throw std::bad_alloc();
+ }
+ }
+
private:
typedef msgpack_vrefbuffer base;