[{"id":3565,"web_url":"https://patchwork.libcamera.org/comment/3565/","msgid":"<20200122150425.GM1124294@oden.dyn.berto.se>","date":"2020-01-22T15:04:25","subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: Replace ARRAY_SIZE\n\twith std::array","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your work.\n\nOn 2020-01-20 19:38:16 +0200, Laurent Pinchart wrote:\n> Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro\n> is used, removing the need for the macro completely. std::array combines\n> the performance and accessibility of C-style arrays with the benefits of\n> a standard container, which is shown here through the ability to carry\n> its size.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  src/libcamera/camera.cpp           | 13 +++++++------\n>  src/libcamera/include/utils.h      |  2 --\n>  src/libcamera/ipa_module.cpp       | 11 ++++++-----\n>  src/libcamera/log.cpp              | 23 ++++++++++++-----------\n>  src/libcamera/utils.cpp            |  5 -----\n>  src/libcamera/v4l2_videodevice.cpp |  7 ++++---\n>  test/ipc/unixsocket.cpp            |  8 ++++----\n>  7 files changed, 33 insertions(+), 36 deletions(-)\n> \n> diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\n> index 79a5f994f9bb..769f16c9ceab 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> @@ -404,20 +405,20 @@ 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 auto camera_state_names = utils::make_array(\n>  \t\"Available\",\n>  \t\"Acquired\",\n>  \t\"Configured\",\n> -\t\"Running\",\n> -};\n> +\t\"Running\"\n> +);\n>  \n>  bool Camera::stateBetween(State low, State high) const\n>  {\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 +433,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 \"\n> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> index 6e9b9259456a..9057f8b0de84 100644\n> --- a/src/libcamera/include/utils.h\n> +++ b/src/libcamera/include/utils.h\n> @@ -18,8 +18,6 @@\n>  #include <sys/time.h>\n>  #include <type_traits>\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 */\n> diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\n> index 2c355ea8b5e5..6d9ff77d5bd4 100644\n> --- a/src/libcamera/ipa_module.cpp\n> +++ b/src/libcamera/ipa_module.cpp\n> @@ -480,7 +480,7 @@ bool IPAModule::match(PipelineHandler *pipe,\n>   */\n>  bool IPAModule::isOpenSource() const\n>  {\n> -\tstatic const char *osLicenses[] = {\n> +\tstatic constexpr auto osLicenses = utils::make_array(\n>  \t\t\"GPL-2.0-only\",\n>  \t\t\"GPL-2.0-or-later\",\n>  \t\t\"GPL-3.0-only\",\n> @@ -488,12 +488,13 @@ bool IPAModule::isOpenSource() const\n>  \t\t\"LGPL-2.1-only\",\n>  \t\t\"LGPL-2.1-or-later\",\n>  \t\t\"LGPL-3.0-only\",\n> -\t\t\"LGPL-3.0-or-later\",\n> -\t};\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>  }\n> diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\n> index 1c82dc68fb0f..fec48c9e7175 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,15 +84,15 @@ 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 auto names = utils::make_array<const char *>(\n>  \t\t\"  DBG\",\n>  \t\t\" INFO\",\n>  \t\t\" WARN\",\n>  \t\t\"  ERR\",\n> -\t\t\"FATAL\",\n> -\t};\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,13 +604,13 @@ void Logger::parseLogLevels()\n>   */\n>  LogSeverity Logger::parseLogLevel(const std::string &level)\n>  {\n> -\tstatic const char *const names[] = {\n> +\tstatic constexpr auto names = utils::make_array<const char *>(\n>  \t\t\"DEBUG\",\n>  \t\t\"INFO\",\n>  \t\t\"WARN\",\n>  \t\t\"ERROR\",\n> -\t\t\"FATAL\",\n> -\t};\n> +\t\t\"FATAL\"\n> +\t);\n>  \n>  \tint severity;\n>  \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;\n> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> index b639cfa83d0c..dfd140ca464a 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\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index 82267730289d..234d8123c7b4 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> @@ -992,13 +993,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) {\n> diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp\n> index 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> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lj1-x243.google.com (mail-lj1-x243.google.com\n\t[IPv6:2a00:1450:4864:20::243])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B6681607F3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jan 2020 16:04:27 +0100 (CET)","by mail-lj1-x243.google.com with SMTP id j26so7131453ljc.12\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jan 2020 07:04:27 -0800 (PST)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\tv26sm20841343ljv.92.2020.01.22.07.04.25\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 22 Jan 2020 07:04:26 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to;\n\tbh=dubDCV6fX7U7vaKQHK4/5uT1+ROXkeosF5FI1rPOJik=;\n\tb=oGjYEQHr7RfV+S8MLUys7bLCztWo4aULYjdrlLza0gEz4Mo6jurtVYUJ7muZnv3fWJ\n\tZSzXLY0zqJEtuEtTpipIAlW6cLXZ4LbjJ+s9N18a+VeGHvfWxIpH7B9JPIuK2Mn0BfM4\n\tQ5W1aglO2k3YJyywjXd0/mUkSqhNtsU9K7MHneriBjcqKS/hmHbvQ51Uj0dKwfFlL9+o\n\tdwyrShuQlfNhfs+rQcF6SldnhV36DmLUTwGczqWdlnY8q+Cwoz723Y1yl5cRXTuVsU19\n\tbhnw80jrM7K9v4tAFSJbBU8l7RxbSLsBIQrGsPu1ScO15Ee1TWQiMwUIckuUH/JIabiL\n\t0/fw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to;\n\tbh=dubDCV6fX7U7vaKQHK4/5uT1+ROXkeosF5FI1rPOJik=;\n\tb=U3lTXiWBonTg+jIkzQq4bwBgjjykWlzRbnSAWVDxRwO6txSwJLbfTkUXEfYmnNfa0S\n\t77/668svltbWSdNj0IU1euO5Ku093k/ZyDQP9pW6B+OqDXS+qNwcYtZ8v/7mVJhTa3qK\n\tUngeVyqXRvXozJD6lhefhbs2eVSSg1biHEs8qUg6rfmg1cxpyvbNyabBM07LIpxOZtdL\n\te/fL6eGZ/9Fn1m4dm6OF2ApnnsESN3tmPL7jjHb/BepuRIunLtxMa4Ve1+ezbO4Xmx1E\n\tcA+Pc3Vk+/r9+iV81oSFOuU+Io5W2gkgM/XZ9IsiaU9+nrPG7fUKnI1t2j93jH78htkC\n\tshlg==","X-Gm-Message-State":"APjAAAXlt8gxkYuMqaqCSOkxBWatcx6YOPZxlWClPjVP8tjkbXoCLyCJ\n\tRX3mofk3dflLBobpL6Zdtkap7jzEZC4=","X-Google-Smtp-Source":"APXvYqyvo8cxwZ4eFGZgMq61M7fei/crDw281FbLqEv2o5ydoXwKRqYVhqjQgd0izrbcImUN8ZjThA==","X-Received":"by 2002:a2e:b4cf:: with SMTP id\n\tr15mr18251993ljm.52.1579705466942; \n\tWed, 22 Jan 2020 07:04:26 -0800 (PST)","Date":"Wed, 22 Jan 2020 16:04:25 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200122150425.GM1124294@oden.dyn.berto.se>","References":"<20200120173816.31829-1-laurent.pinchart@ideasonboard.com>\n\t<20200120173816.31829-4-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200120173816.31829-4-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: Replace ARRAY_SIZE\n\twith std::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":"Wed, 22 Jan 2020 15:04:27 -0000"}}]