{"id":2692,"url":"https://patchwork.libcamera.org/api/patches/2692/?format=json","web_url":"https://patchwork.libcamera.org/patch/2692/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200120002437.6633-6-laurent.pinchart@ideasonboard.com>","date":"2020-01-20T00:24:23","name":"[libcamera-devel,05/19] libcamera: Replace ARRAY_SIZE with std::array","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"350f434f635fd5de5459dc5fa0f5ed6d2eaa9b1c","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2692/mbox/","series":[{"id":641,"url":"https://patchwork.libcamera.org/api/series/641/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=641","date":"2020-01-20T00:24:19","name":"Initial libcamera threading model","version":1,"mbox":"https://patchwork.libcamera.org/series/641/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2692/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2692/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 829DE607E2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 01:24:44 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BB29B504\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 01:24:43 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579479883;\n\tbh=siEXXoSdcqSOVH1xn3Cc+kKBHDs2MhxzxQOo7xyszdM=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=izEhKFB0YKlbKgWeeorg00jeTkJQ3SXZXcQlNgKJ9L2ERZyHRgVOrWnDqZ1S4NI3C\n\tV7O09+bgdqxCsbKHFWNfjlV95DtygR55EVA7ezjngFGly49dtUTrlc2e6OT5yxJNoA\n\tVeFRbmLhFY/XMGROUTdBzRZy5/u0YD7EDsNteFj8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Jan 2020 02:24:23 +0200","Message-Id":"<20200120002437.6633-6-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200120002437.6633-1-laurent.pinchart@ideasonboard.com>","References":"<20200120002437.6633-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 05/19] libcamera: Replace ARRAY_SIZE with\n\tstd::array","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 20 Jan 2020 00:24:46 -0000"},"content":"Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro\nis used, removing the need for the macro completely. std::array combines\nthe performance and accessibility of C-style arrays with the benefits of\na standard container, which is shown here through the ability to carry\nits size.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/camera.cpp           | 10 +++++-----\n src/libcamera/include/utils.h      |  2 --\n src/libcamera/ipa_module.cpp       |  8 ++++----\n src/libcamera/log.cpp              | 15 ++++++++-------\n src/libcamera/utils.cpp            |  5 -----\n src/libcamera/v4l2_videodevice.cpp |  7 ++++---\n test/ipc/unixsocket.cpp            |  8 ++++----\n 7 files changed, 25 insertions(+), 30 deletions(-)","diff":"diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 79a5f994f9bb..3385c08778b8 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -7,6 +7,7 @@\n \n #include <libcamera/camera.h>\n \n+#include <array>\n #include <iomanip>\n \n #include <libcamera/framebuffer_allocator.h>\n@@ -15,7 +16,6 @@\n \n #include \"log.h\"\n #include \"pipeline_handler.h\"\n-#include \"utils.h\"\n \n /**\n  * \\file camera.h\n@@ -404,7 +404,7 @@ Camera::~Camera()\n \t\tLOG(Camera, Error) << \"Removing camera while still in use\";\n }\n \n-static const char *const camera_state_names[] = {\n+static constexpr std::array<const char *, 4> camera_state_names = {\n \t\"Available\",\n \t\"Acquired\",\n \t\"Configured\",\n@@ -416,8 +416,8 @@ bool Camera::stateBetween(State low, State high) const\n \tif (state_ >= low && state_ <= high)\n \t\treturn true;\n \n-\tASSERT(static_cast<unsigned int>(low) < ARRAY_SIZE(camera_state_names) &&\n-\t       static_cast<unsigned int>(high) < ARRAY_SIZE(camera_state_names));\n+\tASSERT(static_cast<unsigned int>(low) < camera_state_names.size() &&\n+\t       static_cast<unsigned int>(high) < camera_state_names.size());\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[state_]\n \t\t\t   << \" state trying operation requiring state between \"\n@@ -432,7 +432,7 @@ bool Camera::stateIs(State state) const\n \tif (state_ == state)\n \t\treturn true;\n \n-\tASSERT(static_cast<unsigned int>(state) < ARRAY_SIZE(camera_state_names));\n+\tASSERT(static_cast<unsigned int>(state) < camera_state_names.size());\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[state_]\n \t\t\t   << \" state trying operation requiring state \"\ndiff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\nindex e467eb21c518..f55c52f72bd5 100644\n--- a/src/libcamera/include/utils.h\n+++ b/src/libcamera/include/utils.h\n@@ -15,8 +15,6 @@\n #include <string.h>\n #include <sys/time.h>\n \n-#define ARRAY_SIZE(a)\t(sizeof(a) / sizeof(a[0]))\n-\n #ifndef __DOXYGEN__\n \n /* uClibc and uClibc-ng don't provide O_TMPFILE */\ndiff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\nindex 2c355ea8b5e5..9c927a62b308 100644\n--- a/src/libcamera/ipa_module.cpp\n+++ b/src/libcamera/ipa_module.cpp\n@@ -22,7 +22,6 @@\n \n #include \"log.h\"\n #include \"pipeline_handler.h\"\n-#include \"utils.h\"\n \n /**\n  * \\file ipa_module.h\n@@ -480,7 +479,7 @@ bool IPAModule::match(PipelineHandler *pipe,\n  */\n bool IPAModule::isOpenSource() const\n {\n-\tstatic const char *osLicenses[] = {\n+\tstatic constexpr std::array<const char *, 8> osLicenses = {\n \t\t\"GPL-2.0-only\",\n \t\t\"GPL-2.0-or-later\",\n \t\t\"GPL-3.0-only\",\n@@ -491,9 +490,10 @@ bool IPAModule::isOpenSource() const\n \t\t\"LGPL-3.0-or-later\",\n \t};\n \n-\tfor (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++)\n-\t\tif (!strcmp(osLicenses[i], info_.license))\n+\tfor (const char *license : osLicenses) {\n+\t\tif (!strcmp(license, info_.license))\n \t\t\treturn true;\n+\t}\n \n \treturn false;\n }\ndiff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\nindex 1dac4666b435..ef0b81f77131 100644\n--- a/src/libcamera/log.cpp\n+++ b/src/libcamera/log.cpp\n@@ -7,6 +7,7 @@\n \n #include \"log.h\"\n \n+#include <array>\n #if HAVE_BACKTRACE\n #include <execinfo.h>\n #endif\n@@ -83,7 +84,7 @@ static int log_severity_to_syslog(LogSeverity severity)\n \n static const char *log_severity_name(LogSeverity severity)\n {\n-\tstatic const char *const names[] = {\n+\tstatic constexpr std::array<const char *, 5> names = {\n \t\t\"  DBG\",\n \t\t\" INFO\",\n \t\t\" WARN\",\n@@ -91,7 +92,7 @@ static const char *log_severity_name(LogSeverity severity)\n \t\t\"FATAL\",\n \t};\n \n-\tif (static_cast<unsigned int>(severity) < ARRAY_SIZE(names))\n+\tif (static_cast<unsigned int>(severity) < names.size())\n \t\treturn names[severity];\n \telse\n \t\treturn \"UNKWN\";\n@@ -405,9 +406,9 @@ void Logger::backtrace()\n \tif (!output)\n \t\treturn;\n \n-\tvoid *buffer[32];\n-\tint num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer));\n-\tchar **strings = backtrace_symbols(buffer, num_entries);\n+\tstd::array<void *, 32> buffer;\n+\tint num_entries = ::backtrace(buffer.data(), buffer.size());\n+\tchar **strings = backtrace_symbols(buffer.data(), num_entries);\n \tif (!strings)\n \t\treturn;\n \n@@ -603,7 +604,7 @@ void Logger::parseLogLevels()\n  */\n LogSeverity Logger::parseLogLevel(const std::string &level)\n {\n-\tstatic const char *const names[] = {\n+\tstatic constexpr std::array<const char *, 5> names = {\n \t\t\"DEBUG\",\n \t\t\"INFO\",\n \t\t\"WARN\",\n@@ -620,7 +621,7 @@ LogSeverity Logger::parseLogLevel(const std::string &level)\n \t\t\tseverity = LogInvalid;\n \t} else {\n \t\tseverity = LogInvalid;\n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) {\n+\t\tfor (unsigned int i = 0; i < names.size(); ++i) {\n \t\t\tif (names[i] == level) {\n \t\t\t\tseverity = i;\n \t\t\t\tbreak;\ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nindex 4beffdab5eb6..74576797ee77 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -22,11 +22,6 @@ namespace libcamera {\n \n namespace utils {\n \n-/**\n- * \\def ARRAY_SIZE(array)\n- * \\brief Determine the number of elements in the static array.\n- */\n-\n /**\n  * \\brief Strip the directory prefix from the path\n  * \\param[in] path The path to process\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 18220b81af21..51be1dcd7fff 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -7,6 +7,7 @@\n \n #include \"v4l2_videodevice.h\"\n \n+#include <array>\n #include <fcntl.h>\n #include <iomanip>\n #include <sstream>\n@@ -991,13 +992,13 @@ int V4L2VideoDevice::exportBuffers(unsigned int count,\n \n \tfor (unsigned i = 0; i < count; ++i) {\n \t\tstruct v4l2_buffer buf = {};\n-\t\tstruct v4l2_plane planes[VIDEO_MAX_PLANES] = {};\n+\t\tstd::array<struct v4l2_plane, VIDEO_MAX_PLANES> planes = {};\n \n \t\tbuf.index = i;\n \t\tbuf.type = bufferType_;\n \t\tbuf.memory = memoryType_;\n-\t\tbuf.length = ARRAY_SIZE(planes);\n-\t\tbuf.m.planes = planes;\n+\t\tbuf.length = planes.size();\n+\t\tbuf.m.planes = planes.data();\n \n \t\tret = ioctl(VIDIOC_QUERYBUF, &buf);\n \t\tif (ret < 0) {\ndiff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp\nindex f53042b88720..5bf543c197fa 100644\n--- a/test/ipc/unixsocket.cpp\n+++ b/test/ipc/unixsocket.cpp\n@@ -6,6 +6,7 @@\n  */\n \n #include <algorithm>\n+#include <array>\n #include <fcntl.h>\n #include <iostream>\n #include <stdlib.h>\n@@ -21,7 +22,6 @@\n #include \"ipc_unixsocket.h\"\n #include \"test.h\"\n #include \"thread.h\"\n-#include \"utils.h\"\n \n #define CMD_CLOSE\t0\n #define CMD_REVERSE\t1\n@@ -303,13 +303,13 @@ protected:\n \t\tIPCUnixSocket::Payload message, response;\n \t\tint ret;\n \n-\t\tstatic const char *strings[2] = {\n+\t\tstatic constexpr std::array<const char *, 2> strings = {\n \t\t\t\"Foo\",\n \t\t\t\"Bar\",\n \t\t};\n \t\tint fds[2];\n \n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) {\n+\t\tfor (unsigned int i = 0; i < strings.size(); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \n \t\t\tfds[i] = open(\"/tmp\", O_TMPFILE | O_RDWR,\n@@ -331,7 +331,7 @@ protected:\n \t\tif (ret)\n \t\t\treturn ret;\n \n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) {\n+\t\tfor (unsigned int i = 0; i < strings.size(); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \t\t\tchar buf[len];\n \n","prefixes":["libcamera-devel","05/19"]}