1
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
10   Copyright (C) 2008-2013 OpenWorks LLP
11      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
45void* ML_(dinfo_zalloc) ( const HChar* cc, SizeT szB ) {
46   void* v;
47   vg_assert(szB > 0);
48   v = VG_(arena_malloc)( VG_AR_DINFO, cc, szB );
49   VG_(memset)(v, 0, szB);
50   return v;
51}
52
53void  ML_(dinfo_shrink_block)( void* ptr, SizeT szB ) {
54   VG_(arena_realloc_shrink)( VG_AR_DINFO, ptr, szB );
55}
56
57void ML_(dinfo_free) ( void* v ) {
58   VG_(arena_free)( VG_AR_DINFO, v );
59}
60
61HChar* ML_(dinfo_strdup) ( const HChar* cc, const HChar* str ) {
62   return VG_(arena_strdup)( VG_AR_DINFO, cc, str );
63}
64
65void* 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);
68   return dst;
69}
70
71void* 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
77static inline Bool host_is_little_endian ( void ) {
78   UInt x = 0x76543210;
79   UChar* p = (UChar*)(&x);
80   return toBool(*p == 0x10);
81}
82
83Short ML_(readUAS_Short)( const UChar* data ) {
84   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
95Int ML_(readUAS_Int) ( const UChar* data ) {
96   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
111Long ML_(readUAS_Long) ( const UChar* data ) {
112   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
135UShort ML_(readUAS_UShort) ( const UChar* data ) {
136   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
147UChar *ML_(writeUAS_UShort) ( UChar* ptr, UShort val ) {
148   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
158UWord 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);
163   } else {
164      vg_assert(0);
165   }
166}
167
168UInt ML_(readUAS_UInt) ( const UChar* data ) {
169   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
184UChar* ML_(writeUAS_UInt) ( UChar* ptr, UInt val ) {
185   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
199ULong ML_(readUAS_ULong) ( const UChar* data ) {
200   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
223UChar* ML_(writeUAS_ULong) ( UChar* ptr, ULong val ) {
224   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
246
247Addr 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);
252   } else {
253      vg_assert(0);
254   }
255}
256
257UChar* 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);
262   } else {
263      vg_assert(0);
264   }
265}
266
267/*--------------------------------------------------------------------*/
268/*--- end                                                   misc.c ---*/
269/*--------------------------------------------------------------------*/
270