blob: 458d3d5c0ee9925a8b724ff201387bafe8bc7be4 [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
50// non-digit char. None of them test that the number fits into 64 bits.
51//
52// Nb: if you're wondering why we don't just have a single VG_(strtol) which
53// takes a base, it's because I wanted it to assert if it was given a bogus
54// base (the standard glibc one sets 'errno' in this case). But
55// m_libcbase.c doesn't import any code, not even vg_assert. --njn
56extern Long VG_(strtoll8) ( Char* str, Char** endptr );
57extern Long VG_(strtoll10) ( Char* str, Char** endptr );
58extern Long VG_(strtoll16) ( Char* str, Char** endptr );
59extern Long VG_(strtoll36) ( Char* str, Char** endptr );
60
61 // Convert a string to a double. After leading whitespace is ignored,
62 // it accepts a non-empty sequence of decimal digits possibly containing
63 // a '.'.
64extern double VG_(strtod) ( Char* str, Char** endptr );
65
sewardj25d7dfb2007-11-09 23:25:46 +000066extern Long VG_(atoll) ( Char* str ); // base 10
67extern Long VG_(atoll16) ( Char* str ); // base 16; leading 0x accepted
68extern Long VG_(atoll36) ( Char* str ); // base 36
njn97405b22005-06-02 03:39:33 +000069
70/* ---------------------------------------------------------------------
71 String functions and macros
72 ------------------------------------------------------------------ */
73
74/* Use this for normal null-termination-style string comparison */
njnfdec4032006-12-14 03:29:18 +000075#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
76 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000077
78extern Int VG_(strlen) ( const Char* str );
79extern Char* VG_(strcat) ( Char* dest, const Char* src );
80extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
81extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
82extern Char* VG_(strcpy) ( Char* dest, const Char* src );
83extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
84extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
85extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
86extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
87extern Char* VG_(strchr) ( const Char* s, Char c );
88extern Char* VG_(strrchr) ( const Char* s, Char c );
njn97405b22005-06-02 03:39:33 +000089
90/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
91extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
92extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, SizeT nmax );
93
94/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
95 last character. */
96extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
97
98/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
99 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
100extern Bool VG_(string_match) ( const Char* pat, const Char* str );
101
102/* ---------------------------------------------------------------------
103 mem* functions
104 ------------------------------------------------------------------ */
105
106extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
sewardjbbec7722007-11-25 14:08:53 +0000107extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
njn97405b22005-06-02 03:39:33 +0000108extern void* VG_(memset) ( void *s, Int c, SizeT sz );
109extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
110
111/* ---------------------------------------------------------------------
112 Address computation helpers
113 ------------------------------------------------------------------ */
114
115// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +0000116#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +0000117#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
118#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
119#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
120#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
121#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
122
njn5363a562005-06-12 04:34:51 +0000123// 'a' -- the alignment -- must be a power of 2.
124// The latter two require the vki-*.h header to be imported also.
125#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
126#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
127#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
128#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
129
njn97405b22005-06-02 03:39:33 +0000130/* ---------------------------------------------------------------------
131 Misc useful functions
132 ------------------------------------------------------------------ */
133
njnfab29902008-03-03 02:15:03 +0000134/* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
135 * to be a shell sort, but is now a quicksort. */
njn97405b22005-06-02 03:39:33 +0000136extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
137 Int (*compar)(void*, void*) );
138
sewardjb8b79ad2008-03-03 01:35:41 +0000139/* Returns the base-2 logarithm of x. Returns -1 if x is not a power
140 of two. */
141extern Int VG_(log2) ( UInt x );
njn97405b22005-06-02 03:39:33 +0000142
sewardj45f4e7c2005-09-27 19:20:21 +0000143// A pseudo-random number generator returning a random UInt. If pSeed
144// is NULL, it uses its own seed, which starts at zero. If pSeed is
145// non-NULL, it uses and updates whatever pSeed points at.
146extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
sewardj9c606bd2008-09-18 18:12:50 +0000147#define VG_RAND_MAX (1ULL << 32)
njn9828b342005-07-08 04:08:59 +0000148
njn97405b22005-06-02 03:39:33 +0000149#endif // __PUB_TOOL_LIBCBASE_H
150
151/*--------------------------------------------------------------------*/
152/*--- end ---*/
153/*--------------------------------------------------------------------*/