From d9a77e220a08441bbf3a85952ca407421851ace3 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 31 Aug 2016 16:49:14 -0600 Subject: [PATCH] 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 --- include/msgpack/unpack.h | 13 ++++++++++++- src/unpack.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/include/msgpack/unpack.h b/include/msgpack/unpack.h index 0f9aede25..036d575eb 100644 --- a/include/msgpack/unpack.h +++ b/include/msgpack/unpack.h @@ -146,6 +146,18 @@ static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, si MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac); +/** + * Deserializes one object and set the number of parsed bytes involved. + * Returns true if it successes. Otherwise false is returned. + * @param mpac pointer to an initialized msgpack_unpacker object. + * @param result pointer to an initialized msgpack_unpacked object. + * @param p_bytes pointer to variable that will be set with the number of parsed bytes. + */ +MSGPACK_DLLEXPORT +msgpack_unpack_return msgpack_unpacker_next_with_size(msgpack_unpacker* mpac, + msgpack_unpacked* result, + size_t *p_bytes); + /** * Initializes a msgpack_unpacked object. * The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*). @@ -267,4 +279,3 @@ static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* resu #endif #endif /* msgpack/unpack.h */ - diff --git a/src/unpack.c b/src/unpack.c index 1bfcebbc6..882b0b2c7 100644 --- a/src/unpack.c +++ b/src/unpack.c @@ -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,