blob: 9d27fe5309240dddea47aac0e79f5a13c78b43b2 [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
njn9f207462009-03-10 22:02:09 +000010 Copyright (C) 2000-2009 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 );
njnea5d2352007-11-11 21:58:21 +000066
njn83df0b62009-02-25 01:01:05 +000067// 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").
njnea5d2352007-11-11 21:58:21 +000071extern double VG_(strtod) ( Char* str, Char** endptr );
72
njn97405b22005-06-02 03:39:33 +000073/* ---------------------------------------------------------------------
74 String functions and macros
75 ------------------------------------------------------------------ */
76
njn83df0b62009-02-25 01:01:05 +000077/* Use this for normal null-termination-style string comparison. */
njnfdec4032006-12-14 03:29:18 +000078#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
79 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn83df0b62009-02-25 01:01:05 +000080#define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
81 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000082
sewardj4f2683a2008-10-26 11:53:30 +000083extern SizeT VG_(strlen) ( const Char* str );
njn97405b22005-06-02 03:39:33 +000084extern Char* VG_(strcat) ( Char* dest, const Char* src );
85extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
86extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
87extern Char* VG_(strcpy) ( Char* dest, const Char* src );
88extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
89extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
njnf76d27a2009-05-28 01:53:07 +000090extern Int VG_(strcasecmp) ( const Char* s1, const Char* s2 );
njn97405b22005-06-02 03:39:33 +000091extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
njnf76d27a2009-05-28 01:53:07 +000092extern Int VG_(strncasecmp) ( const Char* s1, const Char* s2, SizeT nmax );
njn97405b22005-06-02 03:39:33 +000093extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
njnf76d27a2009-05-28 01:53:07 +000094extern Char* VG_(strcasestr) ( const Char* haystack, Char* needle );
njn97405b22005-06-02 03:39:33 +000095extern Char* VG_(strchr) ( const Char* s, Char c );
96extern Char* VG_(strrchr) ( const Char* s, Char c );
njnf9dcb3b2009-05-19 05:50:34 +000097extern SizeT VG_(strspn) ( const Char* s, const Char* accpt );
sewardj4f2683a2008-10-26 11:53:30 +000098extern SizeT VG_(strcspn) ( const Char* s, const char* reject );
njn97405b22005-06-02 03:39:33 +000099
njn97405b22005-06-02 03:39:33 +0000100/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
101 last character. */
102extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
103
njn97405b22005-06-02 03:39:33 +0000104/* ---------------------------------------------------------------------
105 mem* functions
106 ------------------------------------------------------------------ */
107
108extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
sewardjbbec7722007-11-25 14:08:53 +0000109extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
njn97405b22005-06-02 03:39:33 +0000110extern void* VG_(memset) ( void *s, Int c, SizeT sz );
111extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
112
sewardjf7183e32010-03-14 17:19:02 +0000113/* 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. */
117inline __attribute__((always_inline))
118static 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;
sewardj39d234a2010-03-15 09:03:25 +0000134 case 1: p[0] = 0UL; return;
sewardjf7183e32010-03-14 17:19:02 +0000135 case 0: return;
136 default: break;
137 }
138 }
139 VG_(memset)(s, 0, sz);
140}
141
142
njn97405b22005-06-02 03:39:33 +0000143/* ---------------------------------------------------------------------
144 Address computation helpers
145 ------------------------------------------------------------------ */
146
147// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +0000148#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +0000149#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)))
njnf76d27a2009-05-28 01:53:07 +0000152#define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
njn97405b22005-06-02 03:39:33 +0000153#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
njn5363a562005-06-12 04:34:51 +0000156// '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
njn97405b22005-06-02 03:39:33 +0000163/* ---------------------------------------------------------------------
164 Misc useful functions
165 ------------------------------------------------------------------ */
166
njnfab29902008-03-03 02:15:03 +0000167/* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
168 * to be a shell sort, but is now a quicksort. */
njn97405b22005-06-02 03:39:33 +0000169extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
170 Int (*compar)(void*, void*) );
171
sewardjb8b79ad2008-03-03 01:35:41 +0000172/* Returns the base-2 logarithm of x. Returns -1 if x is not a power
njn132fdb02009-02-26 03:52:35 +0000173 of two. Nb: VG_(log2)(1) == 0. */
sewardjb8b79ad2008-03-03 01:35:41 +0000174extern Int VG_(log2) ( UInt x );
njn97405b22005-06-02 03:39:33 +0000175
sewardj45f4e7c2005-09-27 19:20:21 +0000176// 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.
179extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
sewardj9c606bd2008-09-18 18:12:50 +0000180#define VG_RAND_MAX (1ULL << 32)
njn9828b342005-07-08 04:08:59 +0000181
njn97405b22005-06-02 03:39:33 +0000182#endif // __PUB_TOOL_LIBCBASE_H
183
184/*--------------------------------------------------------------------*/
185/*--- end ---*/
186/*--------------------------------------------------------------------*/