unpack: new msgpack_unpacker_next_with_size() function

This new function is an extension of the original msgpack_unpacker_next()
where it now adds third argument to store the number of parsed bytes for
the returned buffer upon a MSGPACK_UNPACK_SUCCESS case.

This is useful for cases where the caller needs to optimize memory usage
in the original buffer,s so upon success retrieval of the object, it can
later deprecate the already 'parsed' bytes.

For more details about the origins of this function please refer to the
following issue on github:

  https://github.com/msgpack/msgpack-c/issues/514

Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
This commit is contained in:
Eduardo Silva
2016-08-31 16:49:14 -06:00
parent 894547582b
commit d9a77e220a
2 changed files with 41 additions and 3 deletions

View File

@@ -510,7 +510,8 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac)
mpac->parsed = 0;
}
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result)
static inline msgpack_unpack_return unpacker_next(msgpack_unpacker* mpac,
msgpack_unpacked* result)
{
int ret;
@@ -529,11 +530,37 @@ msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpa
}
result->zone = msgpack_unpacker_release_zone(mpac);
result->data = msgpack_unpacker_data(mpac);
msgpack_unpacker_reset(mpac);
return MSGPACK_UNPACK_SUCCESS;
}
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac,
msgpack_unpacked* result)
{
int ret;
ret = unpacker_next(mpac, result);
if (ret == MSGPACK_UNPACK_SUCCESS) {
msgpack_unpacker_reset(mpac);
}
return ret;
}
msgpack_unpack_return
msgpack_unpacker_next_with_size(msgpack_unpacker* mpac,
msgpack_unpacked* result, size_t *p_bytes)
{
int ret;
ret = unpacker_next(mpac, result);
if (ret == MSGPACK_UNPACK_SUCCESS) {
*p_bytes = mpac->parsed;
msgpack_unpacker_reset(mpac);
}
return ret;
}
msgpack_unpack_return
msgpack_unpack(const char* data, size_t len, size_t* off,