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, 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 #include "pub_core_basics.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_mallocfree.h"
38 #include "pub_core_xarray.h"
40 #include "priv_misc.h" /* self */
43 void* ML_(dinfo_zalloc
) ( const HChar
* cc
, SizeT szB
) {
46 v
= VG_(arena_malloc
)( VG_AR_DINFO
, cc
, szB
);
47 VG_(memset
)(v
, 0, szB
);
51 void ML_(dinfo_shrink_block
)( void* ptr
, SizeT szB
) {
52 VG_(arena_realloc_shrink
)( VG_AR_DINFO
, ptr
, szB
);
55 void ML_(dinfo_free
) ( void* v
) {
56 VG_(arena_free
)( VG_AR_DINFO
, v
);
59 HChar
* ML_(dinfo_strdup
) ( const HChar
* cc
, const HChar
* str
) {
60 return VG_(arena_strdup
)( VG_AR_DINFO
, cc
, str
);
63 void* ML_(dinfo_memdup
) ( const HChar
* cc
, const void* str
, SizeT nStr
) {
64 void* dst
= VG_(arena_malloc
)( VG_AR_DINFO
, cc
, nStr
);
65 VG_(memcpy
)(dst
, str
, nStr
);
69 void* ML_(dinfo_realloc
) ( const HChar
* cc
, void* ptr
, SizeT new_size
) {
70 void* dst
= VG_(arena_realloc
)( VG_AR_DINFO
, cc
, ptr
, new_size
);
75 static inline Bool
host_is_little_endian ( void ) {
77 UChar
* p
= (UChar
*)(&x
);
78 return toBool(*p
== 0x10);
81 Short
ML_(readUAS_Short
)( const UChar
* data
) {
83 if (host_is_little_endian()) {
85 | ( ((UInt
)data
[1]) << 8 );
88 | ( ((UInt
)data
[0]) << 8 );
93 Int
ML_(readUAS_Int
) ( const UChar
* data
) {
95 if (host_is_little_endian()) {
97 | ( ((UInt
)data
[1]) << 8 )
98 | ( ((UInt
)data
[2]) << 16 )
99 | ( ((UInt
)data
[3]) << 24 );
102 | ( ((UInt
)data
[2]) << 8 )
103 | ( ((UInt
)data
[1]) << 16 )
104 | ( ((UInt
)data
[0]) << 24 );
109 Long
ML_(readUAS_Long
) ( const UChar
* data
) {
111 if (host_is_little_endian()) {
113 | ( ((ULong
)data
[1]) << 8 )
114 | ( ((ULong
)data
[2]) << 16 )
115 | ( ((ULong
)data
[3]) << 24 )
116 | ( ((ULong
)data
[4]) << 32 )
117 | ( ((ULong
)data
[5]) << 40 )
118 | ( ((ULong
)data
[6]) << 48 )
119 | ( ((ULong
)data
[7]) << 56 );
122 | ( ((ULong
)data
[6]) << 8 )
123 | ( ((ULong
)data
[5]) << 16 )
124 | ( ((ULong
)data
[4]) << 24 )
125 | ( ((ULong
)data
[3]) << 32 )
126 | ( ((ULong
)data
[2]) << 40 )
127 | ( ((ULong
)data
[1]) << 48 )
128 | ( ((ULong
)data
[0]) << 56 );
133 UShort
ML_(readUAS_UShort
) ( const UChar
* data
) {
135 if (host_is_little_endian()) {
137 | ( ((UInt
)data
[1]) << 8 );
140 | ( ((UInt
)data
[0]) << 8 );
145 UChar
*ML_(writeUAS_UShort
) ( UChar
* ptr
, UShort val
) {
146 if (host_is_little_endian()) {
148 ptr
[1] = ( val
>> 8 ) & 0xff;
150 ptr
[0] = ( val
>> 8 ) & 0xff;
153 return ptr
+ sizeof(UShort
);
156 UWord
ML_(readUAS_UWord
) ( const UChar
* data
) {
157 if (sizeof(UWord
) == sizeof(UInt
)) {
158 return ML_(read_UInt
)(data
);
159 } else if (sizeof(UWord
) == sizeof(ULong
)) {
160 return ML_(read_ULong
)(data
);
166 UInt
ML_(readUAS_UInt
) ( const UChar
* data
) {
168 if (host_is_little_endian()) {
170 | ( ((UInt
)data
[1]) << 8 )
171 | ( ((UInt
)data
[2]) << 16 )
172 | ( ((UInt
)data
[3]) << 24 );
175 | ( ((UInt
)data
[2]) << 8 )
176 | ( ((UInt
)data
[1]) << 16 )
177 | ( ((UInt
)data
[0]) << 24 );
182 UChar
* ML_(writeUAS_UInt
) ( UChar
* ptr
, UInt val
) {
183 if (host_is_little_endian()) {
185 ptr
[1] = ( val
>> 8 ) & 0xff;
186 ptr
[2] = ( val
>> 16 ) & 0xff;
187 ptr
[3] = ( val
>> 24 ) & 0xff;
189 ptr
[0] = ( val
>> 24 ) & 0xff;
190 ptr
[1] = ( val
>> 16 ) & 0xff;
191 ptr
[2] = ( val
>> 8 ) & 0xff;
194 return ptr
+ sizeof(UInt
);
197 ULong
ML_(readUAS_ULong
) ( const UChar
* data
) {
199 if (host_is_little_endian()) {
201 | ( ((ULong
)data
[1]) << 8 )
202 | ( ((ULong
)data
[2]) << 16 )
203 | ( ((ULong
)data
[3]) << 24 )
204 | ( ((ULong
)data
[4]) << 32 )
205 | ( ((ULong
)data
[5]) << 40 )
206 | ( ((ULong
)data
[6]) << 48 )
207 | ( ((ULong
)data
[7]) << 56 );
210 | ( ((ULong
)data
[6]) << 8 )
211 | ( ((ULong
)data
[5]) << 16 )
212 | ( ((ULong
)data
[4]) << 24 )
213 | ( ((ULong
)data
[3]) << 32 )
214 | ( ((ULong
)data
[2]) << 40 )
215 | ( ((ULong
)data
[1]) << 48 )
216 | ( ((ULong
)data
[0]) << 56 );
221 UChar
* ML_(writeUAS_ULong
) ( UChar
* ptr
, ULong val
) {
222 if (host_is_little_endian()) {
224 ptr
[1] = ( val
>> 8 ) & 0xff;
225 ptr
[2] = ( val
>> 16 ) & 0xff;
226 ptr
[3] = ( val
>> 24 ) & 0xff;
227 ptr
[4] = ( val
>> 32 ) & 0xff;
228 ptr
[5] = ( val
>> 40 ) & 0xff;
229 ptr
[6] = ( val
>> 48 ) & 0xff;
230 ptr
[7] = ( val
>> 56 ) & 0xff;
232 ptr
[0] = ( val
>> 56 ) & 0xff;
233 ptr
[1] = ( val
>> 48 ) & 0xff;
234 ptr
[2] = ( val
>> 40 ) & 0xff;
235 ptr
[3] = ( val
>> 32 ) & 0xff;
236 ptr
[4] = ( val
>> 24 ) & 0xff;
237 ptr
[5] = ( val
>> 16 ) & 0xff;
238 ptr
[6] = ( val
>> 8 ) & 0xff;
241 return ptr
+ sizeof(ULong
);
245 Addr
ML_(readUAS_Addr
) ( const UChar
* data
) {
246 if (sizeof(Addr
) == sizeof(UInt
)) {
247 return ML_(read_UInt
)(data
);
248 } else if (sizeof(Addr
) == sizeof(ULong
)) {
249 return ML_(read_ULong
)(data
);
255 UChar
* ML_(writeUAS_Addr
) ( UChar
* ptr
, Addr val
) {
256 if (sizeof(Addr
) == sizeof(UInt
)) {
257 return ML_(write_UInt
)(ptr
, val
);
258 } else if (sizeof(Addr
) == sizeof(ULong
)) {
259 return ML_(write_ULong
)(ptr
, val
);
265 /*--------------------------------------------------------------------*/
266 /*--- end misc.c ---*/
267 /*--------------------------------------------------------------------*/