Commit | Line | Data |
---|---|---|
88144d4a ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
146a887c | 13 | package org.eclipse.linuxtools.lttng.jni; |
88144d4a ASL |
14 | |
15 | /** | |
16 | * <b><u>Jni_C_Common</u></b> | |
17 | * <p> | |
146a887c | 18 | * Common constante and methods that should be shared between JNI objects |
88144d4a ASL |
19 | */ |
20 | public abstract class Jni_C_Common { | |
146a887c | 21 | |
88144d4a ASL |
22 | // Needed for native types |
23 | public static final int NULL = 0; | |
24 | ||
25 | // C errno correspondance. Used to interpret LTT return value | |
26 | public static final int EOK = 0; | |
27 | public static final int EPERM = 1; | |
28 | public static final int ERANGE = 34; | |
29 | ||
146a887c | 30 | // Timestamps are in nanoseconds |
88144d4a ASL |
31 | public static final long NANO = 1000000000; |
32 | ||
33 | // Native console printing function | |
34 | private native void ltt_printC(String string_to_print); | |
35 | ||
36 | // Load LTTV library (order is important) | |
37 | static { | |
38 | System.loadLibrary("lttvtraceread"); | |
39 | } | |
40 | ||
41 | /** | |
146a887c | 42 | * Java-side console printing function. |
88144d4a ASL |
43 | * |
44 | * Call the C printing function to make sure all printing happen on the same side. | |
45 | * | |
146a887c | 46 | * @param msg |
88144d4a ASL |
47 | */ |
48 | public void printC(String msg) { | |
146a887c | 49 | // Need to escape "%" |
88144d4a ASL |
50 | msg = msg.replaceAll("%", "%%"); |
51 | ltt_printC(msg); | |
52 | } | |
53 | ||
54 | /** | |
146a887c | 55 | * Java-side console printing function. Add a return line at the end of the message. |
88144d4a ASL |
56 | * |
57 | * Call the C printing function to make sure all printing happen on the same side. | |
58 | * | |
146a887c | 59 | * @param msg |
88144d4a ASL |
60 | */ |
61 | public void printlnC(String msg) { | |
62 | printC(msg + "\n"); | |
63 | } | |
64 | ||
65 | /** | |
146a887c FC |
66 | * This method is to be used as an "alternate" .toString()<br> |
67 | * <br> | |
88144d4a ASL |
68 | * Simulates the way Java Object implements "toString()" |
69 | * | |
146a887c | 70 | * @return the Java hashed UID of the object (i.e. : NAME@HASH) |
88144d4a ASL |
71 | */ |
72 | public String getReferenceToString() { | |
73 | return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode()); | |
74 | } | |
146a887c FC |
75 | } |
76 | ||
77 | /** | |
78 | * <b><u>C_Pointer</u></b> | |
79 | * <p> | |
80 | * Class pointer to handle properly "C pointer" <br> | |
81 | * | |
82 | * Can transparently handle pointer of 32 or 64 bits. | |
83 | */ | |
84 | class C_Pointer extends Jni_C_Common { | |
85 | ||
86 | private long ptr = NULL; | |
87 | private boolean isLong = true; | |
88 | ||
89 | public C_Pointer() { | |
90 | ptr = NULL; | |
91 | } | |
92 | ||
93 | public C_Pointer(long newPtr) { | |
94 | ptr = newPtr; | |
95 | isLong = true; | |
96 | } | |
97 | ||
98 | public C_Pointer(int newPtr) { | |
99 | ptr = (long)newPtr; | |
100 | isLong = false; | |
101 | } | |
102 | ||
103 | public long getPointer() { | |
104 | return ptr; | |
105 | } | |
106 | ||
107 | public void setPointer(long newPtr) { | |
108 | ptr = newPtr; | |
109 | isLong = true; | |
110 | } | |
111 | ||
112 | public void setPointer(int newPtr) { | |
113 | ptr = newPtr; | |
114 | isLong = false; | |
115 | } | |
116 | ||
117 | /** | |
118 | * toString() method<br> | |
119 | * <br> | |
120 | * Convert the pointer to a nice looking hexadecimal format | |
121 | * | |
122 | * @return String Attributes of the object concatenated in String | |
123 | */ | |
124 | public String toString() { | |
125 | String returnData = "0x"; | |
126 | ||
127 | if (isLong == true) { | |
128 | returnData += Long.toHexString(ptr); | |
129 | } | |
130 | else { | |
131 | returnData += Integer.toHexString((int) ptr); | |
132 | } | |
133 | ||
134 | return returnData; | |
135 | } | |
136 | } |