1 #ifndef __XEN_TYPESAFE_H__
2 #define __XEN_TYPESAFE_H__
3 
4 /*
5  * Compiler games to gain type safety between different logical integers.
6  *
7  * TYPE_SAFE($TYPE, $FOO) declares:
8  *  * $FOO_t   which encapsulates $TYPE
9  *  * _$FOO()  which boxes a $TYPE as a $FOO_t
10  *  * $FOO_x() which unboxes a $FOO_t to $TYPE
11  *
12  * This makes a $FOO_t and a $BAR_t incompatible even when the box the same
13  * $TYPE.
14  *
15  * It does have some performance cost because the types now have a different
16  * storage attribute, so type safety is only enforced in a debug build.
17  * Non-debug builds degrade to a simple typedef and noops for the functions.
18  */
19 
20 #ifndef NDEBUG
21 
22 #define TYPE_SAFE(_type, _name)                                         \
23     typedef struct { _type _name; } _name##_t;                          \
24     static inline _name##_t _##_name(_type n) { return (_name##_t) { n }; } \
25     static inline _type _name##_x(_name##_t n) { return n._name; }
26 
27 #else
28 
29 #define TYPE_SAFE(_type, _name)                                         \
30     typedef _type _name##_t;                                            \
31     static inline _name##_t _##_name(_type n) { return n; }             \
32     static inline _type _name##_x(_name##_t n) { return n; }
33 
34 #endif
35 
36 #endif /* __XEN_TYPESAFE_H__ */
37 
38 /*
39  * Local variables:
40  * mode: C
41  * c-file-style: "BSD"
42  * c-basic-offset: 4
43  * tab-width: 4
44  * indent-tabs-mode: nil
45  * End:
46  */
47