SupportDefs.h revision 4a57f843
1087882c2SAxel Dörfler/*
2d34daac8SIngo Weinhold * Copyright 2004-2010, Haiku, Inc. All Rights Reserved.
3087882c2SAxel Dörfler * Distributed under the terms of the MIT License.
4087882c2SAxel Dörfler *
5087882c2SAxel Dörfler * Author:
6087882c2SAxel Dörfler *		Erik Jaesler (erik@cgsoftware.com)
7087882c2SAxel Dörfler */
852a38012Sejakowatz#ifndef _SUPPORT_DEFS_H
952a38012Sejakowatz#define _SUPPORT_DEFS_H
1052a38012Sejakowatz
11087882c2SAxel Dörfler
1252a38012Sejakowatz#include <BeBuild.h>
1330fd5147Shaydentech#include <Errors.h>
1452a38012Sejakowatz
152222d055SIngo Weinhold#include <inttypes.h>
16087882c2SAxel Dörfler#include <sys/types.h>
17087882c2SAxel Dörfler
1852a38012Sejakowatz
192222d055SIngo Weinhold/* fixed-size integer types */
202222d055SIngo Weinholdtypedef	__haiku_int8			int8;
212222d055SIngo Weinholdtypedef __haiku_uint8			uint8;
222222d055SIngo Weinholdtypedef	__haiku_int16			int16;
232222d055SIngo Weinholdtypedef __haiku_uint16			uint16;
242222d055SIngo Weinholdtypedef	__haiku_int32			int32;
252222d055SIngo Weinholdtypedef __haiku_uint32			uint32;
262222d055SIngo Weinholdtypedef	__haiku_int64			int64;
272222d055SIngo Weinholdtypedef __haiku_uint64			uint64;
282222d055SIngo Weinhold
292222d055SIngo Weinhold/* shorthand types */
302222d055SIngo Weinholdtypedef volatile int8   		vint8;
312222d055SIngo Weinholdtypedef volatile uint8			vuint8;
322222d055SIngo Weinholdtypedef volatile int16			vint16;
332222d055SIngo Weinholdtypedef volatile uint16			vuint16;
342222d055SIngo Weinholdtypedef volatile int32			vint32;
352222d055SIngo Weinholdtypedef volatile uint32			vuint32;
362222d055SIngo Weinholdtypedef volatile int64			vint64;
372222d055SIngo Weinholdtypedef volatile uint64			vuint64;
3852a38012Sejakowatz
3952a38012Sejakowatztypedef volatile long			vlong;
4052a38012Sejakowatztypedef volatile int			vint;
4152a38012Sejakowatztypedef volatile short			vshort;
4252a38012Sejakowatztypedef volatile char			vchar;
4352a38012Sejakowatz
4452a38012Sejakowatztypedef volatile unsigned long	vulong;
4552a38012Sejakowatztypedef volatile unsigned int	vuint;
4652a38012Sejakowatztypedef volatile unsigned short	vushort;
4752a38012Sejakowatztypedef volatile unsigned char	vuchar;
4852a38012Sejakowatz
4952a38012Sejakowatztypedef unsigned char			uchar;
5052a38012Sejakowatztypedef unsigned short          unichar;
5152a38012Sejakowatz
522222d055SIngo Weinhold/* descriptive types */
5352a38012Sejakowatztypedef int32					status_t;
5452a38012Sejakowatztypedef int64					bigtime_t;
5534a48c70SIngo Weinholdtypedef int64					nanotime_t;
5652a38012Sejakowatztypedef uint32					type_code;
5752a38012Sejakowatztypedef uint32					perform_code;
5852a38012Sejakowatz
59ea021b94SIngo Weinholdtypedef __haiku_phys_addr_t		phys_addr_t;
60ea021b94SIngo Weinholdtypedef phys_addr_t				phys_size_t;
61ea021b94SIngo Weinhold
624a57f843SIngo Weinholdtypedef	__haiku_generic_addr_t	generic_addr_t;
634a57f843SIngo Weinholdtypedef	generic_addr_t			generic_size_t;
644a57f843SIngo Weinhold
6552a38012Sejakowatz
662222d055SIngo Weinhold/* printf()/scanf() format strings for [u]int* types */
670fde9192SIngo Weinhold#define B_PRId8			"d"
680fde9192SIngo Weinhold#define B_PRIi8			"i"
690fde9192SIngo Weinhold#define B_PRId16		"d"
700fde9192SIngo Weinhold#define B_PRIi16		"i"
710fde9192SIngo Weinhold#define B_PRId32		__HAIKU_PRI_PREFIX_32 "d"
720fde9192SIngo Weinhold#define B_PRIi32		__HAIKU_PRI_PREFIX_32 "i"
730fde9192SIngo Weinhold#define B_PRId64		__HAIKU_PRI_PREFIX_64 "d"
740fde9192SIngo Weinhold#define B_PRIi64		__HAIKU_PRI_PREFIX_64 "i"
750fde9192SIngo Weinhold#define B_PRIu8			"u"
760fde9192SIngo Weinhold#define B_PRIo8			"o"
770fde9192SIngo Weinhold#define B_PRIx8			"x"
780fde9192SIngo Weinhold#define B_PRIX8			"X"
790fde9192SIngo Weinhold#define B_PRIu16		"u"
800fde9192SIngo Weinhold#define B_PRIo16		"o"
810fde9192SIngo Weinhold#define B_PRIx16		"x"
820fde9192SIngo Weinhold#define B_PRIX16		"X"
830fde9192SIngo Weinhold#define B_PRIu32		__HAIKU_PRI_PREFIX_32 "u"
840fde9192SIngo Weinhold#define B_PRIo32		__HAIKU_PRI_PREFIX_32 "o"
850fde9192SIngo Weinhold#define B_PRIx32		__HAIKU_PRI_PREFIX_32 "x"
860fde9192SIngo Weinhold#define B_PRIX32		__HAIKU_PRI_PREFIX_32 "X"
870fde9192SIngo Weinhold#define B_PRIu64		__HAIKU_PRI_PREFIX_64 "u"
880fde9192SIngo Weinhold#define B_PRIo64		__HAIKU_PRI_PREFIX_64 "o"
890fde9192SIngo Weinhold#define B_PRIx64		__HAIKU_PRI_PREFIX_64 "x"
900fde9192SIngo Weinhold#define B_PRIX64		__HAIKU_PRI_PREFIX_64 "X"
912222d055SIngo Weinhold
920fde9192SIngo Weinhold#define B_SCNd8 		"hhd"
930fde9192SIngo Weinhold#define B_SCNi8 		"hhi"
940fde9192SIngo Weinhold#define B_SCNd16		"hd"
950fde9192SIngo Weinhold#define B_SCNi16	 	"hi"
960fde9192SIngo Weinhold#define B_SCNd32 		__HAIKU_PRI_PREFIX_32 "d"
970fde9192SIngo Weinhold#define B_SCNi32	 	__HAIKU_PRI_PREFIX_32 "i"
980fde9192SIngo Weinhold#define B_SCNd64		__HAIKU_PRI_PREFIX_64 "d"
990fde9192SIngo Weinhold#define B_SCNi64 		__HAIKU_PRI_PREFIX_64 "i"
1000fde9192SIngo Weinhold#define B_SCNu8 		"hhu"
1010fde9192SIngo Weinhold#define B_SCNo8 		"hho"
1020fde9192SIngo Weinhold#define B_SCNx8 		"hhx"
1030fde9192SIngo Weinhold#define B_SCNu16		"hu"
1040fde9192SIngo Weinhold#define B_SCNo16		"ho"
1050fde9192SIngo Weinhold#define B_SCNx16		"hx"
1060fde9192SIngo Weinhold#define B_SCNu32 		__HAIKU_PRI_PREFIX_32 "u"
1070fde9192SIngo Weinhold#define B_SCNo32 		__HAIKU_PRI_PREFIX_32 "o"
1080fde9192SIngo Weinhold#define B_SCNx32 		__HAIKU_PRI_PREFIX_32 "x"
1090fde9192SIngo Weinhold#define B_SCNu64		__HAIKU_PRI_PREFIX_64 "u"
1100fde9192SIngo Weinhold#define B_SCNo64		__HAIKU_PRI_PREFIX_64 "o"
1110fde9192SIngo Weinhold#define B_SCNx64		__HAIKU_PRI_PREFIX_64 "x"
1122222d055SIngo Weinhold
1132222d055SIngo Weinhold/* printf() format strings for some standard types */
1142222d055SIngo Weinhold/* size_t */
1152222d055SIngo Weinhold#define B_PRIuSIZE		__HAIKU_PRI_PREFIX_ADDR "u"
1162222d055SIngo Weinhold#define B_PRIoSIZE		__HAIKU_PRI_PREFIX_ADDR "o"
1172222d055SIngo Weinhold#define B_PRIxSIZE		__HAIKU_PRI_PREFIX_ADDR "x"
1182222d055SIngo Weinhold#define B_PRIXSIZE		__HAIKU_PRI_PREFIX_ADDR "X"
1192222d055SIngo Weinhold/* ssize_t */
1202222d055SIngo Weinhold#define B_PRIdSSIZE		__HAIKU_PRI_PREFIX_ADDR "d"
1212222d055SIngo Weinhold#define B_PRIiSSIZE		__HAIKU_PRI_PREFIX_ADDR "i"
1222222d055SIngo Weinhold/* addr_t */
1232222d055SIngo Weinhold#define B_PRIuADDR		__HAIKU_PRI_PREFIX_ADDR "u"
1242222d055SIngo Weinhold#define B_PRIoADDR		__HAIKU_PRI_PREFIX_ADDR "o"
1252222d055SIngo Weinhold#define B_PRIxADDR		__HAIKU_PRI_PREFIX_ADDR "x"
1262222d055SIngo Weinhold#define B_PRIXADDR		__HAIKU_PRI_PREFIX_ADDR "X"
127d34daac8SIngo Weinhold/* phys_addr_t */
128d34daac8SIngo Weinhold#define B_PRIuPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "u"
129d34daac8SIngo Weinhold#define B_PRIoPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "o"
130d34daac8SIngo Weinhold#define B_PRIxPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "x"
131d34daac8SIngo Weinhold#define B_PRIXPHYSADDR	__HAIKU_PRI_PREFIX_PHYS_ADDR "X"
1324a57f843SIngo Weinhold/* generic_addr_t */
1334a57f843SIngo Weinhold#define B_PRIuGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "u"
1344a57f843SIngo Weinhold#define B_PRIoGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "o"
1354a57f843SIngo Weinhold#define B_PRIxGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "x"
1364a57f843SIngo Weinhold#define B_PRIXGENADDR	__HAIKU_PRI_PREFIX_GENERIC_ADDR "X"
1372222d055SIngo Weinhold/* off_t */
1382222d055SIngo Weinhold#define B_PRIdOFF		B_PRId64
1392222d055SIngo Weinhold#define B_PRIiOFF		B_PRIi64
1402222d055SIngo Weinhold/* dev_t */
1412222d055SIngo Weinhold#define B_PRIdDEV		B_PRId32
1422222d055SIngo Weinhold#define B_PRIiDEV		B_PRIi32
1432222d055SIngo Weinhold/* ino_t */
1442222d055SIngo Weinhold#define B_PRIdINO		B_PRId64
1452222d055SIngo Weinhold#define B_PRIiINO		B_PRIi64
146cf844822SIngo Weinhold/* time_t */
147cf844822SIngo Weinhold#define B_PRIdTIME		B_PRId32
148cf844822SIngo Weinhold#define B_PRIiTIME		B_PRIi32
1492222d055SIngo Weinhold
1502222d055SIngo Weinhold
151e1f3108aSStephan Aßmus/* Empty string ("") */
15252a38012Sejakowatz#ifdef __cplusplus
153087882c2SAxel Dörflerextern const char *B_EMPTY_STRING;
15452a38012Sejakowatz#endif
15552a38012Sejakowatz
15652a38012Sejakowatz
157e1f3108aSStephan Aßmus/* min and max comparisons */
15852a38012Sejakowatz#ifndef __cplusplus
15961ad5bc2SAxel Dörfler#	ifndef min
16061ad5bc2SAxel Dörfler#		define min(a,b) ((a)>(b)?(b):(a))
16161ad5bc2SAxel Dörfler#	endif
16261ad5bc2SAxel Dörfler#	ifndef max
16361ad5bc2SAxel Dörfler#		define max(a,b) ((a)>(b)?(a):(b))
16461ad5bc2SAxel Dörfler#	endif
1652222d055SIngo Weinhold#endif
16652a38012Sejakowatz
1672222d055SIngo Weinhold/* min() and max() are functions in C++ */
168087882c2SAxel Dörfler#define min_c(a,b) ((a)>(b)?(b):(a))
169087882c2SAxel Dörfler#define max_c(a,b) ((a)>(b)?(a):(b))
170087882c2SAxel Dörfler
17152a38012Sejakowatz
172e1f3108aSStephan Aßmus/* Grandfathering */
17361ad5bc2SAxel Dörfler#ifndef __cplusplus
17461ad5bc2SAxel Dörfler#	include <stdbool.h>
1752222d055SIngo Weinhold#endif
17652a38012Sejakowatz
17752a38012Sejakowatz#ifndef NULL
17861ad5bc2SAxel Dörfler#	define NULL (0)
17952a38012Sejakowatz#endif
18052a38012Sejakowatz
18152a38012Sejakowatz
18252a38012Sejakowatz#ifdef __cplusplus
18352a38012Sejakowatzextern "C" {
18452a38012Sejakowatz#endif
18552a38012Sejakowatz
186e1f3108aSStephan Aßmus/* Atomic functions; previous value is returned */
187087882c2SAxel Dörflerextern int32	atomic_set(vint32 *value, int32 newValue);
188087882c2SAxel Dörflerextern int32	atomic_test_and_set(vint32 *value, int32 newValue, int32 testAgainst);
189087882c2SAxel Dörflerextern int32	atomic_add(vint32 *value, int32 addValue);
190087882c2SAxel Dörflerextern int32	atomic_and(vint32 *value, int32 andValue);
1912222d055SIngo Weinholdextern int32	atomic_or(vint32 *value, int32 orValue);
192087882c2SAxel Dörflerextern int32	atomic_get(vint32 *value);
193d3e72fa5Sbeveloper
194087882c2SAxel Dörflerextern int64	atomic_set64(vint64 *value, int64 newValue);
195087882c2SAxel Dörflerextern int64	atomic_test_and_set64(vint64 *value, int64 newValue, int64 testAgainst);
196087882c2SAxel Dörflerextern int64	atomic_add64(vint64 *value, int64 addValue);
197087882c2SAxel Dörflerextern int64	atomic_and64(vint64 *value, int64 andValue);
1982222d055SIngo Weinholdextern int64	atomic_or64(vint64 *value, int64 orValue);
199087882c2SAxel Dörflerextern int64	atomic_get64(vint64 *value);
20052a38012Sejakowatz
201e1f3108aSStephan Aßmus/* Other stuff */
202087882c2SAxel Dörflerextern void*	get_stack_frame(void);
20352a38012Sejakowatz
20452a38012Sejakowatz#ifdef __cplusplus
20552a38012Sejakowatz}
20652a38012Sejakowatz#endif
20752a38012Sejakowatz
208e1f3108aSStephan Aßmus/* Obsolete or discouraged API */
20952a38012Sejakowatz
210e1f3108aSStephan Aßmus/* use 'true' and 'false' */
21152a38012Sejakowatz#ifndef FALSE
21261ad5bc2SAxel Dörfler#	define FALSE	0
21352a38012Sejakowatz#endif
21452a38012Sejakowatz#ifndef TRUE
21561ad5bc2SAxel Dörfler#	define TRUE		1
21652a38012Sejakowatz#endif
21752a38012Sejakowatz
2184f7b2c67SIngo Weinhold
2194f7b2c67SIngo Weinhold/* Use the built-in atomic functions, if requested and available. */
2204f7b2c67SIngo Weinhold
2214f7b2c67SIngo Weinhold#if defined(B_USE_BUILTIN_ATOMIC_FUNCTIONS) && __GNUC__ >= 4
2224f7b2c67SIngo Weinhold
2234f7b2c67SIngo Weinhold#define atomic_test_and_set(valuePointer, newValue, testAgainst)	\
2244f7b2c67SIngo Weinhold	__sync_val_compare_and_swap(valuePointer, testAgainst, newValue)
2254f7b2c67SIngo Weinhold#define atomic_add(valuePointer, addValue)	\
2264f7b2c67SIngo Weinhold	__sync_fetch_and_add(valuePointer, addValue)
2274f7b2c67SIngo Weinhold#define atomic_and(valuePointer, andValue)	\
2284f7b2c67SIngo Weinhold	__sync_fetch_and_and(valuePointer, andValue)
2294f7b2c67SIngo Weinhold#define atomic_or(valuePointer, orValue)	\
2304f7b2c67SIngo Weinhold	__sync_fetch_and_or(valuePointer, orValue)
2314f7b2c67SIngo Weinhold#define atomic_get(valuePointer)	\
2324f7b2c67SIngo Weinhold	__sync_fetch_and_or(valuePointer, 0)
2334f7b2c67SIngo Weinhold	// No equivalent to atomic_get(). We simulate it via atomic or. On most
2344f7b2c67SIngo Weinhold	// (all?) 32+ bit architectures aligned 32 bit reads will be atomic anyway,
2354f7b2c67SIngo Weinhold	// though.
2364f7b2c67SIngo Weinhold
2374f7b2c67SIngo Weinhold// Note: No equivalent for atomic_set(). It could be simulated by a
2384f7b2c67SIngo Weinhold// get + atomic test and set loop, but calling the atomic_set() implementation
2394f7b2c67SIngo Weinhold// might be faster.
2404f7b2c67SIngo Weinhold
2414f7b2c67SIngo Weinhold#endif	// B_USE_BUILTIN_ATOMIC_FUNCTIONS && __GNUC__ >= 4
2424f7b2c67SIngo Weinhold
2434f7b2c67SIngo Weinhold
244e1f3108aSStephan Aßmus#endif	/* _SUPPORT_DEFS_H */
245