Commit | Line | Data |
---|---|---|
890f9136 SD |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
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 | ||
13 | package org.eclipse.linuxtools.internal.ctf.core.event; | |
14 | ||
15 | import java.util.Comparator; | |
16 | ||
17 | import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; | |
18 | ||
19 | /** | |
20 | * Comparator for CTFCallsite | |
21 | * | |
22 | * @author Simon Delisle | |
23 | * @since 3.0 | |
24 | * | |
25 | */ | |
26 | public class CTFCallsiteComparator implements Comparator<CTFCallsite> { | |
27 | ||
28 | private static final long MASK32 = 0x00000000ffffffffL; | |
29 | ||
30 | /* | |
31 | * The callsites will be sorted by calling addresses. To do this we take IPs | |
32 | * (instruction pointers) and compare them. Java only supports signed | |
33 | * operation and since memory addresses are unsigned, we will convert the | |
34 | * longs into integers that contain the high and low bytes and compare them. | |
35 | */ | |
36 | @Override | |
37 | public int compare(CTFCallsite o1, CTFCallsite o2) { | |
38 | /* | |
39 | * mask32 is 32 zeros followed by 32 ones, when we bitwise and this it | |
40 | * will return the lower 32 bits | |
41 | */ | |
42 | ||
43 | long other = o2.getIp(); | |
44 | /* | |
45 | * To get a high int: we downshift by 32 and bitwise and with the mask | |
46 | * to get rid of the sign | |
47 | * | |
48 | * To get the low int: we bitwise and with the mask. | |
49 | */ | |
50 | long otherHigh = (other >> 32) & MASK32; | |
51 | long otherLow = other & MASK32; | |
52 | long ownHigh = (o1.getIp() >> 32) & MASK32; | |
53 | long ownLow = o1.getIp() & MASK32; | |
54 | /* are the high values different, if so ignore the lower values */ | |
55 | if (ownHigh > otherHigh) { | |
56 | return 1; | |
57 | } | |
58 | if (ownHigh < otherHigh ) { | |
59 | return -1; | |
60 | } | |
61 | /* the high values are the same, compare the lower values */ | |
62 | if (ownLow > otherLow) { | |
63 | return 1; | |
64 | } | |
65 | if (ownLow < otherLow) { | |
66 | return -1; | |
67 | } | |
68 | /* the values are identical */ | |
69 | return 0; | |
70 | } | |
71 | ||
72 | } |