summary refs log tree commit diff
path: root/src/libnm-std-aux
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnm-std-aux')
-rw-r--r--src/libnm-std-aux/c-list-util.h6
-rw-r--r--src/libnm-std-aux/nm-std-aux.h15
-rw-r--r--src/libnm-std-aux/nm-std-utils.c4
-rw-r--r--src/libnm-std-aux/unaligned-fundamental.h40
-rw-r--r--src/libnm-std-aux/unaligned.h20
5 files changed, 64 insertions, 21 deletions
diff --git a/src/libnm-std-aux/c-list-util.h b/src/libnm-std-aux/c-list-util.h
index 4800a3cc..ae2f07ec 100644
--- a/src/libnm-std-aux/c-list-util.h
+++ b/src/libnm-std-aux/c-list-util.h
@@ -42,6 +42,12 @@ c_list_length_is(const CList *list, unsigned long check_len)
     return n == check_len;
 }
 
+static inline int
+c_list_is_empty_or_single(const CList *list)
+{
+    return !list || (list->next->next == list);
+}
+
 #define c_list_for_each_prev(_iter, _list) \
     for (_iter = (_list)->prev; (_iter) != (_list); _iter = (_iter)->prev)
 
diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h
index a5e5abd3..d310305e 100644
--- a/src/libnm-std-aux/nm-std-aux.h
+++ b/src/libnm-std-aux/nm-std-aux.h
@@ -327,9 +327,9 @@ typedef uint64_t _nm_bitwise nm_be64_t;
      *
      * It's useful to check the let the compiler ensure that @value is
      * of a certain type. */
-#define _NM_ENSURE_TYPE(type, value) (_Generic((value), type : (value)))
+#define _NM_ENSURE_TYPE(type, value) (_Generic((value), type: (value)))
 #define _NM_ENSURE_TYPE_CONST(type, value) \
-    (_Generic((value), const type : ((const type)(value)), type : ((const type)(value))))
+    (_Generic((value), const type: ((const type)(value)), type: ((const type)(value))))
 #else
 #define _NM_ENSURE_TYPE(type, value)       (value)
 #define _NM_ENSURE_TYPE_CONST(type, value) ((const type)(value))
@@ -348,7 +348,7 @@ typedef uint64_t _nm_bitwise nm_be64_t;
 
 #if _NM_CC_SUPPORT_GENERIC && (!defined(__clang__) || __clang_major__ > 3)
 #define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) \
-    (_Generic((&(((container *) NULL)->field))[0], type : nm_offsetof(container, field)))
+    (_Generic((&(((container *) NULL)->field))[0], type: nm_offsetof(container, field)))
 #else
 #define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) nm_offsetof(container, field)
 #endif
@@ -1367,6 +1367,12 @@ nm_ptr_to_uintptr(const void *p)
 
 /*****************************************************************************/
 
+/* IFNAMSIZ is both defined in <linux/if.h> and <net/if.h>. In the past, these
+ * headers conflicted, so we cannot simply include either of them in a header-file.*/
+#define NM_IFNAMSIZ 16
+
+/*****************************************************************************/
+
 #define NM_AF_UNSPEC 0  /* AF_UNSPEC */
 #define NM_AF_INET   2  /* AF_INET   */
 #define NM_AF_INET6  10 /* AF_INET6  */
@@ -1419,6 +1425,9 @@ nm_utils_addr_family_to_char(int addr_family)
         (NM_UNIQ_T(_addr_family, uniq) == NM_AF_INET);           \
     })
 
+/* NM_IS_IPv4() is guaranteed to give either 0 or 1! That is an important
+ * guarantee, because we often use that value to index a 2-array (where at
+ * position zero is IPv6 and at position 1 IPv4). */
 #define NM_IS_IPv4(addr_family) _NM_IS_IPv4(NM_UNIQ, addr_family)
 
 static inline int
diff --git a/src/libnm-std-aux/nm-std-utils.c b/src/libnm-std-aux/nm-std-utils.c
index 18692b19..8901378c 100644
--- a/src/libnm-std-aux/nm-std-utils.c
+++ b/src/libnm-std-aux/nm-std-utils.c
@@ -7,9 +7,13 @@
 #include <stdint.h>
 #include <assert.h>
 #include <limits.h>
+#include <net/if.h>
 
 /*****************************************************************************/
 
+NM_STATIC_ASSERT(NM_IFNAMSIZ == IFNAMSIZ);
+
+/*****************************************************************************/
 size_t
 nm_utils_get_next_realloc_size(bool true_realloc, size_t requested)
 {
diff --git a/src/libnm-std-aux/unaligned-fundamental.h b/src/libnm-std-aux/unaligned-fundamental.h
new file mode 100644
index 00000000..a4c810a5
--- /dev/null
+++ b/src/libnm-std-aux/unaligned-fundamental.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <stdint.h>
+
+static inline uint16_t unaligned_read_ne16(const void *_u) {
+        const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
+
+        return u->x;
+}
+
+static inline uint32_t unaligned_read_ne32(const void *_u) {
+        const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
+
+        return u->x;
+}
+
+static inline uint64_t unaligned_read_ne64(const void *_u) {
+        const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
+
+        return u->x;
+}
+
+static inline void unaligned_write_ne16(void *_u, uint16_t a) {
+        struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
+
+        u->x = a;
+}
+
+static inline void unaligned_write_ne32(void *_u, uint32_t a) {
+        struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
+
+        u->x = a;
+}
+
+static inline void unaligned_write_ne64(void *_u, uint64_t a) {
+        struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
+
+        u->x = a;
+}
diff --git a/src/libnm-std-aux/unaligned.h b/src/libnm-std-aux/unaligned.h
index 4100be08..04580cfb 100644
--- a/src/libnm-std-aux/unaligned.h
+++ b/src/libnm-std-aux/unaligned.h
@@ -4,6 +4,8 @@
 #include <endian.h>
 #include <stdint.h>
 
+#include "unaligned-fundamental.h"
+
 /* BE */
 
 static inline uint16_t unaligned_read_be16(const void *_u) {
@@ -79,21 +81,3 @@ static inline void unaligned_write_le64(void *_u, uint64_t a) {
 
         u->x = le64toh(a);
 }
-
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define unaligned_read_ne16 unaligned_read_be16
-#define unaligned_read_ne32 unaligned_read_be32
-#define unaligned_read_ne64 unaligned_read_be64
-
-#define unaligned_write_ne16 unaligned_write_be16
-#define unaligned_write_ne32 unaligned_write_be32
-#define unaligned_write_ne64 unaligned_write_be64
-#else
-#define unaligned_read_ne16 unaligned_read_le16
-#define unaligned_read_ne32 unaligned_read_le32
-#define unaligned_read_ne64 unaligned_read_le64
-
-#define unaligned_write_ne16 unaligned_write_le16
-#define unaligned_write_ne32 unaligned_write_le32
-#define unaligned_write_ne64 unaligned_write_le64
-#endif