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 | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2012 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 | |
| 34 | /* --------------------------------------------------------------------- |
| 35 | Char functions. |
| 36 | ------------------------------------------------------------------ */ |
| 37 | |
| 38 | extern Bool VG_(isspace) ( Char c ); |
| 39 | extern Bool VG_(isdigit) ( Char c ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 40 | extern Char VG_(tolower) ( Char c ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 41 | |
| 42 | /* --------------------------------------------------------------------- |
| 43 | Converting strings to numbers |
| 44 | ------------------------------------------------------------------ */ |
| 45 | |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 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 |
njn | 8a0b704 | 2009-02-20 06:10:44 +0000 | [diff] [blame] | 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. |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 54 | // |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 55 | // 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] | 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 |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 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)". |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 64 | extern Long VG_(strtoll10) ( Char* str, Char** endptr ); |
| 65 | extern Long VG_(strtoll16) ( Char* str, Char** endptr ); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 66 | extern ULong VG_(strtoull10) ( Char* str, Char** endptr ); |
| 67 | extern ULong VG_(strtoull16) ( Char* str, Char** endptr ); |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 68 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 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"). |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 73 | extern double VG_(strtod) ( Char* str, Char** endptr ); |
| 74 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 75 | /* --------------------------------------------------------------------- |
| 76 | String functions and macros |
| 77 | ------------------------------------------------------------------ */ |
| 78 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 79 | /* Use this for normal null-termination-style string comparison. */ |
njn | fdec403 | 2006-12-14 03:29:18 +0000 | [diff] [blame] | 80 | #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 81 | && VG_(strcmp)((s1),(s2))==0) ? True : False ) |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 82 | #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 83 | && VG_(strncmp)((s1),(s2),(n))==0) ? True : False ) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 84 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 85 | extern SizeT VG_(strlen) ( const Char* str ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 92 | extern Int VG_(strcasecmp) ( const Char* s1, const Char* s2 ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 93 | extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 94 | extern Int VG_(strncasecmp) ( const Char* s1, const Char* s2, SizeT nmax ); |
florian | 660c5a4 | 2012-10-18 03:17:32 +0000 | [diff] [blame^] | 95 | extern Char* VG_(strstr) ( const Char* haystack, const Char* needle ); |
| 96 | extern Char* VG_(strcasestr) ( const Char* haystack, const Char* needle ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 97 | extern Char* VG_(strchr) ( const Char* s, Char c ); |
| 98 | extern Char* VG_(strrchr) ( const Char* s, Char c ); |
njn | f9dcb3b | 2009-05-19 05:50:34 +0000 | [diff] [blame] | 99 | extern SizeT VG_(strspn) ( const Char* s, const Char* accpt ); |
florian | 660c5a4 | 2012-10-18 03:17:32 +0000 | [diff] [blame^] | 100 | extern SizeT VG_(strcspn) ( const Char* s, const Char* reject ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 101 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 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 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 116 | /* --------------------------------------------------------------------- |
| 117 | mem* functions |
| 118 | ------------------------------------------------------------------ */ |
| 119 | |
| 120 | extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz ); |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 121 | extern void* VG_(memmove)( void *d, const void *s, SizeT sz ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 | |
sewardj | f7183e3 | 2010-03-14 17:19:02 +0000 | [diff] [blame] | 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)) |
| 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; |
sewardj | 39d234a | 2010-03-15 09:03:25 +0000 | [diff] [blame] | 146 | case 1: p[0] = 0UL; return; |
sewardj | f7183e3 | 2010-03-14 17:19:02 +0000 | [diff] [blame] | 147 | case 0: return; |
| 148 | default: break; |
| 149 | } |
| 150 | } |
| 151 | VG_(memset)(s, 0, sz); |
| 152 | } |
| 153 | |
| 154 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 155 | /* --------------------------------------------------------------------- |
| 156 | Address computation helpers |
| 157 | ------------------------------------------------------------------ */ |
| 158 | |
| 159 | // Check if an address/whatever is aligned |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 160 | #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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))) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 164 | #define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 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 | |
njn | 5363a56 | 2005-06-12 04:34:51 +0000 | [diff] [blame] | 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 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 175 | /* --------------------------------------------------------------------- |
| 176 | Misc useful functions |
| 177 | ------------------------------------------------------------------ */ |
| 178 | |
njn | fab2990 | 2008-03-03 02:15:03 +0000 | [diff] [blame] | 179 | /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used |
| 180 | * to be a shell sort, but is now a quicksort. */ |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 181 | extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size, |
| 182 | Int (*compar)(void*, void*) ); |
| 183 | |
sewardj | aebbf1c | 2011-06-13 13:14:00 +0000 | [diff] [blame] | 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. */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 186 | extern Int VG_(log2) ( UInt x ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 187 | |
sewardj | aebbf1c | 2011-06-13 13:14:00 +0000 | [diff] [blame] | 188 | /* Ditto for 64 bit unsigned numbers. */ |
| 189 | extern Int VG_(log2_64)( ULong x ); |
| 190 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 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 ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 195 | #define VG_RAND_MAX (1ULL << 32) |
njn | 9828b34 | 2005-07-08 04:08:59 +0000 | [diff] [blame] | 196 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 197 | #endif // __PUB_TOOL_LIBCBASE_H |
| 198 | |
| 199 | /*--------------------------------------------------------------------*/ |
| 200 | /*--- end ---*/ |
| 201 | /*--------------------------------------------------------------------*/ |