{"id":10654,"url":"https://patchwork.libcamera.org/api/patches/10654/?format=json","web_url":"https://patchwork.libcamera.org/patch/10654/","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":"<20201211232319.32088-1-laurent.pinchart@ideasonboard.com>","date":"2020-12-11T23:23:19","name":"[libcamera-devel] libcamera: Replace ARRAY_SIZE() with std::size()","commit_ref":"5cfbbcd20731a2160206cc1d935eac2c770376ae","pull_url":null,"state":"accepted","archived":false,"hash":"eeb06b9e5a5ec4b3a191c405cd46684549630a26","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":{"id":14,"url":"https://patchwork.libcamera.org/api/users/14/?format=json","username":"pinchartl","first_name":"Laurent","last_name":"Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/10654/mbox/","series":[{"id":1529,"url":"https://patchwork.libcamera.org/api/series/1529/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1529","date":"2020-12-11T23:23:19","name":"[libcamera-devel] libcamera: Replace ARRAY_SIZE() with std::size()","version":1,"mbox":"https://patchwork.libcamera.org/series/1529/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10654/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10654/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id B1600BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 12 Dec 2020 01:52:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 27679746F2;\n\tSat, 12 Dec 2020 02:52:47 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1871E68053\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 12 Dec 2020 02:52:45 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9955A99\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 12 Dec 2020 00:23:27 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ho/Y7oPO\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1607729007;\n\tbh=vvnGfYvFuNpj+5A+WquP+gKDyiXptKqM/xM6tQq4PsA=;\n\th=From:To:Subject:Date:From;\n\tb=ho/Y7oPOHvIFqMPC0GNyCOquGufyJkl8nGgPY/6tg1Q0Xi+6H2iNeXzQEK7wmSNrF\n\t7PP5tTz5xAQnro1qY+IRv8RfPi1Ay9ZBCWS7S643CCQUMRQdhrxQiBTtzGAx3JtdkP\n\tQ5jJIOW8deVmBQ8rn5Eu9aqF/G07tzM4euXlWtXY=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 12 Dec 2020 01:23:19 +0200","Message-Id":"<20201211232319.32088-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH] libcamera: Replace ARRAY_SIZE() with\n\tstd::size()","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"C++17 has a std::size() function that returns the size of a C-style\narray. Use it instead of the custom ARRAY_SIZE macro.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/utils.h  | 2 --\n src/ipa/raspberrypi/raspberrypi.cpp | 4 ++--\n src/libcamera/camera.cpp            | 8 ++++----\n src/libcamera/log.cpp               | 7 ++++---\n src/libcamera/utils.cpp             | 5 -----\n src/libcamera/v4l2_videodevice.cpp  | 6 +++---\n test/ipc/unixsocket.cpp             | 6 +++---\n 7 files changed, 16 insertions(+), 22 deletions(-)","diff":"diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h\nindex ebb2c4038e19..f08134afb5ba 100644\n--- a/include/libcamera/internal/utils.h\n+++ b/include/libcamera/internal/utils.h\n@@ -17,8 +17,6 @@\n #include <sys/time.h>\n #include <vector>\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/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex 801400214c3a..a7439badb7dc 100644\n--- a/src/ipa/raspberrypi/raspberrypi.cpp\n+++ b/src/ipa/raspberrypi/raspberrypi.cpp\n@@ -6,6 +6,7 @@\n  */\n \n #include <algorithm>\n+#include <array>\n #include <fcntl.h>\n #include <math.h>\n #include <stdint.h>\n@@ -27,7 +28,6 @@\n #include \"libcamera/internal/buffer.h\"\n #include \"libcamera/internal/camera_sensor.h\"\n #include \"libcamera/internal/log.h\"\n-#include \"libcamera/internal/utils.h\"\n \n #include <linux/bcm2835-isp.h>\n \n@@ -1092,7 +1092,7 @@ void IPARPi::applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls)\n \t * Choose smallest cell size that won't exceed 63x48 cells.\n \t */\n \tconst int cellSizes[] = { 16, 32, 64, 128, 256 };\n-\tunsigned int numCells = ARRAY_SIZE(cellSizes);\n+\tunsigned int numCells = std::size(cellSizes);\n \tunsigned int i, w, h, cellSize;\n \tfor (i = 0; i < numCells; i++) {\n \t\tcellSize = cellSizes[i];\ndiff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 3579ba96b6ea..c1de1fee701a 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 <atomic>\n #include <iomanip>\n \n@@ -17,7 +18,6 @@\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/pipeline_handler.h\"\n #include \"libcamera/internal/thread.h\"\n-#include \"libcamera/internal/utils.h\"\n \n /**\n  * \\file camera.h\n@@ -393,7 +393,7 @@ int Camera::Private::isAccessAllowed(State state, bool allowDisconnected) const\n \tif (currentState == state)\n \t\treturn 0;\n \n-\tASSERT(static_cast<unsigned int>(state) < ARRAY_SIZE(camera_state_names));\n+\tASSERT(static_cast<unsigned int>(state) < std::size(camera_state_names));\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[currentState]\n \t\t\t   << \" state trying operation requiring state \"\n@@ -412,8 +412,8 @@ int Camera::Private::isAccessAllowed(State low, State high,\n \tif (currentState >= low && currentState <= high)\n \t\treturn 0;\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) < std::size(camera_state_names) &&\n+\t       static_cast<unsigned int>(high) < std::size(camera_state_names));\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[currentState]\n \t\t\t   << \" state trying operation requiring state between \"\ndiff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\nindex 180eb97ba664..45c7c2d24652 100644\n--- a/src/libcamera/log.cpp\n+++ b/src/libcamera/log.cpp\n@@ -7,6 +7,7 @@\n \n #include \"libcamera/internal/log.h\"\n \n+#include <array>\n #if HAVE_BACKTRACE\n #include <execinfo.h>\n #endif\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) < std::size(names))\n \t\treturn names[severity];\n \telse\n \t\treturn \"UNKWN\";\n@@ -406,7 +407,7 @@ void Logger::backtrace()\n \t\treturn;\n \n \tvoid *buffer[32];\n-\tint num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer));\n+\tint num_entries = ::backtrace(buffer, std::size(buffer));\n \tchar **strings = backtrace_symbols(buffer, num_entries);\n \tif (!strings)\n \t\treturn;\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 < std::size(names); ++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 da85c9c24340..e90375ae115c 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -31,11 +31,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 e2b582842a9b..baf683d6d985 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -7,6 +7,7 @@\n \n #include \"libcamera/internal/v4l2_videodevice.h\"\n \n+#include <array>\n #include <fcntl.h>\n #include <iomanip>\n #include <sstream>\n@@ -26,7 +27,6 @@\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/media_device.h\"\n #include \"libcamera/internal/media_object.h\"\n-#include \"libcamera/internal/utils.h\"\n \n /**\n  * \\file v4l2_videodevice.h\n@@ -860,7 +860,7 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)\n \tpix->num_planes = format->planesCount;\n \tpix->field = V4L2_FIELD_NONE;\n \n-\tASSERT(pix->num_planes <= ARRAY_SIZE(pix->plane_fmt));\n+\tASSERT(pix->num_planes <= std::size(pix->plane_fmt));\n \n \tfor (unsigned int i = 0; i < pix->num_planes; ++i) {\n \t\tpix->plane_fmt[i].bytesperline = format->planes[i].bpl;\n@@ -1255,7 +1255,7 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n \tbuf.index = index;\n \tbuf.type = bufferType_;\n \tbuf.memory = V4L2_MEMORY_MMAP;\n-\tbuf.length = ARRAY_SIZE(v4l2Planes);\n+\tbuf.length = std::size(v4l2Planes);\n \tbuf.m.planes = v4l2Planes;\n \n \tint ret = ioctl(VIDIOC_QUERYBUF, &buf);\ndiff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp\nindex 19a1d7dd8a2d..80157b342795 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@@ -19,7 +20,6 @@\n #include \"libcamera/internal/ipc_unixsocket.h\"\n #include \"libcamera/internal/thread.h\"\n #include \"libcamera/internal/timer.h\"\n-#include \"libcamera/internal/utils.h\"\n \n #include \"test.h\"\n \n@@ -311,7 +311,7 @@ protected:\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 < std::size(strings); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \n \t\t\tfds[i] = open(\"/tmp\", O_TMPFILE | O_RDWR,\n@@ -333,7 +333,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 < std::size(strings); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \t\t\tchar buf[len];\n \n","prefixes":["libcamera-devel"]}