]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
efi: add free_and_xstrdup16() helper modelled after free_and_strdup() in userspace
authorLennart Poettering <lennart@poettering.net>
Mon, 9 Sep 2024 13:46:52 +0000 (15:46 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 10 Sep 2024 04:48:38 +0000 (06:48 +0200)
src/boot/efi/util.c
src/boot/efi/util.h

index ae605bf5a50b78a47a02b126b81ec69af9e0e99a..f5f748bc6cf1a86436c7565a88cd05a2ab717e55 100644 (file)
@@ -492,3 +492,24 @@ void *xmalloc(size_t size) {
         assert_se(BS->AllocatePool(EfiLoaderData, size, &p) == EFI_SUCCESS);
         return p;
 }
+
+bool free_and_xstrdup16(char16_t **p, const char16_t *s) {
+        char16_t *t;
+
+        assert(p);
+
+        /* Replaces a string pointer with a strdup()ed new string,
+         * possibly freeing the old one. */
+
+        if (streq_ptr(*p, s))
+                return false;
+
+        if (s)
+                t = xstrdup16(s);
+        else
+                t = NULL;
+
+        free(*p);
+        *p = t;
+        return true;
+}
index d8d695ea79541a795860fc5862bb7187e8cc238c..50025a34a83030d4c85419f4700adb7a7f86748e 100644 (file)
@@ -55,6 +55,8 @@ static inline void* xmemdup(const void *p, size_t l) {
 
 #define xnew(type, n) ((type *) xmalloc_multiply((n), sizeof(type)))
 
+bool free_and_xstrdup16(char16_t **p, const char16_t *s);
+
 typedef struct {
         EFI_PHYSICAL_ADDRESS addr;
         size_t n_pages;