These are the current versions (actually already 2 years old).
[unix-history] / usr / src / usr.bin / gprof / lookup.c
CommitLineData
c0bc4ef7
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
ddb85eed
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
2124b336
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
c0bc4ef7
DF
16 */
17
6b32aa07 18#ifndef lint
2124b336 19static char sccsid[] = "@(#)lookup.c 5.3 (Berkeley) %G%";
ddb85eed 20#endif /* not lint */
6b32aa07 21
31f0a970 22#include "gprof.h"
6b32aa07
PK
23
24 /*
25 * look up an address in a sorted-by-address namelist
26 * this deals with misses by mapping them to the next lower
27 * entry point.
28 */
29nltype *
30nllookup( address )
31 unsigned long address;
32{
33 register long low;
34 register long middle;
35 register long high;
36# ifdef DEBUG
37 register int probes;
38
39 probes = 0;
40# endif DEBUG
41 for ( low = 0 , high = nname - 1 ; low != high ; ) {
42# ifdef DEBUG
43 probes += 1;
44# endif DEBUG
45 middle = ( high + low ) >> 1;
46 if ( nl[ middle ].value <= address && nl[ middle+1 ].value > address ) {
47# ifdef DEBUG
29da1d26 48 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
49 printf( "[nllookup] %d (%d) probes\n" , probes , nname-1 );
50 }
51# endif DEBUG
52 return &nl[ middle ];
53 }
54 if ( nl[ middle ].value > address ) {
55 high = middle;
56 } else {
57 low = middle + 1;
58 }
59 }
60 fprintf( stderr , "[nllookup] binary search fails???\n" );
61 return 0;
62}
63
64arctype *
65arclookup( parentp , childp )
66 nltype *parentp;
67 nltype *childp;
68{
69 arctype *arcp;
70
71 if ( parentp == 0 || childp == 0 ) {
72 fprintf( "[arclookup] parentp == 0 || childp == 0\n" );
73 return 0;
74 }
75# ifdef DEBUG
29da1d26 76 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
77 printf( "[arclookup] parent %s child %s\n" ,
78 parentp -> name , childp -> name );
79 }
80# endif DEBUG
81 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
82# ifdef DEBUG
29da1d26 83 if ( debug & LOOKUPDEBUG ) {
6b32aa07
PK
84 printf( "[arclookup]\t arc_parent %s arc_child %s\n" ,
85 arcp -> arc_parentp -> name ,
86 arcp -> arc_childp -> name );
87 }
88# endif DEBUG
89 if ( arcp -> arc_childp == childp ) {
90 return arcp;
91 }
92 }
93 return 0;
94}