blob: 14d3949c428a2e71e5a834f34dcd52149f618c3f [file] [log] [blame]
sewardjb8b79ad2008-03-03 01:35:41 +00001
2/*--------------------------------------------------------------------*/
3/*--- Misc simple stuff lacking a better home. misc.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj0f157dd2013-10-18 14:27:36 +000010 Copyright (C) 2008-2013 OpenWorks LLP
sewardjb8b79ad2008-03-03 01:35:41 +000011 info@open-works.co.uk
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 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.
34*/
35
36#include "pub_core_basics.h"
37#include "pub_core_libcbase.h"
38#include "pub_core_libcassert.h"
39#include "pub_core_mallocfree.h"
40#include "pub_core_xarray.h"
41
42#include "priv_misc.h" /* self */
43
44
florian54fe2022012-10-27 23:07:42 +000045void* ML_(dinfo_zalloc) ( const HChar* cc, SizeT szB ) {
sewardjb8b79ad2008-03-03 01:35:41 +000046 void* v;
47 vg_assert(szB > 0);
sewardj9c606bd2008-09-18 18:12:50 +000048 v = VG_(arena_malloc)( VG_AR_DINFO, cc, szB );
sewardjb8b79ad2008-03-03 01:35:41 +000049 VG_(memset)(v, 0, szB);
50 return v;
51}
52
philippe0b9d0642014-06-30 19:47:24 +000053void ML_(dinfo_shrink_block)( void* ptr, SizeT szB ) {
54 VG_(arena_realloc_shrink)( VG_AR_DINFO, ptr, szB );
55}
56
sewardjb8b79ad2008-03-03 01:35:41 +000057void ML_(dinfo_free) ( void* v ) {
58 VG_(arena_free)( VG_AR_DINFO, v );
59}
60
florian1636d332012-11-15 04:27:04 +000061HChar* ML_(dinfo_strdup) ( const HChar* cc, const HChar* str ) {
sewardj9c606bd2008-09-18 18:12:50 +000062 return VG_(arena_strdup)( VG_AR_DINFO, cc, str );
63}
64
florian518850b2014-10-22 22:25:30 +000065void* ML_(dinfo_memdup) ( const HChar* cc, const void* str, SizeT nStr ) {
florian1636d332012-11-15 04:27:04 +000066 void* dst = VG_(arena_malloc)( VG_AR_DINFO, cc, nStr );
sewardj9c606bd2008-09-18 18:12:50 +000067 VG_(memcpy)(dst, str, nStr);
68 return dst;
sewardjb8b79ad2008-03-03 01:35:41 +000069}
70
florian46cc0452014-10-25 19:20:38 +000071void* ML_(dinfo_realloc) ( const HChar* cc, void* ptr, SizeT new_size ) {
72 void* dst = VG_(arena_realloc)( VG_AR_DINFO, cc, ptr, new_size );
73 vg_assert(dst);
74 return dst;
75}
76
tomd32fb2b2011-10-02 10:20:12 +000077static inline Bool host_is_little_endian ( void ) {
78 UInt x = 0x76543210;
79 UChar* p = (UChar*)(&x);
80 return toBool(*p == 0x10);
81}
82
philippec4740692015-05-25 20:15:25 +000083Short ML_(readUAS_Short)( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +000084 Short r = 0;
85 if (host_is_little_endian()) {
86 r = data[0]
87 | ( ((UInt)data[1]) << 8 );
88 } else {
89 r = data[1]
90 | ( ((UInt)data[0]) << 8 );
91 }
92 return r;
93}
94
philippec4740692015-05-25 20:15:25 +000095Int ML_(readUAS_Int) ( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +000096 Int r = 0;
97 if (host_is_little_endian()) {
98 r = data[0]
99 | ( ((UInt)data[1]) << 8 )
100 | ( ((UInt)data[2]) << 16 )
101 | ( ((UInt)data[3]) << 24 );
102 } else {
103 r = data[3]
104 | ( ((UInt)data[2]) << 8 )
105 | ( ((UInt)data[1]) << 16 )
106 | ( ((UInt)data[0]) << 24 );
107 }
108 return r;
109}
110
philippec4740692015-05-25 20:15:25 +0000111Long ML_(readUAS_Long) ( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +0000112 Long r = 0;
113 if (host_is_little_endian()) {
114 r = data[0]
115 | ( ((ULong)data[1]) << 8 )
116 | ( ((ULong)data[2]) << 16 )
117 | ( ((ULong)data[3]) << 24 )
118 | ( ((ULong)data[4]) << 32 )
119 | ( ((ULong)data[5]) << 40 )
120 | ( ((ULong)data[6]) << 48 )
121 | ( ((ULong)data[7]) << 56 );
122 } else {
123 r = data[7]
124 | ( ((ULong)data[6]) << 8 )
125 | ( ((ULong)data[5]) << 16 )
126 | ( ((ULong)data[4]) << 24 )
127 | ( ((ULong)data[3]) << 32 )
128 | ( ((ULong)data[2]) << 40 )
129 | ( ((ULong)data[1]) << 48 )
130 | ( ((ULong)data[0]) << 56 );
131 }
132 return r;
133}
134
philippec4740692015-05-25 20:15:25 +0000135UShort ML_(readUAS_UShort) ( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +0000136 UInt r = 0;
137 if (host_is_little_endian()) {
138 r = data[0]
139 | ( ((UInt)data[1]) << 8 );
140 } else {
141 r = data[1]
142 | ( ((UInt)data[0]) << 8 );
143 }
144 return r;
145}
146
philippec4740692015-05-25 20:15:25 +0000147UChar *ML_(writeUAS_UShort) ( UChar* ptr, UShort val ) {
tom86781fa2011-10-05 08:48:07 +0000148 if (host_is_little_endian()) {
149 ptr[0] = val & 0xff;
150 ptr[1] = ( val >> 8 ) & 0xff;
151 } else {
152 ptr[0] = ( val >> 8 ) & 0xff;
153 ptr[1] = val & 0xff;
154 }
155 return ptr + sizeof(UShort);
156}
157
philippec4740692015-05-25 20:15:25 +0000158UWord ML_(readUAS_UWord) ( const UChar* data ) {
tom86781fa2011-10-05 08:48:07 +0000159 if (sizeof(UWord) == sizeof(UInt)) {
160 return ML_(read_UInt)(data);
161 } else if (sizeof(UWord) == sizeof(ULong)) {
162 return ML_(read_ULong)(data);
163 } else {
164 vg_assert(0);
165 }
166}
167
philippec4740692015-05-25 20:15:25 +0000168UInt ML_(readUAS_UInt) ( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +0000169 UInt r = 0;
170 if (host_is_little_endian()) {
171 r = data[0]
172 | ( ((UInt)data[1]) << 8 )
173 | ( ((UInt)data[2]) << 16 )
174 | ( ((UInt)data[3]) << 24 );
175 } else {
176 r = data[3]
177 | ( ((UInt)data[2]) << 8 )
178 | ( ((UInt)data[1]) << 16 )
179 | ( ((UInt)data[0]) << 24 );
180 }
181 return r;
182}
183
philippec4740692015-05-25 20:15:25 +0000184UChar* ML_(writeUAS_UInt) ( UChar* ptr, UInt val ) {
tom86781fa2011-10-05 08:48:07 +0000185 if (host_is_little_endian()) {
186 ptr[0] = val & 0xff;
187 ptr[1] = ( val >> 8 ) & 0xff;
188 ptr[2] = ( val >> 16 ) & 0xff;
189 ptr[3] = ( val >> 24 ) & 0xff;
190 } else {
191 ptr[0] = ( val >> 24 ) & 0xff;
192 ptr[1] = ( val >> 16 ) & 0xff;
193 ptr[2] = ( val >> 8 ) & 0xff;
194 ptr[3] = val & 0xff;
195 }
196 return ptr + sizeof(UInt);
197}
198
philippec4740692015-05-25 20:15:25 +0000199ULong ML_(readUAS_ULong) ( const UChar* data ) {
tomd32fb2b2011-10-02 10:20:12 +0000200 ULong r = 0;
201 if (host_is_little_endian()) {
202 r = data[0]
203 | ( ((ULong)data[1]) << 8 )
204 | ( ((ULong)data[2]) << 16 )
205 | ( ((ULong)data[3]) << 24 )
206 | ( ((ULong)data[4]) << 32 )
207 | ( ((ULong)data[5]) << 40 )
208 | ( ((ULong)data[6]) << 48 )
209 | ( ((ULong)data[7]) << 56 );
210 } else {
211 r = data[7]
212 | ( ((ULong)data[6]) << 8 )
213 | ( ((ULong)data[5]) << 16 )
214 | ( ((ULong)data[4]) << 24 )
215 | ( ((ULong)data[3]) << 32 )
216 | ( ((ULong)data[2]) << 40 )
217 | ( ((ULong)data[1]) << 48 )
218 | ( ((ULong)data[0]) << 56 );
219 }
220 return r;
221}
222
philippec4740692015-05-25 20:15:25 +0000223UChar* ML_(writeUAS_ULong) ( UChar* ptr, ULong val ) {
tom86781fa2011-10-05 08:48:07 +0000224 if (host_is_little_endian()) {
225 ptr[0] = val & 0xff;
226 ptr[1] = ( val >> 8 ) & 0xff;
227 ptr[2] = ( val >> 16 ) & 0xff;
228 ptr[3] = ( val >> 24 ) & 0xff;
229 ptr[4] = ( val >> 32 ) & 0xff;
230 ptr[5] = ( val >> 40 ) & 0xff;
231 ptr[6] = ( val >> 48 ) & 0xff;
232 ptr[7] = ( val >> 56 ) & 0xff;
233 } else {
234 ptr[0] = ( val >> 56 ) & 0xff;
235 ptr[1] = ( val >> 48 ) & 0xff;
236 ptr[2] = ( val >> 40 ) & 0xff;
237 ptr[3] = ( val >> 32 ) & 0xff;
238 ptr[4] = ( val >> 24 ) & 0xff;
239 ptr[5] = ( val >> 16 ) & 0xff;
240 ptr[6] = ( val >> 8 ) & 0xff;
241 ptr[7] = val & 0xff;
242 }
243 return ptr + sizeof(ULong);
244}
245
tomd32fb2b2011-10-02 10:20:12 +0000246
philippec4740692015-05-25 20:15:25 +0000247Addr ML_(readUAS_Addr) ( const UChar* data ) {
tomfd892fc2011-10-02 12:12:38 +0000248 if (sizeof(Addr) == sizeof(UInt)) {
249 return ML_(read_UInt)(data);
250 } else if (sizeof(Addr) == sizeof(ULong)) {
251 return ML_(read_ULong)(data);
252 } else {
253 vg_assert(0);
254 }
255}
256
philippec4740692015-05-25 20:15:25 +0000257UChar* ML_(writeUAS_Addr) ( UChar* ptr, Addr val ) {
tom86781fa2011-10-05 08:48:07 +0000258 if (sizeof(Addr) == sizeof(UInt)) {
259 return ML_(write_UInt)(ptr, val);
260 } else if (sizeof(Addr) == sizeof(ULong)) {
261 return ML_(write_ULong)(ptr, val);
262 } else {
263 vg_assert(0);
264 }
265}
266
sewardjb8b79ad2008-03-03 01:35:41 +0000267/*--------------------------------------------------------------------*/
268/*--- end misc.c ---*/
269/*--------------------------------------------------------------------*/