2 /*--------------------------------------------------------------------*/
3 /*--- Misc simple stuff lacking a better home. misc.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2008-2017 OpenWorks LLP
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, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
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.
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"
42 #include "priv_misc.h" /* self */
45 void* ML_(dinfo_zalloc
) ( const HChar
* cc
, SizeT szB
) {
48 v
= VG_(arena_malloc
)( VG_AR_DINFO
, cc
, szB
);
49 VG_(memset
)(v
, 0, szB
);
53 void ML_(dinfo_shrink_block
)( void* ptr
, SizeT szB
) {
54 VG_(arena_realloc_shrink
)( VG_AR_DINFO
, ptr
, szB
);
57 void ML_(dinfo_free
) ( void* v
) {
58 VG_(arena_free
)( VG_AR_DINFO
, v
);
61 HChar
* ML_(dinfo_strdup
) ( const HChar
* cc
, const HChar
* str
) {
62 return VG_(arena_strdup
)( VG_AR_DINFO
, cc
, str
);
65 void* ML_(dinfo_memdup
) ( const HChar
* cc
, const void* str
, SizeT nStr
) {
66 void* dst
= VG_(arena_malloc
)( VG_AR_DINFO
, cc
, nStr
);
67 VG_(memcpy
)(dst
, str
, nStr
);
71 void* ML_(dinfo_realloc
) ( const HChar
* cc
, void* ptr
, SizeT new_size
) {
72 void* dst
= VG_(arena_realloc
)( VG_AR_DINFO
, cc
, ptr
, new_size
);
77 static inline Bool
host_is_little_endian ( void ) {
79 UChar
* p
= (UChar
*)(&x
);
80 return toBool(*p
== 0x10);
83 Short
ML_(readUAS_Short
)( const UChar
* data
) {
85 if (host_is_little_endian()) {
87 | ( ((UInt
)data
[1]) << 8 );
90 | ( ((UInt
)data
[0]) << 8 );
95 Int
ML_(readUAS_Int
) ( const UChar
* data
) {
97 if (host_is_little_endian()) {
99 | ( ((UInt
)data
[1]) << 8 )
100 | ( ((UInt
)data
[2]) << 16 )
101 | ( ((UInt
)data
[3]) << 24 );
104 | ( ((UInt
)data
[2]) << 8 )
105 | ( ((UInt
)data
[1]) << 16 )
106 | ( ((UInt
)data
[0]) << 24 );
111 Long
ML_(readUAS_Long
) ( const UChar
* data
) {
113 if (host_is_little_endian()) {
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 );
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 );
135 UShort
ML_(readUAS_UShort
) ( const UChar
* data
) {
137 if (host_is_little_endian()) {
139 | ( ((UInt
)data
[1]) << 8 );
142 | ( ((UInt
)data
[0]) << 8 );
147 UChar
*ML_(writeUAS_UShort
) ( UChar
* ptr
, UShort val
) {
148 if (host_is_little_endian()) {
150 ptr
[1] = ( val
>> 8 ) & 0xff;
152 ptr
[0] = ( val
>> 8 ) & 0xff;
155 return ptr
+ sizeof(UShort
);
158 UWord
ML_(readUAS_UWord
) ( const UChar
* data
) {
159 if (sizeof(UWord
) == sizeof(UInt
)) {
160 return ML_(read_UInt
)(data
);
161 } else if (sizeof(UWord
) == sizeof(ULong
)) {
162 return ML_(read_ULong
)(data
);
168 UInt
ML_(readUAS_UInt
) ( const UChar
* data
) {
170 if (host_is_little_endian()) {
172 | ( ((UInt
)data
[1]) << 8 )
173 | ( ((UInt
)data
[2]) << 16 )
174 | ( ((UInt
)data
[3]) << 24 );
177 | ( ((UInt
)data
[2]) << 8 )
178 | ( ((UInt
)data
[1]) << 16 )
179 | ( ((UInt
)data
[0]) << 24 );
184 UChar
* ML_(writeUAS_UInt
) ( UChar
* ptr
, UInt val
) {
185 if (host_is_little_endian()) {
187 ptr
[1] = ( val
>> 8 ) & 0xff;
188 ptr
[2] = ( val
>> 16 ) & 0xff;
189 ptr
[3] = ( val
>> 24 ) & 0xff;
191 ptr
[0] = ( val
>> 24 ) & 0xff;
192 ptr
[1] = ( val
>> 16 ) & 0xff;
193 ptr
[2] = ( val
>> 8 ) & 0xff;
196 return ptr
+ sizeof(UInt
);
199 ULong
ML_(readUAS_ULong
) ( const UChar
* data
) {
201 if (host_is_little_endian()) {
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 );
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 );
223 UChar
* ML_(writeUAS_ULong
) ( UChar
* ptr
, ULong val
) {
224 if (host_is_little_endian()) {
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;
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;
243 return ptr
+ sizeof(ULong
);
247 Addr
ML_(readUAS_Addr
) ( const UChar
* data
) {
248 if (sizeof(Addr
) == sizeof(UInt
)) {
249 return ML_(read_UInt
)(data
);
250 } else if (sizeof(Addr
) == sizeof(ULong
)) {
251 return ML_(read_ULong
)(data
);
257 UChar
* ML_(writeUAS_Addr
) ( UChar
* ptr
, Addr val
) {
258 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
);
267 /*--------------------------------------------------------------------*/
268 /*--- end misc.c ---*/
269 /*--------------------------------------------------------------------*/