xref: /haiku/headers/os/support/SupportDefs.h (revision ea021b94d81dfcaf8ecb7439a11af3a116fc0247)
1 /*
2  * Copyright 2004-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author:
6  *		Erik Jaesler (erik@cgsoftware.com)
7  */
8 #ifndef _SUPPORT_DEFS_H
9 #define _SUPPORT_DEFS_H
10 
11 
12 #include <BeBuild.h>
13 #include <Errors.h>
14 
15 #include <inttypes.h>
16 #include <sys/types.h>
17 
18 
19 /* fixed-size integer types */
20 typedef	__haiku_int8			int8;
21 typedef __haiku_uint8			uint8;
22 typedef	__haiku_int16			int16;
23 typedef __haiku_uint16			uint16;
24 typedef	__haiku_int32			int32;
25 typedef __haiku_uint32			uint32;
26 typedef	__haiku_int64			int64;
27 typedef __haiku_uint64			uint64;
28 
29 /* shorthand types */
30 typedef volatile int8   		vint8;
31 typedef volatile uint8			vuint8;
32 typedef volatile int16			vint16;
33 typedef volatile uint16			vuint16;
34 typedef volatile int32			vint32;
35 typedef volatile uint32			vuint32;
36 typedef volatile int64			vint64;
37 typedef volatile uint64			vuint64;
38 
39 typedef volatile long			vlong;
40 typedef volatile int			vint;
41 typedef volatile short			vshort;
42 typedef volatile char			vchar;
43 
44 typedef volatile unsigned long	vulong;
45 typedef volatile unsigned int	vuint;
46 typedef volatile unsigned short	vushort;
47 typedef volatile unsigned char	vuchar;
48 
49 typedef unsigned char			uchar;
50 typedef unsigned short          unichar;
51 
52 /* descriptive types */
53 typedef int32					status_t;
54 typedef int64					bigtime_t;
55 typedef int64					nanotime_t;
56 typedef uint32					type_code;
57 typedef uint32					perform_code;
58 
59 typedef __haiku_phys_addr_t		phys_addr_t;
60 typedef phys_addr_t				phys_size_t;
61 
62 
63 /* printf()/scanf() format strings for [u]int* types */
64 #define B_PRId8			"d"
65 #define B_PRIi8			"i"
66 #define B_PRId16		"d"
67 #define B_PRIi16		"i"
68 #define B_PRId32		__HAIKU_PRI_PREFIX_32 "d"
69 #define B_PRIi32		__HAIKU_PRI_PREFIX_32 "i"
70 #define B_PRId64		__HAIKU_PRI_PREFIX_64 "d"
71 #define B_PRIi64		__HAIKU_PRI_PREFIX_64 "i"
72 #define B_PRIu8			"u"
73 #define B_PRIo8			"o"
74 #define B_PRIx8			"x"
75 #define B_PRIX8			"X"
76 #define B_PRIu16		"u"
77 #define B_PRIo16		"o"
78 #define B_PRIx16		"x"
79 #define B_PRIX16		"X"
80 #define B_PRIu32		__HAIKU_PRI_PREFIX_32 "u"
81 #define B_PRIo32		__HAIKU_PRI_PREFIX_32 "o"
82 #define B_PRIx32		__HAIKU_PRI_PREFIX_32 "x"
83 #define B_PRIX32		__HAIKU_PRI_PREFIX_32 "X"
84 #define B_PRIu64		__HAIKU_PRI_PREFIX_64 "u"
85 #define B_PRIo64		__HAIKU_PRI_PREFIX_64 "o"
86 #define B_PRIx64		__HAIKU_PRI_PREFIX_64 "x"
87 #define B_PRIX64		__HAIKU_PRI_PREFIX_64 "X"
88 
89 #define B_SCNd8 		"hhd"
90 #define B_SCNi8 		"hhi"
91 #define B_SCNd16		"hd"
92 #define B_SCNi16	 	"hi"
93 #define B_SCNd32 		__HAIKU_PRI_PREFIX_32 "d"
94 #define B_SCNi32	 	__HAIKU_PRI_PREFIX_32 "i"
95 #define B_SCNd64		__HAIKU_PRI_PREFIX_64 "d"
96 #define B_SCNi64 		__HAIKU_PRI_PREFIX_64 "i"
97 #define B_SCNu8 		"hhu"
98 #define B_SCNo8 		"hho"
99 #define B_SCNx8 		"hhx"
100 #define B_SCNu16		"hu"
101 #define B_SCNo16		"ho"
102 #define B_SCNx16		"hx"
103 #define B_SCNu32 		__HAIKU_PRI_PREFIX_32 "u"
104 #define B_SCNo32 		__HAIKU_PRI_PREFIX_32 "o"
105 #define B_SCNx32 		__HAIKU_PRI_PREFIX_32 "x"
106 #define B_SCNu64		__HAIKU_PRI_PREFIX_64 "u"
107 #define B_SCNo64		__HAIKU_PRI_PREFIX_64 "o"
108 #define B_SCNx64		__HAIKU_PRI_PREFIX_64 "x"
109 
110 /* printf() format strings for some standard types */
111 /* size_t */
112 #define B_PRIuSIZE		__HAIKU_PRI_PREFIX_ADDR "u"
113 #define B_PRIoSIZE		__HAIKU_PRI_PREFIX_ADDR "o"
114 #define B_PRIxSIZE		__HAIKU_PRI_PREFIX_ADDR "x"
115 #define B_PRIXSIZE		__HAIKU_PRI_PREFIX_ADDR "X"
116 /* ssize_t */
117 #define B_PRIdSSIZE		__HAIKU_PRI_PREFIX_ADDR "d"
118 #define B_PRIiSSIZE		__HAIKU_PRI_PREFIX_ADDR "i"
119 /* addr_t */
120 #define B_PRIuADDR		__HAIKU_PRI_PREFIX_ADDR "u"
121 #define B_PRIoADDR		__HAIKU_PRI_PREFIX_ADDR "o"
122 #define B_PRIxADDR		__HAIKU_PRI_PREFIX_ADDR "x"
123 #define B_PRIXADDR		__HAIKU_PRI_PREFIX_ADDR "X"
124 /* phys_addr_t */
125 #define B_PRIuPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "u"
126 #define B_PRIoPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "o"
127 #define B_PRIxPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "x"
128 #define B_PRIXPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "X"
129 /* off_t */
130 #define B_PRIdOFF		B_PRId64
131 #define B_PRIiOFF		B_PRIi64
132 /* dev_t */
133 #define B_PRIdDEV		B_PRId32
134 #define B_PRIiDEV		B_PRIi32
135 /* ino_t */
136 #define B_PRIdINO		B_PRId64
137 #define B_PRIiINO		B_PRIi64
138 /* time_t */
139 #define B_PRIdTIME		B_PRId32
140 #define B_PRIiTIME		B_PRIi32
141 
142 
143 /* Empty string ("") */
144 #ifdef __cplusplus
145 extern const char *B_EMPTY_STRING;
146 #endif
147 
148 
149 /* min and max comparisons */
150 #ifndef __cplusplus
151 #	ifndef min
152 #		define min(a,b) ((a)>(b)?(b):(a))
153 #	endif
154 #	ifndef max
155 #		define max(a,b) ((a)>(b)?(a):(b))
156 #	endif
157 #endif
158 
159 /* min() and max() are functions in C++ */
160 #define min_c(a,b) ((a)>(b)?(b):(a))
161 #define max_c(a,b) ((a)>(b)?(a):(b))
162 
163 
164 /* Grandfathering */
165 #ifndef __cplusplus
166 #	include <stdbool.h>
167 #endif
168 
169 #ifndef NULL
170 #	define NULL (0)
171 #endif
172 
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
178 /* Atomic functions; previous value is returned */
179 extern int32	atomic_set(vint32 *value, int32 newValue);
180 extern int32	atomic_test_and_set(vint32 *value, int32 newValue, int32 testAgainst);
181 extern int32	atomic_add(vint32 *value, int32 addValue);
182 extern int32	atomic_and(vint32 *value, int32 andValue);
183 extern int32	atomic_or(vint32 *value, int32 orValue);
184 extern int32	atomic_get(vint32 *value);
185 
186 extern int64	atomic_set64(vint64 *value, int64 newValue);
187 extern int64	atomic_test_and_set64(vint64 *value, int64 newValue, int64 testAgainst);
188 extern int64	atomic_add64(vint64 *value, int64 addValue);
189 extern int64	atomic_and64(vint64 *value, int64 andValue);
190 extern int64	atomic_or64(vint64 *value, int64 orValue);
191 extern int64	atomic_get64(vint64 *value);
192 
193 /* Other stuff */
194 extern void*	get_stack_frame(void);
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 /* Obsolete or discouraged API */
201 
202 /* use 'true' and 'false' */
203 #ifndef FALSE
204 #	define FALSE	0
205 #endif
206 #ifndef TRUE
207 #	define TRUE		1
208 #endif
209 
210 
211 /* Use the built-in atomic functions, if requested and available. */
212 
213 #if defined(B_USE_BUILTIN_ATOMIC_FUNCTIONS) && __GNUC__ >= 4
214 
215 #define atomic_test_and_set(valuePointer, newValue, testAgainst)	\
216 	__sync_val_compare_and_swap(valuePointer, testAgainst, newValue)
217 #define atomic_add(valuePointer, addValue)	\
218 	__sync_fetch_and_add(valuePointer, addValue)
219 #define atomic_and(valuePointer, andValue)	\
220 	__sync_fetch_and_and(valuePointer, andValue)
221 #define atomic_or(valuePointer, orValue)	\
222 	__sync_fetch_and_or(valuePointer, orValue)
223 #define atomic_get(valuePointer)	\
224 	__sync_fetch_and_or(valuePointer, 0)
225 	// No equivalent to atomic_get(). We simulate it via atomic or. On most
226 	// (all?) 32+ bit architectures aligned 32 bit reads will be atomic anyway,
227 	// though.
228 
229 // Note: No equivalent for atomic_set(). It could be simulated by a
230 // get + atomic test and set loop, but calling the atomic_set() implementation
231 // might be faster.
232 
233 #endif	// B_USE_BUILTIN_ATOMIC_FUNCTIONS && __GNUC__ >= 4
234 
235 
236 #endif	/* _SUPPORT_DEFS_H */
237