blob: d1f7f5c1d15eceb24fb91074bd015d04fbacee7e [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
10 Copyright (C) 2000-2005 Julian Seward
11 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
45extern Long VG_(atoll) ( Char* str ); // base 10
46extern Long VG_(atoll36) ( Char* str ); // base 36
47
48/* ---------------------------------------------------------------------
49 String functions and macros
50 ------------------------------------------------------------------ */
51
52/* Use this for normal null-termination-style string comparison */
53#define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \
54 && VG_(strcmp)((s1),(s2))==0)
55
56extern Int VG_(strlen) ( const Char* str );
57extern Char* VG_(strcat) ( Char* dest, const Char* src );
58extern Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n );
59extern Char* VG_(strpbrk) ( const Char* s, const Char* accpt );
60extern Char* VG_(strcpy) ( Char* dest, const Char* src );
61extern Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest );
62extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
63extern Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax );
64extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
65extern Char* VG_(strchr) ( const Char* s, Char c );
66extern Char* VG_(strrchr) ( const Char* s, Char c );
njn97405b22005-06-02 03:39:33 +000067
68/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
69extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
70extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, SizeT nmax );
71
72/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
73 last character. */
74extern void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest );
75
76/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
77 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
78extern Bool VG_(string_match) ( const Char* pat, const Char* str );
79
80/* ---------------------------------------------------------------------
81 mem* functions
82 ------------------------------------------------------------------ */
83
84extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
85extern void* VG_(memset) ( void *s, Int c, SizeT sz );
86extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
87
88/* ---------------------------------------------------------------------
89 Address computation helpers
90 ------------------------------------------------------------------ */
91
92// Check if an address/whatever is aligned
93#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
94#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
95#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
96#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
97#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
98
njn5363a562005-06-12 04:34:51 +000099// 'a' -- the alignment -- must be a power of 2.
100// The latter two require the vki-*.h header to be imported also.
101#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
102#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
103#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
104#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
105
njn97405b22005-06-02 03:39:33 +0000106/* ---------------------------------------------------------------------
107 Misc useful functions
108 ------------------------------------------------------------------ */
109
110/* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */
111extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
112 Int (*compar)(void*, void*) );
113
114/* Returns the base-2 logarithm of x. */
115extern Int VG_(log2) ( Int x );
116
njn9828b342005-07-08 04:08:59 +0000117// A pseudo-random number generator returning a random UInt, and its
118// seed function.
119extern void VG_(srandom) ( UInt seed );
120extern UInt VG_(random) ( void );
121
njn97405b22005-06-02 03:39:33 +0000122#endif // __PUB_TOOL_LIBCBASE_H
123
124/*--------------------------------------------------------------------*/
125/*--- end ---*/
126/*--------------------------------------------------------------------*/