diff --git a/cpp/msgpack/sbuffer.h b/cpp/msgpack/sbuffer.h index bc0a8fd0..57f424a3 100644 --- a/cpp/msgpack/sbuffer.h +++ b/cpp/msgpack/sbuffer.h @@ -77,6 +77,10 @@ static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf) return tmp; } +static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf) +{ + sbuf->size = 0; +} #ifdef __cplusplus } diff --git a/cpp/msgpack/sbuffer.hpp b/cpp/msgpack/sbuffer.hpp index ca06884e..e4a3f969 100644 --- a/cpp/msgpack/sbuffer.hpp +++ b/cpp/msgpack/sbuffer.hpp @@ -72,6 +72,11 @@ public: return msgpack_sbuffer_release(this); } + void clear() + { + msgpack_sbuffer_clear(this); + } + private: void expand_buffer(size_t len) { diff --git a/cpp/msgpack/vrefbuffer.h b/cpp/msgpack/vrefbuffer.h index 38ead67b..a08e0d0b 100644 --- a/cpp/msgpack/vrefbuffer.h +++ b/cpp/msgpack/vrefbuffer.h @@ -80,6 +80,9 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to); +void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref); + + int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len) { msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data; diff --git a/cpp/msgpack/vrefbuffer.hpp b/cpp/msgpack/vrefbuffer.hpp index c8eca7b6..7e0ffb23 100644 --- a/cpp/msgpack/vrefbuffer.hpp +++ b/cpp/msgpack/vrefbuffer.hpp @@ -78,6 +78,11 @@ public: } } + void clear() + { + msgpack_vrefbuffer_clear(this); + } + private: typedef msgpack_vrefbuffer base; diff --git a/cpp/test/buffer.cc b/cpp/test/buffer.cc index a2e90378..aff0699e 100644 --- a/cpp/test/buffer.cc +++ b/cpp/test/buffer.cc @@ -12,6 +12,14 @@ TEST(buffer, sbuffer) EXPECT_EQ(3, sbuf.size()); EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); + + sbuf.clear(); + sbuf.write("a", 1); + sbuf.write("a", 1); + sbuf.write("a", 1); + + EXPECT_EQ(3, sbuf.size()); + EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); } @@ -32,6 +40,23 @@ TEST(buffer, vrefbuffer) EXPECT_EQ(3, sbuf.size()); EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); + + + vbuf.clear(); + vbuf.write("a", 1); + vbuf.write("a", 1); + vbuf.write("a", 1); + + vec = vbuf.vector(); + veclen = vbuf.vector_size(); + + sbuf.clear(); + for(size_t i=0; i < veclen; ++i) { + sbuf.write((const char*)vec[i].iov_base, vec[i].iov_len); + } + + EXPECT_EQ(3, sbuf.size()); + EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); } diff --git a/cpp/vrefbuffer.c b/cpp/vrefbuffer.c index 136372f6..a27b138e 100644 --- a/cpp/vrefbuffer.c +++ b/cpp/vrefbuffer.c @@ -75,6 +75,25 @@ void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf) free(vbuf->array); } +void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf) +{ + msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next; + msgpack_vrefbuffer_chunk* n; + while(c != NULL) { + n = c->next; + free(c); + c = n; + } + + msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; + msgpack_vrefbuffer_chunk* chunk = ib->head; + chunk->next = NULL; + ib->free = vbuf->chunk_size; + ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); + + vbuf->tail = vbuf->array; +} + int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, const char* buf, unsigned int len) {