blob: 96eae4c588b565472c27f3dffc77d1e57ac84997 [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
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2000-2008 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 );
40
41/* ---------------------------------------------------------------------
42 Converting strings to numbers
43 ------------------------------------------------------------------ */
44
njnea5d2352007-11-11 21:58:21 +000045// 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
njn8a0b7042009-02-20 06:10:44 +000050// non-digit char. Returns 0 if no number could be converted, and 'endptr'
51// is set to the start of the string. None of them test that the number
52// fits into 64 bits.
njnea5d2352007-11-11 21:58:21 +000053//
njn83df0b62009-02-25 01:01:05 +000054// Nb: if you're wondering why we don't just have a single VG_(strtoll) which
njnea5d2352007-11-11 21:58:21 +000055// takes a base, it's because I wanted it to assert if it was given a bogus
56// base (the standard glibc one sets 'errno' in this case). But
57// m_libcbase.c doesn't import any code, not even vg_assert. --njn
njn83df0b62009-02-25 01:01:05 +000058//
59// Nb: we also don't provide VG_(atoll*); these functions are worse than
60// useless because they don't do any error checking and so accept malformed
61// numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0!
62// If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)".
njnea5d2352007-11-11 21:58:21 +000063extern Long VG_(strtoll10) ( Char* str, Char** endptr );
64extern Long VG_(strtoll16) ( Char* str, Char** endptr );
njnea5d2352007-11-11 21:58:21 +000065
njn83df0b62009-02-25 01:01:05 +000066// Convert a string to a double. After leading whitespace is ignored, a
67// '+' or '-' is allowed, and then it accepts a non-empty sequence of
68// decimal digits possibly containing a '.'. Hexadecimal floats are not
69// accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
njnea5d2352007-11-11 21:58:21 +000070extern double VG_(strtod) ( Char* str, Char** endptr );
71
njn97405b22005-06-02 03:39:33 +000072/* ---------------------------------------------------------------------
73 String functions and macros
74 ------------------------------------------------------------------ */
75
njn83df0b62009-02-25 01:01:05 +000076/* Use this for normal null-termination-style string comparison. */
njnfdec4032006-12-14 03:29:18 +000077#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
78 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn83df0b62009-02-25 01:01:05 +000079#define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
80 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000081
sewardj4f2683a2008-10-26 11:53:30 +000082extern SizeT VG_(strlen) ( const Char* str );
njn97405b22005-06-02 03:39:33 +000083extern Char* VG_(strcat) ( Char* dest, const Char* src );
84extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
85extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
86extern Char* VG_(strcpy) ( Char* dest, const Char* src );
87extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
88extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
89extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
90extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
91extern Char* VG_(strchr) ( const Char* s, Char c );
92extern Char* VG_(strrchr) ( const Char* s, Char c );
sewardj4f2683a2008-10-26 11:53:30 +000093extern SizeT VG_(strspn) ( const Char* s, const Char* accept );
94extern SizeT VG_(strcspn) ( const Char* s, const char* reject );
njn97405b22005-06-02 03:39:33 +000095
96/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
97extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
98extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, SizeT nmax );
99
100/* 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
113/* ---------------------------------------------------------------------
114 Address computation helpers
115 ------------------------------------------------------------------ */
116
117// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +0000118#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +0000119#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
120#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
121#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
122#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
123#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
124
njn5363a562005-06-12 04:34:51 +0000125// 'a' -- the alignment -- must be a power of 2.
126// The latter two require the vki-*.h header to be imported also.
127#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
128#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
129#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
130#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
131
njn97405b22005-06-02 03:39:33 +0000132/* ---------------------------------------------------------------------
133 Misc useful functions
134 ------------------------------------------------------------------ */
135
njnfab29902008-03-03 02:15:03 +0000136/* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
137 * to be a shell sort, but is now a quicksort. */
njn97405b22005-06-02 03:39:33 +0000138extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
139 Int (*compar)(void*, void*) );
140
sewardjb8b79ad2008-03-03 01:35:41 +0000141/* Returns the base-2 logarithm of x. Returns -1 if x is not a power
142 of two. */
143extern Int VG_(log2) ( UInt x );
njn97405b22005-06-02 03:39:33 +0000144
sewardj45f4e7c2005-09-27 19:20:21 +0000145// A pseudo-random number generator returning a random UInt. If pSeed
146// is NULL, it uses its own seed, which starts at zero. If pSeed is
147// non-NULL, it uses and updates whatever pSeed points at.
148extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
sewardj9c606bd2008-09-18 18:12:50 +0000149#define VG_RAND_MAX (1ULL << 32)
njn9828b342005-07-08 04:08:59 +0000150
njn97405b22005-06-02 03:39:33 +0000151#endif // __PUB_TOOL_LIBCBASE_H
152
153/*--------------------------------------------------------------------*/
154/*--- end ---*/
155/*--------------------------------------------------------------------*/