blob: 4dcdd946d953751838530346e4b6aee73d6de4ad [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
sewardj89ae8472013-10-18 14:12:58 +000010 Copyright (C) 2004-2013 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
sewardj7ee97522011-05-09 21:45:04 +000046#define LIKELY(x) __builtin_expect(!!(x), 1)
47#define UNLIKELY(x) __builtin_expect(!!(x), 0)
sewardj35421a32004-07-05 13:12:34 +000048
floriana0ef1de2014-12-29 22:18:58 +000049#if !defined(offsetof)
50# define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
51#endif
52
florian1996df02015-03-28 18:48:20 +000053// Poor man's static assert
54#define STATIC_ASSERT(x) extern int vex__unused_array[(x) ? 1 : -1]
sewardj5cf515f2004-06-26 20:10:35 +000055
florian1996df02015-03-28 18:48:20 +000056/* Stuff for panicking and assertion. */
sewardj5cf515f2004-06-26 20:10:35 +000057
sewardj35421a32004-07-05 13:12:34 +000058#define vassert(expr) \
sewardj7ee97522011-05-09 21:45:04 +000059 ((void) (LIKELY(expr) ? 0 : \
florian1996df02015-03-28 18:48:20 +000060 (vex_assert_fail (#expr, \
sewardj5cf515f2004-06-26 20:10:35 +000061 __FILE__, __LINE__, \
62 __PRETTY_FUNCTION__), 0)))
63
64__attribute__ ((__noreturn__))
sewardj58277842005-02-07 03:11:17 +000065extern void vex_assert_fail ( const HChar* expr, const HChar* file,
66 Int line, const HChar* fn );
sewardj5cf515f2004-06-26 20:10:35 +000067__attribute__ ((__noreturn__))
florian76714fd2012-09-13 20:21:42 +000068extern void vpanic ( const HChar* str );
sewardj5cf515f2004-06-26 20:10:35 +000069
florianeebdb2b2014-12-10 16:08:09 +000070__attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2)))
71extern void vfatal ( const HChar* format, ... );
72
sewardj5cf515f2004-06-26 20:10:35 +000073
sewardj35421a32004-07-05 13:12:34 +000074/* Printing */
75
76__attribute__ ((format (printf, 1, 2)))
florian76714fd2012-09-13 20:21:42 +000077extern UInt vex_printf ( const HChar *format, ... );
sewardj35421a32004-07-05 13:12:34 +000078
sewardj41f43bc2004-07-08 14:23:22 +000079__attribute__ ((format (printf, 2, 3)))
florian76714fd2012-09-13 20:21:42 +000080extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... );
sewardj35421a32004-07-05 13:12:34 +000081
sewardj36ca5132004-07-24 13:12:23 +000082
83/* String ops */
84
sewardj58277842005-02-07 03:11:17 +000085extern Bool vex_streq ( const HChar* s1, const HChar* s2 );
florian04fc6b12014-12-29 20:22:26 +000086extern SizeT vex_strlen ( const HChar* str );
87extern void vex_bzero ( void* s, SizeT n );
sewardj36ca5132004-07-24 13:12:23 +000088
89
sewardjd887b862005-01-17 18:34:34 +000090/* Storage management: clear the area, and allocate from it. */
91
92/* By default allocation occurs in the temporary area. However, it is
93 possible to switch to permanent area allocation if that's what you
94 want. Permanent area allocation is very limited, tho. */
95
96typedef
97 enum {
98 VexAllocModeTEMP,
99 VexAllocModePERM
100 }
101 VexAllocMode;
102
103extern void vexSetAllocMode ( VexAllocMode );
104extern VexAllocMode vexGetAllocMode ( void );
sewardj2d6b14a2005-11-23 04:25:07 +0000105extern void vexAllocSanityCheck ( void );
sewardjd887b862005-01-17 18:34:34 +0000106
sewardj2d6b14a2005-11-23 04:25:07 +0000107extern void vexSetAllocModeTEMP_and_clear ( void );
sewardjd887b862005-01-17 18:34:34 +0000108
floriand8e3eca2015-03-13 12:46:49 +0000109/* Allocate in Vex's temporary allocation area. Be careful with this.
110 You can only call it inside an instrumentation or optimisation
111 callback that you have previously specified in a call to
112 LibVEX_Translate. The storage allocated will only stay alive until
113 translation of the current basic block is complete.
114 */
115extern HChar* private_LibVEX_alloc_first;
116extern HChar* private_LibVEX_alloc_curr;
117extern HChar* private_LibVEX_alloc_last;
118extern void private_LibVEX_alloc_OOM(void) __attribute__((noreturn));
119
120/* Allocated memory as returned by LibVEX_Alloc will be aligned on this
121 boundary. */
122#define REQ_ALIGN 8
123
124static inline void* LibVEX_Alloc_inline ( SizeT nbytes )
125{
126 struct align {
127 char c;
128 union {
129 char c;
130 short s;
131 int i;
132 long l;
133 long long ll;
134 float f;
135 double d;
136 /* long double is currently not used and would increase alignment
137 unnecessarily. */
138 /* long double ld; */
139 void *pto;
140 void (*ptf)(void);
141 } x;
142 };
143
144 /* Make sure the compiler does no surprise us */
145 vassert(offsetof(struct align,x) <= REQ_ALIGN);
146
147#if 0
148 /* Nasty debugging hack, do not use. */
149 return malloc(nbytes);
150#else
151 HChar* curr;
152 HChar* next;
153 SizeT ALIGN;
154 ALIGN = offsetof(struct align,x) - 1;
155 nbytes = (nbytes + ALIGN) & ~ALIGN;
156 curr = private_LibVEX_alloc_curr;
157 next = curr + nbytes;
158 if (next >= private_LibVEX_alloc_last)
159 private_LibVEX_alloc_OOM();
160 private_LibVEX_alloc_curr = next;
161 return curr;
162#endif
163}
164
sewardjcef7d3e2009-07-02 12:21:59 +0000165#endif /* ndef __VEX_MAIN_UTIL_H */
sewardjac9af022004-07-05 01:15:34 +0000166
167/*---------------------------------------------------------------*/
sewardjcef7d3e2009-07-02 12:21:59 +0000168/*--- main_util.h ---*/
sewardjac9af022004-07-05 01:15:34 +0000169/*---------------------------------------------------------------*/