Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:31

0001 
0002 /*--------------------------------------------------------------------*/
0003 /*--- Standalone libc stuff.                   pub_tool_libcbase.h ---*/
0004 /*--------------------------------------------------------------------*/
0005 
0006 /*
0007    This file is part of Valgrind, a dynamic binary instrumentation
0008    framework.
0009 
0010    Copyright (C) 2000-2017 Julian Seward
0011       jseward@acm.org
0012 
0013    This program is free software; you can redistribute it and/or
0014    modify it under the terms of the GNU General Public License as
0015    published by the Free Software Foundation; either version 2 of the
0016    License, or (at your option) any later version.
0017 
0018    This program is distributed in the hope that it will be useful, but
0019    WITHOUT ANY WARRANTY; without even the implied warranty of
0020    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0021    General Public License for more details.
0022 
0023    You should have received a copy of the GNU General Public License
0024    along with this program; if not, see <http://www.gnu.org/licenses/>.
0025 
0026    The GNU General Public License is contained in the file COPYING.
0027 */
0028 
0029 #ifndef __PUB_TOOL_LIBCBASE_H
0030 #define __PUB_TOOL_LIBCBASE_H
0031 
0032 #include "pub_tool_basics.h"   // VG_ macro
0033 
0034 /* ---------------------------------------------------------------------
0035    Char functions.
0036    ------------------------------------------------------------------ */
0037 
0038 extern Bool  VG_(isspace) ( HChar c );
0039 extern Bool  VG_(isdigit) ( HChar c );
0040 extern HChar VG_(tolower) ( HChar c );
0041 
0042 /* ---------------------------------------------------------------------
0043    Converting strings to numbers
0044    ------------------------------------------------------------------ */
0045 
0046 // Convert strings to numbers according to various bases.  Leading
0047 // whitespace is ignored.  A subsequent '-' or '+' is accepted.  For strtoll16,
0048 // accepts an initial "0x" or "0X" prefix, but only if it's followed by a
0049 // hex digit (if not, the '0' will be read and then it will stop on the
0050 // "x"/"X".)  If 'endptr' isn't NULL, it gets filled in with the first
0051 // non-digit char.  Returns 0 if no number could be converted, and 'endptr'
0052 // is set to the start of the string.  None of them test that the number
0053 // fits into 64 bits.
0054 //
0055 // Nb: we also don't provide VG_(atoll*);  these functions are worse than
0056 // useless because they don't do any error checking and so accept malformed
0057 // numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0!
0058 // If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)".
0059 extern Long  VG_(strtoll10) ( const HChar* str, HChar** endptr );
0060 extern Long  VG_(strtoll16) ( const HChar* str, HChar** endptr );
0061 extern ULong  VG_(strtoull10) ( const HChar* str, HChar** endptr );
0062 extern ULong  VG_(strtoull16) ( const HChar* str, HChar** endptr );
0063 
0064 // Convert a string to a double.  After leading whitespace is ignored, a
0065 // '+' or '-' is allowed, and then it accepts a non-empty sequence of
0066 // decimal digits possibly containing a '.'.  Hexadecimal floats are not
0067 // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
0068 extern double VG_(strtod)  ( const HChar* str, HChar** endptr );
0069 
0070 /* ---------------------------------------------------------------------
0071    String functions and macros
0072    ------------------------------------------------------------------ */
0073 
0074 /* Use this for normal null-termination-style string comparison. */
0075 #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
0076                            && VG_(strcmp)((s1),(s2))==0) ? True : False )
0077 #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
0078                              && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
0079 
0080 extern SizeT  VG_(strlen)         ( const HChar* str );
0081 extern SizeT  VG_(strnlen)        ( const HChar* str, SizeT n );
0082 extern HChar* VG_(strcat)         ( HChar* dest, const HChar* src );
0083 extern HChar* VG_(strncat)        ( HChar* dest, const HChar* src, SizeT n );
0084 extern HChar* VG_(strpbrk)        ( const HChar* s, const HChar* accpt );
0085 extern HChar* VG_(strcpy)         ( HChar* dest, const HChar* src );
0086 extern HChar* VG_(strncpy)        ( HChar* dest, const HChar* src, SizeT ndest );
0087 extern SizeT  VG_(strlcpy)        ( HChar* dest, const HChar* src, SizeT n );
0088 extern Int    VG_(strcmp)         ( const HChar* s1, const HChar* s2 );
0089 extern Int    VG_(strcasecmp)     ( const HChar* s1, const HChar* s2 );
0090 extern Int    VG_(strncmp)        ( const HChar* s1, const HChar* s2, SizeT nmax );
0091 extern Int    VG_(strncasecmp)    ( const HChar* s1, const HChar* s2, SizeT nmax );
0092 extern HChar* VG_(strstr)         ( const HChar* haystack, const HChar* needle );
0093 extern HChar* VG_(strcasestr)     ( const HChar* haystack, const HChar* needle );
0094 extern HChar* VG_(strchr)         ( const HChar* s, HChar c );
0095 extern HChar* VG_(strrchr)        ( const HChar* s, HChar c );
0096 extern SizeT  VG_(strspn)         ( const HChar* s, const HChar* accpt );
0097 extern SizeT  VG_(strcspn)        ( const HChar* s, const HChar* reject );
0098 
0099 /* strtok* functions and some parsing utilities. */
0100 extern HChar* VG_(strtok_r)       (HChar* s, const HChar* delim, HChar** saveptr);
0101 extern HChar* VG_(strtok)         (HChar* s, const HChar* delim);
0102 
0103 /* Parse a 32- or 64-bit hex number, including leading 0x, from string
0104    starting at *ppc, putting result in *result, advance *ppc past the
0105    characters used, and return True.  Or fail, in which case *ppc and
0106    *result are undefined, and return False. */
0107 extern Bool VG_(parse_Addr) ( const HChar** ppc, Addr* result );
0108 
0109 /* Parse an unsigned 32 bit number, written using decimals only.
0110    Calling conventions are the same as for VG_(parse_Addr). */
0111 extern Bool VG_(parse_UInt) ( const HChar** ppc, UInt* result );
0112 
0113 /* Parse an "enum set" made of one or more words comma separated.
0114    The allowed word values are given in 'tokens', separated by comma.
0115    If a word in 'tokens' is found in 'input', the corresponding bit
0116    will be set in *enum_set (words in 'tokens' are numbered starting from 0).
0117    Using in 'tokens' the special token "-" (a minus character) indicates that
0118    the corresponding bit position cannot be set.
0119    In addition to the words specified in 'tokens', VG_(parse_enum_set)
0120    automatically accept the word "none" to indicate an empty enum_set (0).
0121    If allow_all, VG_(parse_enum_set) automatically accept the word "all"
0122    to indicate an enum_set with all bits corresponding to the words in tokens
0123     set.
0124    If "none" or "all" is present in 'input', no other word can be given
0125    in 'input'.
0126    If parsing is successful, returns True and sets *enum_set.
0127    If parsing fails, returns False. */
0128 extern Bool VG_(parse_enum_set) ( const HChar *tokens,
0129                                   Bool  allow_all,
0130                                   const HChar *input,
0131                                   UInt *enum_set);
0132 
0133 /* ---------------------------------------------------------------------
0134    mem* functions
0135    ------------------------------------------------------------------ */
0136 
0137 extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
0138 extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
0139 extern void* VG_(memset) ( void *s, Int c, SizeT sz );
0140 extern Int   VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
0141 
0142 /* Zero out up to 12 words quickly in-line.  Do not use this for blocks
0143    of size which are unknown at compile time, since the whole point is
0144    for it to be inlined, and then for gcc to remove all code except
0145    for the relevant 'sz' case. */
0146 inline __attribute__((always_inline))
0147 static void VG_(bzero_inline) ( void* s, SizeT sz )
0148 {
0149    if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1)))
0150        && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) {
0151       UWord* p = (UWord*)s;
0152       switch (sz / (SizeT)sizeof(UWord)) {
0153           case 12: p[0] = p[1] = p[2] = p[3]
0154                   = p[4] = p[5] = p[6] = p[7] 
0155                   = p[8] = p[9] = p[10] = p[11] = 0UL; return;
0156           case 11: p[0] = p[1] = p[2] = p[3]
0157                   = p[4] = p[5] = p[6] = p[7] 
0158                   = p[8] = p[9] = p[10] = 0UL; return;
0159           case 10: p[0] = p[1] = p[2] = p[3]
0160                   = p[4] = p[5] = p[6] = p[7] 
0161                   = p[8] = p[9] = 0UL; return;
0162           case 9: p[0] = p[1] = p[2] = p[3]
0163                   = p[4] = p[5] = p[6] = p[7] 
0164                   = p[8] = 0UL; return;
0165           case 8: p[0] = p[1] = p[2] = p[3]
0166                   = p[4] = p[5] = p[6] = p[7] = 0UL; return;
0167           case 7: p[0] = p[1] = p[2] = p[3]
0168                   = p[4] = p[5] = p[6] = 0UL; return;
0169           case 6: p[0] = p[1] = p[2] = p[3]
0170                   = p[4] = p[5] = 0UL; return;
0171           case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return;
0172           case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return;
0173           case 3: p[0] = p[1] = p[2] = 0UL; return;
0174           case 2: p[0] = p[1] = 0UL; return;
0175           case 1: p[0] = 0UL; return;
0176           case 0: return;
0177           default: break;
0178       }
0179    }
0180    VG_(memset)(s, 0, sz);
0181 }
0182 
0183 
0184 /* ---------------------------------------------------------------------
0185    Address computation helpers
0186    ------------------------------------------------------------------ */
0187 
0188 // Check if an address/whatever is aligned
0189 #define VG_IS_2_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
0190 #define VG_IS_4_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
0191 #define VG_IS_8_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
0192 #define VG_IS_16_ALIGNED(aaa_p)   (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
0193 #define VG_IS_32_ALIGNED(aaa_p)   (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
0194 #define VG_IS_64_ALIGNED(aaa_p)   (0 == (((Addr)(aaa_p)) & ((Addr)0x3f)))
0195 #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
0196 #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
0197 
0198 // 'a' -- the alignment -- must be a power of 2.
0199 // The latter two require the vki-*.h header to be imported also.
0200 #define VG_ROUNDDN(p, a)   ((Addr)(p) & ~((Addr)(a)-1))
0201 #define VG_ROUNDUP(p, a)   VG_ROUNDDN((p)+(a)-1, (a))
0202 #define VG_PGROUNDDN(p)    VG_ROUNDDN(p, VKI_PAGE_SIZE)
0203 #define VG_PGROUNDUP(p)    VG_ROUNDUP(p, VKI_PAGE_SIZE)
0204 
0205 /* Converts `Device ID` given as pair of 32-bit values (dev_major, dev_minor)
0206  * to 64-bit dev_t using MMMM Mmmm mmmM MMmm encoding. This is
0207  * downward compatible with legacy systems where dev_t is 16 bits wide,
0208  * encoded as MMmm. It is also downward compatible with the Linux kernel,
0209  * which uses 32-bit dev_t, encoded as mmmM MMmm.
0210  * Original macro can be found in bits/sysmacros.h. */
0211 #define VG_MAKEDEV(__major, __minor)            \
0212    ((((ULong) (__major & 0x00000fffu)) <<  8) | \
0213     (((ULong) (__major & 0xfffff000u)) << 32) | \
0214     (((ULong) (__minor & 0x000000ffu)) <<  0) | \
0215     (((ULong) (__minor & 0xffffff00u)) << 12))
0216 
0217 /* ---------------------------------------------------------------------
0218    Misc useful functions
0219    ------------------------------------------------------------------ */
0220 
0221 /* Like qsort().  The name VG_(ssort) is for historical reasons -- it used
0222  * to be a shell sort, but is now a quicksort. */
0223 extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
0224                         Int (*compar)(const void*, const void*) );
0225 
0226 /* Returns the base-2 logarithm of a 32 bit unsigned number.  Returns
0227  -1 if it is not a power of two.  Nb: VG_(log2)(1) == 0. */
0228 extern Int VG_(log2) ( UInt x );
0229 
0230 /* Ditto for 64 bit unsigned numbers. */
0231 extern Int VG_(log2_64)( ULong x );
0232 
0233 // A pseudo-random number generator returning a random UInt.  If pSeed
0234 // is NULL, it uses its own seed, which starts at zero.  If pSeed is
0235 // non-NULL, it uses and updates whatever pSeed points at.
0236 extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
0237 
0238 /* Update a running Adler-32 checksum with the bytes buf[0..len-1] and
0239    return the updated checksum. If buf is NULL, this function returns
0240    the required initial value for the checksum. An Adler-32 checksum is
0241    almost as reliable as a CRC32 but can be computed much faster. */
0242 extern UInt VG_(adler32)( UInt adler, const UChar* buf, UInt len);
0243 
0244 #endif   // __PUB_TOOL_LIBCBASE_H
0245 
0246 /*--------------------------------------------------------------------*/
0247 /*--- end                                                          ---*/
0248 /*--------------------------------------------------------------------*/