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: package org.apache.harmony.luni.platform;
019:
020: /**
021: * The platform address class is an unsafe virtualization of an OS memory block.
022: *
023: */
024: public class PlatformAddress implements ICommonDataTypes, Comparable {
025:
026: /**
027: * This final field defines the size of an address on this platform.
028: */
029: static final int SIZEOF = Platform.getMemorySystem()
030: .getPointerSize();
031:
032: /**
033: * NULL is the canonical address with address value zero.
034: */
035: public static final PlatformAddress NULL = new PlatformAddress(0, 0);
036:
037: public static final IMemorySpy memorySpy = new RuntimeMemorySpy();
038:
039: static final IMemorySystem osMemory = Platform.getMemorySystem();
040:
041: static final long UNKNOWN = -1;
042:
043: final long osaddr;
044:
045: final long size;
046:
047: PlatformAddress(long address, long size) {
048: super ();
049: osaddr = address;
050: this .size = size;
051: }
052:
053: /**
054: * Sending auto free to an address means that, when this subsystem has
055: * allocated the memory, it will automatically be freed when this object is
056: * collected by the garbage collector if the memory has not already been
057: * freed explicitly.
058: *
059: */
060: public final void autoFree() {
061: memorySpy.autoFree(this );
062: }
063:
064: public PlatformAddress duplicate() {
065: return PlatformAddressFactory.on(osaddr, size);
066: }
067:
068: public PlatformAddress offsetBytes(int offset) {
069: return PlatformAddressFactory
070: .on(osaddr + offset, size - offset);
071: }
072:
073: public PlatformAddress offsetBytes(long offset) {
074: return PlatformAddressFactory
075: .on(osaddr + offset, size - offset);
076: }
077:
078: public final void moveTo(PlatformAddress dest, long numBytes) {
079: osMemory.memmove(dest.osaddr, osaddr, numBytes);
080: }
081:
082: public final boolean equals(Object other) {
083: return (other instanceof PlatformAddress)
084: && (((PlatformAddress) other).osaddr == osaddr);
085: }
086:
087: public final int hashCode() {
088: return (int) osaddr;
089: }
090:
091: public final boolean isNULL() {
092: return this == NULL;
093: }
094:
095: public void free() {
096: // Memory spys can veto the basic free if they determine the memory was
097: // not allocated.
098: if (memorySpy.free(this )) {
099: osMemory.free(osaddr);
100: }
101: }
102:
103: public final void setAddress(int offset, PlatformAddress address) {
104: osMemory.setAddress(osaddr + offset, address.osaddr);
105: }
106:
107: public final PlatformAddress getAddress(int offset) {
108: int addr = getInt(offset);
109: return PlatformAddressFactory.on(addr);
110: }
111:
112: public final void setByte(int offset, byte value) {
113: memorySpy.rangeCheck(this , offset, SIZEOF_JBYTE);
114: osMemory.setByte(osaddr + offset, value);
115: }
116:
117: public final void setByteArray(int offset, byte[] bytes,
118: int bytesOffset, int length) {
119: memorySpy.rangeCheck(this , offset, length * SIZEOF_JBYTE);
120: osMemory.setByteArray(osaddr + offset, bytes, bytesOffset,
121: length);
122: }
123:
124: public final byte getByte(int offset) {
125: memorySpy.rangeCheck(this , offset, SIZEOF_JBYTE);
126: return osMemory.getByte(osaddr + offset);
127: }
128:
129: public final void getByteArray(int offset, byte[] bytes,
130: int bytesOffset, int length) {
131: memorySpy.rangeCheck(this , offset, length * SIZEOF_JBYTE);
132: osMemory.getByteArray(osaddr + offset, bytes, bytesOffset,
133: length);
134: }
135:
136: public final void setShort(int offset, short value, Endianness order) {
137: memorySpy.rangeCheck(this , offset, SIZEOF_JSHORT);
138: osMemory.setShort(osaddr + offset, value, order);
139: }
140:
141: public final void setShort(int offset, short value) {
142: memorySpy.rangeCheck(this , offset, SIZEOF_JSHORT);
143: osMemory.setShort(osaddr + offset, value);
144: }
145:
146: public final short getShort(int offset, Endianness order) {
147: memorySpy.rangeCheck(this , offset, SIZEOF_JSHORT);
148: return osMemory.getShort(osaddr + offset, order);
149: }
150:
151: public final short getShort(int offset) {
152: memorySpy.rangeCheck(this , offset, SIZEOF_JSHORT);
153: return osMemory.getShort(osaddr + offset);
154: }
155:
156: public final void setInt(int offset, int value, Endianness order) {
157: memorySpy.rangeCheck(this , offset, SIZEOF_JINT);
158: osMemory.setInt(osaddr + offset, value, order);
159: }
160:
161: public final void setInt(int offset, int value) {
162: memorySpy.rangeCheck(this , offset, SIZEOF_JINT);
163: osMemory.setInt(osaddr + offset, value);
164: }
165:
166: public final int getInt(int offset, Endianness order) {
167: memorySpy.rangeCheck(this , offset, SIZEOF_JINT);
168: return osMemory.getInt(osaddr + offset, order);
169: }
170:
171: public final int getInt(int offset) {
172: memorySpy.rangeCheck(this , offset, SIZEOF_JINT);
173: return osMemory.getInt(osaddr + offset);
174: }
175:
176: public final void setLong(int offset, long value, Endianness order) {
177: memorySpy.rangeCheck(this , offset, SIZEOF_JLONG);
178: osMemory.setLong(osaddr + offset, value, order);
179: }
180:
181: public final void setLong(int offset, long value) {
182: memorySpy.rangeCheck(this , offset, SIZEOF_JLONG);
183: osMemory.setLong(osaddr + offset, value);
184: }
185:
186: public final long getLong(int offset, Endianness order) {
187: memorySpy.rangeCheck(this , offset, SIZEOF_JLONG);
188: return osMemory.getLong(osaddr + offset, order);
189: }
190:
191: public final long getLong(int offset) {
192: memorySpy.rangeCheck(this , offset, SIZEOF_JLONG);
193: return osMemory.getLong(osaddr + offset);
194: }
195:
196: public final void setFloat(int offset, float value, Endianness order) {
197: memorySpy.rangeCheck(this , offset, SIZEOF_JFLOAT);
198: osMemory.setFloat(osaddr + offset, value, order);
199: }
200:
201: public final void setFloat(int offset, float value) {
202: memorySpy.rangeCheck(this , offset, SIZEOF_JFLOAT);
203: osMemory.setFloat(osaddr + offset, value);
204: }
205:
206: public final float getFloat(int offset, Endianness order) {
207: memorySpy.rangeCheck(this , offset, SIZEOF_JFLOAT);
208: return osMemory.getFloat(osaddr + offset, order);
209: }
210:
211: public final float getFloat(int offset) {
212: memorySpy.rangeCheck(this , offset, SIZEOF_JFLOAT);
213: return osMemory.getFloat(osaddr + offset);
214: }
215:
216: public final void setDouble(int offset, double value,
217: Endianness order) {
218: memorySpy.rangeCheck(this , offset, SIZEOF_JDOUBLE);
219: osMemory.setDouble(osaddr + offset, value, order);
220: }
221:
222: public final void setDouble(int offset, double value) {
223: memorySpy.rangeCheck(this , offset, SIZEOF_JDOUBLE);
224: osMemory.setDouble(osaddr + offset, value);
225: }
226:
227: public final double getDouble(int offset, Endianness order) {
228: memorySpy.rangeCheck(this , offset, SIZEOF_JDOUBLE);
229: return osMemory.getDouble(osaddr + offset, order);
230: }
231:
232: public final double getDouble(int offset) {
233: memorySpy.rangeCheck(this , offset, SIZEOF_JDOUBLE);
234: return osMemory.getDouble(osaddr + offset);
235: }
236:
237: public final long toLong() {
238: return osaddr;
239: }
240:
241: public final String toString() {
242: return "PlatformAddress[" + osaddr + "]"; //$NON-NLS-1$ //$NON-NLS-2$
243: }
244:
245: public final long getSize() {
246: return size;
247: }
248:
249: public final int compareTo(Object other) {
250: if (other == null) {
251: throw new NullPointerException(); // per spec.
252: }
253: if (other instanceof PlatformAddress) {
254: long otherPA = ((PlatformAddress) other).osaddr;
255: if (osaddr == otherPA) {
256: return 0;
257: }
258: return osaddr < otherPA ? -1 : 1;
259: }
260:
261: throw new ClassCastException(); // per spec.
262: }
263: }
|