]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
iovec-util: make "struct iovec" and some helpers also available in EFI mode
authorLennart Poettering <lennart@poettering.net>
Wed, 26 Jun 2024 08:17:26 +0000 (10:17 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 26 Jun 2024 14:01:35 +0000 (16:01 +0200)
The construct is a POSIX invention, but it's just so useful, let's also
define it in EFI mode, so that we can use similar constructs in EFI mode
and userspace.

src/basic/iovec-util.h
src/fundamental/iovec-util-fundamental.h [new file with mode: 0644]
src/fundamental/meson.build

index a499fef9ec1111950d538234141591b4df1214bc..b92257cb0df1df8d2bf4f9c4f970fe65fbb9a399 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/uio.h>
 
 #include "alloc-util.h"
+#include "iovec-util-fundamental.h"
 #include "macro.h"
 
 extern const struct iovec iovec_nul_byte; /* Points to a single NUL byte */
@@ -15,13 +16,6 @@ size_t iovec_total_size(const struct iovec *iovec, size_t n);
 
 bool iovec_increment(struct iovec *iovec, size_t n, size_t k);
 
-/* This accepts both const and non-const pointers */
-#define IOVEC_MAKE(base, len)                                           \
-        (struct iovec) {                                                \
-                .iov_base = (void*) (base),                             \
-                .iov_len = (len),                                       \
-        }
-
 static inline struct iovec* iovec_make_string(struct iovec *iovec, const char *s) {
         assert(iovec);
         /* We don't use strlen_ptr() here, because we don't want to include string-util.h for now */
@@ -38,14 +32,6 @@ static inline struct iovec* iovec_make_string(struct iovec *iovec, const char *s
                 .iov_len = STRLEN(s),           \
         }
 
-static inline void iovec_done(struct iovec *iovec) {
-        /* A _cleanup_() helper that frees the iov_base in the iovec */
-        assert(iovec);
-
-        iovec->iov_base = mfree(iovec->iov_base);
-        iovec->iov_len = 0;
-}
-
 static inline void iovec_done_erase(struct iovec *iovec) {
         assert(iovec);
 
@@ -53,16 +39,6 @@ static inline void iovec_done_erase(struct iovec *iovec) {
         iovec->iov_len = 0;
 }
 
-static inline bool iovec_is_set(const struct iovec *iovec) {
-        /* Checks if the iovec points to a non-empty chunk of memory */
-        return iovec && iovec->iov_len > 0 && iovec->iov_base;
-}
-
-static inline bool iovec_is_valid(const struct iovec *iovec) {
-        /* Checks if the iovec is either NULL, empty or points to a valid bit of memory */
-        return !iovec || (iovec->iov_base || iovec->iov_len == 0);
-}
-
 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value);
 
diff --git a/src/fundamental/iovec-util-fundamental.h b/src/fundamental/iovec-util-fundamental.h
new file mode 100644 (file)
index 0000000..68d5bf4
--- /dev/null
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#if SD_BOOT
+/* struct iovec is a POSIX userspace construct. Let's introduce it also in EFI mode, it's just so useful */
+struct iovec {
+        void *iov_base;
+        size_t iov_len;
+};
+
+static inline void free(void *p);
+#endif
+
+/* This accepts both const and non-const pointers */
+#define IOVEC_MAKE(base, len)                                           \
+        (struct iovec) {                                                \
+                .iov_base = (void*) (base),                             \
+                .iov_len = (len),                                       \
+        }
+
+static inline void iovec_done(struct iovec *iovec) {
+        /* A _cleanup_() helper that frees the iov_base in the iovec */
+        assert(iovec);
+
+        iovec->iov_base = mfree(iovec->iov_base);
+        iovec->iov_len = 0;
+}
+
+static inline bool iovec_is_set(const struct iovec *iovec) {
+        /* Checks if the iovec points to a non-empty chunk of memory */
+        return iovec && iovec->iov_len > 0 && iovec->iov_base;
+}
+
+static inline bool iovec_is_valid(const struct iovec *iovec) {
+        /* Checks if the iovec is either NULL, empty or points to a valid bit of memory */
+        return !iovec || (iovec->iov_base || iovec->iov_len == 0);
+}
index f5f57ac3cbf4f50906259317a32a57f7ea8ce9c3..3e9866ef703f1c84fa09d1654b73e6d9a89db88f 100644 (file)
@@ -5,6 +5,7 @@ fundamental_include = include_directories('.')
 fundamental_sources = files(
         'bootspec-fundamental.c',
         'efivars-fundamental.c',
+        'iovec-util-fundamental.h',
         'sha256-fundamental.c',
         'string-util-fundamental.c',
         'uki.c',