From: Nicholas Nethercote Date: Wed, 28 Mar 2007 01:27:05 +0000 (+0000) Subject: Remove duplicate code -- make XArray use VG_(ssort). X-Git-Tag: svn/VALGRIND_3_3_0~295 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=742ae56613a45237e832bf42506466bbb1dc3cfe;p=thirdparty%2Fvalgrind.git Remove duplicate code -- make XArray use VG_(ssort). Had to change XArray's comparison function to return an Int rather than a Word so it's consistent with the rest of the world. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@6680 --- diff --git a/coregrind/m_debuginfo/readxcoff.c b/coregrind/m_debuginfo/readxcoff.c index 2e3c01fa2e..769937ccd5 100644 --- a/coregrind/m_debuginfo/readxcoff.c +++ b/coregrind/m_debuginfo/readxcoff.c @@ -262,7 +262,7 @@ static Bool eq_string_Name ( Name name, UChar* str ) return False; } -static Word cmp_Names ( Name n1, Name n2 ) +static Int cmp_Names ( Name n1, Name n2 ) { UInt i = 0; while (1) { @@ -384,7 +384,7 @@ static void init_XCoffSym( XCoffSym* sym ) } /* Compare XCoffSyms by their start address. */ -static Word cmp_XCoffSym_by_start ( void* v1, void* v2 ) +static Int cmp_XCoffSym_by_start ( void* v1, void* v2 ) { XCoffSym* s1 = (XCoffSym*)v1; XCoffSym* s2 = (XCoffSym*)v2; @@ -395,7 +395,7 @@ static Word cmp_XCoffSym_by_start ( void* v1, void* v2 ) /* Compare XCoffSyms by a slightly weaker ordering, returning zero (equivalence) for any overlap, and -1 or 1 otherwise. */ -static Word cmp_XCoffSym_by_overlap ( void* v1, void* v2 ) +static Int cmp_XCoffSym_by_overlap ( void* v1, void* v2 ) { XCoffSym* s1 = (XCoffSym*)v1; XCoffSym* s2 = (XCoffSym*)v2; @@ -406,7 +406,7 @@ static Word cmp_XCoffSym_by_overlap ( void* v1, void* v2 ) /* Compare XCoffSyms by their start address, and for equal addresses, use the name as a secondary sort key. */ -static Word cmp_XCoffSym_by_start_then_name ( void* v1, void* v2 ) +static Int cmp_XCoffSym_by_start_then_name ( void* v1, void* v2 ) { XCoffSym* s1 = (XCoffSym*)v1; XCoffSym* s2 = (XCoffSym*)v2; diff --git a/coregrind/m_xarray.c b/coregrind/m_xarray.c index 09dc628ee1..3f7628c570 100644 --- a/coregrind/m_xarray.c +++ b/coregrind/m_xarray.c @@ -40,7 +40,7 @@ struct _XArray { void* (*alloc) ( SizeT ); /* alloc fn (nofail) */ void (*free) ( void* ); /* free fn */ - Word (*cmpFn) ( void*, void* ); /* cmp fn (may be NULL) */ + Int (*cmpFn) ( void*, void* ); /* cmp fn (may be NULL) */ Word elemSzB; /* element size in bytes */ void* arr; /* pointer to elements */ Word usedsizeE; /* # used elements in arr */ @@ -86,7 +86,7 @@ void VG_(deleteXA) ( XArray* xao ) xa->free(xa); } -void VG_(setCmpFnXA) ( XArray* xao, Word (*compar)(void*,void*) ) +void VG_(setCmpFnXA) ( XArray* xao, Int (*compar)(void*,void*) ) { struct _XArray* xa = (struct _XArray*)xao; vg_assert(xa); @@ -140,60 +140,12 @@ Int VG_(addToXA) ( XArray* xao, void* elem ) return xa->usedsizeE-1; } -// Generic shell sort. Like stdlib.h's qsort(). -static void ssort( void* base, Word nmemb, Word size, - Word (*compar)(void*, void*) ) -{ - Int incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280, - 9841, 29524, 88573, 265720, - 797161, 2391484 }; - Int lo = 0; - Int hi = nmemb-1; - Int i, j, h, bigN, hp; - - bigN = hi - lo + 1; if (bigN < 2) return; - hp = 0; while (hp < 14 && incs[hp] < bigN) hp++; hp--; - - #define SORT \ - for ( ; hp >= 0; hp--) { \ - h = incs[hp]; \ - for (i = lo + h; i <= hi; i++) { \ - ASSIGN(v,0, a,i); \ - j = i; \ - while (COMPAR(a,(j-h), v,0) > 0) { \ - ASSIGN(a,j, a,(j-h)); \ - j = j - h; \ - if (j <= (lo + h - 1)) break; \ - } \ - ASSIGN(a,j, v,0); \ - } \ - } - - // General case - { - char* a = base; - char v[size]; // will be at least 'size' bytes - - #define ASSIGN(dst, dsti, src, srci) \ - VG_(memcpy)( &dst[size*(dsti)], &src[size*(srci)], size ); - - #define COMPAR(dst, dsti, src, srci) \ - compar( &dst[size*(dsti)], &src[size*(srci)] ) - - SORT; - - #undef ASSIGN - #undef COMPAR - } - #undef SORT -} - void VG_(sortXA) ( XArray* xao ) { struct _XArray* xa = (struct _XArray*)xao; vg_assert(xa); vg_assert(xa->cmpFn); - ssort( xa->arr, xa->usedsizeE, xa->elemSzB, xa->cmpFn ); + VG_(ssort)( xa->arr, xa->usedsizeE, xa->elemSzB, xa->cmpFn ); xa->sorted = True; } diff --git a/include/pub_tool_xarray.h b/include/pub_tool_xarray.h index 234be204f2..4b97f72757 100644 --- a/include/pub_tool_xarray.h +++ b/include/pub_tool_xarray.h @@ -59,7 +59,7 @@ extern void VG_(deleteXA) ( XArray* ); /* Set the comparison function for this XArray. This clears an internal 'array is sorted' flag, which means you must call sortXA before making further queries with lookupXA. */ -extern void VG_(setCmpFnXA) ( XArray*, Word (*compar)(void*,void*) ); +extern void VG_(setCmpFnXA) ( XArray*, Int (*compar)(void*,void*) ); /* Add an element to an XArray. Element is copied into the XArray. Index at which it was added is returned. Note this will be