blob: 304aa0c81950ad57b982eb03e3c88b3a33b54cb4 [file] [log] [blame]
njn97405b22005-06-02 03:39:33 +00001
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
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2000-2012 Julian Seward
njn97405b22005-06-02 03:39:33 +000011 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
38extern Bool VG_(isspace) ( Char c );
39extern Bool VG_(isdigit) ( Char c );
njnf76d27a2009-05-28 01:53:07 +000040extern Char VG_(tolower) ( Char c );
njn97405b22005-06-02 03:39:33 +000041
42/* ---------------------------------------------------------------------
43 Converting strings to numbers
44 ------------------------------------------------------------------ */
45
njnea5d2352007-11-11 21:58:21 +000046// 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
njn8a0b7042009-02-20 06:10:44 +000051// 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.
njnea5d2352007-11-11 21:58:21 +000054//
njn83df0b62009-02-25 01:01:05 +000055// Nb: if you're wondering why we don't just have a single VG_(strtoll) which
njnea5d2352007-11-11 21:58:21 +000056// 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
njn83df0b62009-02-25 01:01:05 +000059//
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)".
njnea5d2352007-11-11 21:58:21 +000064extern Long VG_(strtoll10) ( Char* str, Char** endptr );
65extern Long VG_(strtoll16) ( Char* str, Char** endptr );
sewardj3b290482011-05-06 21:02:55 +000066extern ULong VG_(strtoull10) ( Char* str, Char** endptr );
67extern ULong VG_(strtoull16) ( Char* str, Char** endptr );
njnea5d2352007-11-11 21:58:21 +000068
njn83df0b62009-02-25 01:01:05 +000069// 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").
njnea5d2352007-11-11 21:58:21 +000073extern double VG_(strtod) ( Char* str, Char** endptr );
74
njn97405b22005-06-02 03:39:33 +000075/* ---------------------------------------------------------------------
76 String functions and macros
77 ------------------------------------------------------------------ */
78
njn83df0b62009-02-25 01:01:05 +000079/* Use this for normal null-termination-style string comparison. */
njnfdec4032006-12-14 03:29:18 +000080#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
81 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn83df0b62009-02-25 01:01:05 +000082#define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
83 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000084
sewardj4f2683a2008-10-26 11:53:30 +000085extern SizeT VG_(strlen) ( const Char* str );
njn97405b22005-06-02 03:39:33 +000086extern Char* VG_(strcat) ( Char* dest, const Char* src );
87extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
88extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
89extern Char* VG_(strcpy) ( Char* dest, const Char* src );
90extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
91extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
njnf76d27a2009-05-28 01:53:07 +000092extern Int VG_(strcasecmp) ( const Char* s1, const Char* s2 );
njn97405b22005-06-02 03:39:33 +000093extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
njnf76d27a2009-05-28 01:53:07 +000094extern Int VG_(strncasecmp) ( const Char* s1, const Char* s2, SizeT nmax );
florian660c5a42012-10-18 03:17:32 +000095extern Char* VG_(strstr) ( const Char* haystack, const Char* needle );
96extern Char* VG_(strcasestr) ( const Char* haystack, const Char* needle );
njn97405b22005-06-02 03:39:33 +000097extern Char* VG_(strchr) ( const Char* s, Char c );
98extern Char* VG_(strrchr) ( const Char* s, Char c );
njnf9dcb3b2009-05-19 05:50:34 +000099extern SizeT VG_(strspn) ( const Char* s, const Char* accpt );
florian660c5a42012-10-18 03:17:32 +0000100extern SizeT VG_(strcspn) ( const Char* s, const Char* reject );
njn97405b22005-06-02 03:39:33 +0000101
sewardj3b290482011-05-06 21:02:55 +0000102/* strtok* functions and some parsing utilities. */
103extern Char* VG_(strtok_r) (Char* s, const Char* delim, Char** saveptr);
104extern 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. */
110extern Bool VG_(parse_Addr) ( UChar** ppc, Addr* result );
111
njn97405b22005-06-02 03:39:33 +0000112/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
113 last character. */
114extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
115
njn97405b22005-06-02 03:39:33 +0000116/* ---------------------------------------------------------------------
117 mem* functions
118 ------------------------------------------------------------------ */
119
120extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
sewardjbbec7722007-11-25 14:08:53 +0000121extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
njn97405b22005-06-02 03:39:33 +0000122extern void* VG_(memset) ( void *s, Int c, SizeT sz );
123extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
124
sewardjf7183e32010-03-14 17:19:02 +0000125/* 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. */
129inline __attribute__((always_inline))
130static 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;
sewardj39d234a2010-03-15 09:03:25 +0000146 case 1: p[0] = 0UL; return;
sewardjf7183e32010-03-14 17:19:02 +0000147 case 0: return;
148 default: break;
149 }
150 }
151 VG_(memset)(s, 0, sz);
152}
153
154
njn97405b22005-06-02 03:39:33 +0000155/* ---------------------------------------------------------------------
156 Address computation helpers
157 ------------------------------------------------------------------ */
158
159// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +0000160#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +0000161#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)))
njnf76d27a2009-05-28 01:53:07 +0000164#define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
njn97405b22005-06-02 03:39:33 +0000165#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
njn5363a562005-06-12 04:34:51 +0000168// '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
njn97405b22005-06-02 03:39:33 +0000175/* ---------------------------------------------------------------------
176 Misc useful functions
177 ------------------------------------------------------------------ */
178
njnfab29902008-03-03 02:15:03 +0000179/* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
180 * to be a shell sort, but is now a quicksort. */
njn97405b22005-06-02 03:39:33 +0000181extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
182 Int (*compar)(void*, void*) );
183
sewardjaebbf1c2011-06-13 13:14:00 +0000184/* 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. */
sewardjb8b79ad2008-03-03 01:35:41 +0000186extern Int VG_(log2) ( UInt x );
njn97405b22005-06-02 03:39:33 +0000187
sewardjaebbf1c2011-06-13 13:14:00 +0000188/* Ditto for 64 bit unsigned numbers. */
189extern Int VG_(log2_64)( ULong x );
190
sewardj45f4e7c2005-09-27 19:20:21 +0000191// 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.
194extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
sewardj9c606bd2008-09-18 18:12:50 +0000195#define VG_RAND_MAX (1ULL << 32)
njn9828b342005-07-08 04:08:59 +0000196
njn97405b22005-06-02 03:39:33 +0000197#endif // __PUB_TOOL_LIBCBASE_H
198
199/*--------------------------------------------------------------------*/
200/*--- end ---*/
201/*--------------------------------------------------------------------*/