1
2 /*--------------------------------------------------------------------*/
3 /*--- Standalone libc stuff. pub_tool_libcbase.h ---*/
4 /*--------------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2011 Julian Seward
11 jseward@acm.org
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
31 #ifndef __PUB_TOOL_LIBCBASE_H
32 #define __PUB_TOOL_LIBCBASE_H
33
34 /* ---------------------------------------------------------------------
35 Char functions.
36 ------------------------------------------------------------------ */
37
38 extern Bool VG_(isspace) ( Char c );
39 extern Bool VG_(isdigit) ( Char c );
40 extern Char VG_(tolower) ( Char c );
41
42 /* ---------------------------------------------------------------------
43 Converting strings to numbers
44 ------------------------------------------------------------------ */
45
46 // Convert strings to numbers according to various bases. Leading
47 // whitespace is ignored. A subsequent '-' or '+' is accepted. For strtoll16,
48 // accepts an initial "0x" or "0X" prefix, but only if it's followed by a
49 // hex digit (if not, the '0' will be read and then it will stop on the
50 // "x"/"X".) If 'endptr' isn't NULL, it gets filled in with the first
51 // non-digit char. Returns 0 if no number could be converted, and 'endptr'
52 // is set to the start of the string. None of them test that the number
53 // fits into 64 bits.
54 //
55 // Nb: if you're wondering why we don't just have a single VG_(strtoll) which
56 // takes a base, it's because I wanted it to assert if it was given a bogus
57 // base (the standard glibc one sets 'errno' in this case). But
58 // m_libcbase.c doesn't import any code, not even vg_assert. --njn
59 //
60 // Nb: we also don't provide VG_(atoll*); these functions are worse than
61 // useless because they don't do any error checking and so accept malformed
62 // numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0!
63 // If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)".
64 extern Long VG_(strtoll10) ( Char* str, Char** endptr );
65 extern Long VG_(strtoll16) ( Char* str, Char** endptr );
66 extern ULong VG_(strtoull10) ( Char* str, Char** endptr );
67 extern ULong VG_(strtoull16) ( Char* str, Char** endptr );
68
69 // Convert a string to a double. After leading whitespace is ignored, a
70 // '+' or '-' is allowed, and then it accepts a non-empty sequence of
71 // decimal digits possibly containing a '.'. Hexadecimal floats are not
72 // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
73 extern double VG_(strtod) ( Char* str, Char** endptr );
74
75 /* ---------------------------------------------------------------------
76 String functions and macros
77 ------------------------------------------------------------------ */
78
79 /* Use this for normal null-termination-style string comparison. */
80 #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
81 && VG_(strcmp)((s1),(s2))==0) ? True : False )
82 #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
83 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
84
85 extern SizeT VG_(strlen) ( const Char* str );
86 extern Char* VG_(strcat) ( Char* dest, const Char* src );
87 extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
88 extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
89 extern Char* VG_(strcpy) ( Char* dest, const Char* src );
90 extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
91 extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
92 extern Int VG_(strcasecmp) ( const Char* s1, const Char* s2 );
93 extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
94 extern Int VG_(strncasecmp) ( const Char* s1, const Char* s2, SizeT nmax );
95 extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
96 extern Char* VG_(strcasestr) ( const Char* haystack, Char* needle );
97 extern Char* VG_(strchr) ( const Char* s, Char c );
98 extern Char* VG_(strrchr) ( const Char* s, Char c );
99 extern SizeT VG_(strspn) ( const Char* s, const Char* accpt );
100 extern SizeT VG_(strcspn) ( const Char* s, const char* reject );
101
102 /* strtok* functions and some parsing utilities. */
103 extern Char* VG_(strtok_r) (Char* s, const Char* delim, Char** saveptr);
104 extern Char* VG_(strtok) (Char* s, const Char* delim);
105
106 /* Parse a 32- or 64-bit hex number, including leading 0x, from string
107 starting at *ppc, putting result in *result, and return True. Or
108 fail, in which case *ppc and *result are undefined, and return
109 False. */
110 extern Bool VG_(parse_Addr) ( UChar** ppc, Addr* result );
111
112 /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
113 last character. */
114 extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
115
116 /* ---------------------------------------------------------------------
117 mem* functions
118 ------------------------------------------------------------------ */
119
120 extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
121 extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
122 extern void* VG_(memset) ( void *s, Int c, SizeT sz );
123 extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
124
125 /* Zero out up to 8 words quickly in-line. Do not use this for blocks
126 of size which are unknown at compile time, since the whole point is
127 for it to be inlined, and then for gcc to remove all code except
128 for the relevant 'sz' case. */
129 inline __attribute__((always_inline))
VG_(bzero_inline)130 static void VG_(bzero_inline) ( void* s, SizeT sz )
131 {
132 if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1)))
133 && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) {
134 UWord* p = (UWord*)s;
135 switch (sz / (SizeT)sizeof(UWord)) {
136 case 8: p[0] = p[1] = p[2] = p[3]
137 = p[4] = p[5] = p[6] = p[7] = 0UL; return;
138 case 7: p[0] = p[1] = p[2] = p[3]
139 = p[4] = p[5] = p[6] = 0UL; return;
140 case 6: p[0] = p[1] = p[2] = p[3]
141 = p[4] = p[5] = 0UL; return;
142 case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return;
143 case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return;
144 case 3: p[0] = p[1] = p[2] = 0UL; return;
145 case 2: p[0] = p[1] = 0UL; return;
146 case 1: p[0] = 0UL; return;
147 case 0: return;
148 default: break;
149 }
150 }
151 VG_(memset)(s, 0, sz);
152 }
153
154
155 /* ---------------------------------------------------------------------
156 Address computation helpers
157 ------------------------------------------------------------------ */
158
159 // Check if an address/whatever is aligned
160 #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
161 #define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
162 #define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
163 #define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
164 #define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
165 #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
166 #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
167
168 // 'a' -- the alignment -- must be a power of 2.
169 // The latter two require the vki-*.h header to be imported also.
170 #define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
171 #define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
172 #define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
173 #define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
174
175 /* ---------------------------------------------------------------------
176 Misc useful functions
177 ------------------------------------------------------------------ */
178
179 /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
180 * to be a shell sort, but is now a quicksort. */
181 extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
182 Int (*compar)(void*, void*) );
183
184 /* Returns the base-2 logarithm of a 32 bit unsigned number. Returns
185 -1 if it is not a power of two. Nb: VG_(log2)(1) == 0. */
186 extern Int VG_(log2) ( UInt x );
187
188 /* Ditto for 64 bit unsigned numbers. */
189 extern Int VG_(log2_64)( ULong x );
190
191 // A pseudo-random number generator returning a random UInt. If pSeed
192 // is NULL, it uses its own seed, which starts at zero. If pSeed is
193 // non-NULL, it uses and updates whatever pSeed points at.
194 extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
195 #define VG_RAND_MAX (1ULL << 32)
196
197 #endif // __PUB_TOOL_LIBCBASE_H
198
199 /*--------------------------------------------------------------------*/
200 /*--- end ---*/
201 /*--------------------------------------------------------------------*/
202