From patchwork Mon Jan 20 17:38:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2715 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 C04EF6081F for ; Mon, 20 Jan 2020 18:38:21 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 62C5410D6 for ; Mon, 20 Jan 2020 18:38:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579541901; bh=WWLXvwTHQaiX6BmRPdeYbn5mDIBJ2XbviXSOEyuI/gw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=okGHnI15CtEfz0NWpH9DR4e/O5ZqWXkyMqnLLS3Ay3ykG3Rvkx5sp/K63CUAfbZOt xugc9cCWFsesCkZhX3hNJHs/q4EJdSpAY61okj3pegtzBpY8FT5DNgSzH0OfWasVvJ iOrLxO0/Ixy55qnqALUI5JeH7KFCD9o5K7uawOu8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jan 2020 19:38:16 +0200 Message-Id: <20200120173816.31829-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200120173816.31829-1-laurent.pinchart@ideasonboard.com> References: <20200120173816.31829-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/3] libcamera: Replace ARRAY_SIZE with std::array X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Jan 2020 17:38:21 -0000 Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro is used, removing the need for the macro completely. std::array combines the performance and accessibility of C-style arrays with the benefits of a standard container, which is shown here through the ability to carry its size. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/libcamera/camera.cpp | 13 +++++++------ src/libcamera/include/utils.h | 2 -- src/libcamera/ipa_module.cpp | 11 ++++++----- src/libcamera/log.cpp | 23 ++++++++++++----------- src/libcamera/utils.cpp | 5 ----- src/libcamera/v4l2_videodevice.cpp | 7 ++++--- test/ipc/unixsocket.cpp | 8 ++++---- 7 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 79a5f994f9bb..769f16c9ceab 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -404,20 +405,20 @@ Camera::~Camera() LOG(Camera, Error) << "Removing camera while still in use"; } -static const char *const camera_state_names[] = { +static constexpr auto camera_state_names = utils::make_array( "Available", "Acquired", "Configured", - "Running", -}; + "Running" +); bool Camera::stateBetween(State low, State high) const { if (state_ >= low && state_ <= high) return true; - ASSERT(static_cast(low) < ARRAY_SIZE(camera_state_names) && - static_cast(high) < ARRAY_SIZE(camera_state_names)); + ASSERT(static_cast(low) < camera_state_names.size() && + static_cast(high) < camera_state_names.size()); LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] << " state trying operation requiring state between " @@ -432,7 +433,7 @@ bool Camera::stateIs(State state) const if (state_ == state) return true; - ASSERT(static_cast(state) < ARRAY_SIZE(camera_state_names)); + ASSERT(static_cast(state) < camera_state_names.size()); LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] << " state trying operation requiring state " diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 6e9b9259456a..9057f8b0de84 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -18,8 +18,6 @@ #include #include -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - #ifndef __DOXYGEN__ /* uClibc and uClibc-ng don't provide O_TMPFILE */ diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 2c355ea8b5e5..6d9ff77d5bd4 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -480,7 +480,7 @@ bool IPAModule::match(PipelineHandler *pipe, */ bool IPAModule::isOpenSource() const { - static const char *osLicenses[] = { + static constexpr auto osLicenses = utils::make_array( "GPL-2.0-only", "GPL-2.0-or-later", "GPL-3.0-only", @@ -488,12 +488,13 @@ bool IPAModule::isOpenSource() const "LGPL-2.1-only", "LGPL-2.1-or-later", "LGPL-3.0-only", - "LGPL-3.0-or-later", - }; + "LGPL-3.0-or-later" + ); - for (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++) - if (!strcmp(osLicenses[i], info_.license)) + for (const char *license : osLicenses) { + if (!strcmp(license, info_.license)) return true; + } return false; } diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index 1c82dc68fb0f..fec48c9e7175 100644 --- a/src/libcamera/log.cpp +++ b/src/libcamera/log.cpp @@ -7,6 +7,7 @@ #include "log.h" +#include #if HAVE_BACKTRACE #include #endif @@ -83,15 +84,15 @@ static int log_severity_to_syslog(LogSeverity severity) static const char *log_severity_name(LogSeverity severity) { - static const char *const names[] = { + static constexpr auto names = utils::make_array( " DBG", " INFO", " WARN", " ERR", - "FATAL", - }; + "FATAL" + ); - if (static_cast(severity) < ARRAY_SIZE(names)) + if (static_cast(severity) < names.size()) return names[severity]; else return "UNKWN"; @@ -405,9 +406,9 @@ void Logger::backtrace() if (!output) return; - void *buffer[32]; - int num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer)); - char **strings = backtrace_symbols(buffer, num_entries); + std::array buffer; + int num_entries = ::backtrace(buffer.data(), buffer.size()); + char **strings = backtrace_symbols(buffer.data(), num_entries); if (!strings) return; @@ -603,13 +604,13 @@ void Logger::parseLogLevels() */ LogSeverity Logger::parseLogLevel(const std::string &level) { - static const char *const names[] = { + static constexpr auto names = utils::make_array( "DEBUG", "INFO", "WARN", "ERROR", - "FATAL", - }; + "FATAL" + ); int severity; @@ -620,7 +621,7 @@ LogSeverity Logger::parseLogLevel(const std::string &level) severity = LogInvalid; } else { severity = LogInvalid; - for (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) { + for (unsigned int i = 0; i < names.size(); ++i) { if (names[i] == level) { severity = i; break; diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index b639cfa83d0c..dfd140ca464a 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -22,11 +22,6 @@ namespace libcamera { namespace utils { -/** - * \def ARRAY_SIZE(array) - * \brief Determine the number of elements in the static array. - */ - /** * \brief Strip the directory prefix from the path * \param[in] path The path to process diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 82267730289d..234d8123c7b4 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -7,6 +7,7 @@ #include "v4l2_videodevice.h" +#include #include #include #include @@ -992,13 +993,13 @@ int V4L2VideoDevice::exportBuffers(unsigned int count, for (unsigned i = 0; i < count; ++i) { struct v4l2_buffer buf = {}; - struct v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + std::array planes = {}; buf.index = i; buf.type = bufferType_; buf.memory = memoryType_; - buf.length = ARRAY_SIZE(planes); - buf.m.planes = planes; + buf.length = planes.size(); + buf.m.planes = planes.data(); ret = ioctl(VIDIOC_QUERYBUF, &buf); if (ret < 0) { diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp index f53042b88720..5bf543c197fa 100644 --- a/test/ipc/unixsocket.cpp +++ b/test/ipc/unixsocket.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -21,7 +22,6 @@ #include "ipc_unixsocket.h" #include "test.h" #include "thread.h" -#include "utils.h" #define CMD_CLOSE 0 #define CMD_REVERSE 1 @@ -303,13 +303,13 @@ protected: IPCUnixSocket::Payload message, response; int ret; - static const char *strings[2] = { + static constexpr std::array strings = { "Foo", "Bar", }; int fds[2]; - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { + for (unsigned int i = 0; i < strings.size(); i++) { unsigned int len = strlen(strings[i]); fds[i] = open("/tmp", O_TMPFILE | O_RDWR, @@ -331,7 +331,7 @@ protected: if (ret) return ret; - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { + for (unsigned int i = 0; i < strings.size(); i++) { unsigned int len = strlen(strings[i]); char buf[len];