From patchwork Tue Sep 22 13:35:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 9706 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B110EC3B5B for ; Tue, 22 Sep 2020 13:36:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7B04662FDF; Tue, 22 Sep 2020 15:36:00 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="vm5yfgT1"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B8A5F6036A for ; Tue, 22 Sep 2020 15:35:59 +0200 (CEST) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 0C981555; Tue, 22 Sep 2020 15:35:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1600781754; bh=8uBsm2yGBXpzYPeq+AyKaw7ypGh+S7GYWG6E+RMnhrk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vm5yfgT1a78qiqWOeKSRSe1Ea9xKi8bXJxKy1CizNd0PnXi5dx5pujTxAnXBXL/U1 93GyxH1i3Us5VppwOkiB2RzVsC6mk8IwvKBSzjzG2kP3yk7KTzD4b328QhJ90/BHnq 1a+h+8FiCZB9Tppbr1D5kRCWQsPun2fwI3oi8fkM= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Sep 2020 22:35:00 +0900 Message-Id: <20200922133537.258098-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200922133537.258098-1-paul.elder@ideasonboard.com> References: <20200922133537.258098-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 01/38] Documentation: coding-style: Document global variable guidelines X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Document the issue related to global variable dependencies and how to avoid them. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- New in v2 --- Documentation/coding-style.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Documentation/coding-style.rst b/Documentation/coding-style.rst index 8af06d6a..967506db 100644 --- a/Documentation/coding-style.rst +++ b/Documentation/coding-style.rst @@ -187,6 +187,21 @@ These rules match the `object ownership rules from the Chromium C++ Style Guide` long term borrowing isn't marked through language constructs, it shall be documented explicitly in details in the API. +Global Variables +~~~~~~~~~~~~~~~~ + +The order of initialization and destructions of global variables cannot be +reasonably controlled. This can cause problems (segfaults) when global +variables depend on each other, or when non-globals depend on globals. +For example, if the declaration of a global variable calls a constructor, +which calls into another global variable that has yet to be initialized, that +is likely to segfault. + +The best solution is to avoid global variables when possible. If required, +such as for implementing factories with auto-registration, avoid dependencies +by running the minimum amount of code in the constructor and destructor, +and move code that contains dependencies to a later point of time. + C Compatibility Headers ~~~~~~~~~~~~~~~~~~~~~~~