chiark / gitweb /
shared/capability: go frugal on space for caps
[elogind.git] / src / shared / util.h
index 179c9615f94fa5f98f3a19fb98803a28b4d5d554..410ce65f14a9d52d20a42bfe701f9f7c1129f6d0 100644 (file)
 #include <sys/inotify.h>
 
 #if SIZEOF_PID_T == 4
-#  define PID_FMT "%" PRIi32
+#  define PID_PRI PRIi32
 #elif SIZEOF_PID_T == 2
-#  define PID_FMT "%" PRIi16
+#  define PID_PRI PRIi16
 #else
 #  error Unknown pid_t size
 #endif
+#define PID_FMT "%" PID_PRI
 
 #if SIZEOF_UID_T == 4
 #  define UID_FMT "%" PRIu32
@@ -347,12 +348,14 @@ static inline uint32_t random_u32(void) {
 }
 
 /* For basic lookup tables with strictly enumerated entries */
-#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                   \
+#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
         scope const char *name##_to_string(type i) {                    \
                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
                         return NULL;                                    \
                 return name##_table[i];                                 \
-        }                                                               \
+        }
+
+#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
         scope type name##_from_string(const char *s) {                  \
                 type i;                                                 \
                 if (!s)                                                 \
@@ -362,11 +365,17 @@ static inline uint32_t random_u32(void) {
                             streq(name##_table[i], s))                  \
                                 return i;                               \
                 return (type) -1;                                       \
-        }                                                               \
+        }
+
+#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                    \
+        _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
+        _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
         struct __useless_struct_to_allow_trailing_semicolon__
 
-#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
-#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
+#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
 
 /* For string conversions where numbers are also acceptable */
 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max)         \
@@ -472,6 +481,8 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus);
 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
 
+#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
+
 int fd_columns(int fd);
 unsigned columns(void);
 int fd_lines(int fd);
@@ -849,7 +860,7 @@ static inline unsigned u32ctz(uint32_t n) {
 #endif
 }
 
-static inline int log2i(int x) {
+static inline unsigned log2i(int x) {
         assert(x > 0);
 
         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
@@ -861,6 +872,16 @@ static inline unsigned log2u(unsigned x) {
         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
 }
 
+static inline unsigned log2u64(uint64_t x) {
+        assert(x > 0);
+
+#if __SIZEOF_LONG_LONG__ == 8
+        return 64 - __builtin_clzll(x) - 1;
+#else
+#error "Wut?"
+#endif
+}
+
 static inline unsigned log2u_round_up(unsigned x) {
         assert(x > 0);