blob: 8627d1f8e0354f938c3bed172b5b9735f4561d88 [file] [log] [blame]
sewardj5cf515f2004-06-26 20:10:35 +00001
2/*---------------------------------------------------------------*/
sewardj752f9062010-05-03 21:38:49 +00003/*--- begin main_util.h ---*/
sewardj5cf515f2004-06-26 20:10:35 +00004/*---------------------------------------------------------------*/
5
sewardjf8ed9d82004-11-12 17:40:23 +00006/*
sewardj752f9062010-05-03 21:38:49 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjf8ed9d82004-11-12 17:40:23 +00009
Elliott Hughesed398002017-06-21 14:41:24 -070010 Copyright (C) 2004-2017 OpenWorks LLP
sewardj752f9062010-05-03 21:38:49 +000011 info@open-works.net
sewardjf8ed9d82004-11-12 17:40:23 +000012
sewardj752f9062010-05-03 21:38:49 +000013 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.
sewardjf8ed9d82004-11-12 17:40:23 +000017
sewardj752f9062010-05-03 21:38:49 +000018 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., 51 Franklin Street, Fifth Floor, Boston, MA
sewardj7bd6ffe2005-08-03 16:07:36 +000026 02110-1301, USA.
27
sewardj752f9062010-05-03 21:38:49 +000028 The GNU General Public License is contained in the file COPYING.
sewardjf8ed9d82004-11-12 17:40:23 +000029
30 Neither the names of the U.S. Department of Energy nor the
31 University of California nor the names of its contributors may be
32 used to endorse or promote products derived from this software
33 without prior written permission.
sewardjf8ed9d82004-11-12 17:40:23 +000034*/
35
sewardjcef7d3e2009-07-02 12:21:59 +000036#ifndef __VEX_MAIN_UTIL_H
37#define __VEX_MAIN_UTIL_H
sewardj5cf515f2004-06-26 20:10:35 +000038
sewardj887a11a2004-07-05 17:26:47 +000039#include "libvex_basictypes.h"
sewardj5cf515f2004-06-26 20:10:35 +000040
41
sewardj35421a32004-07-05 13:12:34 +000042/* Misc. */
43
44#define NULL ((void*)0)
45
Elliott Hughesed398002017-06-21 14:41:24 -070046#if defined(_MSC_VER) // building with MSVC
47# define LIKELY(x) (x)
48# define UNLIKELY(x) (x)
49# define CAST_TO_TYPEOF(x) /**/
50#else
51# define LIKELY(x) __builtin_expect(!!(x), 1)
52# define UNLIKELY(x) __builtin_expect(!!(x), 0)
53# define CAST_TO_TYPEOF(x) (__typeof__(x))
54#endif // defined(_MSC_VER)
sewardj35421a32004-07-05 13:12:34 +000055
floriana0ef1de2014-12-29 22:18:58 +000056#if !defined(offsetof)
57# define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
58#endif
59
florian1996df02015-03-28 18:48:20 +000060// Poor man's static assert
floriana2cdc9f2015-04-04 18:43:11 +000061#define STATIC_ASSERT(x) extern int vex__unused_array[(x) ? 1 : -1] \
62 __attribute__((unused))
sewardj5cf515f2004-06-26 20:10:35 +000063
florian1996df02015-03-28 18:48:20 +000064/* Stuff for panicking and assertion. */
sewardj5cf515f2004-06-26 20:10:35 +000065
sewardj35421a32004-07-05 13:12:34 +000066#define vassert(expr) \
sewardj7ee97522011-05-09 21:45:04 +000067 ((void) (LIKELY(expr) ? 0 : \
florian1996df02015-03-28 18:48:20 +000068 (vex_assert_fail (#expr, \
sewardj5cf515f2004-06-26 20:10:35 +000069 __FILE__, __LINE__, \
70 __PRETTY_FUNCTION__), 0)))
71
72__attribute__ ((__noreturn__))
sewardj58277842005-02-07 03:11:17 +000073extern void vex_assert_fail ( const HChar* expr, const HChar* file,
74 Int line, const HChar* fn );
sewardj5cf515f2004-06-26 20:10:35 +000075__attribute__ ((__noreturn__))
florian76714fd2012-09-13 20:21:42 +000076extern void vpanic ( const HChar* str );
sewardj5cf515f2004-06-26 20:10:35 +000077
florianeebdb2b2014-12-10 16:08:09 +000078__attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2)))
79extern void vfatal ( const HChar* format, ... );
80
sewardj5cf515f2004-06-26 20:10:35 +000081
sewardj35421a32004-07-05 13:12:34 +000082/* Printing */
83
84__attribute__ ((format (printf, 1, 2)))
florian76714fd2012-09-13 20:21:42 +000085extern UInt vex_printf ( const HChar *format, ... );
sewardj35421a32004-07-05 13:12:34 +000086
sewardj41f43bc2004-07-08 14:23:22 +000087__attribute__ ((format (printf, 2, 3)))
florian76714fd2012-09-13 20:21:42 +000088extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... );
sewardj35421a32004-07-05 13:12:34 +000089
sewardj36ca5132004-07-24 13:12:23 +000090
91/* String ops */
92
sewardj58277842005-02-07 03:11:17 +000093extern Bool vex_streq ( const HChar* s1, const HChar* s2 );
florian04fc6b12014-12-29 20:22:26 +000094extern SizeT vex_strlen ( const HChar* str );
95extern void vex_bzero ( void* s, SizeT n );
sewardj36ca5132004-07-24 13:12:23 +000096
97
sewardjd887b862005-01-17 18:34:34 +000098/* Storage management: clear the area, and allocate from it. */
99
100/* By default allocation occurs in the temporary area. However, it is
101 possible to switch to permanent area allocation if that's what you
102 want. Permanent area allocation is very limited, tho. */
103
104typedef
105 enum {
106 VexAllocModeTEMP,
107 VexAllocModePERM
108 }
109 VexAllocMode;
110
111extern void vexSetAllocMode ( VexAllocMode );
112extern VexAllocMode vexGetAllocMode ( void );
sewardj2d6b14a2005-11-23 04:25:07 +0000113extern void vexAllocSanityCheck ( void );
sewardjd887b862005-01-17 18:34:34 +0000114
sewardj2d6b14a2005-11-23 04:25:07 +0000115extern void vexSetAllocModeTEMP_and_clear ( void );
sewardjd887b862005-01-17 18:34:34 +0000116
floriand8e3eca2015-03-13 12:46:49 +0000117/* Allocate in Vex's temporary allocation area. Be careful with this.
118 You can only call it inside an instrumentation or optimisation
119 callback that you have previously specified in a call to
120 LibVEX_Translate. The storage allocated will only stay alive until
121 translation of the current basic block is complete.
122 */
123extern HChar* private_LibVEX_alloc_first;
124extern HChar* private_LibVEX_alloc_curr;
125extern HChar* private_LibVEX_alloc_last;
126extern void private_LibVEX_alloc_OOM(void) __attribute__((noreturn));
127
128/* Allocated memory as returned by LibVEX_Alloc will be aligned on this
129 boundary. */
130#define REQ_ALIGN 8
131
132static inline void* LibVEX_Alloc_inline ( SizeT nbytes )
133{
134 struct align {
135 char c;
136 union {
137 char c;
138 short s;
139 int i;
140 long l;
141 long long ll;
142 float f;
143 double d;
144 /* long double is currently not used and would increase alignment
145 unnecessarily. */
146 /* long double ld; */
147 void *pto;
148 void (*ptf)(void);
149 } x;
150 };
151
152 /* Make sure the compiler does no surprise us */
153 vassert(offsetof(struct align,x) <= REQ_ALIGN);
154
155#if 0
156 /* Nasty debugging hack, do not use. */
157 return malloc(nbytes);
158#else
159 HChar* curr;
160 HChar* next;
161 SizeT ALIGN;
162 ALIGN = offsetof(struct align,x) - 1;
163 nbytes = (nbytes + ALIGN) & ~ALIGN;
164 curr = private_LibVEX_alloc_curr;
165 next = curr + nbytes;
166 if (next >= private_LibVEX_alloc_last)
167 private_LibVEX_alloc_OOM();
168 private_LibVEX_alloc_curr = next;
169 return curr;
170#endif
171}
172
sewardj48729062015-07-07 12:41:33 +0000173/* Misaligned memory access support. */
174
175extern UInt read_misaligned_UInt_LE ( void* addr );
176extern ULong read_misaligned_ULong_LE ( void* addr );
177
178extern void write_misaligned_UInt_LE ( void* addr, UInt w );
179extern void write_misaligned_ULong_LE ( void* addr, ULong w );
180
sewardjcef7d3e2009-07-02 12:21:59 +0000181#endif /* ndef __VEX_MAIN_UTIL_H */
sewardjac9af022004-07-05 01:15:34 +0000182
183/*---------------------------------------------------------------*/
sewardjcef7d3e2009-07-02 12:21:59 +0000184/*--- main_util.h ---*/
sewardjac9af022004-07-05 01:15:34 +0000185/*---------------------------------------------------------------*/