]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: move/redo some macros
authorDavid Lamparter <equinox@opensourcerouting.org>
Wed, 30 Jan 2019 22:18:06 +0000 (23:18 +0100)
committerDavid Lamparter <equinox@diac24.net>
Thu, 18 Apr 2019 10:44:29 +0000 (12:44 +0200)
Signed-off-by: David Lamparter <equinox@diac24.net>
lib/compiler.h
lib/memory.h
lib/zebra.h

index cb4f7531ecdec7b6335efe148cd13ef51c0acb23..02bdbd6afd2402383801fca2b1b7f61ca6a93593 100644 (file)
@@ -69,6 +69,10 @@ extern "C" {
 #define _FALLTHROUGH
 #endif
 
+/* for helper functions defined inside macros */
+#define macro_inline   static inline __attribute__((unused))
+#define macro_pure     static inline __attribute__((unused, pure))
+
 /*
  * for warnings on macros, put in the macro content like this:
  *   #define MACRO BLA CPP_WARN("MACRO has been deprecated")
@@ -92,6 +96,80 @@ extern "C" {
 #define CPP_NOTICE(text)
 #endif
 
+/* MAX / MIN are not commonly defined, but useful */
+/* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
+#ifdef MAX
+#undef MAX
+#endif
+#define MAX(a, b)                                                              \
+       ({                                                                     \
+               typeof(a) _max_a = (a);                                        \
+               typeof(b) _max_b = (b);                                        \
+               _max_a > _max_b ? _max_a : _max_b;                             \
+       })
+#ifdef MIN
+#undef MIN
+#endif
+#define MIN(a, b)                                                              \
+       ({                                                                     \
+               typeof(a) _min_a = (a);                                        \
+               typeof(b) _min_b = (b);                                        \
+               _min_a < _min_b ? _min_a : _min_b;                             \
+       })
+
+#ifndef offsetof
+#ifdef __compiler_offsetof
+#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
+#else
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+#endif
+
+/* this variant of container_of() retains 'const' on pointers without needing
+ * to be told to do so.  The following will all work without warning:
+ *
+ * struct member *p;
+ * const struct member *cp;
+ *
+ * const struct cont *x = container_of(cp, struct cont, member);
+ * const struct cont *x = container_of(cp, const struct cont, member);
+ * const struct cont *x = container_of(p,  struct cont, member);
+ * const struct cont *x = container_of(p,  const struct cont, member);
+ * struct cont *x       = container_of(p,  struct cont, member);
+ *
+ * but the following will generate warnings about stripping const:
+ *
+ * struct cont *x       = container_of(cp, struct cont, member);
+ * struct cont *x       = container_of(cp, const struct cont, member);
+ * struct cont *x       = container_of(p,  const struct cont, member);
+ */
+#ifdef container_of
+#undef container_of
+#endif
+#define container_of(ptr, type, member)                                        \
+       (__builtin_choose_expr(                                                \
+               __builtin_types_compatible_p(typeof(&((type *)0)->member),     \
+                       typeof(ptr))                                           \
+                   ||  __builtin_types_compatible_p(void *, typeof(ptr)),     \
+               ({                                                             \
+                       typeof(((type *)0)->member) *__mptr = (void *)(ptr);   \
+                       (type *)((char *)__mptr - offsetof(type, member));     \
+               }),                                                            \
+               ({                                                             \
+                       typeof(((const type *)0)->member) *__mptr = (ptr);     \
+                       (const type *)((const char *)__mptr -                  \
+                                       offsetof(type, member));               \
+               })                                                             \
+       ))
+
+#define container_of_null(ptr, type, member)                                   \
+       ({                                                                     \
+               typeof(ptr) _tmp = (ptr);                                      \
+               _tmp ? container_of(_tmp, type, member) : NULL;                \
+       })
+
+#define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
+
 #ifdef __cplusplus
 }
 #endif
index 91a02b7966c2aad18882a9c7f84a32c47712a117..0002ea3349ad20e68175f3e3350a0e13dd48609a 100644 (file)
@@ -26,8 +26,6 @@
 extern "C" {
 #endif
 
-#define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
-
 #if defined(HAVE_MALLOC_SIZE) && !defined(HAVE_MALLOC_USABLE_SIZE)
 #define malloc_usable_size(x) malloc_size(x)
 #define HAVE_MALLOC_USABLE_SIZE
index b96fb5a206834dbf414ec309dd76fe3a38f88387..7b83fed926992a2a2711156ba997c46ce64bfcca 100644 (file)
@@ -335,43 +335,6 @@ struct in_pktinfo {
 
 #endif /* ndef BYTE_ORDER */
 
-/* MAX / MIN are not commonly defined, but useful */
-/* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
-#ifdef MAX
-#undef MAX
-#endif
-#define MAX(a, b)                                                              \
-       ({                                                                     \
-               typeof(a) _max_a = (a);                                        \
-               typeof(b) _max_b = (b);                                        \
-               _max_a > _max_b ? _max_a : _max_b;                             \
-       })
-#ifdef MIN
-#undef MIN
-#endif
-#define MIN(a, b)                                                              \
-       ({                                                                     \
-               typeof(a) _min_a = (a);                                        \
-               typeof(b) _min_b = (b);                                        \
-               _min_a < _min_b ? _min_a : _min_b;                             \
-       })
-
-#ifndef offsetof
-#ifdef __compiler_offsetof
-#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
-#else
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
-#endif
-
-#ifndef container_of
-#define container_of(ptr, type, member)                                        \
-       ({                                                                     \
-               const typeof(((type *)0)->member) *__mptr = (ptr);             \
-               (type *)((char *)__mptr - offsetof(type, member));             \
-       })
-#endif
-
 #define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0]))
 
 /* For old definition. */