mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 21:18:23 +01:00
c,cpp: add msgpack_vrefbuffer_migrate, msgpack::vrefbuffer::migrate
This commit is contained in:
parent
232aced926
commit
c2dd22ec10
@ -77,7 +77,7 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
|||||||
const char* buf, unsigned int len)
|
const char* buf, unsigned int len)
|
||||||
{
|
{
|
||||||
if(vbuf->tail == vbuf->end) {
|
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;
|
const size_t nnext = nused * 2;
|
||||||
|
|
||||||
struct iovec* nvec = (struct iovec*)realloc(
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -76,6 +76,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
|
|||||||
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);
|
||||||
|
|
||||||
|
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
|
||||||
|
|
||||||
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
|
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
|
||||||
{
|
{
|
||||||
|
@ -71,6 +71,13 @@ public:
|
|||||||
return msgpack_vrefbuffer_veclen(this);
|
return msgpack_vrefbuffer_veclen(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void migrate(vrefbuffer* to)
|
||||||
|
{
|
||||||
|
if(msgpack_vrefbuffer_migrate(this, to) < 0) {
|
||||||
|
throw std::bad_alloc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef msgpack_vrefbuffer base;
|
typedef msgpack_vrefbuffer base;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user