Skip to contentSkip to conversation

string: import strverscmp_improved from systemd

PATCH Applied Ahmad Fatoum 2 participants 5 messages May 31, 2023 – Jun 1, 2023 Library code

lore
Share & tools
lore
#0 lore raw

cover string: import strverscmp_improved from systemd

The Boot Loader specification now references the UAPI group's version format specification[1] on how blspec entries should be sorted.

In preparation of aligning barebox entry sorting with the specification, import systemd's strverscmp_improved as strverscmp and add some tests for it.

The changes to bootloader spec sorting need some more testing and will follow separately.

Ahmad Fatoum (3):
include: sync min/max definitions with Linux
include: minmax.h: implement compare3 helper
string: import strverscmp_improved from systemd

 include/linux/const.h  |   9 ++
 include/linux/kernel.h | 123 +--------------------------
 include/linux/minmax.h | 189 +++++++++++++++++++++++++++++++++++++++++
 include/string.h       |   2 +
 lib/Kconfig            |   9 +-
 lib/Makefile           |   1 +
 lib/strverscmp.c       | 165 +++++++++++++++++++++++++++++++++++
 test/self/Kconfig      |   6 ++
 test/self/Makefile     |   1 +
 test/self/string.c     | 175 ++++++++++++++++++++++++++++++++++++++
 10 files changed, 557 insertions(+), 123 deletions(-)
 create mode 100644 include/linux/minmax.h
 create mode 100644 lib/strverscmp.c
 create mode 100644 test/self/string.c
Signature · 1 line
2.38.5
#1 lore raw

PATCH 1/3 include: sync min/max definitions with Linux

By syncing to Linux, min/max and friends now return constant expressions if possible, which makes them suitable for use as non-VLA array length.

---
 include/linux/const.h  |   9 +++
 include/linux/kernel.h | 123 +-----------------------------
 include/linux/minmax.h | 169 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 179 insertions(+), 122 deletions(-)
 create mode 100644 include/linux/minmax.h
include/linux/const.h +9 -0
index 07f886d27155..0acddd3a0ed6 100644
@@ -26,4 +26,13 @@
 #define _BITUL(x)	(_AC(1,UL) << (x))
 #define _BITULL(x)	(_AC(1,ULL) << (x))
 
+
+/*
+ * This returns a constant expression while determining if an argument is
+ * a constant expression, most importantly without evaluating the argument.
+ * Glory to Martin Uecker <[email protected]>
+ */
+#define __is_constexpr(x) \
+	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+
 #endif /* !(_LINUX_CONST_H) */
include/linux/kernel.h +1 -122

141 lines not on this page · raw message

include/linux/minmax.h new file +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.38.5
#2 lore raw

PATCH 2/3 include: minmax.h: implement compare3 helper

In reply to Ahmad Fatoum

Define the macro for use in comparison functions. The macro has the same semantics as systemd's CMP() macro with the difference that it returns constant expressions if possible.

---
 include/linux/minmax.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/linux/minmax.h +20 -0
index 396df1121bff..9b6ddad7b3f6 100644
@@ -166,4 +166,24 @@
 #define swap(a, b) \
 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
