diff options
Diffstat (limited to 'shared')
| -rw-r--r-- | shared/Makefile.am | 1 | ||||
| -rw-r--r-- | shared/Makefile.in | 1 | ||||
| -rw-r--r-- | shared/nm-utils/nm-macros-internal.h | 63 | ||||
| -rw-r--r-- | shared/nm-utils/unaligned.h | 129 | ||||
| -rw-r--r-- | shared/nm-version-macros.h | 7 | ||||
| -rw-r--r-- | shared/nm-version-macros.h.in | 5 |
6 files changed, 193 insertions, 13 deletions
diff --git a/shared/Makefile.am b/shared/Makefile.am index adf101bf..b0582825 100644 --- a/shared/Makefile.am +++ b/shared/Makefile.am @@ -5,6 +5,7 @@ EXTRA_DIST = \ nm-utils/nm-shared-utils.c \ nm-utils/nm-shared-utils.h \ nm-utils/nm-test-utils.h \ + nm-utils/unaligned.h \ nm-utils/nm-vpn-editor-plugin-call.h \ nm-utils/nm-vpn-plugin-macros.h \ nm-utils/nm-vpn-plugin-utils.c \ diff --git a/shared/Makefile.in b/shared/Makefile.in index c98c4994..7436ef84 100644 --- a/shared/Makefile.in +++ b/shared/Makefile.in @@ -411,6 +411,7 @@ EXTRA_DIST = \ nm-utils/nm-shared-utils.c \ nm-utils/nm-shared-utils.h \ nm-utils/nm-test-utils.h \ + nm-utils/unaligned.h \ nm-utils/nm-vpn-editor-plugin-call.h \ nm-utils/nm-vpn-plugin-macros.h \ nm-utils/nm-vpn-plugin-utils.c \ diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 9e9f5d8f..de90e3f5 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -22,7 +22,9 @@ #ifndef __NM_MACROS_INTERNAL_H__ #define __NM_MACROS_INTERNAL_H__ +#include <stdio.h> #include <stdlib.h> +#include <errno.h> #include "nm-glib.h" @@ -59,7 +61,31 @@ _nm_auto_free_gstring_impl (GString **str) } #define nm_auto_free_gstring nm_auto(_nm_auto_free_gstring_impl) -/********************************************************/ +static inline void +_nm_auto_close_impl (int *pfd) +{ + if (*pfd >= 0) { + int errsv = errno; + + (void) close (*pfd); + errno = errsv; + } +} +#define nm_auto_close nm_auto(_nm_auto_close_impl) + +static inline void +_nm_auto_fclose_impl (FILE **pfd) +{ + if (*pfd) { + int errsv = errno; + + (void) fclose (*pfd); + errno = errsv; + } +} +#define nm_auto_fclose nm_auto(_nm_auto_fclose_impl) + +/*****************************************************************************/ /* http://stackoverflow.com/a/11172679 */ #define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway) @@ -260,13 +286,11 @@ _NM_IN_STRSET_streq (const char *x, const char *s) /*****************************************************************************/ -#define nm_str_not_empty(str) \ - ({ \ - /* implemented as macro to preserve constness */ \ - typeof (str) __str = (str); \ - _nm_unused const char *__str_type_check = __str; \ - ((__str && __str[0]) ? __str : ((char *) NULL)); \ - }) +static inline const char * +nm_str_not_empty (const char *str) +{ + return str && str[0] ? str : NULL; +} static inline char * nm_strdup_not_empty (const char *str) @@ -302,9 +326,11 @@ nm_strdup_not_empty (const char *str) #if NM_MORE_ASSERTS #define nm_assert(cond) G_STMT_START { g_assert (cond); } G_STMT_END +#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } else { g_assert (FALSE && (cond)); } } G_STMT_END #define nm_assert_not_reached() G_STMT_START { g_assert_not_reached (); } G_STMT_END #else #define nm_assert(cond) G_STMT_START { if (FALSE) { if (cond) { } } } G_STMT_END +#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } } G_STMT_END #define nm_assert_not_reached() G_STMT_START { ; } G_STMT_END #endif @@ -368,6 +394,27 @@ nm_g_object_unref (gpointer obj) g_object_unref (obj); } +/* basically, replaces + * g_clear_pointer (&location, g_free) + * with + * nm_clear_g_free (&location) + * + * Another advantage is that by using a macro and typeof(), it is more + * typesafe and gives you for example a compiler warning when pp is a const + * pointer or points to a const-pointer. + */ +#define nm_clear_g_free(pp) \ + ({ \ + typeof (*(pp)) *_pp = (pp); \ + typeof (**_pp) *_p = *_pp; \ + \ + if (_p) { \ + *_pp = NULL; \ + g_free (_p); \ + } \ + !!_p; \ + }) + static inline gboolean nm_clear_g_source (guint *id) { diff --git a/shared/nm-utils/unaligned.h b/shared/nm-utils/unaligned.h new file mode 100644 index 00000000..7c847a3c --- /dev/null +++ b/shared/nm-utils/unaligned.h @@ -0,0 +1,129 @@ +#pragma once + +/*** + This file is part of systemd. + + Copyright 2014 Tom Gundersen + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <endian.h> +#include <stdint.h> + +/* BE */ + +static inline uint16_t unaligned_read_be16(const void *_u) { + const uint8_t *u = _u; + + return (((uint16_t) u[0]) << 8) | + ((uint16_t) u[1]); +} + +static inline uint32_t unaligned_read_be32(const void *_u) { + const uint8_t *u = _u; + + return (((uint32_t) unaligned_read_be16(u)) << 16) | + ((uint32_t) unaligned_read_be16(u + 2)); +} + +static inline uint64_t unaligned_read_be64(const void *_u) { + const uint8_t *u = _u; + + return (((uint64_t) unaligned_read_be32(u)) << 32) | + ((uint64_t) unaligned_read_be32(u + 4)); +} + +static inline void unaligned_write_be16(void *_u, uint16_t a) { + uint8_t *u = _u; + + u[0] = (uint8_t) (a >> 8); + u[1] = (uint8_t) a; +} + +static inline void unaligned_write_be32(void *_u, uint32_t a) { + uint8_t *u = _u; + + unaligned_write_be16(u, (uint16_t) (a >> 16)); + unaligned_write_be16(u + 2, (uint16_t) a); +} + +static inline void unaligned_write_be64(void *_u, uint64_t a) { + uint8_t *u = _u; + + unaligned_write_be32(u, (uint32_t) (a >> 32)); + unaligned_write_be32(u + 4, (uint32_t) a); +} + +/* LE */ + +static inline uint16_t unaligned_read_le16(const void *_u) { + const uint8_t *u = _u; + + return (((uint16_t) u[1]) << 8) | + ((uint16_t) u[0]); +} + +static inline uint32_t unaligned_read_le32(const void *_u) { + const uint8_t *u = _u; + + return (((uint32_t) unaligned_read_le16(u + 2)) << 16) | + ((uint32_t) unaligned_read_le16(u)); +} + +static inline uint64_t unaligned_read_le64(const void *_u) { + const uint8_t *u = _u; + + return (((uint64_t) unaligned_read_le32(u + 4)) << 32) | + ((uint64_t) unaligned_read_le32(u)); +} + +static inline void unaligned_write_le16(void *_u, uint16_t a) { + uint8_t *u = _u; + + u[0] = (uint8_t) a; + u[1] = (uint8_t) (a >> 8); +} + +static inline void unaligned_write_le32(void *_u, uint32_t a) { + uint8_t *u = _u; + + unaligned_write_le16(u, (uint16_t) a); + unaligned_write_le16(u + 2, (uint16_t) (a >> 16)); +} + +static inline void unaligned_write_le64(void *_u, uint64_t a) { + uint8_t *u = _u; + + unaligned_write_le32(u, (uint32_t) a); + unaligned_write_le32(u + 4, (uint32_t) (a >> 32)); +} + +#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 diff --git a/shared/nm-version-macros.h b/shared/nm-version-macros.h index 14109559..3bf3befa 100644 --- a/shared/nm-version-macros.h +++ b/shared/nm-version-macros.h @@ -45,7 +45,7 @@ * Evaluates to the micro version number of NetworkManager which this source * compiled against. */ -#define NM_MICRO_VERSION (2) +#define NM_MICRO_VERSION (4) /** * NM_CHECK_VERSION: @@ -71,8 +71,9 @@ #define NM_VERSION_1_4 (NM_ENCODE_VERSION (1, 4, 0)) #define NM_VERSION_1_4_2 (NM_ENCODE_VERSION (1, 4, 2)) #define NM_VERSION_1_4_4 (NM_ENCODE_VERSION (1, 4, 4)) +#define NM_VERSION_1_4_6 (NM_ENCODE_VERSION (1, 4, 6)) -#define NM_VERSION_CUR_STABLE NM_VERSION_1_4_2 -#define NM_VERSION_NEXT_STABLE NM_VERSION_1_4_4 +#define NM_VERSION_CUR_STABLE NM_VERSION_1_4_4 +#define NM_VERSION_NEXT_STABLE NM_VERSION_1_4_6 #endif /* __NM_VERSION_MACROS_H__ */ diff --git a/shared/nm-version-macros.h.in b/shared/nm-version-macros.h.in index 62fc1c6d..536ea9ea 100644 --- a/shared/nm-version-macros.h.in +++ b/shared/nm-version-macros.h.in @@ -71,8 +71,9 @@ #define NM_VERSION_1_4 (NM_ENCODE_VERSION (1, 4, 0)) #define NM_VERSION_1_4_2 (NM_ENCODE_VERSION (1, 4, 2)) #define NM_VERSION_1_4_4 (NM_ENCODE_VERSION (1, 4, 4)) +#define NM_VERSION_1_4_6 (NM_ENCODE_VERSION (1, 4, 6)) -#define NM_VERSION_CUR_STABLE NM_VERSION_1_4_2 -#define NM_VERSION_NEXT_STABLE NM_VERSION_1_4_4 +#define NM_VERSION_CUR_STABLE NM_VERSION_1_4_4 +#define NM_VERSION_NEXT_STABLE NM_VERSION_1_4_6 #endif /* __NM_VERSION_MACROS_H__ */ |