Move msgpack to libcommon
authorMichael Jeanson <mjeanson@efficios.com>
Fri, 2 Apr 2021 19:29:21 +0000 (15:29 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 9 Apr 2021 15:51:24 +0000 (11:51 -0400)
msgpack provides common utils used across libraries, move it to
libcommon.

This is part of an effort to standardize our autotools setup across
projects to simplify maintenance.

Change-Id: Iaf8bb9e4504891f6072e68ac0ea24b8a13503e71
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 files changed:
.gitignore
configure.ac
src/Makefile.am
src/common/Makefile.am
src/common/msgpack/msgpack.c [new file with mode: 0644]
src/common/msgpack/msgpack.h [new file with mode: 0644]
src/liblttng-ust/Makefile.am
src/liblttng-ust/event-notifier-notification.c
src/libmsgpack/Makefile.am [deleted file]
src/libmsgpack/msgpack.c [deleted file]
src/libmsgpack/msgpack.h [deleted file]
tests/unit/libmsgpack/Makefile.am
tests/unit/libmsgpack/test_msgpack.c

index 0e4a24644e06f00c5b1b7bc8c1bf28cbbf291117..bb9079bf24c6d784de626fbe977d18f220a649c4 100644 (file)
@@ -146,7 +146,6 @@ cscope.*
 /src/liblttng-ust-libc-wrapper/Makefile
 /src/liblttng-ust-python-agent/Makefile
 /src/liblttng-ust/Makefile
-/src/libmsgpack/Makefile
 /src/libringbuffer/Makefile
 /src/python-lttngust/Makefile
 /src/Makefile
index 4c6eac89bf32e2c204a2e2aff2d6cb66d6272fee..c9bf1e866032458e55a6452b5b7dacfb7cb8b865 100644 (file)
@@ -540,7 +540,6 @@ AC_CONFIG_FILES([
   src/liblttng-ust-libc-wrapper/Makefile
   src/liblttng-ust/Makefile
   src/liblttng-ust-python-agent/Makefile
-  src/libmsgpack/Makefile
   src/libringbuffer/Makefile
   src/lttng-ust-ctl.pc
   src/lttng-ust.pc
index 28a32ba5683768f60de07c6f572e1dd8497cd829..a5e8ac94decb2071c0b37711479401aa8e1bd211 100644 (file)
@@ -5,7 +5,6 @@ SUBDIRS = \
        libringbuffer \
        liblttng-ust-comm \
        libcounter \
-       libmsgpack \
        liblttng-ust \
        liblttng-ust-ctl \
        liblttng-ust-fd \
index e73cc1fa95f65c203eeba5c06ad17b5dbbe7c625..419561eb1d2bd7499330ca428e95782152a42cde 100644 (file)
@@ -38,9 +38,17 @@ noinst_HEADERS += \
 ###                      ###
 
 noinst_LTLIBRARIES = \
+       msgpack/libmsgpack.la \
        snprintf/libsnprintf.la \
        libcommon.la
 
+# msgpack
+msgpack_libmsgpack_la_SOURCES = \
+       msgpack/msgpack.c \
+       msgpack/msgpack.h
+
+msgpack_libmsgpack_la_CFLAGS = -DUST_COMPONENT="libmsgpack" $(AM_CFLAGS)
+
 # snprintf
 snprintf_libsnprintf_la_SOURCES = \
        snprintf/fflush.c \
@@ -63,6 +71,7 @@ libcommon_la_SOURCES = \
        patient.c
 
 libcommon_la_LIBADD = \
+       msgpack/libmsgpack.la \
        snprintf/libsnprintf.la
 
 EXTRA_DIST = snprintf/README
diff --git a/src/common/msgpack/msgpack.c b/src/common/msgpack/msgpack.c
new file mode 100644 (file)
index 0000000..10b06ad
--- /dev/null
@@ -0,0 +1,502 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
+ */
+
+#define _LGPL_SOURCE
+#include <stddef.h>
+
+#define MSGPACK_FIXSTR_ID_MASK         0xA0
+#define MSGPACK_FIXMAP_ID_MASK         0x80
+#define MSGPACK_FIXARRAY_ID_MASK       0x90
+
+#define MSGPACK_NIL_ID         0xC0
+#define MSGPACK_FALSE_ID       0xC2
+#define MSGPACK_TRUE_ID                0xC3
+#define MSGPACK_MAP16_ID       0xDE
+#define MSGPACK_ARRAY16_ID     0xDC
+
+#define MSGPACK_UINT8_ID       0xCC
+#define MSGPACK_UINT16_ID      0xCD
+#define MSGPACK_UINT32_ID      0xCE
+#define MSGPACK_UINT64_ID      0xCF
+
+#define MSGPACK_INT8_ID                0xD0
+#define MSGPACK_INT16_ID       0xD1
+#define MSGPACK_INT32_ID       0xD2
+#define MSGPACK_INT64_ID       0xD3
+
+#define MSGPACK_FLOAT64_ID     0xCB
+#define MSGPACK_STR16_ID       0xDA
+
+#define MSGPACK_FIXINT_MAX             ((1 << 7) - 1)
+#define MSGPACK_FIXINT_MIN             -(1 << 5)
+#define MSGPACK_FIXMAP_MAX_COUNT       15
+#define MSGPACK_FIXARRAY_MAX_COUNT     15
+#define MSGPACK_FIXSTR_MAX_LENGTH      31
+
+#ifdef __KERNEL__
+#include <linux/bug.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include <lttng/msgpack.h>
+
+#define INT8_MIN               (-128)
+#define INT16_MIN              (-32767-1)
+#define INT32_MIN              (-2147483647-1)
+#define INT8_MAX               (127)
+#define INT16_MAX              (32767)
+#define INT32_MAX              (2147483647)
+#define UINT8_MAX              (255)
+#define UINT16_MAX             (65535)
+#define UINT32_MAX             (4294967295U)
+
+#define byteswap_host_to_be16(_tmp) cpu_to_be16(_tmp)
+#define byteswap_host_to_be32(_tmp) cpu_to_be32(_tmp)
+#define byteswap_host_to_be64(_tmp) cpu_to_be64(_tmp)
+
+#define lttng_msgpack_assert(cond) WARN_ON(!(cond))
+
+#else /* __KERNEL__ */
+
+#include <lttng/ust-endian.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "msgpack.h"
+
+#define byteswap_host_to_be16(_tmp) htobe16(_tmp)
+#define byteswap_host_to_be32(_tmp) htobe32(_tmp)
+#define byteswap_host_to_be64(_tmp) htobe64(_tmp)
+
+#define lttng_msgpack_assert(cond) ({ \
+       if (!(cond)) \
+               fprintf(stderr, "Assertion failed. %s:%d\n", __FILE__, __LINE__); \
+       })
+#endif /* __KERNEL__ */
+
+static inline int lttng_msgpack_append_buffer(
+               struct lttng_msgpack_writer *writer,
+               const uint8_t *buf,
+               size_t length)
+{
+       int ret = 0;
+
+       lttng_msgpack_assert(buf);
+
+       /* Ensure we are not trying to write after the end of the buffer. */
+       if (writer->write_pos + length > writer->end_write_pos) {
+               ret = -1;
+               goto end;
+       }
+
+       memcpy(writer->write_pos, buf, length);
+       writer->write_pos += length;
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_append_u8(
+               struct lttng_msgpack_writer *writer, uint8_t value)
+{
+       return lttng_msgpack_append_buffer(writer, &value, sizeof(value));
+}
+
+static inline int lttng_msgpack_append_u16(
+               struct lttng_msgpack_writer *writer, uint16_t value)
+{
+       value = byteswap_host_to_be16(value);
+
+       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
+}
+
+static inline int lttng_msgpack_append_u32(
+               struct lttng_msgpack_writer *writer, uint32_t value)
+{
+       value = byteswap_host_to_be32(value);
+
+       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
+}
+
+static inline int lttng_msgpack_append_u64(
+               struct lttng_msgpack_writer *writer, uint64_t value)
+{
+       value = byteswap_host_to_be64(value);
+
+       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
+}
+
+static inline int lttng_msgpack_append_f64(
+               struct lttng_msgpack_writer *writer, double value)
+{
+
+       union {
+               double d;
+               uint64_t u;
+       } u;
+
+       u.d = value;
+
+       return lttng_msgpack_append_u64(writer, u.u);
+}
+
+static inline int lttng_msgpack_append_i8(
+               struct lttng_msgpack_writer *writer, int8_t value)
+{
+       return lttng_msgpack_append_u8(writer, (uint8_t) value);
+}
+
+static inline int lttng_msgpack_append_i16(
+               struct lttng_msgpack_writer *writer, int16_t value)
+{
+       return lttng_msgpack_append_u16(writer, (uint16_t) value);
+}
+
+static inline int lttng_msgpack_append_i32(
+               struct lttng_msgpack_writer *writer, int32_t value)
+{
+       return lttng_msgpack_append_u32(writer, (uint32_t) value);
+}
+
+static inline int lttng_msgpack_append_i64(
+               struct lttng_msgpack_writer *writer, int64_t value)
+{
+       return lttng_msgpack_append_u64(writer, (uint64_t) value);
+}
+
+static inline int lttng_msgpack_encode_f64(
+               struct lttng_msgpack_writer *writer, double value)
+{
+       int ret;
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_FLOAT64_ID);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_f64(writer, value);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_fixmap(
+               struct lttng_msgpack_writer *writer, uint8_t count)
+{
+       int ret = 0;
+
+       lttng_msgpack_assert(count <= MSGPACK_FIXMAP_MAX_COUNT);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXMAP_ID_MASK | count);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_map16(
+               struct lttng_msgpack_writer *writer, uint16_t count)
+{
+       int ret;
+
+       lttng_msgpack_assert(count > MSGPACK_FIXMAP_MAX_COUNT);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_MAP16_ID);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_u16(writer, count);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_fixarray(
+               struct lttng_msgpack_writer *writer, uint8_t count)
+{
+       int ret = 0;
+
+       lttng_msgpack_assert(count <= MSGPACK_FIXARRAY_MAX_COUNT);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXARRAY_ID_MASK | count);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_array16(
+               struct lttng_msgpack_writer *writer, uint16_t count)
+{
+       int ret;
+
+       lttng_msgpack_assert(count > MSGPACK_FIXARRAY_MAX_COUNT);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_ARRAY16_ID);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_u16(writer, count);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_fixstr(
+               struct lttng_msgpack_writer *writer,
+               const char *str,
+               uint8_t len)
+{
+       int ret;
+
+       lttng_msgpack_assert(len <= MSGPACK_FIXSTR_MAX_LENGTH);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXSTR_ID_MASK | len);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+static inline int lttng_msgpack_encode_str16(
+               struct lttng_msgpack_writer *writer,
+               const char *str,
+               uint16_t len)
+{
+       int ret;
+
+       lttng_msgpack_assert(len > MSGPACK_FIXSTR_MAX_LENGTH);
+
+       ret = lttng_msgpack_append_u8(writer, MSGPACK_STR16_ID);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_u16(writer, len);
+       if (ret)
+               goto end;
+
+       ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
+       if (ret)
+               goto end;
+
+end:
+       return ret;
+}
+
+int lttng_msgpack_begin_map(struct lttng_msgpack_writer *writer, size_t count)
+{
+       int ret;
+
+       if (count >= (1 << 16)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (count <= MSGPACK_FIXMAP_MAX_COUNT)
+               ret = lttng_msgpack_encode_fixmap(writer, count);
+       else
+               ret = lttng_msgpack_encode_map16(writer, count);
+
+       writer->map_nesting++;
+end:
+       return ret;
+}
+
+int lttng_msgpack_end_map(struct lttng_msgpack_writer *writer)
+{
+       lttng_msgpack_assert(writer->map_nesting > 0);
+       writer->map_nesting--;
+       return 0;
+}
+
+int lttng_msgpack_begin_array(
+               struct lttng_msgpack_writer *writer, size_t count)
+{
+       int ret;
+
+       if (count >= (1 << 16)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (count <= MSGPACK_FIXARRAY_MAX_COUNT)
+               ret = lttng_msgpack_encode_fixarray(writer, count);
+       else
+               ret = lttng_msgpack_encode_array16(writer, count);
+
+       writer->array_nesting++;
+end:
+       return ret;
+}
+
+int lttng_msgpack_end_array(struct lttng_msgpack_writer *writer)
+{
+       lttng_msgpack_assert(writer->array_nesting > 0);
+       writer->array_nesting--;
+       return 0;
+}
+
+int lttng_msgpack_write_str(struct lttng_msgpack_writer *writer,
+               const char *str)
+{
+       int ret;
+       size_t length = strlen(str);
+
+       if (length >= (1 << 16)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (length <= MSGPACK_FIXSTR_MAX_LENGTH)
+               ret = lttng_msgpack_encode_fixstr(writer, str, length);
+       else
+               ret = lttng_msgpack_encode_str16(writer, str, length);
+
+end:
+       return ret;
+}
+
+int lttng_msgpack_write_nil(struct lttng_msgpack_writer *writer)
+{
+       return lttng_msgpack_append_u8(writer, MSGPACK_NIL_ID);
+}
+
+int lttng_msgpack_write_true(struct lttng_msgpack_writer *writer)
+{
+       return lttng_msgpack_append_u8(writer, MSGPACK_TRUE_ID);
+}
+
+int lttng_msgpack_write_false(struct lttng_msgpack_writer *writer)
+{
+       return lttng_msgpack_append_u8(writer, MSGPACK_FALSE_ID);
+}
+
+int lttng_msgpack_write_unsigned_integer(
+               struct lttng_msgpack_writer *writer, uint64_t value)
+{
+       int ret = 0;
+
+       if (value <= MSGPACK_FIXINT_MAX) {
+               ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
+               if (ret)
+                       goto end;
+       } else if (value <= UINT8_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT8_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
+               if (ret)
+                       goto end;
+       } else if (value <= UINT16_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT16_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_u16(writer, (uint16_t) value);
+               if (ret)
+                       goto end;
+       } else if (value <= UINT32_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT32_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_u32(writer, (uint32_t) value);
+               if (ret)
+                       goto end;
+       } else {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT64_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_u64(writer, value);
+               if (ret)
+                       goto end;
+       }
+
+end:
+       return ret;
+}
+
+int lttng_msgpack_write_signed_integer(struct lttng_msgpack_writer *writer, int64_t value)
+{
+       int ret;
+
+       if (value >= MSGPACK_FIXINT_MIN && value <= MSGPACK_FIXINT_MAX){
+               ret = lttng_msgpack_append_i8(writer, (int8_t) value);
+               if (ret)
+                       goto end;
+       } else if (value >= INT8_MIN && value <= INT8_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT8_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_i8(writer, (int8_t) value);
+               if (ret)
+                       goto end;
+       } else if (value >= INT16_MIN && value <= INT16_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT16_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_i16(writer, (int16_t) value);
+               if (ret)
+                       goto end;
+       } else if (value >= INT32_MIN && value <= INT32_MAX) {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT32_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_i32(writer, (int32_t) value);
+               if (ret)
+                       goto end;
+       } else {
+               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT64_ID);
+               if (ret)
+                       goto end;
+
+               ret = lttng_msgpack_append_i64(writer, value);
+               if (ret)
+                       goto end;
+       }
+
+end:
+       return ret;
+}
+
+int lttng_msgpack_write_double(struct lttng_msgpack_writer *writer, double value)
+{
+       return lttng_msgpack_encode_f64(writer, value);
+}
+
+void lttng_msgpack_writer_init(struct lttng_msgpack_writer *writer,
+               uint8_t *buffer, size_t size)
+{
+       lttng_msgpack_assert(buffer);
+
+       writer->buffer = buffer;
+       writer->write_pos = buffer;
+       writer->end_write_pos = buffer + size;
+
+       writer->array_nesting = 0;
+       writer->map_nesting = 0;
+}
+
+void lttng_msgpack_writer_fini(struct lttng_msgpack_writer *writer)
+{
+       memset(writer, 0, sizeof(*writer));
+}
diff --git a/src/common/msgpack/msgpack.h b/src/common/msgpack/msgpack.h
new file mode 100644 (file)
index 0000000..140ad38
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
+ */
+
+#ifndef _LTTNG_UST_MSGPACK_H
+#define _LTTNG_UST_MSGPACK_H
+
+#include <stddef.h>
+#ifdef __KERNEL__
+#include <linux/types.h>
+#else /* __KERNEL__ */
+#include <stdint.h>
+#endif /* __KERNEL__ */
+
+struct lttng_msgpack_writer {
+       uint8_t *buffer;
+       uint8_t *write_pos;
+       const uint8_t *end_write_pos;
+       uint8_t array_nesting;
+       uint8_t map_nesting;
+};
+
+void lttng_msgpack_writer_init(
+               struct lttng_msgpack_writer *writer,
+               uint8_t *buffer, size_t size)
+       __attribute__((visibility("hidden")));
+
+void lttng_msgpack_writer_fini(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_nil(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_true(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_false(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_unsigned_integer(
+               struct lttng_msgpack_writer *writer, uint64_t value)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_signed_integer(
+               struct lttng_msgpack_writer *writer, int64_t value)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_double(struct lttng_msgpack_writer *writer, double value)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_write_str(struct lttng_msgpack_writer *writer,
+               const char *value)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_begin_map(struct lttng_msgpack_writer *writer, size_t count)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_end_map(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_begin_array(
+               struct lttng_msgpack_writer *writer, size_t count)
+       __attribute__((visibility("hidden")));
+
+int lttng_msgpack_end_array(struct lttng_msgpack_writer *writer)
+       __attribute__((visibility("hidden")));
+
+#endif /* _LTTNG_UST_MSGPACK_H */
index 8892ff32a7e84001c52db31ae5755002741b829f..eb77c95083844cddbfcd713b72b2130a6f0e3afd 100644 (file)
@@ -139,7 +139,6 @@ liblttng_ust_la_LIBADD = \
        $(top_builddir)/src/liblttng-ust-comm/liblttng-ust-comm.la \
        liblttng-ust-tracepoint.la \
        liblttng-ust-runtime.la liblttng-ust-support.la \
-       $(top_builddir)/src/libmsgpack/libmsgpack.la \
        $(DL_LIBS)
 
 liblttng_ust_la_CFLAGS = -DUST_COMPONENT="liblttng_ust" $(AM_CFLAGS)
index aab66ab26dc2e6076b1d5b8275e6d0904a47866c..32886fdb3a9d9fe19d81fec92dbbb75c8150326f 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "lttng-tracer-core.h"
 #include "ust-events-internal.h"
-#include "../libmsgpack/msgpack.h"
+#include "common/msgpack/msgpack.h"
 #include "lttng-bytecode.h"
 #include "common/patient.h"
 
diff --git a/src/libmsgpack/Makefile.am b/src/libmsgpack/Makefile.am
deleted file mode 100644 (file)
index 53b1684..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-# SPDX-License-Identifier: LGPL-2.1-only
-
-AM_CFLAGS += -fno-strict-aliasing
-
-noinst_LTLIBRARIES = libmsgpack.la
-
-libmsgpack_la_SOURCES = \
-       msgpack.c msgpack.h
-
-libmsgpack_la_CFLAGS = -DUST_COMPONENT="libmsgpack" $(AM_CFLAGS)
diff --git a/src/libmsgpack/msgpack.c b/src/libmsgpack/msgpack.c
deleted file mode 100644 (file)
index 10b06ad..0000000
+++ /dev/null
@@ -1,502 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
- */
-
-#define _LGPL_SOURCE
-#include <stddef.h>
-
-#define MSGPACK_FIXSTR_ID_MASK         0xA0
-#define MSGPACK_FIXMAP_ID_MASK         0x80
-#define MSGPACK_FIXARRAY_ID_MASK       0x90
-
-#define MSGPACK_NIL_ID         0xC0
-#define MSGPACK_FALSE_ID       0xC2
-#define MSGPACK_TRUE_ID                0xC3
-#define MSGPACK_MAP16_ID       0xDE
-#define MSGPACK_ARRAY16_ID     0xDC
-
-#define MSGPACK_UINT8_ID       0xCC
-#define MSGPACK_UINT16_ID      0xCD
-#define MSGPACK_UINT32_ID      0xCE
-#define MSGPACK_UINT64_ID      0xCF
-
-#define MSGPACK_INT8_ID                0xD0
-#define MSGPACK_INT16_ID       0xD1
-#define MSGPACK_INT32_ID       0xD2
-#define MSGPACK_INT64_ID       0xD3
-
-#define MSGPACK_FLOAT64_ID     0xCB
-#define MSGPACK_STR16_ID       0xDA
-
-#define MSGPACK_FIXINT_MAX             ((1 << 7) - 1)
-#define MSGPACK_FIXINT_MIN             -(1 << 5)
-#define MSGPACK_FIXMAP_MAX_COUNT       15
-#define MSGPACK_FIXARRAY_MAX_COUNT     15
-#define MSGPACK_FIXSTR_MAX_LENGTH      31
-
-#ifdef __KERNEL__
-#include <linux/bug.h>
-#include <linux/string.h>
-#include <linux/types.h>
-
-#include <lttng/msgpack.h>
-
-#define INT8_MIN               (-128)
-#define INT16_MIN              (-32767-1)
-#define INT32_MIN              (-2147483647-1)
-#define INT8_MAX               (127)
-#define INT16_MAX              (32767)
-#define INT32_MAX              (2147483647)
-#define UINT8_MAX              (255)
-#define UINT16_MAX             (65535)
-#define UINT32_MAX             (4294967295U)
-
-#define byteswap_host_to_be16(_tmp) cpu_to_be16(_tmp)
-#define byteswap_host_to_be32(_tmp) cpu_to_be32(_tmp)
-#define byteswap_host_to_be64(_tmp) cpu_to_be64(_tmp)
-
-#define lttng_msgpack_assert(cond) WARN_ON(!(cond))
-
-#else /* __KERNEL__ */
-
-#include <lttng/ust-endian.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "msgpack.h"
-
-#define byteswap_host_to_be16(_tmp) htobe16(_tmp)
-#define byteswap_host_to_be32(_tmp) htobe32(_tmp)
-#define byteswap_host_to_be64(_tmp) htobe64(_tmp)
-
-#define lttng_msgpack_assert(cond) ({ \
-       if (!(cond)) \
-               fprintf(stderr, "Assertion failed. %s:%d\n", __FILE__, __LINE__); \
-       })
-#endif /* __KERNEL__ */
-
-static inline int lttng_msgpack_append_buffer(
-               struct lttng_msgpack_writer *writer,
-               const uint8_t *buf,
-               size_t length)
-{
-       int ret = 0;
-
-       lttng_msgpack_assert(buf);
-
-       /* Ensure we are not trying to write after the end of the buffer. */
-       if (writer->write_pos + length > writer->end_write_pos) {
-               ret = -1;
-               goto end;
-       }
-
-       memcpy(writer->write_pos, buf, length);
-       writer->write_pos += length;
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_append_u8(
-               struct lttng_msgpack_writer *writer, uint8_t value)
-{
-       return lttng_msgpack_append_buffer(writer, &value, sizeof(value));
-}
-
-static inline int lttng_msgpack_append_u16(
-               struct lttng_msgpack_writer *writer, uint16_t value)
-{
-       value = byteswap_host_to_be16(value);
-
-       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
-}
-
-static inline int lttng_msgpack_append_u32(
-               struct lttng_msgpack_writer *writer, uint32_t value)
-{
-       value = byteswap_host_to_be32(value);
-
-       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
-}
-
-static inline int lttng_msgpack_append_u64(
-               struct lttng_msgpack_writer *writer, uint64_t value)
-{
-       value = byteswap_host_to_be64(value);
-
-       return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
-}
-
-static inline int lttng_msgpack_append_f64(
-               struct lttng_msgpack_writer *writer, double value)
-{
-
-       union {
-               double d;
-               uint64_t u;
-       } u;
-
-       u.d = value;
-
-       return lttng_msgpack_append_u64(writer, u.u);
-}
-
-static inline int lttng_msgpack_append_i8(
-               struct lttng_msgpack_writer *writer, int8_t value)
-{
-       return lttng_msgpack_append_u8(writer, (uint8_t) value);
-}
-
-static inline int lttng_msgpack_append_i16(
-               struct lttng_msgpack_writer *writer, int16_t value)
-{
-       return lttng_msgpack_append_u16(writer, (uint16_t) value);
-}
-
-static inline int lttng_msgpack_append_i32(
-               struct lttng_msgpack_writer *writer, int32_t value)
-{
-       return lttng_msgpack_append_u32(writer, (uint32_t) value);
-}
-
-static inline int lttng_msgpack_append_i64(
-               struct lttng_msgpack_writer *writer, int64_t value)
-{
-       return lttng_msgpack_append_u64(writer, (uint64_t) value);
-}
-
-static inline int lttng_msgpack_encode_f64(
-               struct lttng_msgpack_writer *writer, double value)
-{
-       int ret;
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_FLOAT64_ID);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_f64(writer, value);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_fixmap(
-               struct lttng_msgpack_writer *writer, uint8_t count)
-{
-       int ret = 0;
-
-       lttng_msgpack_assert(count <= MSGPACK_FIXMAP_MAX_COUNT);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXMAP_ID_MASK | count);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_map16(
-               struct lttng_msgpack_writer *writer, uint16_t count)
-{
-       int ret;
-
-       lttng_msgpack_assert(count > MSGPACK_FIXMAP_MAX_COUNT);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_MAP16_ID);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_u16(writer, count);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_fixarray(
-               struct lttng_msgpack_writer *writer, uint8_t count)
-{
-       int ret = 0;
-
-       lttng_msgpack_assert(count <= MSGPACK_FIXARRAY_MAX_COUNT);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXARRAY_ID_MASK | count);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_array16(
-               struct lttng_msgpack_writer *writer, uint16_t count)
-{
-       int ret;
-
-       lttng_msgpack_assert(count > MSGPACK_FIXARRAY_MAX_COUNT);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_ARRAY16_ID);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_u16(writer, count);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_fixstr(
-               struct lttng_msgpack_writer *writer,
-               const char *str,
-               uint8_t len)
-{
-       int ret;
-
-       lttng_msgpack_assert(len <= MSGPACK_FIXSTR_MAX_LENGTH);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXSTR_ID_MASK | len);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-static inline int lttng_msgpack_encode_str16(
-               struct lttng_msgpack_writer *writer,
-               const char *str,
-               uint16_t len)
-{
-       int ret;
-
-       lttng_msgpack_assert(len > MSGPACK_FIXSTR_MAX_LENGTH);
-
-       ret = lttng_msgpack_append_u8(writer, MSGPACK_STR16_ID);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_u16(writer, len);
-       if (ret)
-               goto end;
-
-       ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
-       if (ret)
-               goto end;
-
-end:
-       return ret;
-}
-
-int lttng_msgpack_begin_map(struct lttng_msgpack_writer *writer, size_t count)
-{
-       int ret;
-
-       if (count >= (1 << 16)) {
-               ret = -1;
-               goto end;
-       }
-
-       if (count <= MSGPACK_FIXMAP_MAX_COUNT)
-               ret = lttng_msgpack_encode_fixmap(writer, count);
-       else
-               ret = lttng_msgpack_encode_map16(writer, count);
-
-       writer->map_nesting++;
-end:
-       return ret;
-}
-
-int lttng_msgpack_end_map(struct lttng_msgpack_writer *writer)
-{
-       lttng_msgpack_assert(writer->map_nesting > 0);
-       writer->map_nesting--;
-       return 0;
-}
-
-int lttng_msgpack_begin_array(
-               struct lttng_msgpack_writer *writer, size_t count)
-{
-       int ret;
-
-       if (count >= (1 << 16)) {
-               ret = -1;
-               goto end;
-       }
-
-       if (count <= MSGPACK_FIXARRAY_MAX_COUNT)
-               ret = lttng_msgpack_encode_fixarray(writer, count);
-       else
-               ret = lttng_msgpack_encode_array16(writer, count);
-
-       writer->array_nesting++;
-end:
-       return ret;
-}
-
-int lttng_msgpack_end_array(struct lttng_msgpack_writer *writer)
-{
-       lttng_msgpack_assert(writer->array_nesting > 0);
-       writer->array_nesting--;
-       return 0;
-}
-
-int lttng_msgpack_write_str(struct lttng_msgpack_writer *writer,
-               const char *str)
-{
-       int ret;
-       size_t length = strlen(str);
-
-       if (length >= (1 << 16)) {
-               ret = -1;
-               goto end;
-       }
-
-       if (length <= MSGPACK_FIXSTR_MAX_LENGTH)
-               ret = lttng_msgpack_encode_fixstr(writer, str, length);
-       else
-               ret = lttng_msgpack_encode_str16(writer, str, length);
-
-end:
-       return ret;
-}
-
-int lttng_msgpack_write_nil(struct lttng_msgpack_writer *writer)
-{
-       return lttng_msgpack_append_u8(writer, MSGPACK_NIL_ID);
-}
-
-int lttng_msgpack_write_true(struct lttng_msgpack_writer *writer)
-{
-       return lttng_msgpack_append_u8(writer, MSGPACK_TRUE_ID);
-}
-
-int lttng_msgpack_write_false(struct lttng_msgpack_writer *writer)
-{
-       return lttng_msgpack_append_u8(writer, MSGPACK_FALSE_ID);
-}
-
-int lttng_msgpack_write_unsigned_integer(
-               struct lttng_msgpack_writer *writer, uint64_t value)
-{
-       int ret = 0;
-
-       if (value <= MSGPACK_FIXINT_MAX) {
-               ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
-               if (ret)
-                       goto end;
-       } else if (value <= UINT8_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT8_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
-               if (ret)
-                       goto end;
-       } else if (value <= UINT16_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT16_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_u16(writer, (uint16_t) value);
-               if (ret)
-                       goto end;
-       } else if (value <= UINT32_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT32_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_u32(writer, (uint32_t) value);
-               if (ret)
-                       goto end;
-       } else {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT64_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_u64(writer, value);
-               if (ret)
-                       goto end;
-       }
-
-end:
-       return ret;
-}
-
-int lttng_msgpack_write_signed_integer(struct lttng_msgpack_writer *writer, int64_t value)
-{
-       int ret;
-
-       if (value >= MSGPACK_FIXINT_MIN && value <= MSGPACK_FIXINT_MAX){
-               ret = lttng_msgpack_append_i8(writer, (int8_t) value);
-               if (ret)
-                       goto end;
-       } else if (value >= INT8_MIN && value <= INT8_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT8_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_i8(writer, (int8_t) value);
-               if (ret)
-                       goto end;
-       } else if (value >= INT16_MIN && value <= INT16_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT16_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_i16(writer, (int16_t) value);
-               if (ret)
-                       goto end;
-       } else if (value >= INT32_MIN && value <= INT32_MAX) {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT32_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_i32(writer, (int32_t) value);
-               if (ret)
-                       goto end;
-       } else {
-               ret = lttng_msgpack_append_u8(writer, MSGPACK_INT64_ID);
-               if (ret)
-                       goto end;
-
-               ret = lttng_msgpack_append_i64(writer, value);
-               if (ret)
-                       goto end;
-       }
-
-end:
-       return ret;
-}
-
-int lttng_msgpack_write_double(struct lttng_msgpack_writer *writer, double value)
-{
-       return lttng_msgpack_encode_f64(writer, value);
-}
-
-void lttng_msgpack_writer_init(struct lttng_msgpack_writer *writer,
-               uint8_t *buffer, size_t size)
-{
-       lttng_msgpack_assert(buffer);
-
-       writer->buffer = buffer;
-       writer->write_pos = buffer;
-       writer->end_write_pos = buffer + size;
-
-       writer->array_nesting = 0;
-       writer->map_nesting = 0;
-}
-
-void lttng_msgpack_writer_fini(struct lttng_msgpack_writer *writer)
-{
-       memset(writer, 0, sizeof(*writer));
-}
diff --git a/src/libmsgpack/msgpack.h b/src/libmsgpack/msgpack.h
deleted file mode 100644 (file)
index 140ad38..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
- */
-
-#ifndef _LTTNG_UST_MSGPACK_H
-#define _LTTNG_UST_MSGPACK_H
-
-#include <stddef.h>
-#ifdef __KERNEL__
-#include <linux/types.h>
-#else /* __KERNEL__ */
-#include <stdint.h>
-#endif /* __KERNEL__ */
-
-struct lttng_msgpack_writer {
-       uint8_t *buffer;
-       uint8_t *write_pos;
-       const uint8_t *end_write_pos;
-       uint8_t array_nesting;
-       uint8_t map_nesting;
-};
-
-void lttng_msgpack_writer_init(
-               struct lttng_msgpack_writer *writer,
-               uint8_t *buffer, size_t size)
-       __attribute__((visibility("hidden")));
-
-void lttng_msgpack_writer_fini(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_nil(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_true(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_false(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_unsigned_integer(
-               struct lttng_msgpack_writer *writer, uint64_t value)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_signed_integer(
-               struct lttng_msgpack_writer *writer, int64_t value)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_double(struct lttng_msgpack_writer *writer, double value)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_write_str(struct lttng_msgpack_writer *writer,
-               const char *value)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_begin_map(struct lttng_msgpack_writer *writer, size_t count)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_end_map(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_begin_array(
-               struct lttng_msgpack_writer *writer, size_t count)
-       __attribute__((visibility("hidden")));
-
-int lttng_msgpack_end_array(struct lttng_msgpack_writer *writer)
-       __attribute__((visibility("hidden")));
-
-#endif /* _LTTNG_UST_MSGPACK_H */
index 60afc0ec41d9113c1aad03f6d701b8f0d1e040df..857b62b4493d8e907eaaefb088504e9c2c975d21 100644 (file)
@@ -5,7 +5,7 @@ AM_CPPFLAGS += -I$(top_srcdir)/tests/utils
 noinst_PROGRAMS = test_msgpack
 test_msgpack_SOURCES = test_msgpack.c
 test_msgpack_LDADD = \
-       $(top_builddir)/src/libmsgpack/libmsgpack.la \
+       $(top_builddir)/src/common/msgpack/libmsgpack.la \
        $(top_builddir)/tests/utils/libtap.a
 
 test_msgpack_CFLAGS = $(AM_CFLAGS)
index 2e1828d4558110d02f97a9015323bf147be13990..339e20beb009408c8a14e5a4301acf76bb5b1fa5 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "tap.h"
 
-#include "../../src/libmsgpack/msgpack.h"
+#include "common/msgpack/msgpack.h"
 
 #define BUFFER_SIZE 4096
 #define NUM_TESTS 23
This page took 0.04053 seconds and 4 git commands to generate.