001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey V. Kuksenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.nativebridge;
021:
022: /**
023: * This class represents pointer to memory for working with native functions.
024: * It is an analogue of <it>void*</it> C type. Every pointed object can be direct
025: * native object or Java object. In both cases NativeBridge provides correct
026: * passing addresses to native functions, native address is passed as is, Java
027: * object will be implicitly locked and address of lock will be passed to native.
028: * NULL pointer can't be wrapped by this object, NULL is represents by Java
029: * null value.
030: * @see org.apache.harmony.misc.accessors.MemoryAccessor
031: * @see org.apache.harmony.misc.accessors.ArrayAccessor
032: */
033: public abstract class VoidPointer {
034:
035: public ByteBase byteBase;
036: boolean explicitlyLocked = false;
037:
038: VoidPointer(int size, boolean direct) {
039: byteBase = new ByteBase(size, direct);
040: }
041:
042: VoidPointer(byte[] arr, int offset, int size) {
043: byteBase = new ByteBase(arr, offset, size);
044: }
045:
046: VoidPointer(long addr) {
047: byteBase = new ByteBase(addr);
048: }
049:
050: VoidPointer(VoidPointer p) {
051: byteBase = p.byteBase;
052: }
053:
054: VoidPointer(ByteBase base) {
055: this .byteBase = base;
056: }
057:
058: public boolean isDirect() {
059: return byteBase.isDirect();
060: }
061:
062: public abstract int size();
063:
064: /**
065: * Frees memory represented by this pointer. In case of direct object -
066: * the native memory is deallocated. In case of locked Java memory - it is
067: * released (back copying values to Java is not performed).
068: * @see org.apache.harmony.misc.accessors.LockedArray#releaseNoCopy()
069: */
070: public void free() {
071: byteBase.free();
072: byteBase = null;
073: }
074:
075: /**
076: * Returns the memory address for this pointer. Performs the explicit lock
077: * of the pointed memory.
078: * If this object is direct pointer to native memory - returns native address,
079: * If this object reference to Java object, then Java object is locked and
080: * memory address for locked Java object is returned.
081: * Explicitly locked object should be released after usage.
082: * @see #release()
083: * @see #free() h
084: */
085: public final long lock() {
086: explicitlyLocked = true;
087: return byteBase.longLockPointer();
088: }
089:
090: /**
091: * Performs the explicit release of Java object. This method does nothing
092: * if object is direct native pointer or non-locked Java memory.
093: */
094: public final void release() {
095: explicitlyLocked = false;
096: byteBase.unlock();
097: }
098:
099: public void unlock() {
100: byteBase.unlock();
101: }
102:
103: /**
104: * Performs the explicit releaseNoCopy of Java object. This method does nothing
105: * if object is direct native pointer or non-locked Java memory.
106: */
107: public final void releaseNoCopy() {
108: explicitlyLocked = false;
109: byteBase.unlockNoCopy();
110: }
111:
112: public long longLockPointer() {
113: explicitlyLocked = true;
114: return byteBase.longLockPointer();
115: }
116:
117: /** performs short lock */
118: public long shortLockPointer() {
119: explicitlyLocked = true;
120: return byteBase.shortLockPointer();
121: }
122:
123: /**
124: * Internal long lock. Used in NativeBridge wrappers for native functions.
125: * @return - memory address.
126: */
127: final long internalLongLock() {
128: return byteBase.longLockPointer();
129: }
130:
131: /**
132: * Internal short lock. Used in NativeBridge wrappers for native functions.
133: * @return - memory address.
134: */
135: final long internalShortLock() {
136: return byteBase.shortLockPointer();
137: }
138:
139: /**
140: * Internal release. Unlocks objects if it was implicitly locked. This
141: * methods don't release exlicitly locked Java objects.
142: */
143: final void internalRelease() {
144: if (!explicitlyLocked) {
145: byteBase.unlock();
146: }
147: }
148:
149: /**
150: * Internal release. Unlocks objects if it was implicitly locked. This
151: * methods don't release exlicitly locked Java objects. This methods gives
152: * a hint, that values should not be copied back to Java object. Used for
153: * "in" parameters of functions.
154: */
155: final void internalReleaseNoCopy() {
156: if (!explicitlyLocked) {
157: byteBase.unlockNoCopy();
158: }
159: }
160:
161: /**
162: * Copies <code>length</code> bytes from src VoidPointer to this.
163: * Offset in a destination is dstOffset. Starting offset in source
164: * is always 0;
165: *
166: * @param src source byte base
167: * @param dstOffset destination
168: * @param length
169: */
170: public void copy(VoidPointer src, int dstOffset, int length) {
171: byteBase.copy(src.byteBase, dstOffset, length);
172: }
173: }
|