From patchwork Mon Feb 24 17:08:25 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Cartwright X-Patchwork-Id: 3710781 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9E3BFBF13A for ; Mon, 24 Feb 2014 17:12:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CC04320103 for ; Mon, 24 Feb 2014 17:12:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4416720160 for ; Mon, 24 Feb 2014 17:12:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752256AbaBXRMY (ORCPT ); Mon, 24 Feb 2014 12:12:24 -0500 Received: from smtp.codeaurora.org ([198.145.11.231]:45083 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752713AbaBXRLF (ORCPT ); Mon, 24 Feb 2014 12:11:05 -0500 Received: from smtp.codeaurora.org (localhost [127.0.0.1]) by smtp.codeaurora.org (Postfix) with ESMTP id D2B5A13EC94; Mon, 24 Feb 2014 17:11:04 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 486) id C6EAE13F11F; Mon, 24 Feb 2014 17:11:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from joshc.qualcomm.com (rrcs-67-52-129-61.west.biz.rr.com [67.52.129.61]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: joshc@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 534D013EFE8; Mon, 24 Feb 2014 17:11:04 +0000 (UTC) Received: by joshc.qualcomm.com (Postfix, from userid 1000) id C261D6353B; Mon, 24 Feb 2014 11:08:27 -0600 (CST) From: Josh Cartwright To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , linux-pm@vger.kernel.org, Andrew Morton Subject: [PATCH 1/3] typecheck: introduce assign_if() and assign_if_enabled() Date: Mon, 24 Feb 2014 11:08:25 -0600 Message-Id: <1393261707-30565-2-git-send-email-joshc@codeaurora.org> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1393261707-30565-1-git-send-email-joshc@codeaurora.org> References: <1393261707-30565-1-git-send-email-joshc@codeaurora.org> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The assign_if() and assign_if_enable() macros are intended to be used in static initializers for function pointers, where the pointer is expected to be NULL when a compile-time condition does not hold. These macros allow for implementing this behavior, without requiring the functions be wrapped in #ifdef conditionals, and while providing typesafety regardless of the value of the conditional. For example, the following pattern is common: #ifdef CONFIG_FOO static void foo_callback(void) { } #else #define foo_callback NULL #endif static struct foo_object foo_obj = { .callback = foo_callback, }; Usage of assign_if_enabled() allows for achieving the same effect without the preprocessor conditional, and in addition, allowing the compiler to typecheck the function regardless of CONFIG_FOO. static void foo_callback(void) { } static struct foo_object foo_obj = { .callback = assign_if_enabled(CONFIG_FOO, foo_callback), }; Cc: Andrew Morton Signed-off-by: Josh Cartwright --- include/linux/typecheck.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/typecheck.h b/include/linux/typecheck.h index eb5b74a..04134c7 100644 --- a/include/linux/typecheck.h +++ b/include/linux/typecheck.h @@ -21,4 +21,22 @@ (void)__tmp; \ }) +/* + * Intended for use in static object initializers, + * assign_if(const_expr, function) evaluates to 'function' if 'const_expr', + * otherwise NULL. + * + * The type of the assign_if() expression is typeof(function), and therefore + * can provide typechecking regardless of 'const_expr'. + * + * gcc considers 'function' to be used and will not generate a 'defined but not + * used' warning when not 'const_expr', however, gcc is smart enough to + * eliminate 'function' if assign_if() is the only reference. + */ +#define assign_if(const_expr,function) \ + ((const_expr) ? function : NULL) + +#define assign_if_enabled(option,function) \ + assign_if(IS_ENABLED(option), function) + #endif /* TYPECHECK_H_INCLUDED */