+
+#define __compare3(x, y)	((x) < (y) ? -1 : (x) > (y) ? 1 : 0)
+
+#define __compare3_once(x, y, unique_x, unique_y) ({	\
+		typeof(x) unique_x = (x);		\
+		typeof(y) unique_y = (y);		\
+		__compare3(unique_x, unique_y); })
+
+/**
+ * compare3 - 3-way comparison of @x and @y
+ * @x: first value
+ * @y: second value
+ *
+ * returns -1 if x < y, 0 if x == y and 1 if x > y
+ */
+#define compare3(x, y) \
+	__builtin_choose_expr(__safe_cmp(x, y), \
+		__compare3(x, y), \
+		__compare3_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
+
 #endif	/* _LINUX_MINMAX_H */
Signature · 1 line
2.38.5
#3 lore raw

PATCH 3/3 string: import strverscmp_improved from systemd

In reply to Ahmad Fatoum

The Boot Loader specification now references the UAPI group's version format specification[1] on how blspec entries should be sorted.

In preparation of aligning barebox entry sorting with the specification, import systemd's strverscmp_improved as strverscmp and add some tests for it.

The selftest is called string.c, because it indirectly tests some string mangling function and in anticipation of adding more string tests in the future.

[1]: https://uapi-group.org/specifications/specs/version_format_specification/#examples

---
 include/string.h   |   2 +
 lib/Kconfig        |   9 ++-
 lib/Makefile       |   1 +
 lib/strverscmp.c   | 165 ++++++++++++++++++++++++++++++++++++++++++
 test/self/Kconfig  |   6 ++
 test/self/Makefile |   1 +
 test/self/string.c | 175 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 lib/strverscmp.c
 create mode 100644 test/self/string.c
include/string.h +2 -0
index 499f2ec03c02..43911b75762f 100644
@@ -18,4 +18,6 @@ void *__nokasan_default_memcpy(void * dest,const void *src,size_t count);
 
 char *parse_assignment(char *str);
 
+int strverscmp(const char *a, const char *b);
+
 #endif /* __STRING_H */
lib/Kconfig +8 -1
index b8bc9d63d4f0..84d2a2573625 100644
@@ -107,7 +107,7 @@ config IMAGE_SPARSE
 	bool
 
 config STMP_DEVICE
-	bool "STMP device support" if COMPILE_TEST
+	bool "STMP device support"
 
 config FSL_QE_FIRMWARE
 	select CRC32
@@ -167,6 +167,13 @@ config PROGRESS_NOTIFIER
 	  This is selected by boards that register a notifier to visualize
 	  progress, like blinking a LED during an update.
 
+config VERSION_CMP
+	bool "version comparison utilities" if COMPILE_TEST
+	help
+	  This is selected by code that needs to compare versions
+	  in a manner compatible with
+	    https://uapi-group.org/specifications/specs/version_format_specification
+
 config PRINTF_UUID
 	bool
 	default y if PRINTF_FULL
lib/Makefile +1 -0
index 38478625423b..185e6221fdd2 100644
@@ -6,6 +6,7 @@ obj-pbl-y		+= ctype.o
 obj-y			+= rbtree.o
 obj-y			+= display_options.o
 obj-y			+= string.o
+obj-$(CONFIG_VERSION_CMP)	+= strverscmp.o
 obj-y			+= strtox.o
 obj-y			+= kstrtox.o
 obj-y			+= vsprintf.o
lib/strverscmp.c new file +165 -0

165 lines not on this page · raw message

test/self/Kconfig +6 -0
index 1d6d8ab53a8d..d1ca6a701df3 100644
@@ -38,6 +38,8 @@ config SELFTEST_ENABLE_ALL
 	imply SELFTEST_JSON
 	imply SELFTEST_DIGEST
 	imply SELFTEST_MMU
+	imply SELFTEST_REGULATOR
+	imply SELFTEST_STRING
 	help
 	  Selects all self-tests compatible with current configuration
 
@@ -81,4 +83,8 @@ config SELFTEST_DIGEST
 	depends on DIGEST
 	select PRINTF_HEXSTR
 
+config SELFTEST_STRING
+	bool "String library selftest"
+	select VERSION_CMP
+
 endif
test/self/Makefile +1 -0
index 269de2e10e88..a66f34671e5a 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_SELFTEST_FS_RAMFS) += ramfs.o
 obj-$(CONFIG_SELFTEST_JSON) += json.o
 obj-$(CONFIG_SELFTEST_DIGEST) += digest.o
 obj-$(CONFIG_SELFTEST_MMU) += mmu.o
+obj-$(CONFIG_SELFTEST_STRING) += string.o
 
 clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtb.z
 clean-files += *.dtbo *.dtbo.S .*.dtso
test/self/string.c new file +175 -0

175 lines not on this page · raw message

Signature · 1 line
2.38.5
Applied
#4 lore raw
In reply to Ahmad Fatoum
Quoted 7 lines · Ahmad Fatoum

The Boot Loader specification now references the UAPI group's version format specification[1] on how blspec entries should be sorted.

In preparation of aligning barebox entry sorting with the specification, import systemd's strverscmp_improved as strverscmp and add some tests for it.

Quoted 7 lines

The changes to bootloader spec sorting need some more testing and will follow separately.

Ahmad Fatoum (3):
include: sync min/max definitions with Linux
include: minmax.h: implement compare3 helper
string: import strverscmp_improved from systemd

Applied, thanks

Sascha

Quoted 21 lines
 include/linux/const.h  |   9 ++
 include/linux/kernel.h | 123 +--------------------------
 include/linux/minmax.h | 189 +++++++++++++++++++++++++++++++++++++++++
 include/string.h       |   2 +
 lib/Kconfig            |   9 +-
 lib/Makefile           |   1 +
 lib/strverscmp.c       | 165 +++++++++++++++++++++++++++++++++++
 test/self/Kconfig      |   6 ++
 test/self/Makefile     |   1 +
 test/self/string.c     | 175 ++++++++++++++++++++++++++++++++++++++
 10 files changed, 557 insertions(+), 123 deletions(-)
 create mode 100644 include/linux/minmax.h
 create mode 100644 lib/strverscmp.c
 create mode 100644 test/self/string.c
Signature · 1 line
2.38.5
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |