mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-20 21:39:53 +01:00
cpp: add sbuffer::clear() and vrefbuffer::clear()
This commit is contained in:
parent
dbebe9771b
commit
fc7da17fa2
@ -77,6 +77,10 @@ static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf)
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf)
|
||||||
|
{
|
||||||
|
sbuf->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,11 @@ public:
|
|||||||
return msgpack_sbuffer_release(this);
|
return msgpack_sbuffer_release(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
msgpack_sbuffer_clear(this);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void expand_buffer(size_t len)
|
void expand_buffer(size_t len)
|
||||||
{
|
{
|
||||||
|
@ -80,6 +80,9 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
|||||||
|
|
||||||
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
|
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)
|
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
|
||||||
{
|
{
|
||||||
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
|
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
|
||||||
|
@ -78,6 +78,11 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
msgpack_vrefbuffer_clear(this);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef msgpack_vrefbuffer base;
|
typedef msgpack_vrefbuffer base;
|
||||||
|
|
||||||
|
@ -12,6 +12,14 @@ TEST(buffer, sbuffer)
|
|||||||
|
|
||||||
EXPECT_EQ(3, sbuf.size());
|
EXPECT_EQ(3, sbuf.size());
|
||||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
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_EQ(3, sbuf.size());
|
||||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
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 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,6 +75,25 @@ void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
|
|||||||
free(vbuf->array);
|
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,
|
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
||||||
const char* buf, unsigned int len)
|
const char* buf, unsigned int len)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user