-> 3.20.0 final
[valgrind.git] / VEX / priv / main_util.h
blob2fa26b062dd0b37e3788f48500dc48e6717fe2bf
2 /*---------------------------------------------------------------*/
3 /*--- begin main_util.h ---*/
4 /*---------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2004-2017 OpenWorks LLP
11 info@open-works.net
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
28 Neither the names of the U.S. Department of Energy nor the
29 University of California nor the names of its contributors may be
30 used to endorse or promote products derived from this software
31 without prior written permission.
34 #ifndef __VEX_MAIN_UTIL_H
35 #define __VEX_MAIN_UTIL_H
37 #include "libvex_basictypes.h"
39 #include "libvex_inner.h"
40 #if defined(ENABLE_INNER_CLIENT_REQUEST)
41 /* Including here memcheck include file is kind of a hack, but this is needed
42 to have self-hosting checking VEX. Note however that this is included only
43 when Valgrind and VEX are configured using --enable-inner. */
44 #include "memcheck/memcheck.h"
45 #endif
47 /* Misc. */
49 #define NULL ((void*)0)
51 #if defined(_MSC_VER) // building with MSVC
52 # define LIKELY(x) (x)
53 # define UNLIKELY(x) (x)
54 # define CAST_TO_TYPEOF(x) /**/
55 #else
56 # define LIKELY(x) __builtin_expect(!!(x), 1)
57 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
58 # define CAST_TO_TYPEOF(x) (__typeof__(x))
59 #endif // defined(_MSC_VER)
61 #if !defined(offsetof)
62 # define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
63 #endif
65 // Poor man's static assert
66 #define STATIC_ASSERT(x) extern int vex__unused_array[(x) ? 1 : -1] \
67 __attribute__((unused))
69 /* Stuff for panicking and assertion. */
71 #define vassert(expr) \
72 ((void) (LIKELY(expr) ? 0 : \
73 (vex_assert_fail (#expr, \
74 __FILE__, __LINE__, \
75 __PRETTY_FUNCTION__), 0)))
77 __attribute__ ((__noreturn__))
78 extern void vex_assert_fail ( const HChar* expr, const HChar* file,
79 Int line, const HChar* fn );
80 __attribute__ ((__noreturn__))
81 extern void vpanic ( const HChar* str );
83 __attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2)))
84 extern void vfatal ( const HChar* format, ... );
87 /* Printing */
89 __attribute__ ((format (printf, 1, 2)))
90 extern UInt vex_printf ( const HChar *format, ... );
92 __attribute__ ((format (printf, 2, 3)))
93 extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... );
96 /* String ops */
98 extern Bool vex_streq ( const HChar* s1, const HChar* s2 );
99 extern SizeT vex_strlen ( const HChar* str );
100 extern void vex_bzero ( void* s, SizeT n );
103 /* Storage management: clear the area, and allocate from it. */
105 /* By default allocation occurs in the temporary area. However, it is
106 possible to switch to permanent area allocation if that's what you
107 want. Permanent area allocation is very limited, tho. */
109 typedef
110 enum {
111 VexAllocModeTEMP,
112 VexAllocModePERM
114 VexAllocMode;
116 extern void vexSetAllocMode ( VexAllocMode );
117 extern VexAllocMode vexGetAllocMode ( void );
118 extern void vexAllocSanityCheck ( void );
120 extern void vexSetAllocModeTEMP_and_clear ( void );
122 /* Allocate in Vex's temporary allocation area. Be careful with this.
123 You can only call it inside an instrumentation or optimisation
124 callback that you have previously specified in a call to
125 LibVEX_Translate. The storage allocated will only stay alive until
126 translation of the current basic block is complete.
128 extern HChar* private_LibVEX_alloc_first;
129 extern HChar* private_LibVEX_alloc_curr;
130 extern HChar* private_LibVEX_alloc_last;
131 extern void private_LibVEX_alloc_OOM(void) __attribute__((noreturn));
133 /* Allocated memory as returned by LibVEX_Alloc will be aligned on this
134 boundary. */
135 #define REQ_ALIGN 8
137 #if defined(ENABLE_INNER)
138 #define VEX_REDZONE_SIZEB (2*REQ_ALIGN)
139 #endif
141 static inline void* LibVEX_Alloc_inline ( SizeT nbytes )
143 struct align {
144 char c;
145 union {
146 char c;
147 short s;
148 int i;
149 long l;
150 long long ll;
151 float f;
152 double d;
153 /* long double is currently not used and would increase alignment
154 unnecessarily. */
155 /* long double ld; */
156 void *pto;
157 void (*ptf)(void);
158 } x;
161 /* Make sure the compiler does no surprise us */
162 vassert(offsetof(struct align,x) <= REQ_ALIGN);
164 #if 0
165 /* Nasty debugging hack, do not use. */
166 return malloc(nbytes);
167 #else
168 HChar* curr;
169 HChar* next;
170 SizeT ALIGN;
171 ALIGN = offsetof(struct align,x) - 1;
172 curr = private_LibVEX_alloc_curr;
173 next = curr + ((nbytes + ALIGN) & ~ALIGN);
174 INNER_REQUEST(next += 2 * VEX_REDZONE_SIZEB);
175 if (next >= private_LibVEX_alloc_last)
176 private_LibVEX_alloc_OOM();
177 private_LibVEX_alloc_curr = next;
178 INNER_REQUEST(curr += VEX_REDZONE_SIZEB);
179 INNER_REQUEST(VALGRIND_MEMPOOL_ALLOC(private_LibVEX_alloc_first,
180 curr, nbytes));
181 return curr;
182 #endif
185 /* Misaligned memory access support. */
187 extern UInt read_misaligned_UInt_LE ( void* addr );
188 extern ULong read_misaligned_ULong_LE ( void* addr );
190 extern void write_misaligned_UInt_LE ( void* addr, UInt w );
191 extern void write_misaligned_ULong_LE ( void* addr, ULong w );
193 #endif /* ndef __VEX_MAIN_UTIL_H */
195 /*---------------------------------------------------------------*/
196 /*--- main_util.h ---*/
197 /*---------------------------------------------------------------*/