xref: /kvm-unit-tests/lib/libcflat.h (revision b006d7eb9c64ed1046041c4eb3c4077be11d8a3d)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright IBM Corp. 2008
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19 
20 #ifndef __LIBCFLAT_H
21 #define __LIBCFLAT_H
22 
23 #include <stdarg.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 #define __unused __attribute__((__unused__))
29 
30 #define xstr(s) xxstr(s)
31 #define xxstr(s) #s
32 
33 #define __ALIGN_MASK(x, mask)	(((x) + (mask)) & ~(mask))
34 #define __ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a) - 1)
35 #define ALIGN(x, a)		__ALIGN((x), (a))
36 
37 typedef uint8_t		u8;
38 typedef int8_t		s8;
39 typedef uint16_t	u16;
40 typedef int16_t		s16;
41 typedef uint32_t	u32;
42 typedef int32_t		s32;
43 typedef uint64_t	u64;
44 typedef int64_t		s64;
45 typedef unsigned long	ulong;
46 
47 typedef _Bool		bool;
48 #define false 0
49 #define true  1
50 
51 #if __SIZEOF_LONG__ == 8
52 #  define __PRI64_PREFIX	"l"
53 #  define __PRIPTR_PREFIX	"l"
54 #else
55 #  define __PRI64_PREFIX	"ll"
56 #  define __PRIPTR_PREFIX
57 #endif
58 #define PRId64  __PRI64_PREFIX	"d"
59 #define PRIu64  __PRI64_PREFIX	"u"
60 #define PRIx64  __PRI64_PREFIX	"x"
61 #define PRIxPTR __PRIPTR_PREFIX	"x"
62 
63 extern void puts(const char *s);
64 extern void exit(int code);
65 extern void abort(void);
66 extern long atol(const char *ptr);
67 
68 extern int printf(const char *fmt, ...)
69 					__attribute__((format(printf, 1, 2)));
70 extern int snprintf(char *buf, int size, const char *fmt, ...)
71 					__attribute__((format(printf, 3, 4)));
72 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
73 					__attribute__((format(printf, 3, 0)));
74 extern int vprintf(const char *fmt, va_list va)
75 					__attribute__((format(printf, 1, 0)));
76 
77 extern void report_prefix_push(const char *prefix);
78 extern void report_prefix_pop(void);
79 extern void report(const char *msg_fmt, bool pass, ...);
80 extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
81 extern void report_abort(const char *msg_fmt, ...);
82 extern int report_summary(void);
83 
84 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
85 
86 #define container_of(ptr, type, member) ({				\
87 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
88 	(type *)( (char *)__mptr - offsetof(type,member) );})
89 
90 #define assert(cond)							\
91 do {									\
92 	if (!(cond))							\
93 		printf("%s:%d: assert failed: %s\n",			\
94 		       __FILE__, __LINE__, #cond),			\
95 		abort();						\
96 } while (0)
97 
98 #endif
99