share subr.c and string.c with pi/pc
[unix-history] / usr / src / usr.bin / gprof / lookup.c
CommitLineData
6b32aa07 1#ifndef lint
29da1d26 2 static char *sccsid = "@(#)lookup.c 1.3 (Berkeley) %G%";
6b32aa07
PK
3#endif lint
4
31f0a970 5#include "gprof.h"
6b32aa07
PK
6
7 /*
8 * look up an address in a sorted-by-address namelist
9 * this deals with misses by mapping them to the next lower
10 * entry point.
11 */
12nltype *
13nllookup( address )
14 unsigned long address;
15{
16 register long low;
17 register long middle;
18 register long high;
19# ifdef DEBUG
20 register int probes;
21
22 probes = 0;
23# endif DEBUG
24 for ( low = 0 , high = nname - 1 ; low != high ; ) {
25# ifdef DEBUG
26 probes += 1;
27# endif DEBUG
28 middle = ( high + low ) >> 1;
29 if ( nl[ middle ].value <= address && nl[ middle+1 ].value > address ) {
30# ifdef DEBUG
29da1d26 31 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
32 printf( "[nllookup] %d (%d) probes\n" , probes , nname-1 );
33 }
34# endif DEBUG
35 return &nl[ middle ];
36 }
37 if ( nl[ middle ].value > address ) {
38 high = middle;
39 } else {
40 low = middle + 1;
41 }
42 }
43 fprintf( stderr , "[nllookup] binary search fails???\n" );
44 return 0;
45}
46
47arctype *
48arclookup( parentp , childp )
49 nltype *parentp;
50 nltype *childp;
51{
52 arctype *arcp;
53
54 if ( parentp == 0 || childp == 0 ) {
55 fprintf( "[arclookup] parentp == 0 || childp == 0\n" );
56 return 0;
57 }
58# ifdef DEBUG
29da1d26 59 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
60 printf( "[arclookup] parent %s child %s\n" ,
61 parentp -> name , childp -> name );
62 }
63# endif DEBUG
64 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
65# ifdef DEBUG
29da1d26 66 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
67 printf( "[arclookup]\t arc_parent %s arc_child %s\n" ,
68 arcp -> arc_parentp -> name ,
69 arcp -> arc_childp -> name );
70 }
71# endif DEBUG
72 if ( arcp -> arc_childp == childp ) {
73 return arcp;
74 }
75 }
76 return 0;
77}