blob: f282bfd755901dec0a2f4a18a02bedf9c1daae4b [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
sewardj9ebd6e02007-01-08 06:01:59 +000010 Copyright (C) 2000-2007 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
sewardj25d7dfb2007-11-09 23:25:46 +000045extern Long VG_(atoll) ( Char* str ); // base 10
46extern Long VG_(atoll16) ( Char* str ); // base 16; leading 0x accepted
47extern Long VG_(atoll36) ( Char* str ); // base 36
njn97405b22005-06-02 03:39:33 +000048
49/* ---------------------------------------------------------------------
50 String functions and macros
51 ------------------------------------------------------------------ */
52
53/* Use this for normal null-termination-style string comparison */
njnfdec4032006-12-14 03:29:18 +000054#define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
55 && VG_(strcmp)((s1),(s2))==0) ? True : False )
njn97405b22005-06-02 03:39:33 +000056
57extern Int VG_(strlen) ( const Char* str );
58extern Char* VG_(strcat) ( Char* dest, const Char* src );
59extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
60extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
61extern Char* VG_(strcpy) ( Char* dest, const Char* src );
62extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
63extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
64extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
65extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
66extern Char* VG_(strchr) ( const Char* s, Char c );
67extern Char* VG_(strrchr) ( const Char* s, Char c );
njn97405b22005-06-02 03:39:33 +000068
69/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
70extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
71extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, SizeT nmax );
72
73/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
74 last character. */
75extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
76
77/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
78 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
79extern Bool VG_(string_match) ( const Char* pat, const Char* str );
80
81/* ---------------------------------------------------------------------
82 mem* functions
83 ------------------------------------------------------------------ */
84
85extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
86extern void* VG_(memset) ( void *s, Int c, SizeT sz );
87extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
88
89/* ---------------------------------------------------------------------
90 Address computation helpers
91 ------------------------------------------------------------------ */
92
93// Check if an address/whatever is aligned
njn1d0825f2006-03-27 11:37:07 +000094#define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
njn97405b22005-06-02 03:39:33 +000095#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
96#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
97#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
98#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
99#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
100
njn5363a562005-06-12 04:34:51 +0000101// 'a' -- the alignment -- must be a power of 2.
102// The latter two require the vki-*.h header to be imported also.
103#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
104#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
105#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
106#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
107
njn97405b22005-06-02 03:39:33 +0000108/* ---------------------------------------------------------------------
109 Misc useful functions
110 ------------------------------------------------------------------ */
111
112/* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */
113extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
114 Int (*compar)(void*, void*) );
115
njncda391f2005-12-22 19:50:45 +0000116/* Returns the base-2 logarithm of x. Returns -1 if x is not a power of two. */
njn97405b22005-06-02 03:39:33 +0000117extern Int VG_(log2) ( Int x );
118
sewardj45f4e7c2005-09-27 19:20:21 +0000119// A pseudo-random number generator returning a random UInt. If pSeed
120// is NULL, it uses its own seed, which starts at zero. If pSeed is
121// non-NULL, it uses and updates whatever pSeed points at.
122extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
njn9828b342005-07-08 04:08:59 +0000123
njn97405b22005-06-02 03:39:33 +0000124#endif // __PUB_TOOL_LIBCBASE_H
125
126/*--------------------------------------------------------------------*/
127/*--- end ---*/
128/*--------------------------------------------------------------------*/