nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 3 | /*--- Header included by every tool C file. pub_tool_basics.h ---*/ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 9 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 10 | Copyright (C) 2000-2017 Julian Seward |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #ifndef __PUB_TOOL_BASICS_H |
| 32 | #define __PUB_TOOL_BASICS_H |
| 33 | |
| 34 | //-------------------------------------------------------------------- |
| 35 | // PURPOSE: This header should be imported by every single C file in |
| 36 | // tools. It contains the basic types and other things needed everywhere. |
| 37 | // There is no corresponding C file because this isn't a module |
| 38 | // containing executable code, it's all just declarations. |
| 39 | //-------------------------------------------------------------------- |
| 40 | |
| 41 | /* --------------------------------------------------------------------- |
| 42 | Other headers to include |
| 43 | ------------------------------------------------------------------ */ |
| 44 | |
florian | af93042 | 2014-12-29 19:06:40 +0000 | [diff] [blame] | 45 | // VEX defines Char, UChar, Short, UShort, Int, UInt, Long, ULong, SizeT, |
florian | 9b3da11 | 2014-12-31 12:11:15 +0000 | [diff] [blame] | 46 | // Addr, Addr32, Addr64, HWord, HChar, Bool, False and True. |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 47 | #include "libvex_basictypes.h" |
| 48 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 49 | // For varargs types |
| 50 | #include <stdarg.h> |
| 51 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 52 | |
| 53 | /* --------------------------------------------------------------------- |
njn | 9c53325 | 2009-04-24 04:57:07 +0000 | [diff] [blame] | 54 | symbol prefixing |
| 55 | ------------------------------------------------------------------ */ |
| 56 | |
| 57 | // All symbols externally visible from Valgrind are prefixed |
| 58 | // as specified here to avoid namespace conflict problems. |
| 59 | // |
| 60 | // VG_ is for symbols exported from modules. ML_ (module-local) is |
| 61 | // for symbols which are not intended to be visible outside modules, |
| 62 | // but which cannot be declared as C 'static's since they need to be |
| 63 | // visible across C files within a given module. It is a mistake for |
| 64 | // a ML_ name to appear in a pub_core_*.h or pub_tool_*.h file. |
| 65 | // Likewise it is a mistake for a VG_ name to appear in a priv_*.h |
| 66 | // file. |
| 67 | |
| 68 | #define VGAPPEND(str1,str2) str1##str2 |
| 69 | |
| 70 | #define VG_(str) VGAPPEND(vgPlain_, str) |
| 71 | #define ML_(str) VGAPPEND(vgModuleLocal_, str) |
| 72 | |
| 73 | |
| 74 | /* --------------------------------------------------------------------- |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 75 | builtin types |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 76 | ------------------------------------------------------------------ */ |
| 77 | |
| 78 | // By choosing the right types, we can get these right for 32-bit and 64-bit |
| 79 | // platforms without having to do any conditional compilation or anything. |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 80 | // POSIX references: |
| 81 | // - http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html |
| 82 | // - http://www.opengroup.org/onlinepubs/009695399/basedefs/stddef.h.html |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 83 | // |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 84 | // Size in bits on: 32-bit archs 64-bit archs |
| 85 | // ------------ ------------ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 86 | typedef unsigned long UWord; // 32 64 |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 87 | typedef signed long Word; // 32 64 |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 88 | |
florian | af93042 | 2014-12-29 19:06:40 +0000 | [diff] [blame] | 89 | // Our equivalent of POSIX 'ssize_t': |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 90 | // - ssize_t is "used for a count of bytes or an error indication". |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 91 | typedef Word SSizeT; // 32 64 |
| 92 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 93 | // Our equivalent of POSIX 'ptrdiff_t': |
| 94 | // - ptrdiff_t is a "signed integer type of the result of subtracting two |
| 95 | // pointers". |
| 96 | // We use it for memory offsets, eg. the offset into a memory block. |
| 97 | typedef Word PtrdiffT; // 32 64 |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 98 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 99 | // Our equivalent of POSIX 'off_t': |
| 100 | // - off_t is "used for file sizes". |
| 101 | // At one point we were using it for memory offsets, but PtrdiffT should be |
| 102 | // used in those cases. |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 103 | // Nb: on Linux, off_t is a signed word-sized int. On Darwin it's |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 104 | // always a signed 64-bit int. So we defined our own Off64T as well. |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 105 | #if defined(VGO_linux) || defined(VGO_solaris) |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 106 | typedef Word OffT; // 32 64 |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 107 | #elif defined(VGO_darwin) |
| 108 | typedef Long OffT; // 64 64 |
| 109 | #else |
| 110 | # error Unknown OS |
| 111 | #endif |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 112 | typedef Long Off64T; // 64 64 |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 113 | |
njn | 21edee3 | 2005-06-11 04:44:38 +0000 | [diff] [blame] | 114 | #if !defined(NULL) |
| 115 | # define NULL ((void*)0) |
| 116 | #endif |
sewardj | bc7df20 | 2005-05-02 10:25:34 +0000 | [diff] [blame] | 117 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 118 | /* This is just too useful to not have around the place somewhere. */ |
| 119 | typedef struct { UWord uw1; UWord uw2; } UWordPair; |
| 120 | |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 121 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 122 | /* --------------------------------------------------------------------- |
| 123 | non-builtin types |
| 124 | ------------------------------------------------------------------ */ |
| 125 | |
| 126 | // These probably shouldn't be here, but moving them to their logical |
| 127 | // modules results in a lot more #includes... |
| 128 | |
| 129 | /* ThreadIds are simply indices into the VG_(threads)[] array. */ |
| 130 | typedef UInt ThreadId; |
| 131 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 132 | /* Many data structures need to allocate and release memory. |
| 133 | The allocation/release functions must be provided by the caller. |
| 134 | The Alloc_Fn_t function must allocate a chunk of memory of size szB. |
| 135 | cc is the Cost Centre for this allocated memory. This constant string |
| 136 | is used to provide Valgrind's heap profiling, activated by |
| 137 | --profile-heap=no|yes. |
| 138 | The corresponding Free_Fn_t frees the memory chunk p. */ |
| 139 | |
| 140 | typedef void* (*Alloc_Fn_t) ( const HChar* cc, SizeT szB ); |
| 141 | typedef void (*Free_Fn_t) ( void* p ); |
| 142 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 143 | /* An abstraction of syscall return values. |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 144 | Linux/MIPS32 and Linux/MIPS64: |
| 145 | When _isError == False, |
| 146 | _val and possible _valEx hold the return value. Whether |
| 147 | _valEx actually holds a valid value depends on which syscall |
| 148 | this SysRes holds of the result of. |
| 149 | When _isError == True, |
| 150 | _val holds the error code. |
| 151 | |
| 152 | Linux/other: |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 153 | When _isError == False, |
| 154 | _val holds the return value. |
| 155 | When _isError == True, |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 156 | _val holds the error code. |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 157 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 158 | Darwin: |
| 159 | Interpretation depends on _mode: |
| 160 | MACH, MDEP: |
| 161 | these can never 'fail' (apparently). The result of the |
| 162 | syscall is a single host word, _wLO. |
| 163 | UNIX: |
| 164 | Can record a double-word error or a double-word result: |
| 165 | When _mode is SysRes_UNIX_OK, _wHI:_wLO holds the result. |
| 166 | When _mode is SysRes_UNIX_ERR, _wHI:_wLO holds the error code. |
| 167 | Probably the high word of an error is always ignored by |
| 168 | userspace, but we have to record it, so that we can correctly |
njn | 38c73d7 | 2009-05-19 01:32:52 +0000 | [diff] [blame] | 169 | update both {R,E}DX and {R,E}AX (in guest state) given a SysRes, |
| 170 | if we're required to. |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 171 | |
| 172 | Solaris: |
| 173 | When _isError == False, |
| 174 | _val and _val2 hold the return value. |
| 175 | When _isError == True, |
| 176 | _val holds the error code. |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 177 | */ |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 178 | #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux) |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 179 | typedef |
| 180 | struct { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 181 | Bool _isError; |
florian | 9c120ae | 2015-04-20 21:02:18 +0000 | [diff] [blame] | 182 | UWord _val; |
florian | 9c120ae | 2015-04-20 21:02:18 +0000 | [diff] [blame] | 183 | UWord _valEx; |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 184 | } |
| 185 | SysRes; |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 186 | |
| 187 | #elif defined(VGO_linux) \ |
| 188 | && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux) |
| 189 | typedef |
| 190 | struct { |
| 191 | Bool _isError; |
| 192 | UWord _val; |
| 193 | } |
| 194 | SysRes; |
| 195 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 196 | #elif defined(VGO_darwin) |
| 197 | typedef |
bart | 116458b | 2009-12-29 14:11:38 +0000 | [diff] [blame] | 198 | enum { |
| 199 | SysRes_MACH=40, // MACH, result is _wLO |
| 200 | SysRes_MDEP, // MDEP, result is _wLO |
| 201 | SysRes_UNIX_OK, // UNIX, success, result is _wHI:_wLO |
| 202 | SysRes_UNIX_ERR // UNIX, error, error is _wHI:_wLO |
| 203 | } |
| 204 | SysResMode; |
| 205 | typedef |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 206 | struct { |
| 207 | UWord _wLO; |
| 208 | UWord _wHI; |
bart | 116458b | 2009-12-29 14:11:38 +0000 | [diff] [blame] | 209 | SysResMode _mode; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 210 | } |
| 211 | SysRes; |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 212 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 213 | #elif defined(VGO_solaris) |
| 214 | typedef |
| 215 | struct { |
| 216 | UWord _val; |
| 217 | UWord _val2; |
| 218 | Bool _isError; |
| 219 | } |
| 220 | SysRes; |
| 221 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 222 | #else |
| 223 | # error "Unknown OS" |
| 224 | #endif |
| 225 | |
| 226 | |
| 227 | /* ---- And now some basic accessor functions for it. ---- */ |
| 228 | |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 229 | #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux) |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 230 | |
| 231 | static inline Bool sr_isError ( SysRes sr ) { |
| 232 | return sr._isError; |
| 233 | } |
| 234 | static inline UWord sr_Res ( SysRes sr ) { |
| 235 | return sr._isError ? 0 : sr._val; |
| 236 | } |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 237 | static inline UWord sr_ResEx ( SysRes sr ) { |
| 238 | return sr._isError ? 0 : sr._valEx; |
| 239 | } |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 240 | static inline UWord sr_Err ( SysRes sr ) { |
| 241 | return sr._isError ? sr._val : 0; |
| 242 | } |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 243 | static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) { |
| 244 | /* This uglyness of hardcoding syscall numbers is necessary to |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 245 | avoid having this header file be dependent on |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 246 | include/vki/vki-scnums-mips{32,64}-linux.h. It seems pretty |
| 247 | safe given that it is inconceivable that the syscall numbers |
| 248 | for such simple syscalls would ever change. To make it |
| 249 | really safe, coregrind/m_vkiscnums.c static-asserts that these |
| 250 | syscall numbers haven't changed, so that the build wil simply |
| 251 | fail if they ever do. */ |
| 252 | # if defined(VGP_mips32_linux) |
| 253 | const UInt __nr_Linux = 4000; |
| 254 | const UInt __nr_pipe = __nr_Linux + 42; |
| 255 | const UInt __nr_pipe2 = __nr_Linux + 328; |
| 256 | # else |
| 257 | const UInt __nr_Linux = 5000; |
| 258 | const UInt __nr_pipe = __nr_Linux + 21; |
| 259 | const UInt __nr_pipe2 = __nr_Linux + 287; |
| 260 | # endif |
| 261 | Bool useEx = sysno == __nr_pipe || sysno == __nr_pipe2; |
florian | d59d978 | 2015-04-01 19:20:16 +0000 | [diff] [blame] | 262 | return sr1._val == sr2._val |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 263 | && (useEx ? (sr1._valEx == sr2._valEx) : True) |
| 264 | && sr1._isError == sr2._isError; |
| 265 | } |
| 266 | |
| 267 | #elif defined(VGO_linux) \ |
| 268 | && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux) |
| 269 | |
| 270 | static inline Bool sr_isError ( SysRes sr ) { |
| 271 | return sr._isError; |
| 272 | } |
| 273 | static inline UWord sr_Res ( SysRes sr ) { |
| 274 | return sr._isError ? 0 : sr._val; |
| 275 | } |
| 276 | static inline UWord sr_Err ( SysRes sr ) { |
| 277 | return sr._isError ? sr._val : 0; |
| 278 | } |
| 279 | static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) { |
| 280 | /* sysno is ignored for Linux/not-MIPS */ |
| 281 | return sr1._val == sr2._val |
| 282 | && sr1._isError == sr2._isError; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 283 | } |
| 284 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 285 | #elif defined(VGO_darwin) |
| 286 | |
| 287 | static inline Bool sr_isError ( SysRes sr ) { |
| 288 | switch (sr._mode) { |
sewardj | 19e2c94 | 2014-10-17 15:05:01 +0000 | [diff] [blame] | 289 | case SysRes_UNIX_ERR: |
| 290 | return True; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 291 | /* should check tags properly and assert here, but we can't here */ |
sewardj | 19e2c94 | 2014-10-17 15:05:01 +0000 | [diff] [blame] | 292 | case SysRes_MACH: |
| 293 | case SysRes_MDEP: |
| 294 | case SysRes_UNIX_OK: |
| 295 | default: |
| 296 | return False; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | static inline UWord sr_Res ( SysRes sr ) { |
| 301 | switch (sr._mode) { |
| 302 | case SysRes_MACH: |
| 303 | case SysRes_MDEP: |
sewardj | 19e2c94 | 2014-10-17 15:05:01 +0000 | [diff] [blame] | 304 | case SysRes_UNIX_OK: |
| 305 | return sr._wLO; |
| 306 | /* should assert, but we can't here */ |
| 307 | case SysRes_UNIX_ERR: |
| 308 | default: |
| 309 | return 0; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | static inline UWord sr_ResHI ( SysRes sr ) { |
| 314 | switch (sr._mode) { |
sewardj | 19e2c94 | 2014-10-17 15:05:01 +0000 | [diff] [blame] | 315 | case SysRes_UNIX_OK: |
| 316 | return sr._wHI; |
| 317 | /* should assert, but we can't here */ |
| 318 | case SysRes_MACH: |
| 319 | case SysRes_MDEP: |
| 320 | case SysRes_UNIX_ERR: |
| 321 | default: |
| 322 | return 0; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | static inline UWord sr_Err ( SysRes sr ) { |
| 327 | switch (sr._mode) { |
sewardj | 19e2c94 | 2014-10-17 15:05:01 +0000 | [diff] [blame] | 328 | case SysRes_UNIX_ERR: |
| 329 | return sr._wLO; |
| 330 | /* should assert, but we can't here */ |
| 331 | case SysRes_MACH: |
| 332 | case SysRes_MDEP: |
| 333 | case SysRes_UNIX_OK: |
| 334 | default: |
| 335 | return 0; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
sewardj | bd664fd | 2015-07-08 17:08:23 +0000 | [diff] [blame] | 339 | static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) { |
| 340 | /* sysno is ignored for Darwin */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 341 | return sr1._mode == sr2._mode |
| 342 | && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI; |
| 343 | } |
| 344 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 345 | #elif defined(VGO_solaris) |
| 346 | |
| 347 | static inline Bool sr_isError ( SysRes sr ) { |
| 348 | return sr._isError; |
| 349 | } |
| 350 | static inline UWord sr_Res ( SysRes sr ) { |
| 351 | return sr._isError ? 0 : sr._val; |
| 352 | } |
| 353 | static inline UWord sr_ResHI ( SysRes sr ) { |
| 354 | return sr._isError ? 0 : sr._val2; |
| 355 | } |
| 356 | static inline UWord sr_Err ( SysRes sr ) { |
| 357 | return sr._isError ? sr._val : 0; |
| 358 | } |
| 359 | static inline Bool sr_EQ ( UInt sysno, SysRes sr1, SysRes sr2 ) { |
| 360 | /* sysno is ignored for Solaris */ |
| 361 | return sr1._val == sr2._val |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 362 | && sr1._isError == sr2._isError |
| 363 | && (!sr1._isError) ? (sr1._val2 == sr2._val2) : True; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 364 | } |
| 365 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 366 | #else |
| 367 | # error "Unknown OS" |
| 368 | #endif |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 369 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 370 | |
| 371 | /* --------------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 372 | Miscellaneous (word size, endianness, regparmness, stringification) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 373 | ------------------------------------------------------------------ */ |
| 374 | |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 375 | /* Word size: this is going to be either 4 or 8. */ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 376 | // It should probably be in m_machine. |
| 377 | #define VG_WORDSIZE VEX_HOST_WORDSIZE |
| 378 | |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 379 | /* Endianness */ |
| 380 | #undef VG_BIGENDIAN |
| 381 | #undef VG_LITTLEENDIAN |
| 382 | |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 383 | #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) \ |
sewardj | f0c1250 | 2014-01-12 12:54:00 +0000 | [diff] [blame] | 384 | || ((defined(VGA_mips32) || defined(VGA_mips64)) && defined (_MIPSEL)) \ |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 385 | || defined(VGA_arm64) || defined(VGA_ppc64le) |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 386 | # define VG_LITTLEENDIAN 1 |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 387 | #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_s390x) \ |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 388 | || ((defined(VGA_mips32) || defined(VGA_mips64)) && defined (_MIPSEB)) |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 389 | # define VG_BIGENDIAN 1 |
njn | 38c73d7 | 2009-05-19 01:32:52 +0000 | [diff] [blame] | 390 | #else |
| 391 | # error Unknown arch |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 392 | #endif |
| 393 | |
florian | d65f726 | 2014-12-29 22:19:24 +0000 | [diff] [blame] | 394 | /* Offsetof */ |
| 395 | #if !defined(offsetof) |
| 396 | # define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb) |
| 397 | #endif |
| 398 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 399 | #if !defined(container_of) |
| 400 | # define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) |
| 401 | #endif |
| 402 | |
florian | d65f726 | 2014-12-29 22:19:24 +0000 | [diff] [blame] | 403 | /* Alignment */ |
| 404 | /* We use a prefix vg_ for vg_alignof as its behaviour slightly |
| 405 | differs from the standard alignof/gcc defined __alignof__ |
| 406 | |
| 407 | vg_alignof returns a "safe" alignement. |
| 408 | "safe" is defined as the alignment chosen by the compiler in |
| 409 | a struct made of a char followed by this type. |
| 410 | |
| 411 | Note that this is not necessarily the "preferred" alignment |
| 412 | for a platform. This preferred alignment is returned by the gcc |
| 413 | __alignof__ and by the standard (in recent standard) alignof. |
| 414 | Compared to __alignof__, vg_alignof gives on some platforms (e.g. |
| 415 | amd64, ppc32, ppc64) a bigger alignment for long double (16 bytes |
| 416 | instead of 8). |
| 417 | On some platforms (e.g. x86), vg_alignof gives a smaller alignment |
| 418 | than __alignof__ for long long and double (4 bytes instead of 8). |
| 419 | If we want to have the "preferred" alignment for the basic types, |
| 420 | then either we need to depend on gcc __alignof__, or on a (too) |
| 421 | recent standard and compiler (implementing <stdalign.h>). |
| 422 | */ |
| 423 | #define vg_alignof(_type) (sizeof(struct {char c;_type _t;})-sizeof(_type)) |
| 424 | |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 425 | /* Regparmness */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 426 | #if defined(VGA_x86) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 427 | # define VG_REGPARM(n) __attribute__((regparm(n))) |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 428 | #elif defined(VGA_amd64) || defined(VGA_ppc32) \ |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 429 | || defined(VGA_ppc64be) || defined(VGA_ppc64le) \ |
| 430 | || defined(VGA_arm) || defined(VGA_s390x) \ |
sewardj | f0c1250 | 2014-01-12 12:54:00 +0000 | [diff] [blame] | 431 | || defined(VGA_mips32) || defined(VGA_mips64) \ |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 432 | || defined(VGA_arm64) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 433 | # define VG_REGPARM(n) /* */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 434 | #else |
| 435 | # error Unknown arch |
| 436 | #endif |
| 437 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 438 | /* Macro games */ |
| 439 | #define VG_STRINGIFZ(__str) #__str |
| 440 | #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str) |
| 441 | |
njn | 64ea7f8 | 2006-10-14 23:26:21 +0000 | [diff] [blame] | 442 | // Where to send bug reports to. |
| 443 | #define VG_BUGS_TO "www.valgrind.org" |
| 444 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 445 | /* Branch prediction hints. */ |
sewardj | 4ee9c56 | 2011-05-09 21:54:44 +0000 | [diff] [blame] | 446 | #if defined(__GNUC__) |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 447 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
sewardj | 4ee9c56 | 2011-05-09 21:54:44 +0000 | [diff] [blame] | 448 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 449 | #else |
| 450 | # define LIKELY(x) (x) |
| 451 | # define UNLIKELY(x) (x) |
| 452 | #endif |
| 453 | |
sewardj | 588adef | 2009-08-15 22:41:51 +0000 | [diff] [blame] | 454 | // printf format string checking for gcc. |
| 455 | // This feature has been supported since at least gcc version 2.95. |
| 456 | // For more information about the format attribute, see |
| 457 | // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html. |
| 458 | #if defined(__GNUC__) |
| 459 | #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y))) |
| 460 | #else |
| 461 | #define PRINTF_CHECK(x, y) |
| 462 | #endif |
| 463 | |
florian | 70a5de1 | 2014-10-22 12:53:16 +0000 | [diff] [blame] | 464 | // Macro to "cast" away constness (from type const T to type T) without |
| 465 | // GCC complaining about it. This macro should be used RARELY. |
| 466 | // x is expected to have type const T |
| 467 | #define CONST_CAST(T,x) \ |
| 468 | ({ \ |
| 469 | union { \ |
| 470 | const T in; \ |
| 471 | T out; \ |
| 472 | } var = { .in = x }; var.out; \ |
| 473 | }) |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 474 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 475 | /* Some architectures (eg. mips, arm) do not support unaligned memory access |
| 476 | by hardware, so GCC warns about suspicious situations. This macro could |
| 477 | be used to avoid these warnings but only after careful examination. */ |
| 478 | #define ASSUME_ALIGNED(D, x) \ |
| 479 | ({ \ |
| 480 | union { \ |
| 481 | void *in; \ |
| 482 | D out; \ |
| 483 | } var; \ |
| 484 | var.in = (void *) (x); var.out; \ |
| 485 | }) |
| 486 | |
florian | c13d544 | 2015-03-28 18:36:01 +0000 | [diff] [blame] | 487 | // Poor man's static assert |
florian | ea73483 | 2015-04-04 18:43:00 +0000 | [diff] [blame] | 488 | #define STATIC_ASSERT(x) extern int VG_(VG_(VG_(unused)))[(x) ? 1 : -1] \ |
| 489 | __attribute__((unused)) |
florian | c13d544 | 2015-03-28 18:36:01 +0000 | [diff] [blame] | 490 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 491 | #define VG_MAX(a,b) ((a) > (b) ? a : b) |
| 492 | #define VG_MIN(a,b) ((a) < (b) ? a : b) |
| 493 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 494 | #endif /* __PUB_TOOL_BASICS_H */ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 495 | |
| 496 | /*--------------------------------------------------------------------*/ |
| 497 | /*--- end ---*/ |
| 498 | /*--------------------------------------------------------------------*/ |