summaryrefslogtreecommitdiff
path: root/src/shared/macro.h
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-04-02 17:33:19 +0200
committerLennart Poettering <lennart@poettering.net>2013-04-02 17:47:59 +0200
commitfa70beaabc56762fdf77e675c3e09bb638d89938 (patch)
tree5363e082e2549e76e679e1532aa1cdc089ceaa6c /src/shared/macro.h
parent5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0 (diff)
macro: add macro for precisely determining length of decimal string formatting of a numeric type
Diffstat (limited to 'src/shared/macro.h')
-rw-r--r--src/shared/macro.h18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 4e5d0f4f2f..f884bf653f 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -67,9 +67,11 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
* @member: the name of the member within the struct.
*
*/
-#define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
+#define container_of(ptr, type, member) \
+ __extension__ ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type,member) ); \
+ })
#undef MAX
#define MAX(a,b) \
@@ -255,4 +257,14 @@ do { \
} \
} while(false)
+/* Returns the number of chars needed to format variables of the
+ * specified type as a decimal string. Adds in extra space for a
+ * negative '-' prefix. */
+
+#define DECIMAL_STR_MAX(type) \
+ (1+(sizeof(type) <= 1 ? 3 : \
+ sizeof(type) <= 2 ? 5 : \
+ sizeof(type) <= 4 ? 10 : \
+ sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
+
#include "log.h"