Update README.s390
[valgrind.git] / coregrind / m_debuginfo / misc.c
blob910d9b2172abf1d599aff54aee593a31848ee03f
2 /*--------------------------------------------------------------------*/
3 /*--- Misc simple stuff lacking a better home. misc.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2008-2017 OpenWorks LLP
11 info@open-works.co.uk
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 ) {
44 void* v;
45 vg_assert(szB > 0);
46 v = VG_(arena_malloc)( VG_AR_DINFO, cc, szB );
47 VG_(memset)(v, 0, szB);
48 return v;
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);
66 return dst;
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 );
71 vg_assert(dst);
72 return dst;
75 static inline Bool host_is_little_endian ( void ) {
76 UInt x = 0x76543210;
77 UChar* p = (UChar*)(&x);
78 return toBool(*p == 0x10);
81 Short ML_(readUAS_Short)( const UChar* data ) {
82 Short r = 0;
83 if (host_is_little_endian()) {
84 r = data[0]
85 | ( ((UInt)data[1]) << 8 );
86 } else {
87 r = data[1]
88 | ( ((UInt)data[0]) << 8 );
90 return r;
93 Int ML_(readUAS_Int) ( const UChar* data ) {
94 Int r = 0;
95 if (host_is_little_endian()) {
96 r = data[0]
97 | ( ((UInt)data[1]) << 8 )
98 | ( ((UInt)data[2]) << 16 )
99 | ( ((UInt)data[3]) << 24 );
100 } else {
101 r = data[3]
102 | ( ((UInt)data[2]) << 8 )
103 | ( ((UInt)data[1]) << 16 )
104 | ( ((UInt)data[0]) << 24 );
106 return r;
109 Long ML_(readUAS_Long) ( const UChar* data ) {
110 Long r = 0;
111 if (host_is_little_endian()) {
112 r = data[0]
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 );
120 } else {
121 r = data[7]
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 );
130 return r;
133 UShort ML_(readUAS_UShort) ( const UChar* data ) {
134 UInt r = 0;
135 if (host_is_little_endian()) {
136 r = data[0]
137 | ( ((UInt)data[1]) << 8 );
138 } else {
139 r = data[1]
140 | ( ((UInt)data[0]) << 8 );
142 return r;
145 UChar *ML_(writeUAS_UShort) ( UChar* ptr, UShort val ) {
146 if (host_is_little_endian()) {
147 ptr[0] = val & 0xff;
148 ptr[1] = ( val >> 8 ) & 0xff;
149 } else {
150 ptr[0] = ( val >> 8 ) & 0xff;
151 ptr[1] = val & 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);
161 } else {
162 vg_assert(0);
166 UInt ML_(readUAS_UInt) ( const UChar* data ) {
167 UInt r = 0;
168 if (host_is_little_endian()) {
169 r = data[0]
170 | ( ((UInt)data[1]) << 8 )
171 | ( ((UInt)data[2]) << 16 )
172 | ( ((UInt)data[3]) << 24 );
173 } else {
174 r = data[3]
175 | ( ((UInt)data[2]) << 8 )
176 | ( ((UInt)data[1]) << 16 )
177 | ( ((UInt)data[0]) << 24 );
179 return r;
182 UChar* ML_(writeUAS_UInt) ( UChar* ptr, UInt val ) {
183 if (host_is_little_endian()) {
184 ptr[0] = val & 0xff;
185 ptr[1] = ( val >> 8 ) & 0xff;
186 ptr[2] = ( val >> 16 ) & 0xff;
187 ptr[3] = ( val >> 24 ) & 0xff;
188 } else {
189 ptr[0] = ( val >> 24 ) & 0xff;
190 ptr[1] = ( val >> 16 ) & 0xff;
191 ptr[2] = ( val >> 8 ) & 0xff;
192 ptr[3] = val & 0xff;
194 return ptr + sizeof(UInt);
197 ULong ML_(readUAS_ULong) ( const UChar* data ) {
198 ULong r = 0;
199 if (host_is_little_endian()) {
200 r = data[0]
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 );
208 } else {
209 r = data[7]
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 );
218 return r;
221 UChar* ML_(writeUAS_ULong) ( UChar* ptr, ULong val ) {
222 if (host_is_little_endian()) {
223 ptr[0] = val & 0xff;
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;
231 } else {
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;
239 ptr[7] = val & 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);
250 } else {
251 vg_assert(0);
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);
260 } else {
261 vg_assert(0);
265 /*--------------------------------------------------------------------*/
266 /*--- end misc.c ---*/
267 /*--------------------------------------------------------------------*/