Commit | Line | Data |
---|---|---|
890f9136 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
890f9136 SD |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Simon Delisle - Initial implementation | |
11 | *******************************************************************************/ | |
12 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.internal.ctf.core.event; |
890f9136 SD |
14 | |
15 | import java.util.Comparator; | |
16 | ||
f357bcd4 | 17 | import org.eclipse.tracecompass.ctf.core.event.CTFCallsite; |
890f9136 SD |
18 | |
19 | /** | |
20 | * Comparator for CTFCallsite | |
21 | * | |
22 | * @author Simon Delisle | |
890f9136 SD |
23 | */ |
24 | public class CTFCallsiteComparator implements Comparator<CTFCallsite> { | |
25 | ||
26 | private static final long MASK32 = 0x00000000ffffffffL; | |
27 | ||
28 | /* | |
29 | * The callsites will be sorted by calling addresses. To do this we take IPs | |
30 | * (instruction pointers) and compare them. Java only supports signed | |
31 | * operation and since memory addresses are unsigned, we will convert the | |
32 | * longs into integers that contain the high and low bytes and compare them. | |
33 | */ | |
34 | @Override | |
35 | public int compare(CTFCallsite o1, CTFCallsite o2) { | |
36 | /* | |
37 | * mask32 is 32 zeros followed by 32 ones, when we bitwise and this it | |
38 | * will return the lower 32 bits | |
39 | */ | |
40 | ||
41 | long other = o2.getIp(); | |
42 | /* | |
43 | * To get a high int: we downshift by 32 and bitwise and with the mask | |
44 | * to get rid of the sign | |
45 | * | |
46 | * To get the low int: we bitwise and with the mask. | |
47 | */ | |
48 | long otherHigh = (other >> 32) & MASK32; | |
49 | long otherLow = other & MASK32; | |
50 | long ownHigh = (o1.getIp() >> 32) & MASK32; | |
51 | long ownLow = o1.getIp() & MASK32; | |
52 | /* are the high values different, if so ignore the lower values */ | |
53 | if (ownHigh > otherHigh) { | |
54 | return 1; | |
55 | } | |
56 | if (ownHigh < otherHigh ) { | |
57 | return -1; | |
58 | } | |
59 | /* the high values are the same, compare the lower values */ | |
60 | if (ownLow > otherLow) { | |
61 | return 1; | |
62 | } | |
63 | if (ownLow < otherLow) { | |
64 | return -1; | |
65 | } | |
66 | /* the values are identical */ | |
67 | return 0; | |
68 | } | |
69 | ||
70 | } |