Backport: Port dynamic buffer to stable 2.9
authorMichael Jeanson <mjeanson@efficios.com>
Fri, 27 Apr 2018 21:27:29 +0000 (17:27 -0400)
committerJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Fri, 21 Sep 2018 04:00:52 +0000 (00:00 -0400)
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
12 files changed:
configure.ac
include/Makefile.am
include/version.h [new file with mode: 0644]
include/version.h.tmpl [deleted file]
src/common/Makefile.am
src/common/buffer-view.c [new file with mode: 0644]
src/common/buffer-view.h [new file with mode: 0644]
src/common/dynamic-buffer.c [new file with mode: 0644]
src/common/dynamic-buffer.h [new file with mode: 0644]
src/common/sessiond-comm/sessiond-comm.h
src/common/utils.c
src/common/utils.h

index 21f51c33a84ccd17485f99fd2567878162cb690c..f035e24e4730a165cfcc48f096f0392baa159d2d 100644 (file)
@@ -522,15 +522,6 @@ AC_CHECK_LIB([pfm], [pfm_initialize],
              ])
 AM_CONDITIONAL([LTTNG_TOOLS_BUILD_WITH_LIBPFM], [test "x$have_libpfm" = "xyes"])
 
-AC_ARG_ENABLE([git-version],
-              [AC_HELP_STRING([--disable-git-version],
-                              [Do not use the git version for the build])],
-              [have_git_version=$enableval], [have_git_version=yes]
-)
-
-AM_CONDITIONAL([LTTNG_TOOLS_BUILD_GIT_SOURCE],
-       [test "x${have_git_version}" = "xyes"])
-
 # For Python
 # SWIG version needed or newer:
 swig_version=2.0.0
index 3f07f2593df79b14cb37b97f87ed55f160e651f6..84c442292bc29ce64557f099a2d304c40392b564 100644 (file)
@@ -1,68 +1,71 @@
-if LTTNG_TOOLS_BUILD_GIT_SOURCE
-GIT_DESCRIBE_CMD = (cd $(top_srcdir); git describe)
-else
-GIT_DESCRIBE_CMD = /bin/true
-endif
-
 ##
-## The version.h file must be verified and generated or updated if the
-## git commit id (called git version here) changed since the last build
-## of lttng-tools.
+## This target generates an include file that contains the git version
+## string of the current branch, it must be continuously updated when
+## we build in the git repo and shipped in dist tarballs to reflect the
+## status of the tree when it was generated. If the tree is clean and
+## the current commit is tag a starting with "v", consider this a
+## release version and set an empty git version.
 ##
