File indexing completed on 2024-11-15 09:59:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifndef __PUB_TOOL_BASICS_H
0030 #define __PUB_TOOL_BASICS_H
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 #include "libvex_basictypes.h"
0046
0047
0048 #include <stdarg.h>
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 #define VGAPPEND(str1,str2) str1##str2
0067
0068 #define VG_(str) VGAPPEND(vgPlain_, str)
0069 #define ML_(str) VGAPPEND(vgModuleLocal_, str)
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 typedef unsigned long UWord;
0085 typedef signed long Word;
0086
0087
0088
0089 typedef Word SSizeT;
0090
0091
0092
0093
0094
0095 typedef Word PtrdiffT;
0096
0097
0098
0099
0100
0101
0102
0103 #if defined(VGO_linux) || defined(VGO_solaris)
0104 typedef Word OffT;
0105 #elif defined(VGO_freebsd)
0106 typedef Long OffT;
0107 #elif defined(VGO_darwin)
0108 typedef Long OffT;
0109 #else
0110 # error Unknown OS
0111 #endif
0112 typedef Long Off64T;
0113
0114 #if !defined(NULL)
0115 # define NULL ((void*)0)
0116 #endif
0117
0118
0119 typedef struct { UWord uw1; UWord uw2; } UWordPair;
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 typedef UInt ThreadId;
0131
0132
0133
0134
0135
0136
0137
0138
0139 typedef struct { UInt n; } DiEpoch;
0140
0141 static inline DiEpoch DiEpoch_INVALID ( void ) {
0142 DiEpoch dep; dep.n = 0; return dep;
0143 }
0144
0145 static inline Bool is_DiEpoch_INVALID ( DiEpoch dep ) {
0146 return dep.n == 0;
0147 }
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 typedef void* (*Alloc_Fn_t) ( const HChar* cc, SizeT szB );
0159 typedef void (*Free_Fn_t) ( void* p );
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
0197 typedef
0198 struct {
0199 Bool _isError;
0200 RegWord _val;
0201 UWord _valEx;
0202 }
0203 SysRes;
0204
0205 #elif defined(VGO_linux) \
0206 && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux)
0207 typedef
0208 struct {
0209 Bool _isError;
0210 UWord _val;
0211 }
0212 SysRes;
0213
0214 #elif defined(VGO_darwin)
0215 typedef
0216 enum {
0217 SysRes_MACH=40,
0218 SysRes_MDEP,
0219 SysRes_UNIX_OK,
0220 SysRes_UNIX_ERR
0221 }
0222 SysResMode;
0223 typedef
0224 struct {
0225 UWord _wLO;
0226 UWord _wHI;
0227 SysResMode _mode;
0228 }
0229 SysRes;
0230 #elif defined(VGO_freebsd)
0231 typedef
0232 struct {
0233 UWord _val;
0234 UWord _val2;
0235 Bool _isError;
0236 #if defined(VGP_amd64_freebsd)
0237 char padding[7];
0238 #else
0239 char padding[3];
0240 #endif
0241 }
0242 SysRes;
0243
0244 #elif defined(VGO_solaris)
0245 typedef
0246 struct {
0247 UWord _val;
0248 UWord _val2;
0249 Bool _isError;
0250 }
0251 SysRes;
0252
0253 #else
0254 # error "Unknown OS"
0255 #endif
0256
0257
0258
0259
0260 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
0261
0262 static inline Bool sr_isError ( SysRes sr ) {
0263 return sr._isError;
0264 }
0265 static inline RegWord sr_Res ( SysRes sr ) {
0266 return sr._isError ? 0 : sr._val;
0267 }
0268 static inline UWord sr_ResEx ( SysRes sr ) {
0269 return sr._isError ? 0 : sr._valEx;
0270 }
0271 static inline UWord sr_Err ( SysRes sr ) {
0272 return sr._isError ? sr._val : 0;
0273 }
0274 static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) {
0275
0276
0277
0278
0279
0280
0281
0282
0283 # if defined(VGP_mips32_linux)
0284 const UInt __nr_Linux = 4000;
0285 const UInt __nr_pipe = __nr_Linux + 42;
0286 const UInt __nr_pipe2 = __nr_Linux + 328;
0287 # elif defined(VGP_mips64_linux) && defined(VGABI_N32)
0288 const UInt __nr_Linux = 6000;
0289 const UInt __nr_pipe = __nr_Linux + 21;
0290 const UInt __nr_pipe2 = __nr_Linux + 291;
0291 # else
0292 const UInt __nr_Linux = 5000;
0293 const UInt __nr_pipe = __nr_Linux + 21;
0294 const UInt __nr_pipe2 = __nr_Linux + 287;
0295 # endif
0296 Bool useEx = sysno == __nr_pipe || sysno == __nr_pipe2;
0297 return sr1._val == sr2._val
0298 && (useEx ? (sr1._valEx == sr2._valEx) : True)
0299 && sr1._isError == sr2._isError;
0300 }
0301
0302 #elif defined(VGO_linux) \
0303 && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux)
0304
0305 static inline Bool sr_isError ( SysRes sr ) {
0306 return sr._isError;
0307 }
0308 static inline UWord sr_Res ( SysRes sr ) {
0309 return sr._isError ? 0 : sr._val;
0310 }
0311 static inline UWord sr_Err ( SysRes sr ) {
0312 #if defined(VGP_nanomips_linux)
0313 return sr._isError ? -sr._val : 0;
0314 #else
0315 return sr._isError ? sr._val : 0;
0316 #endif
0317 }
0318 static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) {
0319
0320 return sr1._val == sr2._val
0321 && sr1._isError == sr2._isError;
0322 }
0323
0324 #elif defined(VGO_freebsd)
0325
0326 static inline Bool sr_isError ( SysRes sr ) {
0327 return sr._isError;
0328 }
0329 static inline UWord sr_Res ( SysRes sr ) {
0330 return sr._isError ? 0 : sr._val;
0331 }
0332 static inline UWord sr_ResHI ( SysRes sr ) {
0333 return sr._isError ? 0 : sr._val2;
0334 }
0335 static inline UWord sr_Err ( SysRes sr ) {
0336 return sr._isError ? sr._val : 0;
0337 }
0338 static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) {
0339 return sr_Res(sr1) == sr_Res(sr2)
0340 && sr_ResHI(sr1) == sr_ResHI(sr2)
0341 && ((sr_isError(sr1) && sr_isError(sr2))
0342 || (!sr_isError(sr1) && !sr_isError(sr2)));
0343 }
0344
0345 #elif defined(VGO_darwin)
0346
0347 static inline Bool sr_isError ( SysRes sr ) {
0348 switch (sr._mode) {
0349 case SysRes_UNIX_ERR:
0350 return True;
0351
0352 case SysRes_MACH:
0353 case SysRes_MDEP:
0354 case SysRes_UNIX_OK:
0355 default:
0356 return False;
0357 }
0358 }
0359
0360 static inline UWord sr_Res ( SysRes sr ) {
0361 switch (sr._mode) {
0362 case SysRes_MACH:
0363 case SysRes_MDEP:
0364 case SysRes_UNIX_OK:
0365 return sr._wLO;
0366
0367 case SysRes_UNIX_ERR:
0368 default:
0369 return 0;
0370 }
0371 }
0372
0373 static inline UWord sr_ResHI ( SysRes sr ) {
0374 switch (sr._mode) {
0375 case SysRes_UNIX_OK:
0376 return sr._wHI;
0377
0378 case SysRes_MACH:
0379 case SysRes_MDEP:
0380 case SysRes_UNIX_ERR:
0381 default:
0382 return 0;
0383 }
0384 }
0385
0386 static inline UWord sr_Err ( SysRes sr ) {
0387 switch (sr._mode) {
0388 case SysRes_UNIX_ERR:
0389 return sr._wLO;
0390
0391 case SysRes_MACH:
0392 case SysRes_MDEP:
0393 case SysRes_UNIX_OK:
0394 default:
0395 return 0;
0396 }
0397 }
0398
0399 static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) {
0400
0401 return sr1._mode == sr2._mode
0402 && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI;
0403 }
0404
0405 #elif defined(VGO_solaris)
0406
0407 static inline Bool sr_isError ( SysRes sr ) {
0408 return sr._isError;
0409 }
0410 static inline UWord sr_Res ( SysRes sr ) {
0411 return sr._isError ? 0 : sr._val;
0412 }
0413 static inline UWord sr_ResHI ( SysRes sr ) {
0414 return sr._isError ? 0 : sr._val2;
0415 }
0416 static inline UWord sr_Err ( SysRes sr ) {
0417 return sr._isError ? sr._val : 0;
0418 }
0419 static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) {
0420
0421 return sr1._val == sr2._val
0422 && sr1._isError == sr2._isError
0423 && (!sr1._isError) ? (sr1._val2 == sr2._val2) : True;
0424 }
0425
0426 #else
0427 # error "Unknown OS"
0428 #endif
0429
0430
0431
0432
0433
0434
0435
0436
0437 #define VG_WORDSIZE VEX_HOST_WORDSIZE
0438
0439
0440 #undef VG_BIGENDIAN
0441 #undef VG_LITTLEENDIAN
0442
0443 #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) \
0444 || ((defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)) \
0445 && defined (_MIPSEL)) || defined(VGA_arm64) || defined(VGA_ppc64le)
0446 # define VG_LITTLEENDIAN 1
0447 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_s390x) \
0448 || ((defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)) \
0449 && defined (_MIPSEB))
0450 # define VG_BIGENDIAN 1
0451 #else
0452 # error Unknown arch
0453 #endif
0454
0455
0456 #if !defined(offsetof)
0457 # define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
0458 #endif
0459
0460 #if !defined(container_of)
0461 # define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
0462 #endif
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484 #define vg_alignof(_type) (sizeof(struct {char c;_type _t;})-sizeof(_type))
0485
0486
0487 #if defined(VGA_x86)
0488 # define VG_REGPARM(n) __attribute__((regparm(n)))
0489 #elif defined(VGA_amd64) || defined(VGA_ppc32) \
0490 || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
0491 || defined(VGA_arm) || defined(VGA_s390x) \
0492 || defined(VGA_mips32) || defined(VGA_mips64) \
0493 || defined(VGA_arm64) || defined(VGA_nanomips)
0494 # define VG_REGPARM(n)
0495 #else
0496 # error Unknown arch
0497 #endif
0498
0499
0500 #define VG_STRINGIFZ(__str) #__str
0501 #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str)
0502
0503
0504 #define VG_BUGS_TO "www.valgrind.org"
0505
0506
0507 #if defined(__GNUC__)
0508 # define LIKELY(x) __builtin_expect(!!(x), 1)
0509 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
0510 #else
0511 # define LIKELY(x) (x)
0512 # define UNLIKELY(x) (x)
0513 #endif
0514
0515
0516
0517
0518
0519 #if defined(__GNUC__)
0520 #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y)))
0521 #else
0522 #define PRINTF_CHECK(x, y)
0523 #endif
0524
0525
0526
0527
0528 #define CONST_CAST(T,x) \
0529 ({ \
0530 union { \
0531 const T in; \
0532 T out; \
0533 } var = { .in = x }; var.out; \
0534 })
0535
0536
0537
0538
0539 #define ASSUME_ALIGNED(D, x) \
0540 ({ \
0541 union { \
0542 void *in; \
0543 D out; \
0544 } var; \
0545 var.in = (void *) (x); var.out; \
0546 })
0547
0548
0549 #define STATIC_ASSERT(x) extern int VG_(VG_(VG_(unused)))[(x) ? 1 : -1] \
0550 __attribute__((unused))
0551
0552 #define VG_MAX(a,b) ((a) > (b) ? a : b)
0553 #define VG_MIN(a,b) ((a) < (b) ? a : b)
0554
0555 #endif
0556
0557
0558
0559