From patchwork Wed Jan 23 08:28:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 343 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C6F9A60B1B for ; Wed, 23 Jan 2019 09:28:34 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 36C7923A for ; Wed, 23 Jan 2019 09:28:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548232114; bh=WLcLTZS08IfFQZrFk4yWNn4aAHXSPVnPQxcQuTuBqJg=; h=From:To:Subject:Date:From; b=qrV35qMSK2JlGb8tN5/5+b15xofoJtzbuL3hzhApW0Pt36dkc8X4yriN7hqba1LYr XJt9sGyMw//gwzHyGHTfaz+H3WfTGP8e7485xXVkCA40eXFDw+QFKe7hIO80hEqOe2 OoWqniARBUoKjF8HWm5h1pmH6aUuMjgOIbyA73c8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 23 Jan 2019 10:28:30 +0200 Message-Id: <20190123082830.10572-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: utils: Add arithmetic operators for struct timespec X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jan 2019 08:28:35 -0000 Implement additions and subtraction of struct timespec through non-member operators to simplify timespec handling. Signed-off-by: Laurent Pinchart --- I wrote this code while working on EINTR handling for the event dispatcher, and later refactored the implementation in a way that doesn't make use of these operators anymore. I thought they could still be useful as reference for later use. This patch is thus not meant to be merged now, but can be picked up later if anyone needs it. src/libcamera/include/utils.h | 6 +++++ src/libcamera/meson.build | 1 + src/libcamera/utils.cpp | 46 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/libcamera/utils.cpp diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 73fa2e69b029..7df20976f36f 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -8,6 +8,7 @@ #define __LIBCAMERA_UTILS_H__ #include +#include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -24,6 +25,11 @@ std::unique_ptr make_unique(Args&&... args) } /* namespace utils */ +struct timespec &operator+=(struct timespec &lhs, const struct timespec &rhs); +struct timespec operator+(struct timespec lhs, const struct timespec &rhs); +struct timespec &operator-=(struct timespec &lhs, const struct timespec &rhs); +struct timespec operator-(struct timespec lhs, const struct timespec &rhs); + } /* namespace libcamera */ #endif /* __LIBCAMERA_UTILS_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index f9f25c0ecf15..72d4a194e19a 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -11,6 +11,7 @@ libcamera_sources = files([ 'pipeline_handler.cpp', 'signal.cpp', 'timer.cpp', + 'utils.cpp', 'v4l2_device.cpp', ]) diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp new file mode 100644 index 000000000000..5014b27d8c7d --- /dev/null +++ b/src/libcamera/utils.cpp @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * utils.cpp - Miscellaneous utility functions + */ + +#include "utils.h" + +namespace libcamera { + +struct timespec &operator+=(struct timespec &lhs, const struct timespec &rhs) +{ + lhs.tv_sec += rhs.tv_sec; + lhs.tv_nsec += rhs.tv_nsec; + if (lhs.tv_nsec >= 1000000000) { + lhs.tv_sec++; + lhs.tv_nsec -= 1000000000; + } + + return lhs; +} + +struct timespec operator+(struct timespec lhs, const struct timespec &rhs) +{ + return lhs += rhs; +} + +struct timespec &operator-=(struct timespec &lhs, const struct timespec &rhs) +{ + lhs.tv_sec -= rhs.tv_sec; + lhs.tv_nsec -= rhs.tv_nsec; + if (lhs.tv_nsec < 0) { + lhs.tv_sec--; + lhs.tv_nsec += 1000000000; + } + + return lhs; +} + +struct timespec operator-(struct timespec lhs, const struct timespec &rhs) +{ + return lhs - rhs; +} + +} /* namespace libcamera */