-version.h:
-       ##
-       ## We first create variables for the current git version and
-       ## the locations of the version.h and version.h.tmpl files.
-       ##
-       @echo $(ECHO_N) "Generating version.h... $(ECHO_C)"
-       @(version_h_tmpl="$(top_srcdir)/include/version.h.tmpl"; \
-       if [ -f "$${version_h_tmpl}" ]; then \
-               version_h="$(top_builddir)/include/version.h"; \
-               ##
-               ## Check whether we are in a git repo.
-               ##
-               git_describe="`$(GIT_DESCRIBE_CMD) 2>/dev/null`"; \
-               if [ $$? -eq 0 ]; then \
-                       git_version="$${git_describe}"; \
-               else \
-                       git_version=""; \
-               fi; \
-               ##
-               ## If the version.h file doesn't exist or is not up to date,
-               ## We replace it by the version.h.tmpl file.
-               ##
-               if [ ! -e "$${version_h}" ] || \
-                       [ "$${version_h_tmpl}" -nt "$${version_h}" ]; then \
-                       cp "$${version_h_tmpl}" "$${version_h}"; \
+## Here is what the inline script does:
+##
+## First, delete any stale "version.i.tmp" file.
+##
+## If "bootstrap" and ".git" exists in the top source directory and the git
+## executable is available, get the current git version string in the form:
+##
+##  "latest_tag"(-"number_of_commits_on_top")(-g"latest_commit_hash")(-dirty)
+##
+## And store it in "version.i.tmp", if the current commit is tagged, the tag
+## starts with "v" and the tree is clean, consider this a release version and
+## overwrite the git version with an empty string in "version.i.tmp".
+##
+## If we don't have a "version.i.tmp" nor a "version.i", generate an empty
+## string as a failover.
+##
+## If we don't have a "version.i" or we have both files and they are different,
+## copy "version.i.tmp" over "version.i". This way the dependent targets are
+## only rebuilt when the version string changes.
+##
+version_verbose = $(version_verbose_@AM_V@)
+version_verbose_ = $(version_verbose_@AM_DEFAULT_V@)
+version_verbose_0 = @echo "  GEN       " $@;
+
+version.i:
+       $(version_verbose)rm -f version.i.tmp; \
+       if (test -r "$(top_srcdir)/bootstrap" && test -r "$(top_srcdir)/.git") && \
+                       test -x "`which git 2>&1;true`"; then \
+               GIT_VERSION_STR="`cd "$(top_srcdir)" && git describe --tags --dirty`"; \
+               GIT_CURRENT_TAG="`cd "$(top_srcdir)" && git describe --tags --exact-match --match="v[0-9]*" HEAD 2> /dev/null`"; \
+               echo "#define GIT_VERSION \"$$GIT_VERSION_STR\"" > version.i.tmp; \
+               if ! $(GREP) -- "-dirty" version.i.tmp > /dev/null && \
+                               test "x$$GIT_CURRENT_TAG" != "x"; then \
+                       echo "#define GIT_VERSION \"\"" > version.i.tmp; \
                fi; \
-               echo $(ECHO_N) "git version: \"$${git_version}\"$(ECHO_C)"; \
-               ##
-               ## We verify that git_version isn't the same as the one
-               ## currently in the file (if there is one), as we don't
-               ## want to update the file if it is already up to date.
-               ##
-               version_match='^#define GIT_VERSION.*'; \
-               old_version=$$($(GREP) "$${version_match}" "$${version_h}"); \
-               new_version="#define GIT_VERSION        \"$${git_version}\""; \
-               if [ x"$${old_version}" != x"$${new_version}" ]; then \
-                       $(SED) -i -e "s'$${version_match}'$${new_version}'" "$${version_h}"; \
-               else \
-                       echo $(ECHO_N) " (cached)$(ECHO_C)"; \
+       fi; \
+       if test ! -f version.i.tmp; then \
+               if test ! -f version.i; then \
+                       echo '#define GIT_VERSION ""' > version.i; \
                fi; \
-               echo $(ECHO_N) "... $(ECHO_C)"; \
-       fi)
-       @echo "$(ECHO_T)ok"
+       elif test ! -f version.i || \
+                       test x"`cat version.i.tmp`" != x"`cat version.i`"; then \
+               mv version.i.tmp version.i; \
+       fi; \
+       rm -f version.i.tmp; \
+       true
 
 ##
-## version.h is defined as a .PHONY file even if it's a real file as
-## we want our routine to be ran for each build.
+## version.i is defined as a .PHONY target even if it's a real file,
+## we want the target to be re-run on every make.
 ##
-.PHONY: version.h
+.PHONY: version.i
 
-CLEANFILES = version.h
+CLEANFILES = version.i.tmp
 
-nodist_noinst_HEADERS = \
-       version.h
+##
+## Only clean "version.i" on dist-clean, we need to keep it on regular
+## clean when it's part of a dist tarball.
+##
+DISTCLEANFILES = version.i
 
 lttnginclude_HEADERS = \
        lttng/health.h \
@@ -76,11 +79,12 @@ lttnginclude_HEADERS = \
        lttng/lttng-error.h \
        lttng/snapshot.h \
        lttng/save.h \
-       lttng/load.h \
-       version.h.tmpl
+       lttng/load.h
 
 noinst_HEADERS = \
        lttng/snapshot-internal.h \
        lttng/health-internal.h \
        lttng/save-internal.h \
-       lttng/load-internal.h
+       lttng/load-internal.h \
+       version.h \
+       version.i
diff --git a/include/version.h b/include/version.h
new file mode 100644 (file)
index 0000000..0f3f7c8
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef VERSION_H
+#define VERSION_H
+
+/*
+ * Copyright (C) 2013-2014 - Raphaël Beamonte <raphael.beamonte@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "version.i"
+
+#endif /* VERSION_H */
diff --git a/include/version.h.tmpl b/include/version.h.tmpl
deleted file mode 100644 (file)
index 133a90a..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef VERSION_H
-#define VERSION_H
-
-/*
- * Copyright (C) 2013-2014 - Raphaël Beamonte <raphael.beamonte@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#define GIT_VERSION    ""
-
-#endif /* VERSION_H */
index 8619b5e25f31b627db462a128286ceb0a7872bd9..7cbc11cb2b5161882b824611f333ffaf21bd9059 100644 (file)
@@ -72,6 +72,8 @@ libcommon_la_SOURCES = error.h error.c utils.c utils.h runas.c runas.h \
                        pipe.c pipe.h readwrite.c readwrite.h \
                        mi-lttng.h mi-lttng.c \
                        daemonize.c daemonize.h \
