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 | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2008 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 |
| 50 | // non-digit char. None of them test that the number fits into 64 bits. |
| 51 | // |
| 52 | // Nb: if you're wondering why we don't just have a single VG_(strtol) which |
| 53 | // takes a base, it's because I wanted it to assert if it was given a bogus |
| 54 | // base (the standard glibc one sets 'errno' in this case). But |
| 55 | // m_libcbase.c doesn't import any code, not even vg_assert. --njn |
| 56 | extern Long VG_(strtoll8) ( Char* str, Char** endptr ); |
| 57 | extern Long VG_(strtoll10) ( Char* str, Char** endptr ); |
| 58 | extern Long VG_(strtoll16) ( Char* str, Char** endptr ); |
| 59 | extern Long VG_(strtoll36) ( Char* str, Char** endptr ); |
| 60 | |
| 61 | // Convert a string to a double. After leading whitespace is ignored, |
| 62 | // it accepts a non-empty sequence of decimal digits possibly containing |
| 63 | // a '.'. |
| 64 | extern double VG_(strtod) ( Char* str, Char** endptr ); |
| 65 | |
sewardj | 25d7dfb | 2007-11-09 23:25:46 +0000 | [diff] [blame] | 66 | extern Long VG_(atoll) ( Char* str ); // base 10 |
| 67 | extern Long VG_(atoll16) ( Char* str ); // base 16; leading 0x accepted |
| 68 | extern Long VG_(atoll36) ( Char* str ); // base 36 |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 69 | |
| 70 | /* --------------------------------------------------------------------- |
| 71 | String functions and macros |
| 72 | ------------------------------------------------------------------ */ |
| 73 | |
| 74 | /* Use this for normal null-termination-style string comparison */ |
njn | fdec403 | 2006-12-14 03:29:18 +0000 | [diff] [blame] | 75 | #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \ |
| 76 | && VG_(strcmp)((s1),(s2))==0) ? True : False ) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 77 | |
| 78 | extern Int VG_(strlen) ( const Char* str ); |
| 79 | extern Char* VG_(strcat) ( Char* dest, const Char* src ); |
| 80 | extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n ); |
| 81 | extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt ); |
| 82 | extern Char* VG_(strcpy) ( Char* dest, const Char* src ); |
| 83 | extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest ); |
| 84 | extern Int VG_(strcmp) ( const Char* s1, const Char* s2 ); |
| 85 | extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax ); |
| 86 | extern Char* VG_(strstr) ( const Char* haystack, Char* needle ); |
| 87 | extern Char* VG_(strchr) ( const Char* s, Char c ); |
| 88 | extern Char* VG_(strrchr) ( const Char* s, Char c ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 89 | |
| 90 | /* Like strcmp() and strncmp(), but stop comparing at any whitespace. */ |
| 91 | extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 ); |
| 92 | extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, SizeT nmax ); |
| 93 | |
| 94 | /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the |
| 95 | last character. */ |
| 96 | extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest ); |
| 97 | |
| 98 | /* Mini-regexp function. Searches for 'pat' in 'str'. Supports |
| 99 | * meta-symbols '*' and '?'. '\' escapes meta-symbols. */ |
| 100 | extern Bool VG_(string_match) ( const Char* pat, const Char* str ); |
| 101 | |
| 102 | /* --------------------------------------------------------------------- |
| 103 | mem* functions |
| 104 | ------------------------------------------------------------------ */ |
| 105 | |
| 106 | extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz ); |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 107 | extern void* VG_(memmove)( void *d, const void *s, SizeT sz ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 108 | extern void* VG_(memset) ( void *s, Int c, SizeT sz ); |
| 109 | extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n ); |
| 110 | |
| 111 | /* --------------------------------------------------------------------- |
| 112 | Address computation helpers |
| 113 | ------------------------------------------------------------------ */ |
| 114 | |
| 115 | // Check if an address/whatever is aligned |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 116 | #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1))) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 117 | #define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3))) |
| 118 | #define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7))) |
| 119 | #define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf))) |
| 120 | #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1)))) |
| 121 | #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1)))) |
| 122 | |
njn | 5363a56 | 2005-06-12 04:34:51 +0000 | [diff] [blame] | 123 | // 'a' -- the alignment -- must be a power of 2. |
| 124 | // The latter two require the vki-*.h header to be imported also. |
| 125 | #define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1)) |
| 126 | #define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a)) |
| 127 | #define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE) |
| 128 | #define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE) |
| 129 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 130 | /* --------------------------------------------------------------------- |
| 131 | Misc useful functions |
| 132 | ------------------------------------------------------------------ */ |
| 133 | |
njn | fab2990 | 2008-03-03 02:15:03 +0000 | [diff] [blame^] | 134 | /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used |
| 135 | * to be a shell sort, but is now a quicksort. */ |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 136 | extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size, |
| 137 | Int (*compar)(void*, void*) ); |
| 138 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 139 | /* Returns the base-2 logarithm of x. Returns -1 if x is not a power |
| 140 | of two. */ |
| 141 | extern Int VG_(log2) ( UInt x ); |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 142 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 143 | // A pseudo-random number generator returning a random UInt. If pSeed |
| 144 | // is NULL, it uses its own seed, which starts at zero. If pSeed is |
| 145 | // non-NULL, it uses and updates whatever pSeed points at. |
| 146 | extern UInt VG_(random) ( /*MOD*/UInt* pSeed ); |
njn | 9828b34 | 2005-07-08 04:08:59 +0000 | [diff] [blame] | 147 | |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 148 | #endif // __PUB_TOOL_LIBCBASE_H |
| 149 | |
| 150 | /*--------------------------------------------------------------------*/ |
| 151 | /*--- end ---*/ |
| 152 | /*--------------------------------------------------------------------*/ |