Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/Native.java
32288 views
/*1* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import sun.misc.Unsafe;28import java.util.Vector;29import java.security.AccessController;30import java.security.PrivilegedAction;3132/**33* This class contains the collection of utility functions to help work with34* native data types on different platforms similarly.35*/3637class Native {3839private static Unsafe unsafe = XlibWrapper.unsafe;4041static int longSize;4243static int dataModel;44static {45String dataModelProp = (String)AccessController.46doPrivileged(47new PrivilegedAction() {48public Object run() {49return System.getProperty("sun.arch.data.model");50}51});52try {53dataModel = Integer.parseInt(dataModelProp);54} catch (Exception e) {55dataModel = 32;56}57if (dataModel == 32) {58longSize = 4;59} else {60longSize = 8;61}62}6364/**65* Set of helper function to read data of different PLATFORM types66* from memory pointer by <code>ptr</code>67* Note, names of types in function are NATIVE PLATFORM types68* and they have the same size as they would have in C compiler69* on the same platform.70*/7172static boolean getBool(long ptr) { return getInt(ptr) != 0; }73static boolean getBool(long ptr, int index) { return getInt(ptr, index) != 0; }74static void putBool(long ptr, boolean data) { putInt(ptr, (data)?(1):(0)); }75static void putBool(long ptr, int index, boolean data) { putInt(ptr, index, (data)?(1):(0)); }767778/**79* Access to C byte data(one byte)80*/81static int getByteSize() { return 1; }82static byte getByte(long ptr) { return unsafe.getByte(ptr); }8384static byte getByte(long ptr, int index) {85return getByte(ptr+index);86}87/**88* Stores to C byte data(one byte)89*/90static void putByte(long ptr, byte data) { unsafe.putByte(ptr, data); }9192static void putByte(long ptr, int index, byte data) {93putByte(ptr+index, data);94}95/**96* Converts length bytes of data pointed by <code>data</code> into byte array97* Returns null if data is zero98* @param data native pointer to native memory99* @param length size in bytes of native memory100*/101static byte[] toBytes(long data, int length) {102if (data == 0) {103return null;104}105byte[] res = new byte[length];106for (int i = 0; i < length; i++, data++) {107res[i] = getByte(data);108}109return res;110}111/**112* Stores byte array into native memory and returns pointer to this memory113* Returns 0 if bytes is null114*/115static long toData(byte[] bytes) {116if (bytes == null) {117return 0;118}119long res = XlibWrapper.unsafe.allocateMemory(bytes.length);120for (int i = 0; i < bytes.length; i++) {121putByte(res+i, bytes[i]);122}123return res;124}125126/**127* Access to C unsigned byte data(one byte)128*/129static int getUByteSize() { return 1; }130static short getUByte(long ptr) { return (short)(0xFF & unsafe.getByte(ptr)); }131132static short getUByte(long ptr, int index) {133return getUByte(ptr+index);134}135136/**137* Stores to C unsigned byte data(one byte)138*/139static void putUByte(long ptr, short data) { unsafe.putByte(ptr, (byte)data); }140141static void putUByte(long ptr, int index, short data) {142putUByte(ptr+index, data);143}144145/**146* Converts length usnigned bytes of data pointed by <code>data</code> into147* short array148* Returns null if data is zero149* @param data native pointer to native memory150* @param length size in bytes of native memory151*/152static short[] toUBytes(long data, int length) {153if (data == 0) {154return null;155}156short[] res = new short[length];157for (int i = 0; i < length; i++, data++) {158res[i] = getUByte(data);159}160return res;161}162/**163* Stores short array as unsigned bytes into native memory and returns pointer164* to this memory165* Returns 0 if bytes is null166*/167static long toUData(short[] bytes) {168if (bytes == null) {169return 0;170}171long res = XlibWrapper.unsafe.allocateMemory(bytes.length);172for (int i = 0; i < bytes.length; i++) {173putUByte(res+i, bytes[i]);174}175return res;176}177178/**179* Access to C short data(two bytes)180*/181static int getShortSize() { return 2; }182static short getShort(long ptr) { return unsafe.getShort(ptr); }183/**184* Stores to C short data(two bytes)185*/186static void putShort(long ptr, short data) { unsafe.putShort(ptr, data); }187static void putShort(long ptr, int index, short data) {188putShort(ptr + index*getShortSize(), data);189}190static long toData(short[] shorts) {191if (shorts == null) {192return 0;193}194long res = XlibWrapper.unsafe.allocateMemory(shorts.length*getShortSize());195for (int i = 0; i < shorts.length; i++) {196putShort(res, i, shorts[i]);197}198return res;199}200201/**202* Access to C unsigned short data(two bytes)203*/204static int getUShortSize() { return 2; }205206static int getUShort(long ptr) { return 0xFFFF & unsafe.getShort(ptr); }207/**208* Stores to C unsigned short data(two bytes)209*/210static void putUShort(long ptr, int data) { unsafe.putShort(ptr, (short)data); }211static void putUShort(long ptr, int index, int data) {212putUShort(ptr + index*getShortSize(), data);213}214215/**216* Stores int array as unsigned shorts into native memory and returns pointer217* to this memory218* Returns 0 if bytes is null219*/220static long toUData(int[] shorts) {221if (shorts == null) {222return 0;223}224long res = XlibWrapper.unsafe.allocateMemory(shorts.length*getShortSize());225for (int i = 0; i < shorts.length; i++) {226putUShort(res, i, shorts[i]);227}228return res;229}230231/**232* Access to C int data(four bytes)233*/234static int getIntSize() { return 4; }235static int getInt(long ptr) { return unsafe.getInt(ptr); }236static int getInt(long ptr, int index) { return getInt(ptr +getIntSize()*index); }237/**238* Stores to C int data(four bytes)239*/240static void putInt(long ptr, int data) { unsafe.putInt(ptr, data); }241static void putInt(long ptr, int index, int data) {242putInt(ptr + index*getIntSize(), data);243}244static long toData(int[] ints) {245if (ints == null) {246return 0;247}248long res = XlibWrapper.unsafe.allocateMemory(ints.length*getIntSize());249for (int i = 0; i < ints.length; i++) {250putInt(res, i, ints[i]);251}252return res;253}254255/**256* Access to C unsigned int data(four bytes)257*/258static int getUIntSize() { return 4; }259static long getUInt(long ptr) { return 0xFFFFFFFFL & unsafe.getInt(ptr); }260static long getUInt(long ptr, int index) { return getUInt(ptr +getIntSize()*index); }261/**262* Stores to C unsigned int data(four bytes)263*/264static void putUInt(long ptr, long data) { unsafe.putInt(ptr, (int)data); }265static void putUInt(long ptr, int index, long data) {266putUInt(ptr + index*getIntSize(), data);267}268269/**270* Stores long array as unsigned intss into native memory and returns pointer271* to this memory272* Returns 0 if bytes is null273*/274static long toUData(long[] ints) {275if (ints == null) {276return 0;277}278long res = XlibWrapper.unsafe.allocateMemory(ints.length*getIntSize());279for (int i = 0; i < ints.length; i++) {280putUInt(res, i, ints[i]);281}282return res;283}284285/**286* Access to C long data(size depends on platform)287*/288static int getLongSize() {289return longSize;290}291static long getLong(long ptr) {292if (XlibWrapper.dataModel == 32) {293return unsafe.getInt(ptr);294} else {295return unsafe.getLong(ptr);296}297}298/**299* Stores to C long data(four bytes)300* Note: <code>data</code> has <code>long</code> type301* to be able to keep 64-bit C <code>long</code> data302*/303static void putLong(long ptr, long data) {304if (XlibWrapper.dataModel == 32) {305unsafe.putInt(ptr, (int)data);306} else {307unsafe.putLong(ptr, data);308}309}310311static void putLong(long ptr, int index, long data) {312putLong(ptr+index*getLongSize(), data);313}314315/**316* Returns index's element of the array of native long pointed by ptr317*/318static long getLong(long ptr, int index) {319return getLong(ptr + index*getLongSize());320}321/**322* Stores Java long[] array into memory. Memory location is treated as array323* of native <code>long</code>s324*/325static void put(long ptr, long[] arr) {326for (int i = 0; i < arr.length; i ++, ptr += getLongSize()) {327putLong(ptr, arr[i]);328}329}330331/**332* Stores Java Vector of Longs into memory. Memory location is treated as array333* of native <code>long</code>s334*/335static void putLong(long ptr, Vector arr) {336for (int i = 0; i < arr.size(); i ++, ptr += getLongSize()) {337putLong(ptr, ((Long)arr.elementAt(i)).longValue());338}339}340341/**342* Stores Java Vector of Longs into memory. Memory location is treated as array343* of native <code>long</code>s. Array is stored in reverse order344*/345static void putLongReverse(long ptr, Vector arr) {346for (int i = arr.size()-1; i >= 0; i--, ptr += getLongSize()) {347putLong(ptr, ((Long)arr.elementAt(i)).longValue());348}349}350/**351* Converts length bytes of data pointed by <code>data</code> into byte array352* Returns null if data is zero353* @param data native pointer to native memory354* @param length size in longs(platform dependent) of native memory355*/356static long[] toLongs(long data, int length) {357if (data == 0) {358return null;359}360long[] res = new long[length];361for (int i = 0; i < length; i++, data += getLongSize()) {362res[i] = getLong(data);363}364return res;365}366static long toData(long[] longs) {367if (longs == null) {368return 0;369}370long res = XlibWrapper.unsafe.allocateMemory(longs.length*getLongSize());371for (int i = 0; i < longs.length; i++) {372putLong(res, i, longs[i]);373}374return res;375}376377378/**379* Access to C "unsigned long" date type, which is XID in X380*/381static long getULong(long ptr) {382if (XlibWrapper.dataModel == 32) {383// Compensate sign-expansion384return ((long)unsafe.getInt(ptr)) & 0xFFFFFFFFL;385} else {386// Can't do anything!!!387return unsafe.getLong(ptr);388}389}390391static void putULong(long ptr, long value) {392putLong(ptr, value);393}394395/**396* Allocates memory for array of native <code>long</code>s of the size <code>length</code>397*/398static long allocateLongArray(int length) {399return unsafe.allocateMemory(getLongSize() * length);400}401402403static long getWindow(long ptr) {404return getLong(ptr);405}406static long getWindow(long ptr, int index) {407return getLong(ptr + getWindowSize()*index);408}409410static void putWindow(long ptr, long window) {411putLong(ptr, window);412}413414static void putWindow(long ptr, int index, long window) {415putLong(ptr, index, window);416}417418/**419* Set of function to return sizes of C data of the appropriate420* type.421*/422static int getWindowSize() {423return getLongSize();424}425426427/**428* Set of function to access CARD32 type. All data which types are derived429* from CARD32 should be accessed using this accessors.430* These types are: XID(Window, Drawable, Font, Pixmap, Cursor, Colormap, GContext, KeySym),431* Atom, Mask, VisualID, Time432*/433static long getCard32(long ptr) {434return getLong(ptr);435}436static void putCard32(long ptr, long value) {437putLong(ptr, value);438}439static long getCard32(long ptr, int index) {440return getLong(ptr, index);441}442static void putCard32(long ptr, int index, long value) {443putLong(ptr, index, value);444}445static int getCard32Size() {446return getLongSize();447}448static long[] card32ToArray(long ptr, int length) {449return toLongs(ptr, length);450}451static long card32ToData(long[] arr) {452return toData(arr);453}454}455456457