[libcamera-devel] [PATCH 01/38] Documentation: coding-style: Document global variable guidelines
Jacopo Mondi
jacopo at jmondi.org
Wed Sep 23 12:13:16 CEST 2020
Hi Paul,
On Tue, Sep 22, 2020 at 10:35:00PM +0900, Paul Elder wrote:
> Document the issue related to global variable dependencies and how to
> avoid them.
>
> Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>
>
> ---
> 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.
> +
While avoiding globals is desirable, sometimes it makes things easier,
and if the type of the variable is trivially destructible and has a
constexpr constructor we should be safe against
initialization/destruction order failures.
This section is fine as it is a 'stay safe' kind-of paragraph, but if
you feel like expanding it you can have some references from here
https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
Thanks
j
> C Compatibility Headers
> ~~~~~~~~~~~~~~~~~~~~~~~
>
> --
> 2.27.0
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel at lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
More information about the libcamera-devel
mailing list