Misc tweaks in segAddr_to_index:
(1) It is always invalid for the incoming segment pointer to be
    out of range. Assert that.
(2) Let the compiler do the address arithmetic. They're good at that
    and therefore:
(3) No asserts needed to check the result.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14933 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_aspacemgr/aspacemgr-linux.c b/coregrind/m_aspacemgr/aspacemgr-linux.c
index bcdd73a..6ab1044 100644
--- a/coregrind/m_aspacemgr/aspacemgr-linux.c
+++ b/coregrind/m_aspacemgr/aspacemgr-linux.c
@@ -1202,19 +1202,12 @@
 }
 
 
-/* Given a pointer to a seg, tries to figure out which one it is in
-   nsegments[..].  Very paranoid. */
+/* Map segment pointer to segment index. */
 static Int segAddr_to_index ( const NSegment* seg )
 {
-   Int i;
-   if (seg < &nsegments[0] || seg >= &nsegments[nsegments_used])
-      return -1;
-   i = ((const UChar*)seg - (const UChar*)(&nsegments[0])) / sizeof(NSegment);
-   if (i < 0 || i >= nsegments_used)
-      return -1;
-   if (seg == &nsegments[i])
-      return i;
-   return -1;
+   aspacem_assert(seg >= &nsegments[0] && seg < &nsegments[nsegments_used]);
+
+   return seg - &nsegments[0];
 }
 
 
@@ -1223,8 +1216,7 @@
 NSegment const * VG_(am_next_nsegment) ( const NSegment* here, Bool fwds )
 {
    Int i = segAddr_to_index(here);
-   if (i < 0 || i >= nsegments_used)
-      return NULL;
+
    if (fwds) {
       i++;
       if (i >= nsegments_used)
@@ -2710,7 +2702,6 @@
 {
    aspacem_assert(seg != NULL);
    Int i = segAddr_to_index( seg );
-   aspacem_assert(i >= 0 && i < nsegments_used);
    if (nsegments[i].kind == SkAnonC) {
       nsegments[i].isCH = True;
    } else {
@@ -2725,7 +2716,6 @@
 {
    aspacem_assert(seg != NULL);
    Int i = segAddr_to_index( seg );
-   aspacem_assert(i >= 0 && i < nsegments_used);
    if (nsegments[i].kind == SkAnonC || nsegments[i].kind == SkFileC) {
       nsegments[i].hasT = True;
    }
@@ -2817,7 +2807,6 @@
       probably means you passed in a bogus SEG. */
    aspacem_assert(seg != NULL);
    segA = segAddr_to_index( seg );
-   aspacem_assert(segA >= 0 && segA < nsegments_used);
 
    if (nsegments[segA].kind != SkAnonC)
       return False;