blob: 34331730eef2a2970dafe5a9d9ca24a61baef739 [file] [log] [blame]
njn97405b22005-06-02 03:39:33 +00001
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
sewardjb3a1e4b2015-08-21 11:32:26 +000010 Copyright (C) 2000-2015 Julian Seward
njn97405b22005-06-02 03:39:33 +000011 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
florian535fb1b2013-09-15 13:54:34 +000034#include "pub_tool_basics.h" // VG_ macro
35
njn97405b22005-06-02 03:39:33 +000036/* ---------------------------------------------------------------------
37 Char functions.
38 ------------------------------------------------------------------ */
39
florian19f91bb2012-11-10 22:29:54 +000040extern Bool VG_(isspace) ( HChar c );
41extern Bool VG_(isdigit) ( HChar c );
42extern HChar VG_(tolower) ( HChar c );
njn97405b22005-06-02 03:39:33 +000043
44/* ---------------------------------------------------------------------
45 Converting strings to numbers
46 ------------------------------------------------------------------ */
47
njnea5d2352007-11-11 21:58:21 +000048// Convert strings to numbers according to various bases. Leading
49// whitespace is ignored. A subsequent '-' or '+' is accepted. For strtoll16,
50// accepts an initial "0x" or "0X" prefix, but only if it's followed by a
51// hex digit (if not, the '0' will be read and then it will stop on the
52// "x"/"X".) If 'endptr' isn't NULL, it gets filled in with the first
njn8a0b7042009-02-20 06:10:44 +000053// non-digit char. Returns 0 if no number could be converted, and 'endptr'
54// is set to the start of the string. None of them test that the number
55// fits into 64 bits.
njnea5d2352007-11-11 21:58:21 +000056//
njn83df0b62009-02-25 01:01:05 +000057// Nb: we also don't provide VG_(atoll*); these functions are worse than
58// useless because they don't do any error checking and so accept malformed
59// numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0!
60// If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)".
florian19f91bb2012-11-10 22:29:54 +000061extern Long VG_(strtoll10) ( const HChar* str, HChar** endptr );
62extern Long VG_(strtoll16) ( const HChar* str, HChar** endptr );
63extern ULong VG_(strtoull10) ( const HChar* str, HChar** endptr );
64extern ULong VG_(strtoull16) ( const HChar* str, HChar** endptr );
njnea5d2352007-11-11 21:58:21 +000065
njn83df0b62009-02-25 01:01:05 +000066// Convert a string to a double. After leading whitespace is ignored, a
67// '+' or '-' is allowed, and then it accepts a non-empty sequence of
68// decimal digits possibly containing a '.'. Hexadecimal floats are not
69// accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
florian19f91bb2012-11-10 22:29:54 +000070extern double VG_(strtod) ( const HChar* str, HChar** endptr );
njnea5d2352007-11-11 21:58:21 +000071
njn97405b22005-06-02 03:39:33 +000072/* ---------------------------------------------------------------------
73 String functions and macros
74 ------------------------------------------------------------------ */
75
njn83df0b62009-02-25 01:01:05 +000076/* Use this for normal null-termination-style string comparison. */
njnfdec4032006-12-14 03:29:18 +000077#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
78 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn83df0b62009-02-25 01:01:05 +000079#define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
80 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000081
florian19f91bb2012-11-10 22:29:54 +000082extern SizeT VG_(strlen) ( const HChar* str );
83extern HChar* VG_(strcat) ( HChar* dest, const HChar* src );
84extern HChar* VG_(strncat) ( HChar* dest, const HChar* src, SizeT n );
85extern HChar* VG_(strpbrk) ( const HChar* s, const HChar* accpt );
86extern HChar* VG_(strcpy) ( HChar* dest, const HChar* src );
87extern HChar* VG_(strncpy) ( HChar* dest, const HChar* src, SizeT ndest );
88extern Int VG_(strcmp) ( const HChar* s1, const HChar* s2 );
89extern Int VG_(strcasecmp) ( const HChar* s1, const HChar* s2 );
90extern Int VG_(strncmp) ( const HChar* s1, const HChar* s2, SizeT nmax );
91extern Int VG_(strncasecmp) ( const HChar* s1, const HChar* s2, SizeT nmax );
92extern HChar* VG_(strstr) ( const HChar* haystack, const HChar* needle );
93extern HChar* VG_(strcasestr) ( const HChar* haystack, const HChar* needle );
94extern HChar* VG_(strchr) ( const HChar* s, HChar c );
95extern HChar* VG_(strrchr) ( const HChar* s, HChar c );
96extern SizeT VG_(strspn) ( const HChar* s, const HChar* accpt );
97extern SizeT VG_(strcspn) ( const HChar* s, const HChar* reject );
njn97405b22005-06-02 03:39:33 +000098
sewardj3b290482011-05-06 21:02:55 +000099/* strtok* functions and some parsing utilities. */
florian19f91bb2012-11-10 22:29:54 +0000100extern HChar* VG_(strtok_r) (HChar* s, const HChar* delim, HChar** saveptr);
101extern HChar* VG_(strtok) (HChar* s, const HChar* delim);
sewardj3b290482011-05-06 21:02:55 +0000102
103/* Parse a 32- or 64-bit hex number, including leading 0x, from string
Elliott Hughesa0664b92017-04-18 17:46:52 -0700104 starting at *ppc, putting result in *result, advance *ppc past the
105 characters used, and return True. Or fail, in which case *ppc and
106 *result are undefined, and return False. */
florian19f91bb2012-11-10 22:29:54 +0000107extern Bool VG_(parse_Addr) ( const HChar** ppc, Addr* result );
sewardj3b290482011-05-06 21:02:55 +0000108
Elliott Hughesa0664b92017-04-18 17:46:52 -0700109/* Parse an unsigned 32 bit number, written using decimals only.
110 Calling conventions are the same as for VG_(parse_Addr). */
111extern Bool VG_(parse_UInt) ( const HChar** ppc, UInt* result );
112
philippe0c2923f2014-04-19 09:52:32 +0000113/* Parse an "enum set" made of one or more words comma separated.
philippea7d70b12014-04-19 10:27:28 +0000114 The allowed word values are given in 'tokens', separated by comma.
115 If a word in 'tokens' is found in 'input', the corresponding bit
116 will be set in *enum_set (words in 'tokens' are numbered starting from 0).
117 Using in 'tokens' the special token "-" (a minus character) indicates that
118 the corresponding bit position cannot be set.
119 In addition to the words specified in 'tokens', VG_(parse_enum_set)
philippeec905f72014-08-17 20:03:51 +0000120 automatically accept the word "none" to indicate an empty enum_set (0).
121 If allow_all, VG_(parse_enum_set) automatically accept the word "all"
122 to indicate an enum_set with all bits corresponding to the words in tokens
123 set.
philippea7d70b12014-04-19 10:27:28 +0000124 If "none" or "all" is present in 'input', no other word can be given
125 in 'input'.
philippe0c2923f2014-04-19 09:52:32 +0000126 If parsing is successful, returns True and sets *enum_set.
127 If parsing fails, returns False. */
128extern Bool VG_(parse_enum_set) ( const HChar *tokens,
philippeec905f72014-08-17 20:03:51 +0000129 Bool allow_all,
philippe0c2923f2014-04-19 09:52:32 +0000130 const HChar *input,
131 UInt *enum_set);
132
njn97405b22005-06-02 03:39:33 +0000133/* ---------------------------------------------------------------------
134 mem* functions
135 ------------------------------------------------------------------ */
136
137extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
sewardjbbec7722007-11-25 14:08:53 +0000138extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
njn97405b22005-06-02 03:39:33 +0000139extern void* VG_(memset) ( void *s, Int c, SizeT sz );
140extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
141
sewardjcc490742014-01-13 00:17:54 +0000142/* Zero out up to 12 words quickly in-line. Do not use this for blocks
sewardjf7183e32010-03-14 17:19:02 +0000143 of size which are unknown at compile time, since the whole point is
144 for it to be inlined, and then for gcc to remove all code except
145 for the relevant 'sz' case. */
146inline __attribute__((always_inline))
147static void VG_(bzero_inline) ( void* s, SizeT sz )
148{
149 if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1)))
150 && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) {
151 UWord* p = (UWord*)s;
152 switch (sz / (SizeT)sizeof(UWord)) {
sewardjcc490742014-01-13 00:17:54 +0000153 case 12: p[0] = p[1] = p[2] = p[3]
154 = p[4] = p[5] = p[6] = p[7]
155 = p[8] = p[9] = p[10] = p[11] = 0UL; return;
156 case 11: p[0] = p[1] = p[2] = p[3]
157 = p[4] = p[5] = p[6] = p[7]
158 = p[8] = p[9] = p[10] = 0UL; return;
159 case 10: p[0] = p[1] = p[2] = p[3]
160 = p[4] = p[5] = p[6] = p[7]
161 = p[8] = p[9] = 0UL; return;
162 case 9: p[0] = p[1] = p[2] = p[3]
163 = p[4] = p[5] = p[6] = p[7]
164 = p[8] = 0UL; return;
sewardjf7183e32010-03-14 17:19:02 +0000165 case 8: p[0] = p[1] = p[2] = p[3]
166 = p[4] = p[5] = p[6] = p[7] = 0UL; return;
167 case 7: p[0] = p[1] = p[2] = p[3]
168 = p[4] = p[5] = p[6] = 0UL; return;
169 case 6: p[0] = p[1] = p[2] = p[3]
170 = p[4] = p[5] = 0UL; return;
171 case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return;
172 case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return;
173 case 3: p[0] = p[1] = p[2] = 0UL; return;
174 case 2: p[0] = p[1] = 0UL; return;
sewardj39d234a2010-03-15 09:03:25 +0000175 case 1: p[0] = 0UL; return;
sewardjf7183e32010-03-14 17:19:02 +0000176 case 0: return;
177 default: break;
178 }
179 }
180 VG_(memset)(s, 0, sz);
181}
182
183
njn97405b22005-06-02 03:39:33 +0000184/* ---------------------------------------------------------------------
185 Address computation helpers
186 ------------------------------------------------------------------ */
187
188// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +0000189#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +0000190#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
191#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
192#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
njnf76d27a2009-05-28 01:53:07 +0000193#define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
njn97405b22005-06-02 03:39:33 +0000194#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
195#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
196
njn5363a562005-06-12 04:34:51 +0000197// 'a' -- the alignment -- must be a power of 2.
198// The latter two require the vki-*.h header to be imported also.
199#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
200#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
201#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
202#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
203
njn97405b22005-06-02 03:39:33 +0000204/* ---------------------------------------------------------------------
205 Misc useful functions
206 ------------------------------------------------------------------ */
207
njnfab29902008-03-03 02:15:03 +0000208/* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
209 * to be a shell sort, but is now a quicksort. */
njn97405b22005-06-02 03:39:33 +0000210extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
florian6bd9dc12012-11-23 16:17:43 +0000211 Int (*compar)(const void*, const void*) );
njn97405b22005-06-02 03:39:33 +0000212
sewardjaebbf1c2011-06-13 13:14:00 +0000213/* Returns the base-2 logarithm of a 32 bit unsigned number. Returns
214 -1 if it is not a power of two. Nb: VG_(log2)(1) == 0. */
sewardjb8b79ad2008-03-03 01:35:41 +0000215extern Int VG_(log2) ( UInt x );
njn97405b22005-06-02 03:39:33 +0000216
sewardjaebbf1c2011-06-13 13:14:00 +0000217/* Ditto for 64 bit unsigned numbers. */
218extern Int VG_(log2_64)( ULong x );
219
sewardj45f4e7c2005-09-27 19:20:21 +0000220// A pseudo-random number generator returning a random UInt. If pSeed
221// is NULL, it uses its own seed, which starts at zero. If pSeed is
222// non-NULL, it uses and updates whatever pSeed points at.
223extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
njn9828b342005-07-08 04:08:59 +0000224
sewardj5d616df2013-07-02 08:07:15 +0000225/* Update a running Adler-32 checksum with the bytes buf[0..len-1] and
226 return the updated checksum. If buf is NULL, this function returns
227 the required initial value for the checksum. An Adler-32 checksum is
228 almost as reliable as a CRC32 but can be computed much faster. */
229extern UInt VG_(adler32)( UInt adler, const UChar* buf, UInt len);
230
njn97405b22005-06-02 03:39:33 +0000231#endif // __PUB_TOOL_LIBCBASE_H
232
233/*--------------------------------------------------------------------*/
234/*--- end ---*/
235/*--------------------------------------------------------------------*/