+                       dynamic-buffer.h dynamic-buffer.c \
+                       buffer-view.h buffer-view.c \
                        unix.c unix.h \
                        filter.c filter.h context.c context.h
 
diff --git a/src/common/buffer-view.c b/src/common/buffer-view.c
new file mode 100644 (file)
index 0000000..4e84b03
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License, version 2.1 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <common/buffer-view.h>
+#include <common/dynamic-buffer.h>
+#include <common/error.h>
+#include <assert.h>
+
+LTTNG_HIDDEN
+struct lttng_buffer_view lttng_buffer_view_from_view(
+               const struct lttng_buffer_view *src, size_t offset,
+               ptrdiff_t len)
+{
+       struct lttng_buffer_view view = { .data = NULL, .size = 0 };
+
+       assert(src);
+
+       if (offset > src->size) {
+               ERR("Attempt to create buffer view with invalid offset");
+               goto end;
+       }
+
+       if (len != -1 && len > (src->size - offset)) {
+               ERR("Attempt to create buffer view with invalid length");
+               goto end;
+       }
+
+       view.data = src->data + offset;
+       view.size = len == -1 ? (src->size - offset) : len;
+end:
+       return view;
+}
+
+LTTNG_HIDDEN
+struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer(
+               const struct lttng_dynamic_buffer *src, size_t offset,
+               ptrdiff_t len)
+{
+       struct lttng_buffer_view view = { .data = NULL, .size = 0 };
+
+       assert(src);
+
+       if (offset > src->size) {
+               ERR("Attempt to create buffer view with invalid offset");
+               goto end;
+       }
+
+       if (len != -1 && len > (src->size - offset)) {
+               ERR("Attempt to create buffer view with invalid length");
+               goto end;
+       }
+
+       view.data = src->data + offset;
+       view.size = len == -1 ? (src->size - offset) : len;
+end:
+       return view;
+}
diff --git a/src/common/buffer-view.h b/src/common/buffer-view.h
new file mode 100644 (file)
index 0000000..d2b18f6
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License, version 2.1 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef LTTNG_BUFFER_VIEW_H
+#define LTTNG_BUFFER_VIEW_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <common/macros.h>
+
+struct lttng_dynamic_buffer;
+
+struct lttng_buffer_view {
+       const char *data;
+       size_t size;
+};
+
+/**
+ * Return a buffer view referencing a subset of the memory referenced by another
+ * view.
+ *
+ * @src                Source view to reference
+ * @offset     Offset to apply to the source memory content
+ * @len                Length of the memory contents to reference. Passing -1 will
+ *             cause the view to reference the whole view from the offset
+ *             provided.
+ *
+ * Note that a buffer view never assumes the ownership of the memory it
+ * references.
+ */
+LTTNG_HIDDEN
+struct lttng_buffer_view lttng_buffer_view_from_view(
+               const struct lttng_buffer_view *src, size_t offset,
+               ptrdiff_t len);
+
+/**
+ * Return a buffer view referencing a subset of the memory referenced by a
+ * dynamic buffer.
+ *
+ * @src                Source dynamic buffer to reference
+ * @offset     Offset to apply to the source memory content
+ * @len                Length of the memory contents to reference. Passing -1 will
+ *             cause the view to reference the whole dynamic buffer from the
+ *             offset provided.
+ *
+ * Note that a buffer view never assumes the ownership of the memory it
+ * references.
+ */
+LTTNG_HIDDEN
+struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer(
+               const struct lttng_dynamic_buffer *src, size_t offset,
+               ptrdiff_t len);
+
+#endif /* LTTNG_BUFFER_VIEW_H */
diff --git a/src/common/dynamic-buffer.c b/src/common/dynamic-buffer.c
new file mode 100644 (file)
index 0000000..fd39f81
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License, version 2.1 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <common/dynamic-buffer.h>
+#include <common/utils.h>
+#include <assert.h>
+
+/*
+ * Round to (upper) power of two, val is returned if it already is a power of
+ * two.
+ */
+static
+size_t round_to_power_of_2(size_t val)
+{
+       int order;
+       size_t rounded;
+
+       order = utils_get_count_order_u64(val);
+       assert(order >= 0);
+       rounded = (1ULL << order);
+       assert(rounded >= val);
+
+       return rounded;
+}
+
+LTTNG_HIDDEN
+void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer)
+{
+       assert(buffer);
+       memset(buffer, 0, sizeof(*buffer));
+}
+
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer,
+               const void *buf, size_t len)
+{
+       int ret = 0;
+
+       if (!buffer || (!buf && len)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (len == 0) {
+               /* Not an error, no-op. */
+               goto end;
+       }
+
+       assert(buffer->_capacity >= buffer->size);
+       if (buffer->_capacity < (len + buffer->size)) {
+               ret = lttng_dynamic_buffer_set_capacity(buffer,
+                               buffer->_capacity +
+                               (len - (buffer->_capacity - buffer->size)));
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       memcpy(buffer->data + buffer->size, buf, len);
+       buffer->size += len;
+end:
+       return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
+               struct lttng_dynamic_buffer *src_buffer)
+{
+       int ret;
+
+       if (!dst_buffer || !src_buffer) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data,
+                       src_buffer->size);
+end:
+       return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
+               size_t new_size)
+{
+       int ret = 0;
+
+       if (!buffer) {
+               goto end;
+       }
+
+       if (new_size == buffer->size) {
+               goto end;
+       }
+
+       if (new_size > buffer->_capacity) {
+               size_t original_size = buffer->size;
+               size_t original_capacity = buffer->_capacity;
+
+               ret = lttng_dynamic_buffer_set_capacity(buffer, new_size);
+               if (ret) {
+                       goto end;
+               }
+
+               /*
+                * Zero-initialize the space that was left in the buffer at the
+                * before we increased its capacity (original capacity - original size).
+                * The newly acquired capacity (new capacity - original capacity)
+                * is zeroed by lttng_dynamic_buffer_set_capacity().
+                */
+               memset(buffer->data + original_size, 0,
+                               original_capacity - original_size);
+       } else if (new_size > buffer->size) {
+               memset(buffer->data + buffer->size, 0, new_size - buffer->size);
+       } else {
+               /*
+                * Shrinking size. There is no need to zero-out the newly
+                * released memory as it will either be:
+                *   - overwritten by lttng_dynamic_buffer_append,
+                *   - expanded later, which will zero-out the memory
+                *
+                * Users of external APIs are encouraged to set the buffer's
+                * size _before_ making such calls.
+                */
+       }
+       buffer->size = new_size;
+end:
+       return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
+               size_t demanded_capacity)
+{
+       int ret = 0;
+       void *new_buf;
+       size_t new_capacity = round_to_power_of_2(demanded_capacity);
+
+       if (!buffer || demanded_capacity < buffer->size) {
+               /*
+                * Shrinking a buffer's size by changing its capacity is
+                * unsupported.
+                */
+               ret = -1;
+               goto end;
+       }
+
+       if (new_capacity == buffer->_capacity) {
+               goto end;
+       }
+
+       /* Memory is initialized by the size increases. */
+       new_buf = realloc(buffer->data, new_capacity);
+       if (!new_buf) {
+               ret = -1;
+               goto end;
+       }
+       buffer->data = new_buf;
+       buffer->_capacity = new_capacity;
+end:
+       return ret;
+}
+
+/* Release any memory used by the dynamic buffer. */
+LTTNG_HIDDEN
+void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
+{
+       if (!buffer) {
+               return;
+       }
+       buffer->size = 0;
+       buffer->_capacity = 0;
+       free(buffer->data);
+}
diff --git a/src/common/dynamic-buffer.h b/src/common/dynamic-buffer.h
new file mode 100644 (file)
index 0000000..d42a6a6
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License, version 2.1 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef LTTNG_DYNAMIC_BUFFER_H
+#define LTTNG_DYNAMIC_BUFFER_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <common/macros.h>
+
+struct lttng_dynamic_buffer {
+       char *data;
+       /* size is the buffer's currently used capacity. */
+       size_t size;
+       /*
+        * capacity shall not be accessed by users directly, it is meant for
+        * internal use only.
+        */
+       size_t _capacity;
+};
+
+/*
+ * Initialize a dynamic buffer. This performs no allocation and is meant
+ * to be used instead of memset or explicit initialization of the buffer.
+ */
+LTTNG_HIDDEN
+void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer);
+
+/*
+ * Append the content of a raw memory buffer to the end of a dynamic buffer
+ * (after its current "size"). The dynamic buffer's size is increased by
+ * "len", and its capacity is adjusted automatically.
+ */
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer,
+               const void *buf, size_t len);
+
+/*
+ * Performs the same action as lttng_dynamic_buffer_append(), but using another
+ * dynamic buffer as the source buffer. The source buffer's size is used in lieu
+ * of "len".
+ */
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
+               struct lttng_dynamic_buffer *src_buffer);
+
+/*
+ * Set the buffer's size to new_size. The capacity of the buffer will
+ * be expanded (if necessary) to accomodate new_size. Areas acquired by
+ * a size increase will be zeroed.
+ *
+ * Be careful to expand the buffer's size _before_ calling out external
+ * APIs (e.g. read(3)) which may populate the buffer as setting the size
+ * after will zero-out the result of the operation.
+ *
+ * Shrinking a buffer does not zero the old content. If the buffer may contain
+ * sensititve information, it must be cleared manually _before_ changing the
+ * size.
+ *
+ * NOTE: It is striclty _invalid_ to access memory after _size_, regardless
+ *       of prior calls to set_capacity().
+ */
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
+               size_t new_size);
+
+/*
+ * Set the buffer's capacity to accomodate the new_capacity, allocating memory
+ * as necessary. The buffer's content is preserved. Setting a buffer's capacity
+ * is meant as a _hint_ to the underlying buffer and is only optimization; no
+ * guarantee is offered that subsequent calls to append or set_size will succeed.
+ *
+ * If the current size > new_capacity, the operation will fail.
+ */
+LTTNG_HIDDEN
+int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
+               size_t new_capacity);
+
+/* Release any memory used by the dynamic buffer. */
+LTTNG_HIDDEN
+void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer);
+
+#endif /* LTTNG_DYNAMIC_BUFFER_H */
index 0c2667031b55bd5603adaca04b09bf5fcab808c1..0fba38642165c8b94920d4424cac5dc59b514d55 100644 (file)
@@ -37,6 +37,7 @@
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <sys/un.h>
+#include <stdbool.h>
 
 #include "inet.h"
 #include "inet6.h"
