diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:54:23 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:54:44 +0000 |
commit | 836b47cb7e99a977c5a23b059ca1d0b5065d310e (patch) | |
tree | 1604da8f482d02effa033c94a84be42bc0c848c3 /fluent-bit/lib/msgpack-c/src | |
parent | Releasing debian version 1.44.3-2. (diff) | |
download | netdata-836b47cb7e99a977c5a23b059ca1d0b5065d310e.tar.xz netdata-836b47cb7e99a977c5a23b059ca1d0b5065d310e.zip |
Merging upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'fluent-bit/lib/msgpack-c/src')
-rw-r--r-- | fluent-bit/lib/msgpack-c/src/objectc.c | 482 | ||||
-rw-r--r-- | fluent-bit/lib/msgpack-c/src/unpack.c | 702 | ||||
-rw-r--r-- | fluent-bit/lib/msgpack-c/src/version.c | 22 | ||||
-rw-r--r-- | fluent-bit/lib/msgpack-c/src/vrefbuffer.c | 250 | ||||
-rw-r--r-- | fluent-bit/lib/msgpack-c/src/zone.c | 222 |
5 files changed, 0 insertions, 1678 deletions
diff --git a/fluent-bit/lib/msgpack-c/src/objectc.c b/fluent-bit/lib/msgpack-c/src/objectc.c deleted file mode 100644 index b730a284..00000000 --- a/fluent-bit/lib/msgpack-c/src/objectc.c +++ /dev/null @@ -1,482 +0,0 @@ -/* - * MessagePack for C dynamic typing routine - * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki - * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -#if defined(_KERNEL_MODE) -# undef _NO_CRT_STDIO_INLINE -# define _NO_CRT_STDIO_INLINE -#endif - -#include "msgpack/object.h" -#include "msgpack/pack.h" -#include <ctype.h> - -#include <stdio.h> -#include <string.h> - -#if defined(_MSC_VER) -#if _MSC_VER >= 1800 -#include <inttypes.h> -#else -#define PRIu64 "I64u" -#define PRIi64 "I64i" -#define PRIi8 "i" -#endif -#else -#include <inttypes.h> -#endif - -#if defined(_KERNEL_MODE) -# undef snprintf -# define snprintf _snprintf -#endif - -int msgpack_pack_object(msgpack_packer* pk, msgpack_object d) -{ - switch(d.type) { - case MSGPACK_OBJECT_NIL: - return msgpack_pack_nil(pk); - - case MSGPACK_OBJECT_BOOLEAN: - if(d.via.boolean) { - return msgpack_pack_true(pk); - } else { - return msgpack_pack_false(pk); - } - - case MSGPACK_OBJECT_POSITIVE_INTEGER: - return msgpack_pack_uint64(pk, d.via.u64); - - case MSGPACK_OBJECT_NEGATIVE_INTEGER: - return msgpack_pack_int64(pk, d.via.i64); - - case MSGPACK_OBJECT_FLOAT32: - return msgpack_pack_float(pk, (float)d.via.f64); - - case MSGPACK_OBJECT_FLOAT64: - return msgpack_pack_double(pk, d.via.f64); - - case MSGPACK_OBJECT_STR: - { - int ret = msgpack_pack_str(pk, d.via.str.size); - if(ret < 0) { return ret; } - return msgpack_pack_str_body(pk, d.via.str.ptr, d.via.str.size); - } - - case MSGPACK_OBJECT_BIN: - { - int ret = msgpack_pack_bin(pk, d.via.bin.size); - if(ret < 0) { return ret; } - return msgpack_pack_bin_body(pk, d.via.bin.ptr, d.via.bin.size); - } - - case MSGPACK_OBJECT_EXT: - { - int ret = msgpack_pack_ext(pk, d.via.ext.size, d.via.ext.type); - if(ret < 0) { return ret; } - return msgpack_pack_ext_body(pk, d.via.ext.ptr, d.via.ext.size); - } - - case MSGPACK_OBJECT_ARRAY: - { - int ret = msgpack_pack_array(pk, d.via.array.size); - if(ret < 0) { - return ret; - } - else { - msgpack_object* o = d.via.array.ptr; - msgpack_object* const oend = d.via.array.ptr + d.via.array.size; - for(; o != oend; ++o) { - ret = msgpack_pack_object(pk, *o); - if(ret < 0) { return ret; } - } - - return 0; - } - } - - case MSGPACK_OBJECT_MAP: - { - int ret = msgpack_pack_map(pk, d.via.map.size); - if(ret < 0) { - return ret; - } - else { - msgpack_object_kv* kv = d.via.map.ptr; - msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size; - for(; kv != kvend; ++kv) { - ret = msgpack_pack_object(pk, kv->key); - if(ret < 0) { return ret; } - ret = msgpack_pack_object(pk, kv->val); - if(ret < 0) { return ret; } - } - - return 0; - } - } - - default: - return -1; - } -} - -#if !defined(_KERNEL_MODE) - -static void msgpack_object_bin_print(FILE* out, const char *ptr, size_t size) -{ - size_t i; - for (i = 0; i < size; ++i) { - if (ptr[i] == '"') { - fputs("\\\"", out); - } else if (isprint((unsigned char)ptr[i])) { - fputc(ptr[i], out); - } else { - fprintf(out, "\\x%02x", (unsigned char)ptr[i]); - } - } -} - -void msgpack_object_print(FILE* out, msgpack_object o) -{ - switch(o.type) { - case MSGPACK_OBJECT_NIL: - fprintf(out, "nil"); - break; - - case MSGPACK_OBJECT_BOOLEAN: - fprintf(out, (o.via.boolean ? "true" : "false")); - break; - - case MSGPACK_OBJECT_POSITIVE_INTEGER: -#if defined(PRIu64) - fprintf(out, "%" PRIu64, o.via.u64); -#else - if (o.via.u64 > ULONG_MAX) - fprintf(out, "over 4294967295"); - else - fprintf(out, "%lu", (unsigned long)o.via.u64); -#endif - break; - - case MSGPACK_OBJECT_NEGATIVE_INTEGER: -#if defined(PRIi64) - fprintf(out, "%" PRIi64, o.via.i64); -#else - if (o.via.i64 > LONG_MAX) - fprintf(out, "over +2147483647"); - else if (o.via.i64 < LONG_MIN) - fprintf(out, "under -2147483648"); - else - fprintf(out, "%ld", (signed long)o.via.i64); -#endif - break; - - case MSGPACK_OBJECT_FLOAT32: - case MSGPACK_OBJECT_FLOAT64: - fprintf(out, "%f", o.via.f64); - break; - - case MSGPACK_OBJECT_STR: - fprintf(out, "\""); - fwrite(o.via.str.ptr, o.via.str.size, 1, out); - fprintf(out, "\""); - break; - - case MSGPACK_OBJECT_BIN: - fprintf(out, "\""); - msgpack_object_bin_print(out, o.via.bin.ptr, o.via.bin.size); - fprintf(out, "\""); - break; - - case MSGPACK_OBJECT_EXT: -#if defined(PRIi8) - fprintf(out, "(ext: %" PRIi8 ")", o.via.ext.type); -#else - fprintf(out, "(ext: %d)", (int)o.via.ext.type); -#endif - fprintf(out, "\""); - msgpack_object_bin_print(out, o.via.ext.ptr, o.via.ext.size); - fprintf(out, "\""); - break; - - case MSGPACK_OBJECT_ARRAY: - fprintf(out, "["); - if(o.via.array.size != 0) { - msgpack_object* p = o.via.array.ptr; - msgpack_object* const pend = o.via.array.ptr + o.via.array.size; - msgpack_object_print(out, *p); - ++p; - for(; p < pend; ++p) { - fprintf(out, ", "); - msgpack_object_print(out, *p); - } - } - fprintf(out, "]"); - break; - - case MSGPACK_OBJECT_MAP: - fprintf(out, "{"); - if(o.via.map.size != 0) { - msgpack_object_kv* p = o.via.map.ptr; - msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size; - msgpack_object_print(out, p->key); - fprintf(out, "=>"); - msgpack_object_print(out, p->val); - ++p; - for(; p < pend; ++p) { - fprintf(out, ", "); - msgpack_object_print(out, p->key); - fprintf(out, "=>"); - msgpack_object_print(out, p->val); - } - } - fprintf(out, "}"); - break; - - default: - // FIXME -#if defined(PRIu64) - fprintf(out, "#<UNKNOWN %i %" PRIu64 ">", o.type, o.via.u64); -#else - if (o.via.u64 > ULONG_MAX) - fprintf(out, "#<UNKNOWN %i over 4294967295>", o.type); - else - fprintf(out, "#<UNKNOWN %i %lu>", o.type, (unsigned long)o.via.u64); -#endif - - } -} - -#endif - -#define MSGPACK_CHECKED_CALL(ret, func, aux_buffer, aux_buffer_size, ...) \ - ret = func(aux_buffer, aux_buffer_size, __VA_ARGS__); \ - if (ret <= 0 || ret >= (int)aux_buffer_size) return 0; \ - aux_buffer = aux_buffer + ret; \ - aux_buffer_size = aux_buffer_size - ret \ - -static int msgpack_object_bin_print_buffer(char *buffer, size_t buffer_size, const char *ptr, size_t size) -{ - size_t i; - char *aux_buffer = buffer; - size_t aux_buffer_size = buffer_size; - int ret; - - for (i = 0; i < size; ++i) { - if (ptr[i] == '"') { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\\\""); - } else if (isprint((unsigned char)ptr[i])) { - if (aux_buffer_size > 0) { - memcpy(aux_buffer, ptr + i, 1); - aux_buffer = aux_buffer + 1; - aux_buffer_size = aux_buffer_size - 1; - } - } else { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\\x%02x", (unsigned char)ptr[i]); - } - } - - return (int)(buffer_size - aux_buffer_size); -} - -int msgpack_object_print_buffer(char *buffer, size_t buffer_size, msgpack_object o) -{ - char *aux_buffer = buffer; - size_t aux_buffer_size = buffer_size; - int ret; - switch(o.type) { - case MSGPACK_OBJECT_NIL: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "nil"); - break; - - case MSGPACK_OBJECT_BOOLEAN: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, (o.via.boolean ? "true" : "false")); - break; - - case MSGPACK_OBJECT_POSITIVE_INTEGER: -#if defined(PRIu64) - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%" PRIu64, o.via.u64); -#else - if (o.via.u64 > ULONG_MAX) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "over 4294967295"); - } else { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%lu", (unsigned long)o.via.u64); - } -#endif - break; - - case MSGPACK_OBJECT_NEGATIVE_INTEGER: -#if defined(PRIi64) - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%" PRIi64, o.via.i64); -#else - if (o.via.i64 > LONG_MAX) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "over +2147483647"); - } else if (o.via.i64 < LONG_MIN) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "under -2147483648"); - } else { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%ld", (signed long)o.via.i64); - } -#endif - break; - - case MSGPACK_OBJECT_FLOAT32: - case MSGPACK_OBJECT_FLOAT64: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%f", o.via.f64); - break; - - case MSGPACK_OBJECT_STR: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "%.*s", (int)o.via.str.size, o.via.str.ptr); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - break; - - case MSGPACK_OBJECT_BIN: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.bin.ptr, o.via.bin.size); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - break; - - case MSGPACK_OBJECT_EXT: -#if defined(PRIi8) - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "(ext: %" PRIi8 ")", o.via.ext.type); -#else - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "(ext: %d)", (int)o.via.ext.type); -#endif - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.ext.ptr, o.via.ext.size); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\""); - break; - - case MSGPACK_OBJECT_ARRAY: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "["); - if(o.via.array.size != 0) { - msgpack_object* p = o.via.array.ptr; - msgpack_object* const pend = o.via.array.ptr + o.via.array.size; - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, *p); - ++p; - for(; p < pend; ++p) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, ", "); - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, *p); - } - } - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "]"); - break; - - case MSGPACK_OBJECT_MAP: - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "{"); - if(o.via.map.size != 0) { - msgpack_object_kv* p = o.via.map.ptr; - msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size; - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, p->key); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "=>"); - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, p->val); - ++p; - for(; p < pend; ++p) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, ", "); - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, p->key); - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "=>"); - MSGPACK_CHECKED_CALL(ret, msgpack_object_print_buffer, aux_buffer, aux_buffer_size, p->val); - } - } - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "}"); - break; - - default: - // FIXME -#if defined(PRIu64) - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "#<UNKNOWN %i %" PRIu64 ">", o.type, o.via.u64); -#else - if (o.via.u64 > ULONG_MAX) { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "#<UNKNOWN %i over 4294967295>", o.type); - } else { - MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "#<UNKNOWN %i %lu>", o.type, (unsigned long)o.via.u64); - } -#endif - } - - return (int)(buffer_size - aux_buffer_size); -} - -#undef MSGPACK_CHECKED_CALL - -bool msgpack_object_equal(const msgpack_object x, const msgpack_object y) -{ - if(x.type != y.type) { return false; } - - switch(x.type) { - case MSGPACK_OBJECT_NIL: - return true; - - case MSGPACK_OBJECT_BOOLEAN: - return x.via.boolean == y.via.boolean; - - case MSGPACK_OBJECT_POSITIVE_INTEGER: - return x.via.u64 == y.via.u64; - - case MSGPACK_OBJECT_NEGATIVE_INTEGER: - return x.via.i64 == y.via.i64; - - case MSGPACK_OBJECT_FLOAT32: - case MSGPACK_OBJECT_FLOAT64: - return x.via.f64 == y.via.f64; - - case MSGPACK_OBJECT_STR: - return x.via.str.size == y.via.str.size && - memcmp(x.via.str.ptr, y.via.str.ptr, x.via.str.size) == 0; - - case MSGPACK_OBJECT_BIN: - return x.via.bin.size == y.via.bin.size && - memcmp(x.via.bin.ptr, y.via.bin.ptr, x.via.bin.size) == 0; - - case MSGPACK_OBJECT_EXT: - return x.via.ext.size == y.via.ext.size && - x.via.ext.type == y.via.ext.type && - memcmp(x.via.ext.ptr, y.via.ext.ptr, x.via.ext.size) == 0; - - case MSGPACK_OBJECT_ARRAY: - if(x.via.array.size != y.via.array.size) { - return false; - } else if(x.via.array.size == 0) { - return true; - } else { - msgpack_object* px = x.via.array.ptr; - msgpack_object* const pxend = x.via.array.ptr + x.via.array.size; - msgpack_object* py = y.via.array.ptr; - do { - if(!msgpack_object_equal(*px, *py)) { - return false; - } - ++px; - ++py; - } while(px < pxend); - return true; - } - - case MSGPACK_OBJECT_MAP: - if(x.via.map.size != y.via.map.size) { - return false; - } else if(x.via.map.size == 0) { - return true; - } else { - msgpack_object_kv* px = x.via.map.ptr; - msgpack_object_kv* const pxend = x.via.map.ptr + x.via.map.size; - msgpack_object_kv* py = y.via.map.ptr; - do { - if(!msgpack_object_equal(px->key, py->key) || !msgpack_object_equal(px->val, py->val)) { - return false; - } - ++px; - ++py; - } while(px < pxend); - return true; - } - - default: - return false; - } -} diff --git a/fluent-bit/lib/msgpack-c/src/unpack.c b/fluent-bit/lib/msgpack-c/src/unpack.c deleted file mode 100644 index 9341cb08..00000000 --- a/fluent-bit/lib/msgpack-c/src/unpack.c +++ /dev/null @@ -1,702 +0,0 @@ -/* - * MessagePack for C unpacking routine - * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki - * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -#include "msgpack/unpack.h" -#include "msgpack/unpack_define.h" -#include "msgpack/util.h" -#include <stdlib.h> - -#ifdef _msgpack_atomic_counter_header -#include _msgpack_atomic_counter_header -#endif - - -typedef struct { - msgpack_zone** z; - bool referenced; -} unpack_user; - - -#define msgpack_unpack_struct(name) \ - struct template ## name - -#define msgpack_unpack_func(ret, name) \ - ret template ## name - -#define msgpack_unpack_callback(name) \ - template_callback ## name - -#define msgpack_unpack_object msgpack_object - -#define msgpack_unpack_user unpack_user - - -struct template_context; -typedef struct template_context template_context; - -static void template_init(template_context* ctx); - -static msgpack_object template_data(template_context* ctx); - -static int template_execute( - template_context* ctx, const char* data, size_t len, size_t* off); - - -static inline msgpack_object template_callback_root(unpack_user* u) -{ - msgpack_object o; - MSGPACK_UNUSED(u); - o.type = MSGPACK_OBJECT_NIL; - return o; -} - -static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = d; - return 0; -} - -static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = d; - return 0; -} - -static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = d; - return 0; -} - -static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = d; - return 0; -} - -static inline int template_callback_int8(unpack_user* u, int8_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - if(d >= 0) { - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = (uint64_t)d; - return 0; - } - else { - o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; - o->via.i64 = d; - return 0; - } -} - -static inline int template_callback_int16(unpack_user* u, int16_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - if(d >= 0) { - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = (uint64_t)d; - return 0; - } - else { - o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; - o->via.i64 = d; - return 0; - } -} - -static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - if(d >= 0) { - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = (uint64_t)d; - return 0; - } - else { - o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; - o->via.i64 = d; - return 0; - } -} - -static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - if(d >= 0) { - o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; - o->via.u64 = (uint64_t)d; - return 0; - } - else { - o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; - o->via.i64 = d; - return 0; - } -} - -static inline int template_callback_float(unpack_user* u, float d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_FLOAT32; - o->via.f64 = d; - return 0; -} - -static inline int template_callback_double(unpack_user* u, double d, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_FLOAT64; - o->via.f64 = d; - return 0; -} - -static inline int template_callback_nil(unpack_user* u, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_NIL; - return 0; -} - -static inline int template_callback_true(unpack_user* u, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_BOOLEAN; - o->via.boolean = true; - return 0; -} - -static inline int template_callback_false(unpack_user* u, msgpack_object* o) -{ - MSGPACK_UNUSED(u); - o->type = MSGPACK_OBJECT_BOOLEAN; - o->via.boolean = false; - return 0; -} - -static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o) -{ - size_t size; - // Let's leverage the fact that sizeof(msgpack_object) is a compile time constant - // to check for int overflows. - // Note - while n is constrained to 32-bit, the product of n * sizeof(msgpack_object) - // might not be constrained to 4GB on 64-bit systems -#if SIZE_MAX == UINT_MAX - if (n > SIZE_MAX/sizeof(msgpack_object)) - return MSGPACK_UNPACK_NOMEM_ERROR; -#endif - - o->type = MSGPACK_OBJECT_ARRAY; - o->via.array.size = 0; - - size = n * sizeof(msgpack_object); - - if (*u->z == NULL) { - *u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); - if(*u->z == NULL) { - return MSGPACK_UNPACK_NOMEM_ERROR; - } - } - - // Unsure whether size = 0 should be an error, and if so, what to return - o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(*u->z, size); - if(o->via.array.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; } - return 0; -} - -static inline int template_callback_array_item(unpack_user* u, msgpack_object* c, msgpack_object o) -{ - MSGPACK_UNUSED(u); -#if defined(__GNUC__) && !defined(__clang__) - memcpy(&c->via.array.ptr[c->via.array.size], &o, sizeof(msgpack_object)); -#else /* __GNUC__ && !__clang__ */ - c->via.array.ptr[c->via.array.size] = o; -#endif /* __GNUC__ && !__clang__ */ - ++c->via.array.size; - return 0; -} - -static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_object* o) -{ - size_t size; - // Let's leverage the fact that sizeof(msgpack_object_kv) is a compile time constant - // to check for int overflows - // Note - while n is constrained to 32-bit, the product of n * sizeof(msgpack_object) - // might not be constrained to 4GB on 64-bit systems - - // Note - this will always be false on 64-bit systems -#if SIZE_MAX == UINT_MAX - if (n > SIZE_MAX/sizeof(msgpack_object_kv)) - return MSGPACK_UNPACK_NOMEM_ERROR; -#endif - - o->type = MSGPACK_OBJECT_MAP; - o->via.map.size = 0; - - size = n * sizeof(msgpack_object_kv); - - if (*u->z == NULL) { - *u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); - if(*u->z == NULL) { - return MSGPACK_UNPACK_NOMEM_ERROR; - } - } - - // Should size = 0 be an error? If so, what error to return? - o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(*u->z, size); - if(o->via.map.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; } - return 0; -} - -static inline int template_callback_map_item(unpack_user* u, msgpack_object* c, msgpack_object k, msgpack_object v) -{ - MSGPACK_UNUSED(u); -#if defined(__GNUC__) && !defined(__clang__) - memcpy(&c->via.map.ptr[c->via.map.size].key, &k, sizeof(msgpack_object)); - memcpy(&c->via.map.ptr[c->via.map.size].val, &v, sizeof(msgpack_object)); -#else /* __GNUC__ && !__clang__ */ - c->via.map.ptr[c->via.map.size].key = k; - c->via.map.ptr[c->via.map.size].val = v; -#endif /* __GNUC__ && !__clang__ */ - ++c->via.map.size; - return 0; -} - -static inline int template_callback_str(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o) -{ - MSGPACK_UNUSED(b); - if (*u->z == NULL) { - *u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); - if(*u->z == NULL) { - return MSGPACK_UNPACK_NOMEM_ERROR; - } - } - o->type = MSGPACK_OBJECT_STR; - o->via.str.ptr = p; - o->via.str.size = l; - u->referenced = true; - return 0; -} - -static inline int template_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o) -{ - MSGPACK_UNUSED(b); - if (*u->z == NULL) { - *u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); - if(*u->z == NULL) { - return MSGPACK_UNPACK_NOMEM_ERROR; - } - } - o->type = MSGPACK_OBJECT_BIN; - o->via.bin.ptr = p; - o->via.bin.size = l; - u->referenced = true; - return 0; -} - -static inline int template_callback_ext(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o) -{ - MSGPACK_UNUSED(b); - if (l == 0) { - return MSGPACK_UNPACK_PARSE_ERROR; - } - if (*u->z == NULL) { - *u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); - if(*u->z == NULL) { - return MSGPACK_UNPACK_NOMEM_ERROR; - } - } - o->type = MSGPACK_OBJECT_EXT; - o->via.ext.type = *p; - o->via.ext.ptr = p + 1; - o->via.ext.size = l - 1; - u->referenced = true; - return 0; -} - -#include "msgpack/unpack_template.h" - - -#define CTX_CAST(m) ((template_context*)(m)) -#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced - -#define COUNTER_SIZE (sizeof(_msgpack_atomic_counter_t)) - - -static inline void init_count(void* buffer) -{ - *(volatile _msgpack_atomic_counter_t*)buffer = 1; -} - -static inline void decr_count(void* buffer) -{ - // atomic if(--*(_msgpack_atomic_counter_t*)buffer == 0) { free(buffer); } - if(_msgpack_sync_decr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer) == 0) { - free(buffer); - } -} - -static inline void incr_count(void* buffer) -{ - // atomic ++*(_msgpack_atomic_counter_t*)buffer; - _msgpack_sync_incr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer); -} - -static inline _msgpack_atomic_counter_t get_count(void* buffer) -{ - return *(volatile _msgpack_atomic_counter_t*)buffer; -} - -bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size) -{ - char* buffer; - void* ctx; - - if(initial_buffer_size < COUNTER_SIZE) { - initial_buffer_size = COUNTER_SIZE; - } - - buffer = (char*)malloc(initial_buffer_size); - if(buffer == NULL) { - return false; - } - - ctx = malloc(sizeof(template_context)); - if(ctx == NULL) { - free(buffer); - return false; - } - - mpac->buffer = buffer; - mpac->used = COUNTER_SIZE; - mpac->free = initial_buffer_size - mpac->used; - mpac->off = COUNTER_SIZE; - mpac->parsed = 0; - mpac->initial_buffer_size = initial_buffer_size; - mpac->z = NULL; - mpac->ctx = ctx; - - init_count(mpac->buffer); - - template_init(CTX_CAST(mpac->ctx)); - CTX_CAST(mpac->ctx)->user.z = &mpac->z; - CTX_CAST(mpac->ctx)->user.referenced = false; - - return true; -} - -void msgpack_unpacker_destroy(msgpack_unpacker* mpac) -{ - msgpack_zone_free(mpac->z); - free(mpac->ctx); - decr_count(mpac->buffer); -} - -msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size) -{ - msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker)); - if(mpac == NULL) { - return NULL; - } - - if(!msgpack_unpacker_init(mpac, initial_buffer_size)) { - free(mpac); - return NULL; - } - - return mpac; -} - -void msgpack_unpacker_free(msgpack_unpacker* mpac) -{ - msgpack_unpacker_destroy(mpac); - free(mpac); -} - -bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size) -{ - if(mpac->used == mpac->off && get_count(mpac->buffer) == 1 - && !CTX_REFERENCED(mpac)) { - // rewind buffer - mpac->free += mpac->used - COUNTER_SIZE; - mpac->used = COUNTER_SIZE; - mpac->off = COUNTER_SIZE; - - if(mpac->free >= size) { - return true; - } - } - - if(mpac->off == COUNTER_SIZE) { - char* tmp; - size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE - while(next_size < size + mpac->used) { - size_t tmp_next_size = next_size * 2; - if (tmp_next_size <= next_size) { - next_size = size + mpac->used; - break; - } - next_size = tmp_next_size; - } - - tmp = (char*)realloc(mpac->buffer, next_size); - if(tmp == NULL) { - return false; - } - - mpac->buffer = tmp; - mpac->free = next_size - mpac->used; - - } else { - char* tmp; - size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE - size_t not_parsed = mpac->used - mpac->off; - while(next_size < size + not_parsed + COUNTER_SIZE) { - size_t tmp_next_size = next_size * 2; - if (tmp_next_size <= next_size) { - next_size = size + not_parsed + COUNTER_SIZE; - break; - } - next_size = tmp_next_size; - } - - tmp = (char*)malloc(next_size); - if(tmp == NULL) { - return false; - } - - init_count(tmp); - - memcpy(tmp+COUNTER_SIZE, mpac->buffer+mpac->off, not_parsed); - - if(CTX_REFERENCED(mpac)) { - if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) { - free(tmp); - return false; - } - CTX_REFERENCED(mpac) = false; - } else { - decr_count(mpac->buffer); - } - - mpac->buffer = tmp; - mpac->used = not_parsed + COUNTER_SIZE; - mpac->free = next_size - mpac->used; - mpac->off = COUNTER_SIZE; - } - - return true; -} - -int msgpack_unpacker_execute(msgpack_unpacker* mpac) -{ - size_t off = mpac->off; - int ret = template_execute(CTX_CAST(mpac->ctx), - mpac->buffer, mpac->used, &mpac->off); - if(mpac->off > off) { - mpac->parsed += mpac->off - off; - } - return ret; -} - -msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac) -{ - return template_data(CTX_CAST(mpac->ctx)); -} - -msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) -{ - msgpack_zone* old = mpac->z; - - if (old == NULL) return NULL; - if(!msgpack_unpacker_flush_zone(mpac)) { - return NULL; - } - - mpac->z = NULL; - CTX_CAST(mpac->ctx)->user.z = &mpac->z; - - return old; -} - -void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac) -{ - msgpack_zone_clear(mpac->z); -} - -bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac) -{ - if(CTX_REFERENCED(mpac)) { - if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) { - return false; - } - CTX_REFERENCED(mpac) = false; - - incr_count(mpac->buffer); - } - - return true; -} - -void msgpack_unpacker_reset(msgpack_unpacker* mpac) -{ - template_init(CTX_CAST(mpac->ctx)); - // don't reset referenced flag - mpac->parsed = 0; -} - -static inline msgpack_unpack_return unpacker_next(msgpack_unpacker* mpac, - msgpack_unpacked* result) -{ - int ret; - - msgpack_unpacked_destroy(result); - - ret = msgpack_unpacker_execute(mpac); - - if(ret < 0) { - result->zone = NULL; - memset(&result->data, 0, sizeof(msgpack_object)); - return (msgpack_unpack_return)ret; - } - - if(ret == 0) { - return MSGPACK_UNPACK_CONTINUE; - } - result->zone = msgpack_unpacker_release_zone(mpac); - result->data = msgpack_unpacker_data(mpac); - - return MSGPACK_UNPACK_SUCCESS; -} - -msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, - msgpack_unpacked* result) -{ - msgpack_unpack_return 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) -{ - msgpack_unpack_return ret; - - ret = unpacker_next(mpac, result); - if (ret == MSGPACK_UNPACK_SUCCESS || ret == MSGPACK_UNPACK_CONTINUE) { - *p_bytes = mpac->parsed; - } - - if (ret == MSGPACK_UNPACK_SUCCESS) { - msgpack_unpacker_reset(mpac); - } - - return ret; -} - -msgpack_unpack_return -msgpack_unpack(const char* data, size_t len, size_t* off, - msgpack_zone* result_zone, msgpack_object* result) -{ - size_t noff = 0; - if(off != NULL) { noff = *off; } - - if(len <= noff) { - // FIXME - return MSGPACK_UNPACK_CONTINUE; - } - else { - int e; - template_context ctx; - template_init(&ctx); - - ctx.user.z = &result_zone; - ctx.user.referenced = false; - - e = template_execute(&ctx, data, len, &noff); - if(e < 0) { - return (msgpack_unpack_return)e; - } - - if(off != NULL) { *off = noff; } - - if(e == 0) { - return MSGPACK_UNPACK_CONTINUE; - } - - *result = template_data(&ctx); - - if(noff < len) { - return MSGPACK_UNPACK_EXTRA_BYTES; - } - - return MSGPACK_UNPACK_SUCCESS; - } -} - -msgpack_unpack_return -msgpack_unpack_next(msgpack_unpacked* result, - const char* data, size_t len, size_t* off) -{ - size_t noff = 0; - msgpack_unpacked_destroy(result); - - if(off != NULL) { noff = *off; } - - if(len <= noff) { - return MSGPACK_UNPACK_CONTINUE; - } - - { - int e; - template_context ctx; - template_init(&ctx); - - ctx.user.z = &result->zone; - ctx.user.referenced = false; - - e = template_execute(&ctx, data, len, &noff); - - if(off != NULL) { *off = noff; } - - if(e < 0) { - msgpack_zone_free(result->zone); - result->zone = NULL; - return (msgpack_unpack_return)e; - } - - if(e == 0) { - return MSGPACK_UNPACK_CONTINUE; - } - - result->data = template_data(&ctx); - - return MSGPACK_UNPACK_SUCCESS; - } -} - -#if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND) -// FIXME: Dirty hack to avoid a bus error caused by OS X's old gcc. -static void dummy_function_to_avoid_bus_error() -{ -} -#endif diff --git a/fluent-bit/lib/msgpack-c/src/version.c b/fluent-bit/lib/msgpack-c/src/version.c deleted file mode 100644 index 83f75108..00000000 --- a/fluent-bit/lib/msgpack-c/src/version.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "msgpack.h" - -const char* msgpack_version(void) -{ - return MSGPACK_VERSION; -} - -int msgpack_version_major(void) -{ - return MSGPACK_VERSION_MAJOR; -} - -int msgpack_version_minor(void) -{ - return MSGPACK_VERSION_MINOR; -} - -int msgpack_version_revision(void) -{ - return MSGPACK_VERSION_REVISION; -} - diff --git a/fluent-bit/lib/msgpack-c/src/vrefbuffer.c b/fluent-bit/lib/msgpack-c/src/vrefbuffer.c deleted file mode 100644 index 6c3ce3a0..00000000 --- a/fluent-bit/lib/msgpack-c/src/vrefbuffer.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * MessagePack for C zero-copy buffer implementation - * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki - * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -#include "msgpack/vrefbuffer.h" -#include <stdlib.h> -#include <string.h> - -#define MSGPACK_PACKER_MAX_BUFFER_SIZE 9 - -struct msgpack_vrefbuffer_chunk { - struct msgpack_vrefbuffer_chunk* next; - /* data ... */ -}; - -bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, - size_t ref_size, size_t chunk_size) -{ - size_t nfirst; - struct iovec* array; - msgpack_vrefbuffer_chunk* chunk; - - if (ref_size == 0) { - ref_size = MSGPACK_VREFBUFFER_REF_SIZE; - } - if(chunk_size == 0) { - chunk_size = MSGPACK_VREFBUFFER_CHUNK_SIZE; - } - vbuf->chunk_size = chunk_size; - vbuf->ref_size = - ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ? - ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ; - - if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size) { - return false; - } - - nfirst = (sizeof(struct iovec) < 72/2) ? - 72 / sizeof(struct iovec) : 8; - - array = (struct iovec*)malloc( - sizeof(struct iovec) * nfirst); - if(array == NULL) { - return false; - } - - vbuf->tail = array; - vbuf->end = array + nfirst; - vbuf->array = array; - - chunk = (msgpack_vrefbuffer_chunk*)malloc( - sizeof(msgpack_vrefbuffer_chunk) + chunk_size); - if(chunk == NULL) { - free(array); - return false; - } - else { - msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; - - ib->free = chunk_size; - ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); - ib->head = chunk; - chunk->next = NULL; - - return true; - } -} - -void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf) -{ - msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head; - while(true) { - msgpack_vrefbuffer_chunk* n = c->next; - free(c); - if(n != NULL) { - c = n; - } else { - break; - } - } - free(vbuf->array); -} - -void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf) -{ - msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next; - msgpack_vrefbuffer_chunk* n; - while(c != NULL) { - n = c->next; - free(c); - c = n; - } - - { - msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; - msgpack_vrefbuffer_chunk* chunk = ib->head; - chunk->next = NULL; - ib->free = vbuf->chunk_size; - ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); - - vbuf->tail = vbuf->array; - } -} - -int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, - const char* buf, size_t len) -{ - if(vbuf->tail == vbuf->end) { - const size_t nused = (size_t)(vbuf->tail - vbuf->array); - const size_t nnext = nused * 2; - - struct iovec* nvec = (struct iovec*)realloc( - vbuf->array, sizeof(struct iovec)*nnext); - if(nvec == NULL) { - return -1; - } - - vbuf->array = nvec; - vbuf->end = nvec + nnext; - vbuf->tail = nvec + nused; - } - - vbuf->tail->iov_base = (char*)buf; - vbuf->tail->iov_len = len; - ++vbuf->tail; - - return 0; -} - -int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf, - const char* buf, size_t len) -{ - msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; - char* m; - - if(ib->free < len) { - msgpack_vrefbuffer_chunk* chunk; - size_t sz = vbuf->chunk_size; - if(sz < len) { - sz = len; - } - - if((sizeof(msgpack_vrefbuffer_chunk) + sz) < sz){ - return -1; - } - chunk = (msgpack_vrefbuffer_chunk*)malloc( - sizeof(msgpack_vrefbuffer_chunk) + sz); - if(chunk == NULL) { - return -1; - } - - chunk->next = ib->head; - ib->head = chunk; - ib->free = sz; - ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); - } - - m = ib->ptr; - memcpy(m, buf, len); - ib->free -= len; - ib->ptr += len; - - if(vbuf->tail != vbuf->array && m == - (const char*)((vbuf->tail-1)->iov_base) + (vbuf->tail-1)->iov_len) { - (vbuf->tail-1)->iov_len += len; - return 0; - } else { - return msgpack_vrefbuffer_append_ref(vbuf, m, len); - } -} - -int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to) -{ - size_t sz = vbuf->chunk_size; - msgpack_vrefbuffer_chunk* empty; - - if((sizeof(msgpack_vrefbuffer_chunk) + sz) < sz){ - return -1; - } - - empty = (msgpack_vrefbuffer_chunk*)malloc( - sizeof(msgpack_vrefbuffer_chunk) + sz); - if(empty == NULL) { - return -1; - } - - empty->next = NULL; - - { - const size_t nused = (size_t)(vbuf->tail - vbuf->array); - if(to->tail + nused < vbuf->end) { - struct iovec* nvec; - const size_t tosize = (size_t)(to->tail - to->array); - const size_t reqsize = nused + tosize; - size_t nnext = (size_t)(to->end - to->array) * 2; - while(nnext < reqsize) { - size_t tmp_nnext = nnext * 2; - if (tmp_nnext <= nnext) { - nnext = reqsize; - break; - } - nnext = tmp_nnext; - } - - nvec = (struct iovec*)realloc( - to->array, sizeof(struct iovec)*nnext); - if(nvec == NULL) { - free(empty); - return -1; - } - - to->array = nvec; - to->end = nvec + nnext; - to->tail = nvec + tosize; - } - - memcpy(to->tail, vbuf->array, sizeof(struct iovec)*nused); - - to->tail += nused; - vbuf->tail = vbuf->array; - - { - msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; - msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer; - - msgpack_vrefbuffer_chunk* last = ib->head; - while(last->next != NULL) { - last = last->next; - } - last->next = toib->head; - toib->head = ib->head; - - if(toib->free < ib->free) { - toib->free = ib->free; - toib->ptr = ib->ptr; - } - - ib->head = empty; - ib->free = sz; - ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk); - } - } - - return 0; -} diff --git a/fluent-bit/lib/msgpack-c/src/zone.c b/fluent-bit/lib/msgpack-c/src/zone.c deleted file mode 100644 index 372a1f5b..00000000 --- a/fluent-bit/lib/msgpack-c/src/zone.c +++ /dev/null @@ -1,222 +0,0 @@ -/* - * MessagePack for C memory pool implementation - * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki - * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -#include "msgpack/zone.h" -#include <stdlib.h> -#include <string.h> - -struct msgpack_zone_chunk { - struct msgpack_zone_chunk* next; - /* data ... */ -}; - -static inline bool init_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size) -{ - msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc( - sizeof(msgpack_zone_chunk) + chunk_size); - if(chunk == NULL) { - return false; - } - - cl->head = chunk; - cl->free = chunk_size; - cl->ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk); - chunk->next = NULL; - - return true; -} - -static inline void destroy_chunk_list(msgpack_zone_chunk_list* cl) -{ - msgpack_zone_chunk* c = cl->head; - while(true) { - msgpack_zone_chunk* n = c->next; - free(c); - if(n != NULL) { - c = n; - } else { - break; - } - } -} - -static inline void clear_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size) -{ - msgpack_zone_chunk* c = cl->head; - while(true) { - msgpack_zone_chunk* n = c->next; - if(n != NULL) { - free(c); - c = n; - } else { - cl->head = c; - break; - } - } - cl->head->next = NULL; - cl->free = chunk_size; - cl->ptr = ((char*)cl->head) + sizeof(msgpack_zone_chunk); -} - -void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size) -{ - msgpack_zone_chunk_list* const cl = &zone->chunk_list; - msgpack_zone_chunk* chunk; - - size_t sz = zone->chunk_size; - - while(sz < size) { - size_t tmp_sz = sz * 2; - if (tmp_sz <= sz) { - sz = size; - break; - } - sz = tmp_sz; - } - - chunk = (msgpack_zone_chunk*)malloc( - sizeof(msgpack_zone_chunk) + sz); - if (chunk == NULL) { - return NULL; - } - else { - char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk); - chunk->next = cl->head; - cl->head = chunk; - cl->free = sz - size; - cl->ptr = ptr + size; - - return ptr; - } -} - - -static inline void init_finalizer_array(msgpack_zone_finalizer_array* fa) -{ - fa->tail = NULL; - fa->end = NULL; - fa->array = NULL; -} - -static inline void call_finalizer_array(msgpack_zone_finalizer_array* fa) -{ - msgpack_zone_finalizer* fin = fa->tail; - for(; fin != fa->array; --fin) { - (*(fin-1)->func)((fin-1)->data); - } -} - -static inline void destroy_finalizer_array(msgpack_zone_finalizer_array* fa) -{ - call_finalizer_array(fa); - free(fa->array); -} - -static inline void clear_finalizer_array(msgpack_zone_finalizer_array* fa) -{ - call_finalizer_array(fa); - fa->tail = fa->array; -} - -bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone, - void (*func)(void* data), void* data) -{ - msgpack_zone_finalizer_array* const fa = &zone->finalizer_array; - msgpack_zone_finalizer* tmp; - - const size_t nused = (size_t)(fa->end - fa->array); - - size_t nnext; - if(nused == 0) { - nnext = (sizeof(msgpack_zone_finalizer) < 72/2) ? - 72 / sizeof(msgpack_zone_finalizer) : 8; - - } else { - nnext = nused * 2; - } - - tmp = (msgpack_zone_finalizer*)realloc(fa->array, - sizeof(msgpack_zone_finalizer) * nnext); - if(tmp == NULL) { - return false; - } - - fa->array = tmp; - fa->end = tmp + nnext; - fa->tail = tmp + nused; - - fa->tail->func = func; - fa->tail->data = data; - - ++fa->tail; - - return true; -} - - -bool msgpack_zone_is_empty(msgpack_zone* zone) -{ - msgpack_zone_chunk_list* const cl = &zone->chunk_list; - msgpack_zone_finalizer_array* const fa = &zone->finalizer_array; - return cl->free == zone->chunk_size && cl->head->next == NULL && - fa->tail == fa->array; -} - - -void msgpack_zone_destroy(msgpack_zone* zone) -{ - destroy_finalizer_array(&zone->finalizer_array); - destroy_chunk_list(&zone->chunk_list); -} - -void msgpack_zone_clear(msgpack_zone* zone) -{ - clear_finalizer_array(&zone->finalizer_array); - clear_chunk_list(&zone->chunk_list, zone->chunk_size); -} - -bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size) -{ - zone->chunk_size = chunk_size; - - if(!init_chunk_list(&zone->chunk_list, chunk_size)) { - return false; - } - - init_finalizer_array(&zone->finalizer_array); - - return true; -} - -msgpack_zone* msgpack_zone_new(size_t chunk_size) -{ - msgpack_zone* zone = (msgpack_zone*)malloc( - sizeof(msgpack_zone)); - if(zone == NULL) { - return NULL; - } - - zone->chunk_size = chunk_size; - - if(!init_chunk_list(&zone->chunk_list, chunk_size)) { - free(zone); - return NULL; - } - - init_finalizer_array(&zone->finalizer_array); - - return zone; -} - -void msgpack_zone_free(msgpack_zone* zone) -{ - if(zone == NULL) { return; } - msgpack_zone_destroy(zone); - free(zone); -} |