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 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2009 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 ); |
| 40 | |
| 41 | /* --------------------------------------------------------------------- |
| 42 | Converting strings to numbers |
| 43 | ------------------------------------------------------------------ */ |
| 44 | |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 45 | // Convert strings to numbers according to various bases. Leading |
| 46 | // whitespace is ignored. A subsequent '-' or '+' is accepted. For strtoll16, |
| 47 | // accepts an initial "0x" or "0X" prefix, but only if it's followed by a |
| 48 | // hex digit (if not, the '0' will be read and then it will stop on the |
| 49 | // "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] | 50 | // non-digit char. Returns 0 if no number could be converted, and 'endptr' |
| 51 | // is set to the start of the string. None of them test that the number |
| 52 | // fits into 64 bits. |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 53 | // |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 54 | // 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] | 55 | // takes a base, it's because I wanted it to assert if it was given a bogus |
| 56 | // base (the standard glibc one sets 'errno' in this case). But |
| 57 | // m_libcbase.c doesn't import any code, not even vg_assert. --njn |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 58 | // |
| 59 | // Nb: we also don't provide VG_(atoll*); these functions are worse than |
| 60 | // useless because they don't do any error checking and so accept malformed |
| 61 | // numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0! |
| 62 | // If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)". |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 63 | extern Long VG_(strtoll10) ( Char* str, Char** endptr ); |
| 64 | extern Long VG_(strtoll16) ( Char* str, Char** endptr ); |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 65 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 66 | // 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"). |
njn | ea5d235 | 2007-11-11 21:58:21 +0000 | [diff] [blame] | 70 | extern double VG_(strtod) ( Char* str, Char** endptr ); |
| 71 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 72 | /* --------------------------------------------------------------------- |
| 73 | String functions and macros |
| 74 | ------------------------------------------------------------------ */ |
| 75 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 76 | /* Use this for normal null-termination-style string comparison. */ |
njn | fdec403 | 2006-12-14 03:29:18 +0000 | [diff] [blame] | 77 | #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 78 | && VG_(strcmp)((s1),(s2))==0) ? True : False ) |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 79 | #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 80 | && VG_(strncmp)((s1),(s2),(n))==0) ? True : False ) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 81 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 82 | extern SizeT VG_(strlen) ( const Char* str ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 83 | extern Char* VG_(strcat) ( Char* dest, const Char* src ); |
| 84 | extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n ); |
| 85 | extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt ); |
| 86 | extern Char* VG_(strcpy) ( Char* dest, const Char* src ); |
| 87 | extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest ); |
| 88 | extern Int VG_(strcmp) ( const Char* s1, const Char* s2 ); |
| 89 | extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax ); |
| 90 | extern Char* VG_(strstr) ( const Char* haystack, Char* needle ); |
| 91 | extern Char* VG_(strchr) ( const Char* s, Char c ); |
| 92 | extern Char* VG_(strrchr) ( const Char* s, Char c ); |
njn | f9dcb3b | 2009-05-19 05:50:34 +0000 | [diff] [blame^] | 93 | extern SizeT VG_(strspn) ( const Char* s, const Char* accpt ); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 94 | extern SizeT VG_(strcspn) ( const Char* s, const char* reject ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 95 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 96 | /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the |
| 97 | last character. */ |
| 98 | extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest ); |
| 99 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 100 | /* --------------------------------------------------------------------- |
| 101 | mem* functions |
| 102 | ------------------------------------------------------------------ */ |
| 103 | |
| 104 | extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz ); |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 105 | extern void* VG_(memmove)( void *d, const void *s, SizeT sz ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 106 | extern void* VG_(memset) ( void *s, Int c, SizeT sz ); |
| 107 | extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n ); |
| 108 | |
| 109 | /* --------------------------------------------------------------------- |
| 110 | Address computation helpers |
| 111 | ------------------------------------------------------------------ */ |
| 112 | |
| 113 | // Check if an address/whatever is aligned |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 114 | #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 115 | #define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3))) |
| 116 | #define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7))) |
| 117 | #define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf))) |
| 118 | #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1)))) |
| 119 | #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1)))) |
| 120 | |
njn | 5363a56 | 2005-06-12 04:34:51 +0000 | [diff] [blame] | 121 | // 'a' -- the alignment -- must be a power of 2. |
| 122 | // The latter two require the vki-*.h header to be imported also. |
| 123 | #define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1)) |
| 124 | #define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a)) |
| 125 | #define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE) |
| 126 | #define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE) |
| 127 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 128 | /* --------------------------------------------------------------------- |
| 129 | Misc useful functions |
| 130 | ------------------------------------------------------------------ */ |
| 131 | |
njn | fab2990 | 2008-03-03 02:15:03 +0000 | [diff] [blame] | 132 | /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used |
| 133 | * to be a shell sort, but is now a quicksort. */ |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 134 | extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size, |
| 135 | Int (*compar)(void*, void*) ); |
| 136 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 137 | /* Returns the base-2 logarithm of x. Returns -1 if x is not a power |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 138 | of two. Nb: VG_(log2)(1) == 0. */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 139 | extern Int VG_(log2) ( UInt x ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 140 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 141 | // A pseudo-random number generator returning a random UInt. If pSeed |
| 142 | // is NULL, it uses its own seed, which starts at zero. If pSeed is |
| 143 | // non-NULL, it uses and updates whatever pSeed points at. |
| 144 | extern UInt VG_(random) ( /*MOD*/UInt* pSeed ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 145 | #define VG_RAND_MAX (1ULL << 32) |
njn | 9828b34 | 2005-07-08 04:08:59 +0000 | [diff] [blame] | 146 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 147 | #endif // __PUB_TOOL_LIBCBASE_H |
| 148 | |
| 149 | /*--------------------------------------------------------------------*/ |
| 150 | /*--- end ---*/ |
| 151 | /*--------------------------------------------------------------------*/ |