diff options
Diffstat (limited to 'cpp/src/msgpack')
| -rw-r--r-- | cpp/src/msgpack/object.h | 8 | ||||
| -rw-r--r-- | cpp/src/msgpack/pack.h | 15 | ||||
| -rw-r--r-- | cpp/src/msgpack/sbuffer.h | 29 | ||||
| -rw-r--r-- | cpp/src/msgpack/unpack.h | 139 | ||||
| -rw-r--r-- | cpp/src/msgpack/unpack.hpp | 15 | ||||
| -rw-r--r-- | cpp/src/msgpack/vrefbuffer.h | 45 | ||||
| -rw-r--r-- | cpp/src/msgpack/zbuffer.h | 43 | ||||
| -rw-r--r-- | cpp/src/msgpack/zone.h | 7 |
8 files changed, 273 insertions, 28 deletions
diff --git a/cpp/src/msgpack/object.h b/cpp/src/msgpack/object.h index 71a27bb..cf0b4c1 100644 --- a/cpp/src/msgpack/object.h +++ b/cpp/src/msgpack/object.h @@ -26,6 +26,12 @@ extern "C" { #endif +/** + * @defgroup msgpack_object Dynamically typed object + * @ingroup msgpack + * @{ + */ + typedef enum { MSGPACK_OBJECT_NIL = 0x00, MSGPACK_OBJECT_BOOLEAN = 0x01, @@ -81,6 +87,8 @@ void msgpack_object_print(FILE* out, msgpack_object o); bool msgpack_object_equal(const msgpack_object x, const msgpack_object y); +/** @} */ + #ifdef __cplusplus } diff --git a/cpp/src/msgpack/pack.h b/cpp/src/msgpack/pack.h index 1525e0f..1252895 100644 --- a/cpp/src/msgpack/pack.h +++ b/cpp/src/msgpack/pack.h @@ -27,6 +27,19 @@ extern "C" { #endif +/** + * @defgroup msgpack_buffer Buffers + * @ingroup msgpack + * @{ + * @} + */ + +/** + * @defgroup msgpack_pack Serializer + * @ingroup msgpack + * @{ + */ + typedef int (*msgpack_packer_write)(void* data, const char* buf, unsigned int len); typedef struct msgpack_packer { @@ -74,6 +87,8 @@ static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l); int msgpack_pack_object(msgpack_packer* pk, msgpack_object d); +/** @} */ + #define msgpack_pack_inline_func(name) \ inline int msgpack_pack ## name diff --git a/cpp/src/msgpack/sbuffer.h b/cpp/src/msgpack/sbuffer.h index 57f424a..778dea7 100644 --- a/cpp/src/msgpack/sbuffer.h +++ b/cpp/src/msgpack/sbuffer.h @@ -21,15 +21,17 @@ #include <stdlib.h> #include <string.h> -#ifndef MSGPACK_SBUFFER_INIT_SIZE -#define MSGPACK_SBUFFER_INIT_SIZE 8192 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_sbuffer Simple buffer + * @ingroup msgpack_buffer + * @{ + */ + typedef struct msgpack_sbuffer { size_t size; char* data; @@ -46,6 +48,22 @@ static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf) free(sbuf->data); } +static inline msgpack_sbuffer* msgpack_sbuffer_new(void) +{ + return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer)); +} + +static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf) +{ + if(sbuf == NULL) { return; } + msgpack_sbuffer_destroy(sbuf); + free(sbuf); +} + +#ifndef MSGPACK_SBUFFER_INIT_SIZE +#define MSGPACK_SBUFFER_INIT_SIZE 8192 +#endif + static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len) { msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data; @@ -82,6 +100,9 @@ static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf) sbuf->size = 0; } +/** @} */ + + #ifdef __cplusplus } #endif diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h index e17d0d8..82698fc 100644 --- a/cpp/src/msgpack/unpack.h +++ b/cpp/src/msgpack/unpack.h @@ -20,12 +20,36 @@ #include "msgpack/zone.h" #include "msgpack/object.h" +#include <string.h> #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_unpack Deserializer + * @ingroup msgpack + * @{ + */ + +typedef struct msgpack_unpacked { + msgpack_zone* zone; + msgpack_object data; +} msgpack_unpacked; + +bool msgpack_unpack_next(msgpack_unpacked* result, + const char* data, size_t len, size_t* off); + +/** @} */ + + +/** + * @defgroup msgpack_unpacker Streaming deserializer + * @ingroup msgpack + * @{ + */ + typedef struct msgpack_unpacker { char* buffer; size_t used; @@ -38,18 +62,102 @@ typedef struct msgpack_unpacker { } msgpack_unpacker; +#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE +#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024) +#endif + +/** + * Initializes a streaming deserializer. + * The initialized deserializer must be destroyed by msgpack_unpacker_destroy(msgpack_unpacker*). + */ bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size); + +/** + * Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t). + */ void msgpack_unpacker_destroy(msgpack_unpacker* mpac); + +/** + * Creates a streaming deserializer. + * The created deserializer must be destroyed by msgpack_unpacker_free(msgpack_unpacker*). + */ msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size); + +/** + * Frees a streaming deserializer created by msgpack_unpacker_new(size_t). + */ void msgpack_unpacker_free(msgpack_unpacker* mpac); + +#ifndef MSGPACK_UNPACKER_RESERVE_SIZE +#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024) +#endif + +/** + * Reserves free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_buffer(msgpack_unpacker*), + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size); + +/** + * Gets pointer to the free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac); + +/** + * Gets size of the free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac); + +/** + * Notifies the deserializer that the internal buffer filled. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer(msgpack_unpacker*) and + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*). + */ static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size); +/** + * Deserializes one object. + * Returns true if it successes. Otherwise false is returned. + * @param pac pointer to an initialized msgpack_unpacked object. + */ +bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac); + +/** + * Initializes a msgpack_unpacked object. + * The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*). + * Use the object with msgpack_unpacker_next(msgpack_unpacker*, msgpack_unpacked*) or + * msgpack_unpack_next(msgpack_unpacked*, const char*, size_t, size_t*). + */ +static inline void msgpack_unpacked_init(msgpack_unpacked* result); + +/** + * Destroys a streaming deserializer initialized by msgpack_unpacked(). + */ +static inline void msgpack_unpacked_destroy(msgpack_unpacked* result); + +/** + * Releases the memory zone from msgpack_unpacked object. + * The released zone must be freed by msgpack_zone_free(msgpack_zone*). + */ +static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result); + + int msgpack_unpacker_execute(msgpack_unpacker* mpac); msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac); @@ -63,7 +171,10 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac); static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac); +/** @} */ + +// obsolete typedef enum { MSGPACK_UNPACK_SUCCESS = 2, MSGPACK_UNPACK_EXTRA_BYTES = 1, @@ -71,9 +182,10 @@ typedef enum { MSGPACK_UNPACK_PARSE_ERROR = -1, } msgpack_unpack_return; +// obsolete msgpack_unpack_return msgpack_unpack(const char* data, size_t len, size_t* off, - msgpack_zone* z, msgpack_object* result); + msgpack_zone* result_zone, msgpack_object* result); static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac); @@ -115,6 +227,31 @@ size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac) } +void msgpack_unpacked_init(msgpack_unpacked* result) +{ + memset(result, 0, sizeof(msgpack_unpacked)); +} + +void msgpack_unpacked_destroy(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone_free(result->zone); + result->zone = NULL; + memset(&result->data, 0, sizeof(msgpack_object)); + } +} + +msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone* z = result->zone; + result->zone = NULL; + return z; + } + return NULL; +} + + #ifdef __cplusplus } #endif diff --git a/cpp/src/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp index 56ce0f6..7b45017 100644 --- a/cpp/src/msgpack/unpack.hpp +++ b/cpp/src/msgpack/unpack.hpp @@ -24,8 +24,9 @@ #include <memory> #include <stdexcept> +// backward compatibility #ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE -#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE (32*1024) +#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE #endif namespace msgpack { @@ -64,12 +65,12 @@ private: class unpacker : public msgpack_unpacker { public: - unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE); + unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE); ~unpacker(); public: /*! 1. reserve buffer. at least `size' bytes of capacity will be ready */ - void reserve_buffer(size_t size); + void reserve_buffer(size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); /*! 2. read data to the buffer() up to buffer_capacity() bytes */ char* buffer(); @@ -160,7 +161,7 @@ private: }; -static bool unpack(unpacked* result, +static void unpack(unpacked* result, const char* data, size_t len, size_t* offset = NULL); @@ -312,7 +313,7 @@ inline void unpacker::remove_nonparsed_buffer() } -inline bool unpack(unpacked* result, +inline void unpack(unpacked* result, const char* data, size_t len, size_t* offset) { msgpack::object obj; @@ -326,12 +327,12 @@ inline bool unpack(unpacked* result, case UNPACK_SUCCESS: result->get() = obj; result->zone() = z; - return false; + return; case UNPACK_EXTRA_BYTES: result->get() = obj; result->zone() = z; - return true; + return; case UNPACK_CONTINUE: throw unpack_error("insufficient bytes"); diff --git a/cpp/src/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h index a08e0d0..123499d 100644 --- a/cpp/src/msgpack/vrefbuffer.h +++ b/cpp/src/msgpack/vrefbuffer.h @@ -19,6 +19,7 @@ #define MSGPACK_VREFBUFFER_H__ #include "msgpack/zone.h" +#include <stdlib.h> #ifndef _WIN32 #include <sys/uio.h> @@ -29,19 +30,17 @@ struct iovec { }; #endif -#ifndef MSGPACK_VREFBUFFER_REF_SIZE -#define MSGPACK_VREFBUFFER_REF_SIZE 32 -#endif - -#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE -#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_vrefbuffer Vectored Referencing buffer + * @ingroup msgpack_buffer + * @{ + */ + struct msgpack_vrefbuffer_chunk; typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk; @@ -63,10 +62,21 @@ typedef struct msgpack_vrefbuffer { } msgpack_vrefbuffer; +#ifndef MSGPACK_VREFBUFFER_REF_SIZE +#define MSGPACK_VREFBUFFER_REF_SIZE 32 +#endif + +#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE +#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192 +#endif + bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, size_t ref_size, size_t chunk_size); void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf); +static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size); +static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf); + static inline int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len); static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref); @@ -82,6 +92,25 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to) void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref); +/** @} */ + + +msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size) +{ + msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer)); + if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) { + free(vbuf); + return NULL; + } + return vbuf; +} + +void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf) +{ + if(vbuf == NULL) { return; } + msgpack_vrefbuffer_destroy(vbuf); + free(vbuf); +} int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len) { diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h index 2a32206..abb9c50 100644 --- a/cpp/src/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -23,30 +23,34 @@ #include <string.h> #include <zlib.h> -#ifndef MSGPACK_ZBUFFER_INIT_SIZE -#define MSGPACK_ZBUFFER_INIT_SIZE 8192 -#endif - -#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE -#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_zbuffer Compressed buffer + * @ingroup msgpack_buffer + * @{ + */ + typedef struct msgpack_zbuffer { z_stream stream; char* data; size_t init_size; } msgpack_zbuffer; +#ifndef MSGPACK_ZBUFFER_INIT_SIZE +#define MSGPACK_ZBUFFER_INIT_SIZE 8192 +#endif static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, int level, size_t init_size); static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf); +static inline msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size); +static inline void msgpack_zbuffer_free(msgpack_zbuffer* zbuf); + static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf); static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf); @@ -57,6 +61,10 @@ static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf); static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf); +#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE +#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 +#endif + static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len); static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf); @@ -80,6 +88,23 @@ void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf) free(zbuf->data); } +msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size) +{ + msgpack_zbuffer* zbuf = (msgpack_zbuffer*)malloc(sizeof(msgpack_zbuffer)); + if(!msgpack_zbuffer_init(zbuf, level, init_size)) { + free(zbuf); + return NULL; + } + return zbuf; +} + +void msgpack_zbuffer_free(msgpack_zbuffer* zbuf) +{ + if(zbuf == NULL) { return; } + msgpack_zbuffer_destroy(zbuf); + free(zbuf); +} + bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf) { size_t used = (char*)zbuf->stream.next_out - zbuf->data; @@ -171,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf) return tmp; } +/** @} */ + #ifdef __cplusplus } diff --git a/cpp/src/msgpack/zone.h b/cpp/src/msgpack/zone.h index ce5be6d..0e811df 100644 --- a/cpp/src/msgpack/zone.h +++ b/cpp/src/msgpack/zone.h @@ -25,6 +25,12 @@ extern "C" { #endif +/** + * @defgroup msgpack_zone Memory zone + * @ingroup msgpack + * @{ + */ + typedef struct msgpack_zone_finalizer { void (*func)(void* data); void* data; @@ -71,6 +77,7 @@ bool msgpack_zone_is_empty(msgpack_zone* zone); void msgpack_zone_clear(msgpack_zone* zone); +/** @} */ #ifndef MSGPACK_ZONE_ALIGN |
