summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-11-11 15:31:17 +0100
committerLennart Poettering <lennart@poettering.net>2021-11-11 17:23:34 +0100
commitf22abf384d1d2a6d1a303502a57366df7dd4b08e (patch)
tree2d9ed3b010c9eeb00b51e508a0afdd502109a771
parentfundamental: rename type.h → types-fundamental.h (diff)
downloadsystemd-f22abf384d1d2a6d1a303502a57366df7dd4b08e.tar.xz
systemd-f22abf384d1d2a6d1a303502a57366df7dd4b08e.zip
types-fundamental: introduce sd_true + sd_false
I think we should stick to the rule that stuff defined in types-fundamental.h either: 1. adds a prefixed concept "sd_xyz" that maps differently in the two environments 2. adds a non-prefixed concept "xyz" that adds a type otherwise missing in one of the two environments but with the same definition as in the other. i.e. if have have some concept that might differ the way its set up in the two environments it really should be prefixed by "sd_" to make clear it has semantics we defined. Only drop the prefix if it really means the exact same thin in all environments. Now, sd_bool is defined prefixed, because its either mapped to "BOOLEAN" (which is an integer) in UEFI or "bool" (which is C99 _Bool) in userspace. size_t is not defined prefixed, because it's mapped to the same thing ultimately (on the UEFI its mapped to UINTN, but that in turn is defined as being the type for the size of memory objects, thus it's really the same as userspace size_t). So far "true" and "false" where defined unprefixed even though they map to values of different types. typeof(true) in userspace would reveal _Bool, but typeof(false) in UEFI would reveal BOOLEAN. The distinction actually does matter in comparisons (i.e. (_Bool) 1 == (_Bool) 2 holds while (BOOLEAN) 1 == (BOOLEAN) 2 does not hold). Hence, let's add sd_true and sd_false, thus indicating we defined our own concept here, and it has similar but different semantics in UEFI and in userspace.
-rw-r--r--src/fundamental/bootspec-fundamental.c4
-rw-r--r--src/fundamental/macro-fundamental.h8
-rw-r--r--src/fundamental/types-fundamental.h22
3 files changed, 26 insertions, 8 deletions
diff --git a/src/fundamental/bootspec-fundamental.c b/src/fundamental/bootspec-fundamental.c
index 51078397b6..9c4aee9744 100644
--- a/src/fundamental/bootspec-fundamental.c
+++ b/src/fundamental/bootspec-fundamental.c
@@ -44,7 +44,7 @@ sd_bool bootspec_pick_name_version(
good_version = os_image_version ?: (os_version ?: (os_version_id ? : os_build_id));
if (!good_name || !good_version)
- return false;
+ return sd_false;
if (ret_name)
*ret_name = good_name;
@@ -52,5 +52,5 @@ sd_bool bootspec_pick_name_version(
if (ret_version)
*ret_version = good_version;
- return true;
+ return sd_true;
}
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
index c52957a55c..cd9e60cf60 100644
--- a/src/fundamental/macro-fundamental.h
+++ b/src/fundamental/macro-fundamental.h
@@ -85,8 +85,8 @@
#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
#define __ONCE(o) \
({ \
- static bool (o) = false; \
- __sync_bool_compare_and_swap(&(o), false, true); \
+ static bool (o) = sd_false; \
+ __sync_bool_compare_and_swap(&(o), sd_false, sd_true); \
})
#undef MAX
@@ -236,7 +236,7 @@
#define IN_SET(x, ...) \
({ \
- sd_bool _found = false; \
+ sd_bool _found = sd_false; \
/* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \
* type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
* the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \
@@ -245,7 +245,7 @@
assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \
switch(x) { \
FOR_EACH_MAKE_CASE(__VA_ARGS__) \
- _found = true; \
+ _found = sd_true; \
break; \
default: \
break; \
diff --git a/src/fundamental/types-fundamental.h b/src/fundamental/types-fundamental.h
index 2a9a114bbc..5977e40c6c 100644
--- a/src/fundamental/types-fundamental.h
+++ b/src/fundamental/types-fundamental.h
@@ -1,6 +1,20 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+/* This defines a number of basic types that are one thing in userspace and another in the UEFI environment,
+ * but mostly the same in concept and behaviour.
+ *
+ * Note: if the definition of these types/values has slightly different semantics in userspace and in the
+ * UEFI environment then please prefix its name with "sd_" to make clear these types have special semantics,
+ * and *we* defined them. Otherwise, if the types are effectively 100% identical in behaviour in userspace
+ * and UEFI environment you can omit the prefix. (Examples: sd_char is 8 bit in userspace and 16 bit in UEFI
+ * space hence it should have the sd_ prefix; but size_t in userspace and UINTN in UEFI environment are 100%
+ * defined the same way ultimately, hence it's OK to just define size_t as alias to UINTN in UEFI
+ * environment, so that size_t can be used everywhere, without any "sd_" prefix.)
+ *
+ * Note: we generally prefer the userspace names of types and concepts. i.e. if in doubt please name types
+ * after the userspace vocabulary, and let's keep UEFI vocabulary specific to the UEFI build environment. */
+
#ifdef SD_BOOT
#include <efi.h>
@@ -9,8 +23,8 @@ typedef CHAR16 sd_char;
typedef INTN sd_int;
typedef UINTN size_t;
-#define true TRUE
-#define false FALSE
+#define sd_true TRUE
+#define sd_false FALSE
#else
#include <stdbool.h>
#include <stdint.h>
@@ -18,4 +32,8 @@ typedef UINTN size_t;
typedef bool sd_bool;
typedef char sd_char;
typedef int sd_int;
+
+#define sd_true true
+#define sd_false false
+
#endif