1
2 /*--------------------------------------------------------------------*/
3 /*--- Misc simple stuff lacking a better home. priv_misc.h ---*/
4 /*--------------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2008-2015 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 #ifndef __PRIV_MISC_H
37 #define __PRIV_MISC_H
38
39 #include "pub_core_basics.h" // SizeT
40
41 /* Allocate(zeroed), free, strdup, memdup, shrink, all in VG_AR_DINFO.
42 The allocation functions never return NULL. */
43 void* ML_(dinfo_zalloc)( const HChar* cc, SizeT szB );
44 void ML_(dinfo_free)( void* v );
45 HChar* ML_(dinfo_strdup)( const HChar* cc, const HChar* str );
46 void* ML_(dinfo_memdup)( const HChar* cc, const void* str, SizeT nStr );
47 void* ML_(dinfo_realloc) ( const HChar* cc, void* ptr, SizeT new_size );
48 void ML_(dinfo_shrink_block)( void* ptr, SizeT szB );
49
50 /* Define functions to read/write types of various sizes from/to a
51 (potentially unaligned) UChar *data buffer.
52 Some archs can do efficient unaligned access. For these archs,
53 do the load/store directly. For others, call the UAS (Un Aligned Safe)
54 functions. */
55 #if defined(VGA_x86) || defined(VGA_amd64)
56
57 #define DEF_READ(type) \
58 static inline type VGAPPEND(vgModuleLocal_read_,type) ( const UChar* data ) \
59 { \
60 return (*(const type*)(data)); \
61 } \
62 type VGAPPEND(vgModuleLocal_readUAS_,type) ( const UChar* data )
63
64 #define DEF_WRITE(type) \
65 static inline UChar* VGAPPEND(vgModuleLocal_write_,type) ( UChar* ptr, type val ) \
66 { \
67 (*(type*)(ptr)) = val; \
68 return ptr + sizeof(type); \
69 } \
70 UChar* VGAPPEND(vgModuleLocal_writeUAS_,type) ( UChar* ptr, type val )
71
72 #else
73
74 #define DEF_READ(type) \
75 type VGAPPEND(vgModuleLocal_readUAS_,type) ( const UChar* data ); \
76 static inline type VGAPPEND(vgModuleLocal_read_,type) ( const UChar* data ) \
77 { \
78 return VGAPPEND(vgModuleLocal_readUAS_,type)(data); \
79 }
80
81 #define DEF_WRITE(type) \
82 UChar* VGAPPEND(vgModuleLocal_writeUAS_,type) ( UChar* ptr, type val ); \
83 static inline UChar* VGAPPEND(vgModuleLocal_write_,type) ( UChar* ptr, type val ) \
84 { \
85 return VGAPPEND(vgModuleLocal_writeUAS_,type)(ptr,val); \
86 }
87
88 #endif
89
90 /* Defines a bunch of functions such as
91 Short ML_(read_Short)( const UChar* data );
92 Int ML_(read_Int)( const UChar* data );
93 ... */
94 DEF_READ(Short);
95 DEF_READ(Int);
96 DEF_READ(Long);
97 DEF_READ(UShort);
98 DEF_READ(UWord);
99 DEF_READ(UInt);
100 DEF_READ(ULong);
101 DEF_READ(Addr);
102
103 /* Defines a bunch of functions such as
104 UChar* ML_(write_UShort)( UChar* ptr, UShort val );
105 UChar* ML_(write_UInt)( UChar* ptr, UInt val );
106 ... */
107 DEF_WRITE(UShort);
108 DEF_WRITE(UInt);
109 DEF_WRITE(ULong);
110 DEF_WRITE(Addr);
111
ML_(read_UChar)112 static inline UChar ML_(read_UChar)( const UChar* data )
113 {
114 return data[0];
115 }
ML_(write_UChar)116 static inline UChar* ML_(write_UChar)( UChar* ptr, UChar val )
117 {
118 ptr[0] = val;
119 return ptr + sizeof(UChar);
120 }
121
122 /* A handy type, a la Haskell's Maybe type. Yes, I know, C sucks.
123 Been there. Done that. Seen the movie. Got the T-shirt. Etc. */
124 typedef struct { ULong ul; Bool b; } MaybeULong;
125
126
127 #endif /* ndef __PRIV_MISC_H */
128
129 /*--------------------------------------------------------------------*/
130 /*--- end priv_misc.h ---*/
131 /*--------------------------------------------------------------------*/
132