2 /*--------------------------------------------------------------------*/
3 /*--- Entirely standalone libc stuff. m_libcbase.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_libcbase.h"
34 /* ---------------------------------------------------------------------
36 ------------------------------------------------------------------ */
38 Bool VG_(isspace) ( Char c )
40 return (c == ' ' || c == '\n' || c == '\t' ||
41 c == '\f' || c == '\v' || c == '\r');
44 Bool VG_(isdigit) ( Char c )
46 return (c >= '0' && c <= '9');
49 /* ---------------------------------------------------------------------
50 Converting strings to numbers
51 ------------------------------------------------------------------ */
53 static Bool is_dec_digit(Char c, Long* digit)
55 if (c >= '0' && c <= '9') { *digit = (Long)(c - '0'); return True; }
59 static Bool is_hex_digit(Char c, Long* digit)
61 if (c >= '0' && c <= '9') { *digit = (Long)(c - '0'); return True; }
62 if (c >= 'A' && c <= 'F') { *digit = (Long)((c - 'A') + 10); return True; }
63 if (c >= 'a' && c <= 'f') { *digit = (Long)((c - 'a') + 10); return True; }
67 Long VG_(strtoll10) ( Char* str, Char** endptr )
69 Bool neg = False, converted = False;
70 Long n = 0, digit = 0;
73 // Skip leading whitespace.
74 while (VG_(isspace)(*str)) str++;
76 // Allow a leading '-' or '+'.
77 if (*str == '-') { str++; neg = True; }
78 else if (*str == '+') { str++; }
80 while (is_dec_digit(*str, &digit)) {
81 converted = True; // Ok, we've actually converted a digit.
86 if (!converted) str = str0; // If nothing converted, endptr points to
87 if (neg) n = -n; // the start of the string.
88 if (endptr) *endptr = str; // Record first failing character.
92 Long VG_(strtoll16) ( Char* str, Char** endptr )
94 Bool neg = False, converted = False;
95 Long n = 0, digit = 0;
98 // Skip leading whitespace.
99 while (VG_(isspace)(*str)) str++;
101 // Allow a leading '-' or '+'.
102 if (*str == '-') { str++; neg = True; }
103 else if (*str == '+') { str++; }
105 // Allow leading "0x", but only if there's a hex digit
108 && (*(str+1) == 'x' || *(str+1) == 'X')
109 && is_hex_digit( *(str+2), &digit )) {
113 while (is_hex_digit(*str, &digit)) {
114 converted = True; // Ok, we've actually converted a digit.
119 if (!converted) str = str0; // If nothing converted, endptr points to
120 if (neg) n = -n; // the start of the string.
121 if (endptr) *endptr = str; // Record first failing character.
125 double VG_(strtod) ( Char* str, Char** endptr )
129 double n = 0, frac = 0, x = 0.1;
131 // Skip leading whitespace.
132 while (VG_(isspace)(*str)) str++;
134 // Allow a leading '-' or '+'.
135 if (*str == '-') { str++; neg = True; }
136 else if (*str == '+') { str++; }
138 while (is_dec_digit(*str, &digit)) {
145 while (is_dec_digit(*str, &digit)) {
154 if (endptr) *endptr = str; // Record first failing character.
158 Char VG_(tolower) ( Char c )
160 if ( c >= 'A' && c <= 'Z' ) {
161 return c - 'A' + 'a';
167 /* ---------------------------------------------------------------------
169 ------------------------------------------------------------------ */
171 SizeT VG_(strlen) ( const Char* str )
174 while (str[i] != 0) i++;
178 Char* VG_(strcat) ( Char* dest, const Char* src )
180 Char* dest_orig = dest;
181 while (*dest) dest++;
182 while (*src) *dest++ = *src++;
187 Char* VG_(strncat) ( Char* dest, const Char* src, SizeT n )
189 Char* dest_orig = dest;
190 while (*dest) dest++;
191 while (*src && n > 0) { *dest++ = *src++; n--; }
196 Char* VG_(strpbrk) ( const Char* s, const Char* accpt )
209 Char* VG_(strcpy) ( Char* dest, const Char* src )
211 Char* dest_orig = dest;
212 while (*src) *dest++ = *src++;
217 /* Copy bytes, not overrunning the end of dest and always ensuring
219 void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest )
224 if (src[i] == 0) return;
225 if (i >= ndest-1) return;
231 Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest )
235 if (i >= ndest) return dest; /* reached limit */
238 /* reached NUL; pad rest with zeroes as required */
239 while (i < ndest) dest[i++] = 0;
245 Int VG_(strcmp) ( const Char* s1, const Char* s2 )
248 if (*s1 == 0 && *s2 == 0) return 0;
249 if (*s1 == 0) return -1;
250 if (*s2 == 0) return 1;
252 if (*(UChar*)s1 < *(UChar*)s2) return -1;
253 if (*(UChar*)s1 > *(UChar*)s2) return 1;
259 Int VG_(strcasecmp) ( const Char* s1, const Char* s2 )
262 UChar c1 = (UChar)VG_(tolower)(*s1);
263 UChar c2 = (UChar)VG_(tolower)(*s2);
264 if (c1 == 0 && c2 == 0) return 0;
265 if (c1 == 0) return -1;
266 if (c2 == 0) return 1;
268 if (c1 < c2) return -1;
269 if (c1 > c2) return 1;
275 Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax )
279 if (n >= nmax) return 0;
280 if (*s1 == 0 && *s2 == 0) return 0;
281 if (*s1 == 0) return -1;
282 if (*s2 == 0) return 1;
284 if (*(UChar*)s1 < *(UChar*)s2) return -1;
285 if (*(UChar*)s1 > *(UChar*)s2) return 1;
291 Int VG_(strncasecmp) ( const Char* s1, const Char* s2, SizeT nmax )
297 if (n >= nmax) return 0;
298 c1 = (UChar)VG_(tolower)(*s1);
299 c2 = (UChar)VG_(tolower)(*s2);
300 if (c1 == 0 && c2 == 0) return 0;
301 if (c1 == 0) return -1;
302 if (c2 == 0) return 1;
304 if (c1 < c2) return -1;
305 if (c1 > c2) return 1;
311 Char* VG_(strstr) ( const Char* haystack, Char* needle )
314 if (haystack == NULL)
316 n = VG_(strlen)(needle);
318 if (haystack[0] == 0)
320 if (VG_(strncmp)(haystack, needle, n) == 0)
321 return (Char*)haystack;
326 Char* VG_(strcasestr) ( const Char* haystack, Char* needle )
329 if (haystack == NULL)
331 n = VG_(strlen)(needle);
333 if (haystack[0] == 0)
335 if (VG_(strncasecmp)(haystack, needle, n) == 0)
336 return (Char*)haystack;
341 Char* VG_(strchr) ( const Char* s, Char c )
344 if (*s == c) return (Char*)s;
345 if (*s == 0) return NULL;
350 Char* VG_(strrchr) ( const Char* s, Char c )
352 Int n = VG_(strlen)(s);
354 if (s[n] == c) return (Char*)s + n;
359 SizeT VG_(strspn) ( const Char* s, const Char* accpt )
363 for (p = s; *p != '\0'; ++p) {
364 for (a = accpt; *a != '\0'; ++a)
375 SizeT VG_(strcspn) ( const Char* s, const char* reject )
379 if (VG_(strchr) (reject, *s++) == NULL)
388 /* ---------------------------------------------------------------------
390 ------------------------------------------------------------------ */
392 void* VG_(memcpy) ( void *dest, const void *src, SizeT sz )
394 const UChar* s = (const UChar*)src;
395 UChar* d = (UChar*)dest;
396 const UInt* sI = (const UInt*)src;
397 UInt* dI = (UInt*)dest;
399 if (VG_IS_4_ALIGNED(dI) && VG_IS_4_ALIGNED(sI)) {
419 s = (const UChar*)sI;
429 void* VG_(memmove)(void *dest, const void *src, SizeT sz)
435 for (i = 0; i < sz; i++) {
436 ((UChar*)dest)[i] = ((UChar*)src)[i];
439 else if (dest > src) {
440 for (i = 0; i < sz; i++) {
441 ((UChar*)dest)[sz-i-1] = ((UChar*)src)[sz-i-1];
447 void* VG_(memset) ( void *destV, Int c, SizeT sz )
450 Char* d = (Char*)destV;
451 while ((!VG_IS_4_ALIGNED(d)) && sz >= 1) {
482 Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n )
485 const UChar *p1 = s1;
486 const UChar *p2 = s2;
503 /* ---------------------------------------------------------------------
504 Misc useful functions
505 ------------------------------------------------------------------ */
507 /////////////////////////////////////////////////////////////
508 /////////////////////////////////////////////////////////////
509 /// begin Bentley-McIlroy style quicksort
510 /// See "Engineering a Sort Function". Jon L Bentley, M. Douglas
511 /// McIlroy. Software Practice and Experience Vol 23(11), Nov 1993.
513 #define BM_MIN(a, b) \
516 #define BM_SWAPINIT(a, es) \
517 swaptype = ((a-(Char*)0) | es) % sizeof(Word) ? 2 \
518 : es > (SizeT)sizeof(Word) ? 1 \
521 #define BM_EXCH(a, b, t) \
522 (t = a, a = b, b = t)
524 #define BM_SWAP(a, b) \
526 ? bm_swapfunc(a, b, es, swaptype) \
527 : (void)BM_EXCH(*(Word*)(a), *(Word*)(b), t)
529 #define BM_VECSWAP(a, b, n) \
530 if (n > 0) bm_swapfunc(a, b, n, swaptype)
532 #define BM_PVINIT(pv, pm) \
534 pv = a, BM_SWAP(pv, pm); \
536 pv = (Char*)&v, v = *(Word*)pm
538 static Char* bm_med3 ( Char* a, Char* b, Char* c,
539 Int (*cmp)(void*,void*) ) {
541 ? (cmp(b, c) < 0 ? b : cmp(a, c) < 0 ? c : a)
542 : (cmp(b, c) > 0 ? b : cmp(a, c) > 0 ? c : a);
545 static void bm_swapfunc ( Char* a, Char* b, SizeT n, Int swaptype )
549 for ( ; n > 0; a += sizeof(Word), b += sizeof(Word),
551 BM_EXCH(*(Word*)a, *(Word*)b, t);
554 for ( ; n > 0; a += 1, b += 1, n -= 1)
559 static void bm_qsort ( Char* a, SizeT n, SizeT es,
560 Int (*cmp)(void*,void*) )
562 Char *pa, *pb, *pc, *pd, *pl, *pm, *pn, *pv;
569 for (pm = a + es; pm < a + n*es; pm += es)
570 for (pl = pm; pl > a && cmp(pl-es, pl) > 0; pl -= es)
580 pl = bm_med3(pl, pl+s, pl+2*s, cmp);
581 pm = bm_med3(pm-s, pm, pm+s, cmp);
582 pn = bm_med3(pn-2*s, pn-s, pn, cmp);
584 pm = bm_med3(pl, pm, pn, cmp);
588 pc = pd = a + (n-1)*es;
590 while (pb <= pc && (r = cmp(pb, pv)) <= 0) {
591 if (r == 0) { BM_SWAP(pa, pb); pa += es; }
594 while (pc >= pb && (r = cmp(pc, pv)) >= 0) {
595 if (r == 0) { BM_SWAP(pc, pd); pd -= es; }
604 s = BM_MIN(pa-a, pb-pa ); BM_VECSWAP(a, pb-s, s);
605 s = BM_MIN(pd-pc, pn-pd-es); BM_VECSWAP(pb, pn-s, s);
606 /* Now recurse. Do the smaller partition first with an explicit
607 recursion, then do the larger partition using a tail call.
608 Except we can't rely on gcc to implement a tail call in any sane
609 way, so simply jump back to the start. This guarantees stack
610 growth can never exceed O(log N) even in the worst case. */
615 bm_qsort(a, s1/es, es, cmp);
618 /* bm_qsort(pn-s2, s2/es, es, cmp); */
619 a = pn-s2; n = s2/es; es = es; cmp = cmp;
624 bm_qsort(pn-s2, s2/es, es, cmp);
627 /* bm_qsort(a, s1/es, es, cmp); */
628 a = a; n = s1/es; es = es; cmp = cmp;
641 /// end Bentley-McIlroy style quicksort
642 /////////////////////////////////////////////////////////////
643 /////////////////////////////////////////////////////////////
645 /* Returns the base-2 logarithm of x. Returns -1 if x is not a power
647 Int VG_(log2) ( UInt x )
650 /* Any more than 32 and we overflow anyway... */
651 for (i = 0; i < 32; i++) {
652 if ((1U << i) == x) return i;
658 // Generic quick sort.
659 void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
660 Int (*compar)(void*, void*) )
662 bm_qsort(base,nmemb,size,compar);
666 // This random number generator is based on the one suggested in Kernighan
667 // and Ritchie's "The C Programming Language".
669 // A pseudo-random number generator returning a random UInt. If pSeed
670 // is NULL, it uses its own seed, which starts at zero. If pSeed is
671 // non-NULL, it uses and updates whatever pSeed points at.
673 static UInt seed = 0;
675 UInt VG_(random)( /*MOD*/UInt* pSeed )
680 *pSeed = (1103515245 * *pSeed + 12345);
684 /*--------------------------------------------------------------------*/
686 /*--------------------------------------------------------------------*/