SupportDefs.h revision a4cdc607
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 */
20typedef	__haiku_int8			int8;
21typedef __haiku_uint8			uint8;
22typedef	__haiku_int16			int16;
23typedef __haiku_uint16			uint16;
24typedef	__haiku_int32			int32;
25typedef __haiku_uint32			uint32;
26typedef	__haiku_int64			int64;
27typedef __haiku_uint64			uint64;
28
29/* shorthand types */
30typedef volatile int8   		vint8;
31typedef volatile uint8			vuint8;
32typedef volatile int16			vint16;
33typedef volatile uint16			vuint16;
34typedef volatile int32			vint32;
35typedef volatile uint32			vuint32;
36typedef volatile int64			vint64;
37typedef volatile uint64			vuint64;
38
39typedef volatile long			vlong;
40typedef volatile int			vint;
41typedef volatile short			vshort;
42typedef volatile char			vchar;
43
44typedef volatile unsigned long	vulong;
45typedef volatile unsigned int	vuint;
46typedef volatile unsigned short	vushort;
47typedef volatile unsigned char	vuchar;
48
49typedef unsigned char			uchar;
50typedef unsigned short          unichar;
51
52/* descriptive types */
53typedef int32					status_t;
54typedef int64					bigtime_t;
55typedef int64					nanotime_t;
56typedef uint32					type_code;
57typedef uint32					perform_code;
58
59typedef __haiku_phys_addr_t		phys_addr_t;
60typedef phys_addr_t				phys_size_t;
61
62typedef	__haiku_generic_addr_t	generic_addr_t;
63typedef	generic_addr_t			generic_size_t;
64
65
66/* printf()/scanf() format strings for [u]int* types */
67#define B_PRId8			"d"
68#define B_PRIi8			"i"
69#define B_PRId16		"d"
70#define B_PRIi16		"i"
71#define B_PRId32		__HAIKU_PRI_PREFIX_32 "d"
72#define B_PRIi32		__HAIKU_PRI_PREFIX_32 "i"
73#define B_PRId64		__HAIKU_PRI_PREFIX_64 "d"
74#define B_PRIi64		__HAIKU_PRI_PREFIX_64 "i"
75#define B_PRIu8			"u"
76#define B_PRIo8			"o"
77#define B_PRIx8			"x"
78#define B_PRIX8			"X"
79#define B_PRIu16		"u"
80#define B_PRIo16		"o"
81#define B_PRIx16		"x"
82#define B_PRIX16		"X"
83#define B_PRIu32		__HAIKU_PRI_PREFIX_32 "u"
84#define B_PRIo32		__HAIKU_PRI_PREFIX_32 "o"
85#define B_PRIx32		__HAIKU_PRI_PREFIX_32 "x"
86#define B_PRIX32		__HAIKU_PRI_PREFIX_32 "X"
87#define B_PRIu64		__HAIKU_PRI_PREFIX_64 "u"
88#define B_PRIo64		__HAIKU_PRI_PREFIX_64 "o"
89#define B_PRIx64		__HAIKU_PRI_PREFIX_64 "x"
90#define B_PRIX64		__HAIKU_PRI_PREFIX_64 "X"
91
92#define B_SCNd8 		"hhd"
93#define B_SCNi8 		"hhi"
94#define B_SCNd16		"hd"
95#define B_SCNi16	 	"hi"
96#define B_SCNd32 		__HAIKU_PRI_PREFIX_32 "d"
97#define B_SCNi32	 	__HAIKU_PRI_PREFIX_32 "i"
98#define B_SCNd64		__HAIKU_PRI_PREFIX_64 "d"
99#define B_SCNi64 		__HAIKU_PRI_PREFIX_64 "i"
100#define B_SCNu8 		"hhu"
101#define B_SCNo8 		"hho"
102#define B_SCNx8 		"hhx"
103#define B_SCNu16		"hu"
104#define B_SCNo16		"ho"
105#define B_SCNx16		"hx"
106#define B_SCNu32 		__HAIKU_PRI_PREFIX_32 "u"
107#define B_SCNo32 		__HAIKU_PRI_PREFIX_32 "o"
108#define B_SCNx32 		__HAIKU_PRI_PREFIX_32 "x"
109#define B_SCNu64		__HAIKU_PRI_PREFIX_64 "u"
110#define B_SCNo64		__HAIKU_PRI_PREFIX_64 "o"
111#define B_SCNx64		__HAIKU_PRI_PREFIX_64 "x"
112
113/* printf() format strings for some standard types */
114/* size_t */
115#define B_PRIuSIZE		__HAIKU_PRI_PREFIX_ADDR "u"
116#define B_PRIoSIZE		__HAIKU_PRI_PREFIX_ADDR "o"
117#define B_PRIxSIZE		__HAIKU_PRI_PREFIX_ADDR "x"
118#define B_PRIXSIZE		__HAIKU_PRI_PREFIX_ADDR "X"
119/* ssize_t */
120#define B_PRIdSSIZE		__HAIKU_PRI_PREFIX_ADDR "d"
121#define B_PRIiSSIZE		__HAIKU_PRI_PREFIX_ADDR "i"
122/* addr_t */
123#define B_PRIuADDR		__HAIKU_PRI_PREFIX_ADDR "u"
124#define B_PRIoADDR		__HAIKU_PRI_PREFIX_ADDR "o"
125#define B_PRIxADDR		__HAIKU_PRI_PREFIX_ADDR "x"
126#define B_PRIXADDR		__HAIKU_PRI_PREFIX_ADDR "X"
127/* phys_addr_t */
128#define B_PRIuPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "u"
129#define B_PRIoPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "o"
130#define B_PRIxPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "x"
131#define B_PRIXPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "X"
132/* generic_addr_t */
133#define B_PRIuGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "u"
134#define B_PRIoGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "o"
135#define B_PRIxGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "x"
136#define B_PRIXGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "X"
137/* off_t */
138#define B_PRIdOFF		B_PRId64
139#define B_PRIiOFF		B_PRIi64
140#define B_PRIxOFF		B_PRIx64
141/* dev_t */
142#define B_PRIdDEV		B_PRId32
143#define B_PRIiDEV		B_PRIi32
144/* ino_t */
145#define B_PRIdINO		B_PRId64
146#define B_PRIiINO		B_PRIi64
147/* time_t */
148#define B_PRIdTIME		B_PRId32
149#define B_PRIiTIME		B_PRIi32
150/* bigtime_t */
151#define B_PRIdBIGTIME	B_PRId64
152#define B_PRIiBIGTIME	B_PRIi64
153
154
155/* Printed width of a pointer with the %p format (minus 0x prefix). */
156#ifdef B_HAIKU_64_BIT
157#	define B_PRINTF_POINTER_WIDTH	16
158#else
159#	define B_PRINTF_POINTER_WIDTH	8
160#endif
161
162
163/* Empty string ("") */
164#ifdef __cplusplus
165extern const char *B_EMPTY_STRING;
166#endif
167
168
169/* min and max comparisons */
170#ifndef __cplusplus
171#	ifndef min
172#		define min(a,b) ((a)>(b)?(b):(a))
173#	endif
174#	ifndef max
175#		define max(a,b) ((a)>(b)?(a):(b))
176#	endif
177#endif
178
179/* min() and max() are functions in C++ */
180#define min_c(a,b) ((a)>(b)?(b):(a))
181#define max_c(a,b) ((a)>(b)?(a):(b))
182
183
184/* Grandfathering */
185#ifndef __cplusplus
186#	include <stdbool.h>
187#endif
188
189#ifndef NULL
190#	define NULL (0)
191#endif
192
193
194#ifdef __cplusplus
195extern "C" {
196#endif
197
198/* Other stuff */
199extern void*	get_stack_frame(void);
200
201#ifdef __cplusplus
202}
203#endif
204
205/* Obsolete or discouraged API */
206
207/* use 'true' and 'false' */
208#ifndef FALSE
209#	define FALSE	0
210#endif
211#ifndef TRUE
212#	define TRUE		1
213#endif
214
215
216/* Use the built-in atomic functions, if requested and available. */
217
218#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
219
220
221static inline void
222atomic_set(int32* value, int32 newValue)
223{
224	__atomic_store_n(value, newValue, __ATOMIC_RELEASE);
225}
226
227
228static inline int32
229atomic_get_and_set(int32* value, int32 newValue)
230{
231	return __atomic_exchange_n(value, newValue, __ATOMIC_SEQ_CST);
232}
233
234
235static inline int32
236atomic_test_and_set(int32* value, int32 newValue, int32 testAgainst)
237{
238	__atomic_compare_exchange_n(value, &testAgainst, newValue, 1,
239		__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
240	return testAgainst;
241}
242
243
244static inline int32
245atomic_add(int32* value, int32 addValue)
246{
247	return __atomic_fetch_add(value, addValue, __ATOMIC_SEQ_CST);
248}
249
250
251static inline int32
252atomic_and(int32* value, int32 andValue)
253{
254	return __atomic_fetch_and(value, andValue, __ATOMIC_SEQ_CST);
255}
256
257
258static inline int32
259atomic_or(int32* value, int32 orValue)
260{
261	return __atomic_fetch_or(value, orValue, __ATOMIC_SEQ_CST);
262}
263
264
265static inline int32
266atomic_get(int32* value)
267{
268	return __atomic_load_n(value, __ATOMIC_ACQUIRE);
269}
270
271
272static inline void
273atomic_set64(int64* value, int64 newValue)
274{
275	__atomic_store_n(value, newValue, __ATOMIC_RELEASE);
276}
277
278
279static inline int64
280atomic_get_and_set64(int64* value, int64 newValue)
281{
282	return __atomic_exchange_n(value, newValue, __ATOMIC_SEQ_CST);
283}
284
285
286static inline int64
287atomic_test_and_set64(int64* value, int64 newValue, int64 testAgainst)
288{
289	__atomic_compare_exchange_n(value, &testAgainst, newValue, 1,
290		__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
291	return testAgainst;
292}
293
294
295static inline int64
296atomic_add64(int64* value, int64 addValue)
297{
298	return __atomic_fetch_add(value, addValue, __ATOMIC_SEQ_CST);
299}
300
301
302static inline int64
303atomic_and64(int64* value, int64 andValue)
304{
305	return __atomic_fetch_and(value, andValue, __ATOMIC_SEQ_CST);
306}
307
308
309static inline int64
310atomic_or64(int64* value, int64 orValue)
311{
312	return __atomic_fetch_or(value, orValue, __ATOMIC_SEQ_CST);
313}
314
315
316static inline int64
317atomic_get64(int64* value)
318{
319	return __atomic_load_n(value, __ATOMIC_ACQUIRE);
320}
321
322
323#else	// __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
324
325#ifdef __cplusplus
326extern "C" {
327#endif
328
329/* Atomic functions; previous value is returned */
330extern void		atomic_set(int32* value, int32 newValue);
331extern int32	atomic_get_and_set(int32* value, int32 newValue);
332extern int32	atomic_test_and_set(int32 *value, int32 newValue, int32 testAgainst);
333extern int32	atomic_add(int32 *value, int32 addValue);
334extern int32	atomic_and(int32 *value, int32 andValue);
335extern int32	atomic_or(int32 *value, int32 orValue);
336extern int32	atomic_get(int32 *value);
337
338extern void		atomic_set64(int64* value, int64 newValue);
339extern int64	atomic_get_and_set64(int64* value, int64 newValue);
340extern int64	atomic_test_and_set64(int64 *value, int64 newValue, int64 testAgainst);
341extern int64	atomic_add64(int64 *value, int64 addValue);
342extern int64	atomic_and64(int64 *value, int64 andValue);
343extern int64	atomic_or64(int64 *value, int64 orValue);
344extern int64	atomic_get64(int64 *value);
345
346#ifdef __cplusplus
347}
348#endif
349
350#endif
351
352
353#endif	/* _SUPPORT_DEFS_H */
354