index 24c3b8ca146ff1fafe8de8da3dd059c7f947f415..25d510854f972438085c98a8b6f41fd06001bf47 100644 (file)
@@ -1017,6 +1017,59 @@ static inline unsigned int fls_u32(uint32_t x)
 #define HAS_FLS_U32
 #endif
 
+#if defined(__x86_64)
+static inline
+unsigned int fls_u64(uint64_t x)
+{
+       long r;
+
+       asm("bsrq %1,%0\n\t"
+           "jnz 1f\n\t"
+           "movq $-1,%0\n\t"
+           "1:\n\t"
+           : "=r" (r) : "rm" (x));
+       return r + 1;
+}
+#define HAS_FLS_U64
+#endif
+
+#ifndef HAS_FLS_U64
+static __attribute__((unused))
+unsigned int fls_u64(uint64_t x)
+{
+       unsigned int r = 64;
+
+       if (!x)
+               return 0;
+
+       if (!(x & 0xFFFFFFFF00000000ULL)) {
+               x <<= 32;
+               r -= 32;
+       }
+       if (!(x & 0xFFFF000000000000ULL)) {
+               x <<= 16;
+               r -= 16;
+       }
+       if (!(x & 0xFF00000000000000ULL)) {
+               x <<= 8;
+               r -= 8;
+       }
+       if (!(x & 0xF000000000000000ULL)) {
+               x <<= 4;
+               r -= 4;
+       }
+       if (!(x & 0xC000000000000000ULL)) {
+               x <<= 2;
+               r -= 2;
+       }
+       if (!(x & 0x8000000000000000ULL)) {
+               x <<= 1;
+               r -= 1;
+       }
+       return r;
+}
+#endif
+
 #ifndef HAS_FLS_U32
 static __attribute__((unused)) unsigned int fls_u32(uint32_t x)
 {
@@ -1063,6 +1116,20 @@ int utils_get_count_order_u32(uint32_t x)
        return fls_u32(x - 1);
 }
 
+/*
+ * Return the minimum order for which x <= (1UL << order).
+ * Return -1 if x is 0.
+ */
+LTTNG_HIDDEN
+int utils_get_count_order_u64(uint64_t x)
+{
+       if (!x) {
+               return -1;
+       }
+
+       return fls_u64(x - 1);
+}
+
 /**
  * Obtain the value of LTTNG_HOME environment variable, if exists.
  * Otherwise returns the value of HOME.
index 7285f5c3032932ec03531ddb7ef7973f403fbc6b..0daf5b98df70d7add96b6a7fda2fac53e85ad681 100644 (file)
@@ -48,6 +48,7 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
                int *stream_fd);
 int utils_parse_size_suffix(char const * const str, uint64_t * const size);
 int utils_get_count_order_u32(uint32_t x);
+int utils_get_count_order_u64(uint64_t x);
 char *utils_get_home_dir(void);
 char *utils_get_user_home_dir(uid_t uid);
 char *utils_get_kmod_probes_list(void);
This page took 0.038059 seconds and 5 git commands to generate.