Skip to content

Commit

Permalink
unpack: new msgpack_unpacker_next_with_size() function
Browse files Browse the repository at this point in the history
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:

  #514

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Aug 31, 2016
1 parent 8945475 commit d9a77e2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 12 additions & 1 deletion include/msgpack/unpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*).
Expand Down Expand Up @@ -267,4 +279,3 @@ static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* resu
#endif

#endif /* msgpack/unpack.h */

31 changes: 29 additions & 2 deletions src/unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down

0 comments on commit d9a77e2

Please sign in to comment.