Home | History | Annotate | Download | only in include
      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 
     10    Copyright (C) 2000-2010 Julian Seward
     11       jseward (at) 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 extern Char VG_(tolower) ( Char c );
     41 
     42 /* ---------------------------------------------------------------------
     43    Converting strings to numbers
     44    ------------------------------------------------------------------ */
     45 
     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
     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.
     54 //
     55 // Nb: if you're wondering why we don't just have a single VG_(strtoll) which
     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
     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)".
     64 extern Long  VG_(strtoll10) ( Char* str, Char** endptr );
     65 extern Long  VG_(strtoll16) ( Char* str, Char** endptr );
     66 
     67 // Convert a string to a double.  After leading whitespace is ignored, a
     68 // '+' or '-' is allowed, and then it accepts a non-empty sequence of
     69 // decimal digits possibly containing a '.'.  Hexadecimal floats are not
     70 // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
     71 extern double VG_(strtod)  ( Char* str, Char** endptr );
     72 
     73 /* ---------------------------------------------------------------------
     74    String functions and macros
     75    ------------------------------------------------------------------ */
     76 
     77 /* Use this for normal null-termination-style string comparison. */
     78 #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
     79                            && VG_(strcmp)((s1),(s2))==0) ? True : False )
     80 #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
     81                              && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
     82 
     83 extern SizeT VG_(strlen)         ( const Char* str );
     84 extern Char* VG_(strcat)         ( Char* dest, const Char* src );
     85 extern Char* VG_(strncat)        ( Char* dest, const Char* src, SizeT n );
     86 extern Char* VG_(strpbrk)        ( const Char* s, const Char* accpt );
     87 extern Char* VG_(strcpy)         ( Char* dest, const Char* src );
     88 extern Char* VG_(strncpy)        ( Char* dest, const Char* src, SizeT ndest );
     89 extern Int   VG_(strcmp)         ( const Char* s1, const Char* s2 );
     90 extern Int   VG_(strcasecmp)     ( const Char* s1, const Char* s2 );
     91 extern Int   VG_(strncmp)        ( const Char* s1, const Char* s2, SizeT nmax );
     92 extern Int   VG_(strncasecmp)    ( const Char* s1, const Char* s2, SizeT nmax );
     93 extern Char* VG_(strstr)         ( const Char* haystack, Char* needle );
     94 extern Char* VG_(strcasestr)     ( const Char* haystack, Char* needle );
     95 extern Char* VG_(strchr)         ( const Char* s, Char c );
     96 extern Char* VG_(strrchr)        ( const Char* s, Char c );
     97 extern SizeT VG_(strspn)         ( const Char* s, const Char* accpt );
     98 extern SizeT VG_(strcspn)        ( const Char* s, const char* reject );
     99 
    100 /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
    101    last character. */
    102 extern void  VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
    103 
    104 /* ---------------------------------------------------------------------
    105    mem* functions
    106    ------------------------------------------------------------------ */
    107 
    108 extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
    109 extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
    110 extern void* VG_(memset) ( void *s, Int c, SizeT sz );
    111 extern Int   VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
    112 
    113 /* Zero out up to 8 words quickly in-line.  Do not use this for blocks
    114    of size which are unknown at compile time, since the whole point is
    115    for it to be inlined, and then for gcc to remove all code except
    116    for the relevant 'sz' case. */
    117 inline __attribute__((always_inline))
    118 static void VG_(bzero_inline) ( void* s, SizeT sz )
    119 {
    120    if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1)))
    121        && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) {
    122       UWord* p = (UWord*)s;
    123       switch (sz / (SizeT)sizeof(UWord)) {
    124           case 8: p[0] = p[1] = p[2] = p[3]
    125                   = p[4] = p[5] = p[6] = p[7] = 0UL; return;
    126           case 7: p[0] = p[1] = p[2] = p[3]
    127                   = p[4] = p[5] = p[6] = 0UL; return;
    128           case 6: p[0] = p[1] = p[2] = p[3]
    129                   = p[4] = p[5] = 0UL; return;
    130           case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return;
    131           case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return;
    132           case 3: p[0] = p[1] = p[2] = 0UL; return;
    133           case 2: p[0] = p[1] = 0UL; return;
    134           case 1: p[0] = 0UL; return;
    135           case 0: return;
    136           default: break;
    137       }
    138    }
    139    VG_(memset)(s, 0, sz);
    140 }
    141 
    142 
    143 /* ---------------------------------------------------------------------
    144    Address computation helpers
    145    ------------------------------------------------------------------ */
    146 
    147 // Check if an address/whatever is aligned
    148 #define VG_IS_2_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
    149 #define VG_IS_4_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
    150 #define VG_IS_8_ALIGNED(aaa_p)    (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
    151 #define VG_IS_16_ALIGNED(aaa_p)   (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
    152 #define VG_IS_32_ALIGNED(aaa_p)   (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
    153 #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
    154 #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
    155 
    156 // 'a' -- the alignment -- must be a power of 2.
    157 // The latter two require the vki-*.h header to be imported also.
    158 #define VG_ROUNDDN(p, a)   ((Addr)(p) & ~((Addr)(a)-1))
    159 #define VG_ROUNDUP(p, a)   VG_ROUNDDN((p)+(a)-1, (a))
    160 #define VG_PGROUNDDN(p)    VG_ROUNDDN(p, VKI_PAGE_SIZE)
    161 #define VG_PGROUNDUP(p)    VG_ROUNDUP(p, VKI_PAGE_SIZE)
    162 
    163 /* ---------------------------------------------------------------------
    164    Misc useful functions
    165    ------------------------------------------------------------------ */
    166 
    167 /* Like qsort().  The name VG_(ssort) is for historical reasons -- it used
    168  * to be a shell sort, but is now a quicksort. */
    169 extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
    170                         Int (*compar)(void*, void*) );
    171 
    172 /* Returns the base-2 logarithm of x.  Returns -1 if x is not a power
    173    of two.  Nb: VG_(log2)(1) == 0.  */
    174 extern Int VG_(log2) ( UInt x );
    175 
    176 // A pseudo-random number generator returning a random UInt.  If pSeed
    177 // is NULL, it uses its own seed, which starts at zero.  If pSeed is
    178 // non-NULL, it uses and updates whatever pSeed points at.
    179 extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
    180 #define VG_RAND_MAX (1ULL << 32)
    181 
    182 #endif   // __PUB_TOOL_LIBCBASE_H
    183 
    184 /*--------------------------------------------------------------------*/
    185 /*--- end                                                          ---*/
    186 /*--------------------------------------------------------------------*/
    187