lookup.c revision 1591
167754Smsmith/* 267754Smsmith * Copyright (c) 1983, 1993 367754Smsmith * The Regents of the University of California. All rights reserved. 467754Smsmith * 567754Smsmith * Redistribution and use in source and binary forms, with or without 667754Smsmith * modification, are permitted provided that the following conditions 7217365Sjkim * are met: 8272444Sjkim * 1. Redistributions of source code must retain the above copyright 970243Smsmith * notice, this list of conditions and the following disclaimer. 1067754Smsmith * 2. Redistributions in binary form must reproduce the above copyright 11217365Sjkim * notice, this list of conditions and the following disclaimer in the 12217365Sjkim * documentation and/or other materials provided with the distribution. 13217365Sjkim * 3. All advertising materials mentioning features or use of this software 14217365Sjkim * must display the following acknowledgement: 15217365Sjkim * This product includes software developed by the University of 16217365Sjkim * California, Berkeley and its contributors. 17217365Sjkim * 4. Neither the name of the University nor the names of its contributors 18217365Sjkim * may be used to endorse or promote products derived from this software 19217365Sjkim * without specific prior written permission. 20217365Sjkim * 21217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22217365Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23217365Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24217365Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2567754Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2967754Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30217365Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31217365Sjkim * SUCH DAMAGE. 32217365Sjkim */ 33217365Sjkim 34217365Sjkim#ifndef lint 35217365Sjkimstatic char sccsid[] = "@(#)lookup.c 8.1 (Berkeley) 6/6/93"; 36217365Sjkim#endif /* not lint */ 37217365Sjkim 38217365Sjkim#include "gprof.h" 39217365Sjkim 40217365Sjkim /* 41217365Sjkim * look up an address in a sorted-by-address namelist 42217365Sjkim * this deals with misses by mapping them to the next lower 4367754Smsmith * entry point. 4467754Smsmith */ 4567754Smsmithnltype * 4667754Smsmithnllookup( address ) 47151937Sjkim unsigned long address; 4867754Smsmith{ 49193341Sjkim register long low; 50151937Sjkim register long middle; 51151937Sjkim register long high; 5267754Smsmith# ifdef DEBUG 53151937Sjkim register int probes; 54167802Sjkim 55167802Sjkim probes = 0; 56167802Sjkim# endif DEBUG 57167802Sjkim for ( low = 0 , high = nname - 1 ; low != high ; ) { 58167802Sjkim# ifdef DEBUG 5967754Smsmith probes += 1; 60167802Sjkim# endif DEBUG 61151937Sjkim middle = ( high + low ) >> 1; 62151937Sjkim if ( nl[ middle ].value <= address && nl[ middle+1 ].value > address ) { 6367754Smsmith# ifdef DEBUG 64151937Sjkim if ( debug & LOOKUPDEBUG ) { 65151937Sjkim printf( "[nllookup] %d (%d) probes\n" , probes , nname-1 ); 66151937Sjkim } 67151937Sjkim# endif DEBUG 68151937Sjkim return &nl[ middle ]; 69151937Sjkim } 70151937Sjkim if ( nl[ middle ].value > address ) { 71151937Sjkim high = middle; 72151937Sjkim } else { 7367754Smsmith low = middle + 1; 74151937Sjkim } 7567754Smsmith } 76151937Sjkim# ifdef DEBUG 7767754Smsmith if ( debug & LOOKUPDEBUG ) { 78228110Sjkim fprintf( stderr , "[nllookup] (%d) binary search fails\n" , 79228110Sjkim nname-1 ); 80228110Sjkim } 81228110Sjkim# endif DEBUG 82228110Sjkim return 0; 83228110Sjkim} 84228110Sjkim 85228110Sjkimarctype * 86228110Sjkimarclookup( parentp , childp ) 87228110Sjkim nltype *parentp; 88228110Sjkim nltype *childp; 89228110Sjkim{ 90228110Sjkim arctype *arcp; 91228110Sjkim 92228110Sjkim if ( parentp == 0 || childp == 0 ) { 93228110Sjkim fprintf( stderr, "[arclookup] parentp == 0 || childp == 0\n" ); 94228110Sjkim return 0; 95228110Sjkim } 96228110Sjkim# ifdef DEBUG 97228110Sjkim if ( debug & LOOKUPDEBUG ) { 98228110Sjkim printf( "[arclookup] parent %s child %s\n" , 99228110Sjkim parentp -> name , childp -> name ); 100228110Sjkim } 101228110Sjkim# endif DEBUG 102228110Sjkim for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) { 103228110Sjkim# ifdef DEBUG 104228110Sjkim if ( debug & LOOKUPDEBUG ) { 105228110Sjkim printf( "[arclookup]\t arc_parent %s arc_child %s\n" , 106228110Sjkim arcp -> arc_parentp -> name , 107228110Sjkim arcp -> arc_childp -> name ); 108228110Sjkim } 109228110Sjkim# endif DEBUG 110228110Sjkim if ( arcp -> arc_childp == childp ) { 111228110Sjkim return arcp; 11267754Smsmith } 113228110Sjkim } 114228110Sjkim return 0; 115151937Sjkim} 116114237Snjl