src/flb_mp.c
int flb_mp_map_header_init(struct flb_mp_map_header *mh, msgpack_packer *mp_pck)
{
    msgpack_sbuffer *mp_sbuf;

    mp_sbuf = (msgpack_sbuffer *) mp_pck->data;

    /* map sbuffer */
    mh->data = mp_pck->data;

    /* Reset entries */
    mh->entries = 0;

    /* Store the next byte available */
    mh->offset = mp_sbuf->size;

    /*
     * Pack a map with size = 65536, so we force the underlaying msgpack-c
     * to use a 32 bit buffer size (0xdf), reference:
     *
     * - https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family
     */
    return msgpack_pack_map(mp_pck, 65536);
}
/*
 * msgpack-c requires to set the number of the entries in a map beforehand. For our
 * use case this adds some complexity, having developers to count all possible
 * entries that might be added.
 *
 * As a workaround and to avoid map's recomposition over and over, this simple API
 * allows to initialize the array header, 'register' new entries (as counters) and
 * finalize, upon finalization the proper array header size is adjusted.
 *
 * To make things easier, we make sure msgpack-c always register an array type of
 * 32 bits (identified by 0xdf, for number of entries >= 65536). Yes, for every
 * array using this API it will use 2 more bytes, not a big ideal. So whoever
 * uses this API, use it only if you don't know the exact number of entries to add.
 *
 * MANDATORY: make sure to always initialize, register every entry and finalize,
 * otherwise you will get a corrupted or incomplete msgpack buffer.
 *
 * Usage example
 * =============
 *
 *  struct flb_mp_map_head mh;
 *
 *  flb_mp_map_header_init(&mh, mp_pck);
 *
 *  -- First key/value entry --
 *  flb_mp_map_header_append(&mh);
 *  msgpack_pack_str(mp_pck, 4);
 *  msgpack_pack_str_body(mp_pck, "cool", 4);
 *  msgpack_pack_true(mp_pck);
 *
 *  -- Second key/value entry --
 *  flb_mp_map_header_append(&mh);
 *  msgpack_pack_str(mp_pck, 4);
 *  msgpack_pack_str_body(mp_pck, "slow", 4);
 *  msgpack_pack_false(mp_pck);
 *
 *  -- Finalize Map --
 *  flb_mp_map_header_end(&mh);
 */
msgpack-c requires to set the number of the entries in a map beforehand. For our
use case this adds some complexity, having developers to count all possible
entries that might be added.

As a workaround and to avoid map's recomposition over and over, this simple API
allows to initialize the array header, 'register' new entries (as counters) and
finalize, upon finalization the proper array header size is adjusted.

To make things easier, we make sure msgpack-c always register an array type of
32 bits (identified by 0xdf, for number of entries >= 65536). Yes, for every
array using this API it will use 2 more bytes, not a big ideal. So whoever
uses this API, use it only if you don't know the exact number of entries to add.

make sure to always initialize, register every entry and finalize,
otherwise you will get a corrupted or incomplete msgpack buffer.

Usage example










msgpack - c requires to set the number of the entries in a map beforehand . For our use case this adds some complexity having developers to count all possible entries that might be added . As a workaround and to avoid map ' s recomposition over and over this simple API allows to initialize the array header ' register ' new entries ( as counters ) and finalize upon finalization the proper array header size is adjusted . To make things easier we make sure msgpack - c always register an array type of 32 bits ( identified by 0xdf for number of entries > = 65536 ) . Yes for every array using this API it will use 2 more bytes not a big ideal . So whoever uses this API use it only if you don ' t know the exact number of entries to add . make sure to always initialize register every entry and finalize otherwise you will get a corrupted or incomplete msgpack buffer . Usage example
int flb_mp_map_header_init(struct flb_mp_map_header *mh, msgpack_packer *mp_pck)
{
    msgpack_sbuffer *mp_sbuf;
    mp_sbuf = (msgpack_sbuffer *) mp_pck->data;
    mh->data = mp_pck->data;
    mh->entries = 0;
    mh->offset = mp_sbuf->size;
    return msgpack_pack_map(mp_pck, 65536);
}
int flb_mp_map_header_init ( struct flb_mp_map_header * mh , msgpack_packer * mp_pck ) { msgpack_sbuffer * mp_sbuf ; mp_sbuf = ( msgpack_sbuffer * ) mp_pck -> data ; mh -> data = mp_pck -> data ; mh -> entries = 0 ; mh -> offset = mp_sbuf -> size ; return msgpack_pack_map ( mp_pck , 65536 ) ; }
msgpack-c requires to set the number of the entries in a map beforehand.
msgpack - c requires to set the number of the entries in a map beforehand .
/* map sbuffer */ /* Reset entries */ /* Store the next byte available */ /*
     * Pack a map with size = 65536, so we force the underlaying msgpack-c
     * to use a 32 bit buffer size (0xdf), reference:
     *
     * - https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family
     */