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 60 /* printf()/scanf() format strings for [u]int* types */ 61 #define B_PRId8 "d" 62 #define B_PRIi8 "i" 63 #define B_PRId16 "d" 64 #define B_PRIi16 "i" 65 #define B_PRId32 __HAIKU_PRI_PREFIX_32 "d" 66 #define B_PRIi32 __HAIKU_PRI_PREFIX_32 "i" 67 #define B_PRId64 __HAIKU_PRI_PREFIX_64 "d" 68 #define B_PRIi64 __HAIKU_PRI_PREFIX_64 "i" 69 #define B_PRIu8 "u" 70 #define B_PRIo8 "o" 71 #define B_PRIx8 "x" 72 #define B_PRIX8 "X" 73 #define B_PRIu16 "u" 74 #define B_PRIo16 "o" 75 #define B_PRIx16 "x" 76 #define B_PRIX16 "X" 77 #define B_PRIu32 __HAIKU_PRI_PREFIX_32 "u" 78 #define B_PRIo32 __HAIKU_PRI_PREFIX_32 "o" 79 #define B_PRIx32 __HAIKU_PRI_PREFIX_32 "x" 80 #define B_PRIX32 __HAIKU_PRI_PREFIX_32 "X" 81 #define B_PRIu64 __HAIKU_PRI_PREFIX_64 "u" 82 #define B_PRIo64 __HAIKU_PRI_PREFIX_64 "o" 83 #define B_PRIx64 __HAIKU_PRI_PREFIX_64 "x" 84 #define B_PRIX64 __HAIKU_PRI_PREFIX_64 "X" 85 86 #define B_SCNd8 "hhd" 87 #define B_SCNi8 "hhi" 88 #define B_SCNd16 "hd" 89 #define B_SCNi16 "hi" 90 #define B_SCNd32 __HAIKU_PRI_PREFIX_32 "d" 91 #define B_SCNi32 __HAIKU_PRI_PREFIX_32 "i" 92 #define B_SCNd64 __HAIKU_PRI_PREFIX_64 "d" 93 #define B_SCNi64 __HAIKU_PRI_PREFIX_64 "i" 94 #define B_SCNu8 "hhu" 95 #define B_SCNo8 "hho" 96 #define B_SCNx8 "hhx" 97 #define B_SCNu16 "hu" 98 #define B_SCNo16 "ho" 99 #define B_SCNx16 "hx" 100 #define B_SCNu32 __HAIKU_PRI_PREFIX_32 "u" 101 #define B_SCNo32 __HAIKU_PRI_PREFIX_32 "o" 102 #define B_SCNx32 __HAIKU_PRI_PREFIX_32 "x" 103 #define B_SCNu64 __HAIKU_PRI_PREFIX_64 "u" 104 #define B_SCNo64 __HAIKU_PRI_PREFIX_64 "o" 105 #define B_SCNx64 __HAIKU_PRI_PREFIX_64 "x" 106 107 /* printf() format strings for some standard types */ 108 /* size_t */ 109 #define B_PRIuSIZE __HAIKU_PRI_PREFIX_ADDR "u" 110 #define B_PRIoSIZE __HAIKU_PRI_PREFIX_ADDR "o" 111 #define B_PRIxSIZE __HAIKU_PRI_PREFIX_ADDR "x" 112 #define B_PRIXSIZE __HAIKU_PRI_PREFIX_ADDR "X" 113 /* ssize_t */ 114 #define B_PRIdSSIZE __HAIKU_PRI_PREFIX_ADDR "d" 115 #define B_PRIiSSIZE __HAIKU_PRI_PREFIX_ADDR "i" 116 /* addr_t */ 117 #define B_PRIuADDR __HAIKU_PRI_PREFIX_ADDR "u" 118 #define B_PRIoADDR __HAIKU_PRI_PREFIX_ADDR "o" 119 #define B_PRIxADDR __HAIKU_PRI_PREFIX_ADDR "x" 120 #define B_PRIXADDR __HAIKU_PRI_PREFIX_ADDR "X" 121 /* phys_addr_t */ 122 #define B_PRIuPHYSADDR __HAIKU_PRI_PREFIX_PHYS_ADDR "u" 123 #define B_PRIoPHYSADDR __HAIKU_PRI_PREFIX_PHYS_ADDR "o" 124 #define B_PRIxPHYSADDR __HAIKU_PRI_PREFIX_PHYS_ADDR "x" 125 #define B_PRIXPHYSADDR __HAIKU_PRI_PREFIX_PHYS_ADDR "X" 126 /* off_t */ 127 #define B_PRIdOFF B_PRId64 128 #define B_PRIiOFF B_PRIi64 129 /* dev_t */ 130 #define B_PRIdDEV B_PRId32 131 #define B_PRIiDEV B_PRIi32 132 /* ino_t */ 133 #define B_PRIdINO B_PRId64 134 #define B_PRIiINO B_PRIi64 135 /* time_t */ 136 #define B_PRIdTIME B_PRId32 137 #define B_PRIiTIME B_PRIi32 138 139 140 /* Empty string ("") */ 141 #ifdef __cplusplus 142 extern const char *B_EMPTY_STRING; 143 #endif 144 145 146 /* min and max comparisons */ 147 #ifndef __cplusplus 148 # ifndef min 149 # define min(a,b) ((a)>(b)?(b):(a)) 150 # endif 151 # ifndef max 152 # define max(a,b) ((a)>(b)?(a):(b)) 153 # endif 154 #endif 155 156 /* min() and max() are functions in C++ */ 157 #define min_c(a,b) ((a)>(b)?(b):(a)) 158 #define max_c(a,b) ((a)>(b)?(a):(b)) 159 160 161 /* Grandfathering */ 162 #ifndef __cplusplus 163 # include <stdbool.h> 164 #endif 165 166 #ifndef NULL 167 # define NULL (0) 168 #endif 169 170 171 #ifdef __cplusplus 172 extern "C" { 173 #endif 174 175 /* Atomic functions; previous value is returned */ 176 extern int32 atomic_set(vint32 *value, int32 newValue); 177 extern int32 atomic_test_and_set(vint32 *value, int32 newValue, int32 testAgainst); 178 extern int32 atomic_add(vint32 *value, int32 addValue); 179 extern int32 atomic_and(vint32 *value, int32 andValue); 180 extern int32 atomic_or(vint32 *value, int32 orValue); 181 extern int32 atomic_get(vint32 *value); 182 183 extern int64 atomic_set64(vint64 *value, int64 newValue); 184 extern int64 atomic_test_and_set64(vint64 *value, int64 newValue, int64 testAgainst); 185 extern int64 atomic_add64(vint64 *value, int64 addValue); 186 extern int64 atomic_and64(vint64 *value, int64 andValue); 187 extern int64 atomic_or64(vint64 *value, int64 orValue); 188 extern int64 atomic_get64(vint64 *value); 189 190 /* Other stuff */ 191 extern void* get_stack_frame(void); 192 193 #ifdef __cplusplus 194 } 195 #endif 196 197 /* Obsolete or discouraged API */ 198 199 /* use 'true' and 'false' */ 200 #ifndef FALSE 201 # define FALSE 0 202 #endif 203 #ifndef TRUE 204 # define TRUE 1 205 #endif 206 207 208 /* Use the built-in atomic functions, if requested and available. */ 209 210 #if defined(B_USE_BUILTIN_ATOMIC_FUNCTIONS) && __GNUC__ >= 4 211 212 #define atomic_test_and_set(valuePointer, newValue, testAgainst) \ 213 __sync_val_compare_and_swap(valuePointer, testAgainst, newValue) 214 #define atomic_add(valuePointer, addValue) \ 215 __sync_fetch_and_add(valuePointer, addValue) 216 #define atomic_and(valuePointer, andValue) \ 217 __sync_fetch_and_and(valuePointer, andValue) 218 #define atomic_or(valuePointer, orValue) \ 219 __sync_fetch_and_or(valuePointer, orValue) 220 #define atomic_get(valuePointer) \ 221 __sync_fetch_and_or(valuePointer, 0) 222 // No equivalent to atomic_get(). We simulate it via atomic or. On most 223 // (all?) 32+ bit architectures aligned 32 bit reads will be atomic anyway, 224 // though. 225 226 // Note: No equivalent for atomic_set(). It could be simulated by a 227 // get + atomic test and set loop, but calling the atomic_set() implementation 228 // might be faster. 229 230 #endif // B_USE_BUILTIN_ATOMIC_FUNCTIONS && __GNUC__ >= 4 231 232 233 #endif /* _SUPPORT_DEFS_H */ 234