njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2013 Julian Seward |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 | |
florian | 535fb1b | 2013-09-15 13:54:34 +0000 | [diff] [blame] | 34 | #include "pub_tool_basics.h" // VG_ macro |
| 35 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | /* --------------------------------------------------------------------- |
| 37 | Char functions. |
| 38 | ------------------------------------------------------------------ */ |
| 39 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 40 | extern Bool VG_(isspace) ( HChar c ); |
| 41 | extern Bool VG_(isdigit) ( HChar c ); |
| 42 | extern HChar VG_(tolower) ( HChar c ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 43 | |
| 44 | /* --------------------------------------------------------------------- |
| 45 | Converting strings to numbers |
| 46 | ------------------------------------------------------------------ */ |
| 47 | |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 48 | // 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 |
njn | 8a0b704 | 2009-02-20 06:10:44 +0000 | [diff] [blame] | 53 | // 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. |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 56 | // |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 57 | // Nb: if you're wondering why we don't just have a single VG_(strtoll) which |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 58 | // takes a base, it's because I wanted it to assert if it was given a bogus |
| 59 | // base (the standard glibc one sets 'errno' in this case). But |
| 60 | // m_libcbase.c doesn't import any code, not even vg_assert. --njn |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 61 | // |
| 62 | // Nb: we also don't provide VG_(atoll*); these functions are worse than |
| 63 | // useless because they don't do any error checking and so accept malformed |
| 64 | // numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0! |
| 65 | // If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)". |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 66 | extern Long VG_(strtoll10) ( const HChar* str, HChar** endptr ); |
| 67 | extern Long VG_(strtoll16) ( const HChar* str, HChar** endptr ); |
| 68 | extern ULong VG_(strtoull10) ( const HChar* str, HChar** endptr ); |
| 69 | extern ULong VG_(strtoull16) ( const HChar* str, HChar** endptr ); |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 70 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 71 | // Convert a string to a double. After leading whitespace is ignored, a |
| 72 | // '+' or '-' is allowed, and then it accepts a non-empty sequence of |
| 73 | // decimal digits possibly containing a '.'. Hexadecimal floats are not |
| 74 | // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN"). |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 75 | extern double VG_(strtod) ( const HChar* str, HChar** endptr ); |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 76 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 77 | /* --------------------------------------------------------------------- |
| 78 | String functions and macros |
| 79 | ------------------------------------------------------------------ */ |
| 80 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 81 | /* Use this for normal null-termination-style string comparison. */ |
njn | fdec403 | 2006-12-14 03:29:18 +0000 | [diff] [blame] | 82 | #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 83 | && VG_(strcmp)((s1),(s2))==0) ? True : False ) |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 84 | #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 85 | && VG_(strncmp)((s1),(s2),(n))==0) ? True : False ) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 86 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 87 | extern SizeT VG_(strlen) ( const HChar* str ); |
| 88 | extern HChar* VG_(strcat) ( HChar* dest, const HChar* src ); |
| 89 | extern HChar* VG_(strncat) ( HChar* dest, const HChar* src, SizeT n ); |
| 90 | extern HChar* VG_(strpbrk) ( const HChar* s, const HChar* accpt ); |
| 91 | extern HChar* VG_(strcpy) ( HChar* dest, const HChar* src ); |
| 92 | extern HChar* VG_(strncpy) ( HChar* dest, const HChar* src, SizeT ndest ); |
| 93 | extern Int VG_(strcmp) ( const HChar* s1, const HChar* s2 ); |
| 94 | extern Int VG_(strcasecmp) ( const HChar* s1, const HChar* s2 ); |
| 95 | extern Int VG_(strncmp) ( const HChar* s1, const HChar* s2, SizeT nmax ); |
| 96 | extern Int VG_(strncasecmp) ( const HChar* s1, const HChar* s2, SizeT nmax ); |
| 97 | extern HChar* VG_(strstr) ( const HChar* haystack, const HChar* needle ); |
| 98 | extern HChar* VG_(strcasestr) ( const HChar* haystack, const HChar* needle ); |
| 99 | extern HChar* VG_(strchr) ( const HChar* s, HChar c ); |
| 100 | extern HChar* VG_(strrchr) ( const HChar* s, HChar c ); |
| 101 | extern SizeT VG_(strspn) ( const HChar* s, const HChar* accpt ); |
| 102 | extern SizeT VG_(strcspn) ( const HChar* s, const HChar* reject ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 103 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 104 | /* strtok* functions and some parsing utilities. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 105 | extern HChar* VG_(strtok_r) (HChar* s, const HChar* delim, HChar** saveptr); |
| 106 | extern HChar* VG_(strtok) (HChar* s, const HChar* delim); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 107 | |
| 108 | /* Parse a 32- or 64-bit hex number, including leading 0x, from string |
| 109 | starting at *ppc, putting result in *result, and return True. Or |
| 110 | fail, in which case *ppc and *result are undefined, and return |
| 111 | False. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 112 | extern Bool VG_(parse_Addr) ( const HChar** ppc, Addr* result ); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 113 | |
philippe | 0c2923f | 2014-04-19 09:52:32 +0000 | [diff] [blame] | 114 | /* Parse an "enum set" made of one or more words comma separated. |
philippe | a7d70b1 | 2014-04-19 10:27:28 +0000 | [diff] [blame] | 115 | The allowed word values are given in 'tokens', separated by comma. |
| 116 | If a word in 'tokens' is found in 'input', the corresponding bit |
| 117 | will be set in *enum_set (words in 'tokens' are numbered starting from 0). |
| 118 | Using in 'tokens' the special token "-" (a minus character) indicates that |
| 119 | the corresponding bit position cannot be set. |
| 120 | In addition to the words specified in 'tokens', VG_(parse_enum_set) |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame^] | 121 | automatically accept the word "none" to indicate an empty enum_set (0). |
| 122 | If allow_all, VG_(parse_enum_set) automatically accept the word "all" |
| 123 | to indicate an enum_set with all bits corresponding to the words in tokens |
| 124 | set. |
philippe | a7d70b1 | 2014-04-19 10:27:28 +0000 | [diff] [blame] | 125 | If "none" or "all" is present in 'input', no other word can be given |
| 126 | in 'input'. |
philippe | 0c2923f | 2014-04-19 09:52:32 +0000 | [diff] [blame] | 127 | If parsing is successful, returns True and sets *enum_set. |
| 128 | If parsing fails, returns False. */ |
| 129 | extern Bool VG_(parse_enum_set) ( const HChar *tokens, |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame^] | 130 | Bool allow_all, |
philippe | 0c2923f | 2014-04-19 09:52:32 +0000 | [diff] [blame] | 131 | const HChar *input, |
| 132 | UInt *enum_set); |
| 133 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 134 | /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the |
| 135 | last character. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 136 | extern void VG_(strncpy_safely) ( HChar* dest, const HChar* src, SizeT ndest ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 137 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 138 | /* --------------------------------------------------------------------- |
| 139 | mem* functions |
| 140 | ------------------------------------------------------------------ */ |
| 141 | |
| 142 | extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz ); |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 143 | extern void* VG_(memmove)( void *d, const void *s, SizeT sz ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 144 | extern void* VG_(memset) ( void *s, Int c, SizeT sz ); |
| 145 | extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n ); |
| 146 | |
sewardj | cc49074 | 2014-01-13 00:17:54 +0000 | [diff] [blame] | 147 | /* Zero out up to 12 words quickly in-line. Do not use this for blocks |
sewardj | f7183e3 | 2010-03-14 17:19:02 +0000 | [diff] [blame] | 148 | of size which are unknown at compile time, since the whole point is |
| 149 | for it to be inlined, and then for gcc to remove all code except |
| 150 | for the relevant 'sz' case. */ |
| 151 | inline __attribute__((always_inline)) |
| 152 | static void VG_(bzero_inline) ( void* s, SizeT sz ) |
| 153 | { |
| 154 | if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1))) |
| 155 | && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) { |
| 156 | UWord* p = (UWord*)s; |
| 157 | switch (sz / (SizeT)sizeof(UWord)) { |
sewardj | cc49074 | 2014-01-13 00:17:54 +0000 | [diff] [blame] | 158 | case 12: p[0] = p[1] = p[2] = p[3] |
| 159 | = p[4] = p[5] = p[6] = p[7] |
| 160 | = p[8] = p[9] = p[10] = p[11] = 0UL; return; |
| 161 | case 11: p[0] = p[1] = p[2] = p[3] |
| 162 | = p[4] = p[5] = p[6] = p[7] |
| 163 | = p[8] = p[9] = p[10] = 0UL; return; |
| 164 | case 10: p[0] = p[1] = p[2] = p[3] |
| 165 | = p[4] = p[5] = p[6] = p[7] |
| 166 | = p[8] = p[9] = 0UL; return; |
| 167 | case 9: p[0] = p[1] = p[2] = p[3] |
| 168 | = p[4] = p[5] = p[6] = p[7] |
| 169 | = p[8] = 0UL; return; |
sewardj | f7183e3 | 2010-03-14 17:19:02 +0000 | [diff] [blame] | 170 | case 8: p[0] = p[1] = p[2] = p[3] |
| 171 | = p[4] = p[5] = p[6] = p[7] = 0UL; return; |
| 172 | case 7: p[0] = p[1] = p[2] = p[3] |
| 173 | = p[4] = p[5] = p[6] = 0UL; return; |
| 174 | case 6: p[0] = p[1] = p[2] = p[3] |
| 175 | = p[4] = p[5] = 0UL; return; |
| 176 | case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return; |
| 177 | case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return; |
| 178 | case 3: p[0] = p[1] = p[2] = 0UL; return; |
| 179 | case 2: p[0] = p[1] = 0UL; return; |
sewardj | 39d234a | 2010-03-15 09:03:25 +0000 | [diff] [blame] | 180 | case 1: p[0] = 0UL; return; |
sewardj | f7183e3 | 2010-03-14 17:19:02 +0000 | [diff] [blame] | 181 | case 0: return; |
| 182 | default: break; |
| 183 | } |
| 184 | } |
| 185 | VG_(memset)(s, 0, sz); |
| 186 | } |
| 187 | |
| 188 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 189 | /* --------------------------------------------------------------------- |
| 190 | Address computation helpers |
| 191 | ------------------------------------------------------------------ */ |
| 192 | |
| 193 | // Check if an address/whatever is aligned |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 194 | #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 195 | #define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3))) |
| 196 | #define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7))) |
| 197 | #define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf))) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 198 | #define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 199 | #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1)))) |
| 200 | #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1)))) |
| 201 | |
njn | 5363a56 | 2005-06-12 04:34:51 +0000 | [diff] [blame] | 202 | // 'a' -- the alignment -- must be a power of 2. |
| 203 | // The latter two require the vki-*.h header to be imported also. |
| 204 | #define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1)) |
| 205 | #define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a)) |
| 206 | #define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE) |
| 207 | #define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE) |
| 208 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 209 | /* --------------------------------------------------------------------- |
| 210 | Misc useful functions |
| 211 | ------------------------------------------------------------------ */ |
| 212 | |
njn | fab2990 | 2008-03-03 02:15:03 +0000 | [diff] [blame] | 213 | /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used |
| 214 | * to be a shell sort, but is now a quicksort. */ |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 215 | extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size, |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 216 | Int (*compar)(const void*, const void*) ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 217 | |
sewardj | aebbf1c | 2011-06-13 13:14:00 +0000 | [diff] [blame] | 218 | /* Returns the base-2 logarithm of a 32 bit unsigned number. Returns |
| 219 | -1 if it is not a power of two. Nb: VG_(log2)(1) == 0. */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 220 | extern Int VG_(log2) ( UInt x ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 221 | |
sewardj | aebbf1c | 2011-06-13 13:14:00 +0000 | [diff] [blame] | 222 | /* Ditto for 64 bit unsigned numbers. */ |
| 223 | extern Int VG_(log2_64)( ULong x ); |
| 224 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 225 | // A pseudo-random number generator returning a random UInt. If pSeed |
| 226 | // is NULL, it uses its own seed, which starts at zero. If pSeed is |
| 227 | // non-NULL, it uses and updates whatever pSeed points at. |
| 228 | extern UInt VG_(random) ( /*MOD*/UInt* pSeed ); |
njn | 9828b34 | 2005-07-08 04:08:59 +0000 | [diff] [blame] | 229 | |
sewardj | 5d616df | 2013-07-02 08:07:15 +0000 | [diff] [blame] | 230 | /* Update a running Adler-32 checksum with the bytes buf[0..len-1] and |
| 231 | return the updated checksum. If buf is NULL, this function returns |
| 232 | the required initial value for the checksum. An Adler-32 checksum is |
| 233 | almost as reliable as a CRC32 but can be computed much faster. */ |
| 234 | extern UInt VG_(adler32)( UInt adler, const UChar* buf, UInt len); |
| 235 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 236 | #endif // __PUB_TOOL_LIBCBASE_H |
| 237 | |
| 238 | /*--------------------------------------------------------------------*/ |
| 239 | /*--- end ---*/ |
| 240 | /*--------------------------------------------------------------------*/ |