Received: by 10.213.65.68 with SMTP id h4csp1551710imn; Thu, 15 Mar 2018 03:02:28 -0700 (PDT) X-Google-Smtp-Source: AG47ELtWx12TpeUBrCw43nEDPeOzgkyNCxTEFaSTLJwbX8eYdO+NbGEwt+cxhwfABDHDgiOlfCcB X-Received: by 10.99.39.131 with SMTP id n125mr6260516pgn.292.1521108148672; Thu, 15 Mar 2018 03:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521108148; cv=none; d=google.com; s=arc-20160816; b=O8H9xhKdz0a+ltNI12rTZ0KasEwdUwZoqacw/Riny1+baKbJKCuADz5ZTKhyH3vn8E QQbdI6xHUgjyMD0OKJxWwFFd/XArukT0NLOIBDuR9XiVnad2UqJLGR+1tJiwN9dBHp47 3SAIh9MKNRpyYnusAXoTgehyhnF+1UwHe4Vp0hQheFPDxcXl4n5tFAPpmU+I5G8k7M9z arIjtBRc33iAUSn7p0R+yglSnlJ3X48+aNWnyXuv634RwYnC8H+b9jw4o9BDcWYGXFD4 1APmbSugmuf4XVD738DI96MMeapYlqpRs0pLTj9XR+cffQHhNQiyjhz5XArh0zEouUXQ 04+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=+EtMnpE1vNGL5Ga6x5j/y3y/dSlDjerO70oPnRWUjf8=; b=L7TAzk3CghJEUSYwKRpJNBSJUp5NQfYH7lj2PW3AlBCp6pDh0K9MShqslYOM8D8h5I bynF460t2hycTrbBxiGcDonOnuMneNi7odewtTFc0cXzkGy+xMEg4SERS9hz5ofGQxdg 7z2quyVgnU1/DfSTp9czbvVh6OkLzt1TiP+5h7bm8doNY5pvItXu9h8TM754+T8gR/HC lXBI+frj6NSMrCI0M8KS6nkalZ3QZjIk7c86fJa8U/yeV+77Zt1G8hm1T4bsrDqQwKPh /MQCLKiGsbsiXnLOA02817C4+aYgdVOxcUKlpP9h48+i2Kp4wT/OiU2FeVPh1rgDOwx6 GC6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=Ljp4vaBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k11-v6si3660799plt.531.2018.03.15.03.02.14; Thu, 15 Mar 2018 03:02:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=Ljp4vaBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751769AbeCOKBF (ORCPT + 99 others); Thu, 15 Mar 2018 06:01:05 -0400 Received: from galahad.ideasonboard.com ([185.26.127.97]:58098 "EHLO galahad.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751362AbeCOKBD (ORCPT ); Thu, 15 Mar 2018 06:01:03 -0400 Received: from CookieMonster.cookiemonster.local (unknown [149.254.234.209]) by galahad.ideasonboard.com (Postfix) with ESMTPSA id 0FAFD203A0; Thu, 15 Mar 2018 10:58:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1521107928; bh=m8NVoMrH375oSC0KmklyK1qI/cFMy/ZbM/D4nB7J+d8=; h=From:To:Cc:Subject:Date:From; b=Ljp4vaBsXLCi2+qgDGej0cUPwn/eNnwkQvPI+Uk/ggkgDYGI6Ocf+8Z02d+DaNMLg YrnwGSc/AMY3BVV1hlM0HHguKIEkfS3E6r/HfJiB1Ge+Q49ZD1bxZhvBbMQVNx86Mk qjVnVx78ew1bcv8jELjr85y49oPbflaeJDHnXlSk= From: Kieran Bingham To: linux-kernel@vger.kernel.org Cc: Laurent Pinchart , linux-media@vger.kernel.org, Kieran Bingham , Andrew Morton , Ingo Molnar , Thomas Gleixner , Kees Cook , Herbert Xu , Masahiro Yamada , Greg Kroah-Hartman , Krzysztof Kozlowski , Randy Dunlap , Ian Abbott Subject: [PATCH][RFC] kernel.h: provide array iterator Date: Thu, 15 Mar 2018 11:00:50 +0100 Message-Id: <1521108052-26861-1-git-send-email-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Simplify array iteration with a helper to iterate each entry in an array. Utilise the existing ARRAY_SIZE macro to identify the length of the array and pointer arithmetic to process each item as a for loop. Signed-off-by: Kieran Bingham --- include/linux/kernel.h | 10 ++++++++++ 1 file changed, 10 insertions(+) The use of static arrays to store data is a common use case throughout the kernel. Along with that is the obvious need to iterate that data. In fact there are just shy of 5000 instances of iterating a static array: git grep "for .*ARRAY_SIZE" | wc -l 4943 When working on the UVC driver - I found that I needed to split one such iteration into two parts, and at the same time felt that this could be refactored to be cleaner / easier to read. I do however worry that this simple short patch might not be desired or could also be heavily bikeshedded due to it's potential wide spread use (though perhaps that would be a good thing to have more users) ... but here it is, along with an example usage below which is part of a separate series. The aim is to simplify iteration on static arrays, in the same way that we have iterators for lists. The use of the ARRAY_SIZE macro, provides all the protections given by "__must_be_array(arr)" to this macro too. Regards Kieran ============================================================================= Example Usage from a pending UVC development: +#define for_each_uvc_urb(uvc_urb, uvc_streaming) \ + for_each_array_element(uvc_urb, uvc_streaming->uvc_urb) /* * Uninitialize isochronous/bulk URBs and free transfer buffers. */ static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers) { - struct urb *urb; - unsigned int i; + struct uvc_urb *uvc_urb; uvc_video_stats_stop(stream); - for (i = 0; i < UVC_URBS; ++i) { - struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; + for_each_uvc_urb(uvc_urb, stream) + usb_kill_urb(uvc_urb->urb); - urb = uvc_urb->urb; - if (urb == NULL) - continue; + flush_workqueue(stream->async_wq); - usb_kill_urb(urb); - usb_free_urb(urb); + for_each_uvc_urb(uvc_urb, stream) { + usb_free_urb(uvc_urb->urb); uvc_urb->urb = NULL; } if (free_buffers) uvc_free_urb_buffers(stream); } ============================================================================= diff --git a/include/linux/kernel.h b/include/linux/kernel.h index ce51455e2adf..95d7dae248b7 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -70,6 +70,16 @@ */ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) +/** + * for_each_array_element - Iterate all items in an array + * @elem: pointer of array type for iteration cursor + * @array: array to be iterated + */ +#define for_each_array_element(elem, array) \ + for (elem = &(array)[0]; \ + elem < &(array)[ARRAY_SIZE(array)]; \ + ++elem) + #define u64_to_user_ptr(x) ( \ { \ typecheck(u64, x); \ -- 2.7.4