Mapped object code improvements: added license, additional documentation, reformatted code, now using LWJGLUtil.log instead of System.err.

Added system properties for bytecode transformer debug output. (org.lwjgl.util.mapped.PrintTiming & org.lwjgl.util.mapped.PrintActivity)
Added support for bounds checking the view of mapped objects. Enabled with org.lwjgl.util.mapped.Checks
Added tests for mapped objects. (org.lwjgl.test.mapped package)
Added "[LWJGL] " prefix to all LWJGL generated debug messages.
This commit is contained in:
Ioannis Tsakpinis 2011-07-12 13:29:04 +00:00
parent b3e18e1d84
commit 585549f1f1
22 changed files with 2404 additions and 1156 deletions

View File

@ -448,7 +448,7 @@ public class LWJGLUtil {
/**
* Gets a boolean property as a privileged action.
*/
private static boolean getPrivilegedBoolean(final String property_name) {
public static boolean getPrivilegedBoolean(final String property_name) {
Boolean value = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
return Boolean.getBoolean(property_name);
@ -464,7 +464,7 @@ public class LWJGLUtil {
*/
public static void log(String msg) {
if (DEBUG) {
System.err.println(msg);
System.err.println("[LWJGL] " + msg);
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType(sizeof = 4)
public class MappedFloat extends MappedObject {
public MappedFloat() {
this.test();
}
public float value;
public void test() {
this.value = 4;
}
@Override
public String toString() {
return "MappedFloat[" + value + "]";
}
}

View File

@ -0,0 +1,237 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObjectUnsafe;
import java.nio.ByteBuffer;
import java.util.Arrays;
import static org.lwjgl.util.mapped.MappedHelper.*;
/** @author Riven */
@SuppressWarnings("static-access")
public class MappedObjectBench {
static class InstanceVec3 {
float x, y, z;
@Override
public String toString() {
return "InstanceVec3[" + x + ", " + y + ", " + z + "]";
}
}
static class ArrayVec3 {
float[] a;
int i;
@Override
public String toString() {
return "ArrayVec3[" + a[i * 3 + 0] + ", " + a[i * 3 + 1] + ", " + a[i * 3 + 2] + "]";
}
}
static void benchmarkInstances() {
final int runs = 64;
final int iterations = 64 * 1024;
InstanceVec3 vec1 = new InstanceVec3();
InstanceVec3 vec2 = new InstanceVec3();
InstanceVec3 vec3 = new InstanceVec3();
long[] took = new long[runs];
for ( int run = 0; run < runs; run++ ) {
long t0 = System.nanoTime();
for ( int iteration = 0; iteration < iterations; iteration++ ) {
vec1.x = 13;
vec1.y += vec1.y * vec1.x + 0.3f;
vec1.z += vec2.y + vec1.x + 0.3f;
vec2.z += vec2.y + vec1.x;
vec3.z += vec2.z + vec1.y;
}
long t1 = System.nanoTime();
took[run] = t1 - t0;
}
Arrays.sort(took);
System.out.println("instance took: " + took[took.length / 2] / 1024 + "us");
System.out.println(vec1);
System.out.println(vec2);
System.out.println(vec3);
}
static void benchmarkMapped() {
final int runs = 64;
final int iterations = 64 * 1024;
ByteBuffer bb = ByteBuffer.allocateDirect(200);
MappedVec3 vecs = MappedVec3.map(bb);
MappedVec3 vec1 = vecs.dup();
MappedVec3 vec2 = vecs.dup();
MappedVec3 vec3 = vecs.dup();
vec1.view = 0;
vec2.view = 1;
vec3.view = 2;
long[] took = new long[runs];
for ( int run = 0; run < runs; run++ ) {
long t0 = System.nanoTime();
for ( int iteration = 0; iteration < iterations; iteration += 2 ) {
vec1.x = 13;
vec1.y += vec1.y * vec1.x + 0.3f;
vec1.z += vec2.y + vec1.x + 0.3f;
vec2.z += vec2.y + vec1.x;
vec3.z += vec2.z + vec1.y;
vec1.x = 13;
vec1.y += vec1.y * vec1.x + 0.3f;
vec1.z += vec2.y + vec1.x + 0.3f;
vec2.z += vec2.y + vec1.x;
vec3.z += vec2.z + vec1.y;
}
long t1 = System.nanoTime();
took[run] = t1 - t0;
}
Arrays.sort(took);
System.out.println("mapped took: " + took[took.length / 2] / 1024 + "us");
System.out.println(vec1);
System.out.println(vec2);
System.out.println(vec3);
System.out.println(bb);
}
static void benchmarkIndirectArray() {
final int runs = 64;
final int iterations = 64 * 1024;
float[] bb = new float[200];
ArrayVec3 vec1 = new ArrayVec3();
ArrayVec3 vec2 = new ArrayVec3();
ArrayVec3 vec3 = new ArrayVec3();
vec1.a = bb;
vec2.a = bb;
vec3.a = bb;
vec1.i = 0;
vec2.i = 1;
vec3.i = 2;
long[] took = new long[runs];
for ( int run = 0; run < runs; run++ ) {
long t0 = System.nanoTime();
for ( int iteration = 0; iteration < iterations; iteration++ ) {
vec1.a[vec1.i * 3 + 0] = 13;
vec1.a[vec1.i * 3 + 1] += vec1.a[vec1.i * 3 + 1] * vec1.a[vec1.i * 3 + 0] + 0.3f;
vec1.a[vec1.i * 3 + 2] += vec2.a[vec2.i * 3 + 1] + vec1.a[vec1.i * 3 + 0] + 0.3f;
vec2.a[vec2.i * 3 + 2] += vec2.a[vec2.i * 3 + 1] + vec1.a[vec1.i * 3 + 0];
vec3.a[vec3.i * 3 + 2] += vec2.a[vec2.i * 3 + 2] + vec2.a[vec2.i * 3 + 1];
}
long t1 = System.nanoTime();
took[run] = t1 - t0;
}
Arrays.sort(took);
System.out.println("array took: " + took[took.length / 2] / 1024 + "us");
System.out.println(vec1);
System.out.println(vec2);
System.out.println(vec3);
System.out.println(bb);
}
static void benchmarkDirectArray() {
final int runs = 64;
final int iterations = 64 * 1024;
float[] bb = new float[200];
long[] took = new long[runs];
for ( int run = 0; run < runs; run++ ) {
long t0 = System.nanoTime();
for ( int iteration = 0; iteration < iterations; iteration++ ) {
bb[1 * 3 + 0] = 13;
bb[1 * 3 + 1] += bb[1 * 3 + 1] * bb[1 * 3 + 0] + 0.3f;
bb[1 * 3 + 2] += bb[2 * 3 + 1] + bb[1 * 3 + 0] + 0.3f;
bb[2 * 3 + 2] += bb[2 * 3 + 1] + bb[1 * 3 + 0];
bb[3 * 3 + 2] += bb[2 * 3 + 2] + bb[2 * 3 + 1];
}
long t1 = System.nanoTime();
took[run] = t1 - t0;
}
Arrays.sort(took);
System.out.println("array2 took: " + took[took.length / 2] / 1024 + "us");
System.out.println(bb);
}
static void benchmarkUnsafe() {
final int runs = 64;
final int iterations = 64 * 1024;
ByteBuffer bb = ByteBuffer.allocateDirect(200);
long addr = MappedObjectUnsafe.getBufferBaseAddress(bb);
long[] took = new long[runs];
for ( int run = 0; run < runs; run++ ) {
long t0 = System.nanoTime();
for ( int iteration = 0; iteration < iterations; iteration++ ) {
fput(13, addr + (1 * 3 + 0) * 4);
fput(fget(addr + (1 * 3 + 1) * 4) + fget(addr + (1 * 3 + 1) * 4) * fget(addr + (1 * 3 + 0) * 4) + 0.3f, addr + (1 * 3 + 1) * 4);
fput(fget(addr + (1 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4) + fget(addr + (1 * 3 + 0) * 4) + 0.3f, addr + (1 * 3 + 2) * 4);
fput(fget(addr + (2 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4) + fget(addr + (1 * 3 + 0) * 4), addr + (2 * 3 + 2) * 4);
fput(fget(addr + (3 * 3 + 2) * 4) + fget(addr + (2 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4), addr + (3 * 3 + 2) * 4);
}
long t1 = System.nanoTime();
took[run] = t1 - t0;
}
Arrays.sort(took);
System.out.println("unsafe took: " + took[took.length / 2] / 1024 + "us");
System.out.println(bb);
}
}

View File

@ -0,0 +1,159 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedHelper;
import org.lwjgl.util.mapped.MappedObjectUnsafe;
import java.nio.ByteBuffer;
/** @author Riven */
@SuppressWarnings("static-access")
public class MappedObjectTests1 {
static class Test {
int value;
}
static void testViewField() {
Test test = new Test();
test.value = 13;
test.value += 1;
test.value++;
System.out.println(test);
ByteBuffer bb = ByteBuffer.allocateDirect(200);
MappedFloat vecs = MappedFloat.map(bb);
// verify 'malloc' and SIZEOF
{
MappedFloat vecs1 = MappedFloat.malloc(1234);
assert (vecs1.stride == MappedFloat.SIZEOF);
assert (vecs1.stride * 1234 == vecs1.backingByteBuffer().capacity());
assert (MappedFloat.SIZEOF * 1234 == vecs1.backingByteBuffer().capacity());
}
// manipulate 'mapped.value'
{
assert (vecs.value == 0.0f); // 4.0 is set in constructor, but runViewConstructor is not called
vecs.value = 1.1f;
assert (vecs.value == 1.1f);
}
// manipulate 'view' with assignment
{
assert (vecs.view == 0);
vecs.view = 1;
assert (vecs.view == 1);
assert (vecs.value != 1.1f); // old view
vecs.view = 0;
}
// manipulate 'view' with iinc
{
assert (vecs.view == 0);
vecs.view++;
assert (vecs.view == 1);
assert (vecs.value != 1.1f); // old view
vecs.view = 0;
}
// test bound check
{
assert (vecs.view == 0);
try {
vecs.view = 50;
System.out.println("org.lwjgl.util.mapped.Checks is false or there is a bug in bounds checking.");
vecs.view = 0;
} catch (IndexOutOfBoundsException e) {
// expected, ignore
}
assert (vecs.view == 0);
}
// test dup
{
int newStride = 16;
int doOffset = 0;
vecs.view = 0;
MappedFloat.configure(vecs, newStride, doOffset);
MappedFloat dec2 = vecs.dup();
MappedFloat.configure(dec2, newStride, doOffset);
String s1 = vecs.baseAddress + "," + vecs.viewAddress + "," + vecs.stride + "," + vecs.SIZEOF;
String s2 = dec2.baseAddress + "," + dec2.viewAddress + "," + dec2.stride + "," + dec2.SIZEOF;
// System.out.println(s1);
// System.out.println(s2);
assert (s1.equals(s2));
dec2.view++;
String s3 = vecs.baseAddress + "," + vecs.viewAddress + "," + vecs.stride + "," + vecs.SIZEOF;
String s4 = dec2.baseAddress + "," + dec2.viewAddress + "," + dec2.stride + "," + dec2.SIZEOF;
// System.out.println(s3);
// System.out.println(s4);
assert (!s3.equals(s4));
}
// test newBuffer
{
long addr1 = MappedObjectUnsafe.getBufferBaseAddress(bb);
ByteBuffer bb2 = MappedHelper.newBuffer(addr1, bb.capacity());
long addr2 = MappedObjectUnsafe.getBufferBaseAddress(bb);
System.out.println(bb);
System.out.println(bb2);
System.out.println(addr1);
System.out.println(addr2);
}
// test 'copy'
{
vecs.value = 13.37f;
MappedFloat dec2 = vecs.dup();
dec2.view = 1;
System.out.println(vecs);
System.out.println(dec2);
vecs.copyTo(dec2);
System.out.println(vecs);
System.out.println(dec2);
assert (dec2.value == 13.37f);
vecs.value = 73.31f;
vecs.copyRange(dec2, 1);
assert (dec2.value == 73.31f);
}
}
}

View File

@ -0,0 +1,130 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import java.nio.ByteBuffer;
/** @author Riven */
@SuppressWarnings("static-access")
public class MappedObjectTests2 {
static void testWriteFieldAccess(MappedVec3 vecs) {
// write access results into a transform-time IllegalAccessException
System.out.println(vecs.baseAddress); // test read-access
System.out.println(vecs.viewAddress); // test read-access
System.out.println(vecs.align); // test read-access
System.out.println(MappedVec3.SIZEOF); // test read-access
}
static void testFields() {
ByteBuffer bb = ByteBuffer.allocateDirect(200);
MappedVec3 vecs = MappedVec3.map(bb);
testWriteFieldAccess(vecs);
vecs.x = 13.13f;
vecs.y = 14.14f;
vecs.z = 15.15f;
System.out.println(vecs.viewAddress);
assert (vecs.x == 13.13f);
assert (vecs.y == 14.14f);
assert (vecs.z == 15.15f);
vecs.view = 0;
assert (vecs.x == 13.13f);
assert (vecs.y == 14.14f);
assert (vecs.z == 15.15f);
System.out.println(vecs);
vecs.view = 1;
System.out.println(vecs);
assert (vecs.x == 0.0f);
assert (vecs.y == 0.0f);
assert (vecs.z == 0.0f);
// now it becomes weird: offsets and strides
vecs.view = 1;
vecs.x = 0.1234f;
vecs.view = 0;
// test stride & sizeof
{
long a1 = vecs.viewAddress;
vecs.view = 1;
long a2 = vecs.viewAddress;
assert (a2 - a1 == MappedVec3.SIZEOF);
assert (a2 - a1 == vecs.stride);
vecs.view = 0;
}
int newStride = 16;
MappedVec3.configure(vecs, newStride, +4);
assert (vecs.z == 0.1234f); // vecs[1].x ended up in vecs[0].z due to 1float offset
MappedVec3.configure(vecs, newStride, +8);
assert (vecs.z == 0.0000f); // vecs[1].z ended up in vecs[0].z due to 2float offset
// test new stride
{
long a1 = vecs.viewAddress;
vecs.view = 1;
long a2 = vecs.viewAddress;
assert (a2 - a1 == newStride);
vecs.view = 0;
}
// example: GPU => VBO => VTN
{
MappedVec3 v = MappedVec3.map(bb);
MappedVec2 t = MappedVec2.map(bb);
MappedVec3 n = MappedVec3.map(bb);
int stride = MappedVec3.SIZEOF + MappedVec2.SIZEOF + MappedVec3.SIZEOF;
assert (stride == 32);
MappedVec3.configure(v, stride, 0);
MappedVec2.configure(t, stride, MappedVec3.SIZEOF);
MappedVec3.configure(n, stride, MappedVec3.SIZEOF + MappedVec2.SIZEOF);
}
}
}

View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static org.lwjgl.util.mapped.MappedObject.*;
/** @author Riven */
@SuppressWarnings("static-access")
public class MappedObjectTests3 {
static void testMappedBuffer() {
int elementCount = 4;
MappedSomething some = MappedSomething.malloc(elementCount);
assert (some.data != some.data);
long addr1 = MappedObjectUnsafe.getBufferBaseAddress(some.backingByteBuffer());
ByteBuffer mapped = some.data; // creates new ByteBuffer instance
long addr2 = MappedObjectUnsafe.getBufferBaseAddress(mapped);
assert (addr2 - addr1 == 4);
assert (mapped.capacity() == MappedSomething.SIZEOF - 4);
some.view++;
mapped = some.data; // creates new ByteBuffer instance
long addr3 = MappedObjectUnsafe.getBufferBaseAddress(mapped);
assert (addr3 - addr1 == 4 + MappedSomething.SIZEOF);
assert (addr3 - addr2 == 0 + MappedSomething.SIZEOF);
assert (mapped.capacity() == MappedSomething.SIZEOF - 4);
}
static void testForeach() {
int elementCount = 4;
MappedSomething some = MappedSomething.malloc(elementCount);
int i = 0;
for ( MappedSomething item : foreach(some, elementCount) ) {
assert (item.view == i++);
}
assert (some.view != elementCount);
System.out.println("current.view=" + some.view + ", not " + elementCount + ", as you might expect");
}
@MappedType(sizeof = 12)
public static class Xyz extends MappedObject {
int x, y, z;
}
static void testConstructor() {
int capacity = 1024;
ByteBuffer bb = ByteBuffer.allocateDirect(capacity).order(ByteOrder.nativeOrder());
long address = MappedObjectUnsafe.getBufferBaseAddress(bb);
MappedFloat mf = MappedFloat.map(address, capacity);
assert (address == mf.baseAddress);
assert (mf.value == 0.0f);
mf.view = 1;
assert (mf.value == 0.0f);
mf.runViewConstructor();
assert (mf.value == 4.0f);
Xyz.malloc(3);
}
static void testMappedSet() {
MappedVec2 vec2 = MappedVec2.malloc(3);
MappedVec3 vec3 = MappedVec3.malloc(3);
MappedSet2 set = MappedSet.create(vec2, vec3);
assert (vec2.view == 0);
assert (vec3.view == 0);
set.view = 2;
assert (vec2.view == 2);
assert (vec3.view == 2);
set.view = 0;
assert (vec2.view == 0);
assert (vec3.view == 0);
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedField;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
import java.nio.ByteBuffer;
/** @author Riven */
@MappedType(sizeof = 64)
public class MappedSomething extends MappedObject {
@MappedField(byteOffset = 0)
public int used;
@MappedField(byteOffset = 4, byteLength = 64 - 4)
public ByteBuffer data;
@Override
public String toString() {
return "MappedSomething[" + used + "]";
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType(sizeof = 8)
public class MappedVec2 extends MappedObject {
public float x;
public float y;
@Override
public String toString() {
return "MappedVec2[" + x + "," + y + "]";
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType(sizeof = 12)
public class MappedVec3 extends MappedObject {
public float x;
public float y;
public float z;
@Override
public String toString() {
return "[" + x + "," + y + "," + z + "]";
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObjectClassLoader;
import org.lwjgl.util.mapped.MappedObjectTransformer;
/** @author Riven */
public class TestMappedObject {
static {
boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side effect!!!
if ( !assertsEnabled )
throw new RuntimeException("Asserts must be enabled for this test.");
}
public static void main(String[] args) {
MappedObjectTransformer.register(MappedFloat.class);
MappedObjectTransformer.register(MappedVec2.class);
MappedObjectTransformer.register(MappedVec3.class);
MappedObjectTransformer.register(MappedSomething.class);
MappedObjectTransformer.register(MappedObjectTests3.Xyz.class);
if ( MappedObjectClassLoader.fork(TestMappedObject.class, args) ) {
return;
}
MappedObjectTests1.testViewField();
MappedObjectTests2.testFields();
// MappedObjectBench.benchmarkMapped();
// MappedObjectBench.benchmarkInstances();
// MappedObjectBench.benchmarkIndirectArray();
// MappedObjectBench.benchmarkDirectArray();
// MappedObjectBench.benchmarkUnsafe();
MappedObjectTests3.testMappedBuffer();
MappedObjectTests3.testForeach();
MappedObjectTests3.testConstructor();
MappedObjectTests3.testMappedSet();
}
}

View File

@ -1,19 +1,65 @@
/*
* Created on Jun 24, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used on fields of {@link MappedType} classes,
* to manually specify byte offsets and lengths. This is useful when the
* mapped fields require custom alignment. {@link java.nio.ByteBuffer}
* fields are required to have this annotation with a hardcoded byte length.
*
* @author Riven
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MappedField {
long byteOffset();
long byteLength() default -1;
}
/**
* Specifies the field byte offset within the mapped object.
*
* @return the field byte offset
*/
long byteOffset();
/**
* Specifies the field byte length. Required for {@link java.nio.ByteBuffer} fields.
*
* @return the field byte length
*/
long byteLength() default -1;
}

View File

@ -1,52 +1,72 @@
/*
* Created on Jul 4, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import java.util.Iterator;
/**
* Iterable implementation for {@link MappedObject}.
*
* @author Riven
*/
public class MappedForeach<T extends MappedObject> implements Iterable<T> {
public class MappedForeach<T extends MappedObject> implements Iterable<T>
{
final T mapped;
final int elementCount;
private final T mapped;
private final int elementCount;
MappedForeach(T mapped, int elementCount)
{
this.mapped = mapped;
this.elementCount = elementCount;
}
MappedForeach(T mapped, int elementCount) {
this.mapped = mapped;
this.elementCount = elementCount;
}
@Override
public Iterator<T> iterator()
{
return new Iterator<T>()
{
private int index = 0;
public Iterator<T> iterator() {
return new Iterator<T>() {
@Override
public boolean hasNext()
{
return this.index < (MappedForeach.this.elementCount);
}
private int index;
@Override
public T next()
{
mapped.viewAddress = mapped.baseAddress + (this.index++) * mapped.stride;
public boolean hasNext() {
return this.index < (MappedForeach.this.elementCount);
}
return mapped;
}
public T next() {
mapped.viewAddress = mapped.baseAddress + (this.index++) * mapped.stride;
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
}
return mapped;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}

View File

@ -1,181 +1,188 @@
/*
* Created on Jun 28, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import java.nio.ByteBuffer;
/**
* [INTERNAL USE ONLY]
* <p/>
* Helper class used by the bytecode transformer.
*
* @author Riven
*/
public class MappedHelper {
public class MappedHelper
{
public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof)
{
if (mo.baseAddress != 0L)
throw new IllegalStateException("this method should not be called by user-code");
public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof) {
if ( mo.baseAddress != 0L )
throw new IllegalStateException("this method should not be called by user-code");
if (buffer == null)
throw new NullPointerException("buffer");
if (!buffer.isDirect())
throw new IllegalArgumentException("bytebuffer must be direct");
mo.preventGC = buffer;
if ( buffer == null )
throw new NullPointerException("buffer");
if ( !buffer.isDirect() )
throw new IllegalArgumentException("bytebuffer must be direct");
mo.preventGC = buffer;
if (align <= 0)
throw new IllegalArgumentException("invalid alignment");
mo.align = align;
if ( align <= 0 )
throw new IllegalArgumentException("invalid alignment");
mo.align = align;
if (sizeof % align != 0)
throw new IllegalStateException("sizeof not a multiple of alignment");
mo.stride = sizeof;
if ( sizeof % align != 0 )
throw new IllegalStateException("sizeof not a multiple of alignment");
mo.stride = sizeof;
long addr = MappedObjectUnsafe.getBufferBaseAddress(buffer);
if (addr % align != 0)
throw new IllegalStateException("buffer address not aligned on " + align + " bytes");
long addr = MappedObjectUnsafe.getBufferBaseAddress(buffer);
if ( addr % align != 0 )
throw new IllegalStateException("buffer address not aligned on " + align + " bytes");
mo.baseAddress = mo.viewAddress = addr;
}
mo.baseAddress = mo.viewAddress = addr;
}
public static void put_views(MappedSet2 set, int view)
{
set.view(view);
}
public static void put_views(MappedSet2 set, int view) {
set.view(view);
}
public static void put_views(MappedSet3 set, int view)
{
set.view(view);
}
public static void put_views(MappedSet3 set, int view) {
set.view(view);
}
public static void put_views(MappedSet4 set, int view)
{
set.view(view);
}
public static void put_views(MappedSet4 set, int view) {
set.view(view);
}
public static void put_view(MappedObject mapped, int view)
{
mapped.viewAddress = mapped.baseAddress + view * mapped.stride;
}
public static void put_view(MappedObject mapped, int view) {
mapped.setViewAddress(mapped.baseAddress + view * mapped.stride);
}
public static int get_view(MappedObject mapped)
{
return (int) (mapped.viewAddress - mapped.baseAddress) / mapped.stride;
}
public static int get_view(MappedObject mapped) {
return (int)(mapped.viewAddress - mapped.baseAddress) / mapped.stride;
}
public static MappedObject dup(MappedObject src, MappedObject dst)
{
dst.baseAddress = src.baseAddress;
dst.viewAddress = src.viewAddress;
dst.stride = src.stride;
dst.align = src.align;
dst.preventGC = src.preventGC;
return dst;
}
public static MappedObject dup(MappedObject src, MappedObject dst) {
dst.baseAddress = src.baseAddress;
dst.viewAddress = src.viewAddress;
dst.stride = src.stride;
dst.align = src.align;
dst.preventGC = src.preventGC;
return dst;
}
public static MappedObject slice(MappedObject src, MappedObject dst)
{
dst.baseAddress = src.viewAddress; // !
dst.viewAddress = src.viewAddress;
dst.stride = src.stride;
dst.align = src.align;
dst.preventGC = src.preventGC;
return dst;
}
public static MappedObject slice(MappedObject src, MappedObject dst) {
dst.baseAddress = src.viewAddress; // !
dst.viewAddress = src.viewAddress;
dst.stride = src.stride;
dst.align = src.align;
dst.preventGC = src.preventGC;
return dst;
}
public static void copy(MappedObject src, MappedObject dst, int bytes)
{
MappedObjectUnsafe.INSTANCE.copyMemory(src.viewAddress, dst.viewAddress, bytes);
}
public static void copy(MappedObject src, MappedObject dst, int bytes) {
MappedObjectUnsafe.INSTANCE.copyMemory(src.viewAddress, dst.viewAddress, bytes);
}
public static ByteBuffer newBuffer(long address, int capacity)
{
return MappedObjectUnsafe.newBuffer(address, capacity);
}
public static ByteBuffer newBuffer(long address, int capacity) {
return MappedObjectUnsafe.newBuffer(address, capacity);
}
// ---- primitive fields read/write
// ---- primitive fields read/write
// byte
// byte
public static void bput(byte value, long addr)
{
MappedObjectUnsafe.INSTANCE.putByte(addr, value);
}
public static void bput(byte value, long addr) {
MappedObjectUnsafe.INSTANCE.putByte(addr, value);
}
public static byte bget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getByte(addr);
}
public static byte bget(long addr) {
return MappedObjectUnsafe.INSTANCE.getByte(addr);
}
// short
// short
public static void sput(short value, long addr)
{
MappedObjectUnsafe.INSTANCE.putShort(addr, value);
}
public static void sput(short value, long addr) {
MappedObjectUnsafe.INSTANCE.putShort(addr, value);
}
public static short sget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getShort(addr);
}
public static short sget(long addr) {
return MappedObjectUnsafe.INSTANCE.getShort(addr);
}
// char
// char
public static void cput(char value, long addr)
{
MappedObjectUnsafe.INSTANCE.putChar(addr, value);
}
public static void cput(char value, long addr) {
MappedObjectUnsafe.INSTANCE.putChar(addr, value);
}
public static char cget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getChar(addr);
}
public static char cget(long addr) {
return MappedObjectUnsafe.INSTANCE.getChar(addr);
}
// int
// int
public static void iput(int value, long addr)
{
MappedObjectUnsafe.INSTANCE.putInt(addr, value);
}
public static void iput(int value, long addr) {
MappedObjectUnsafe.INSTANCE.putInt(addr, value);
}
public static int iget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getInt(addr);
}
public static int iget(long addr) {
return MappedObjectUnsafe.INSTANCE.getInt(addr);
}
// float
// float
public static void fput(float value, long addr)
{
MappedObjectUnsafe.INSTANCE.putFloat(addr, value);
}
public static void fput(float value, long addr) {
MappedObjectUnsafe.INSTANCE.putFloat(addr, value);
}
public static float fget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getFloat(addr);
}
public static float fget(long addr) {
return MappedObjectUnsafe.INSTANCE.getFloat(addr);
}
// long
// long
public static void jput(long value, long addr)
{
MappedObjectUnsafe.INSTANCE.putLong(addr, value);
}
public static void jput(long value, long addr) {
MappedObjectUnsafe.INSTANCE.putLong(addr, value);
}
public static long jget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getLong(addr);
}
public static long jget(long addr) {
return MappedObjectUnsafe.INSTANCE.getLong(addr);
}
// double
// double
public static void dput(double value, long addr)
{
MappedObjectUnsafe.INSTANCE.putDouble(addr, value);
}
public static void dput(double value, long addr) {
MappedObjectUnsafe.INSTANCE.putDouble(addr, value);
}
public static double dget(long addr) {
return MappedObjectUnsafe.INSTANCE.getDouble(addr);
}
public static double dget(long addr)
{
return MappedObjectUnsafe.INSTANCE.getDouble(addr);
}
}

View File

@ -1,185 +1,248 @@
/*
* Created on Jun 25, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import org.lwjgl.LWJGLUtil;
import java.nio.ByteBuffer;
/**
* Base superclass of all mapped objects. Classes that require
* data mapping should extend this class and also be annotated
* with {@link MappedType}.
* <p/>
* Subclasses may only specify the default constructor. Any code
* inside that constructor is optional, but will not run when the
* view is instantiated, see {@link #runViewConstructor()}.
* <p/>
* Bounds checking may be enabled through a JVM system property: org.lwjgl.util.mapped.Checks=true
*
* @author Riven
*/
public class MappedObject {
public class MappedObject
{
public MappedObject()
{
//
}
static final boolean CHECKS = LWJGLUtil.getPrivilegedBoolean("org.lwjgl.util.mapped.Checks");
// these fields are not assignable/writable by user-code
public long baseAddress;
public long viewAddress;
public int stride;
public int align;
public MappedObject() {
//
}
/**
* Holds the value of sizeof of the sub-type of this MappedObject<br>
* <br>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.SIZEOF</code> will yield 8 (2 floats)<br>
* <code>Vec3.SIZEOF</code> will yield 12 (3 floats)<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
/** The mapped object base memory address, in bytes. Read-only. */
public long baseAddress;
// any method that calls these field will have its call-site modified
public static int SIZEOF = -1; // 'final' per subtype
public int view; // read/write
/** The mapped object view memory address, in bytes. Read-only. */
public long viewAddress;
public final void next()
{
this.viewAddress += this.stride;
}
/** The mapped object stride, in bytes. Read-only. */
public int stride;
/**
* Creates a MappedObject instance, mapping the memory region of the specified direct ByteBuffer.<br>
* <br>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.map(buffer)</code> will return a mapped Vec2 instance.<br>
* <code>Vec3.map(buffer)</code> will return a mapped Vec3 instance.<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
/** The mapped object memory alignment, in bytes. Read-only. */
public int align;
@SuppressWarnings("unused")
public static <T extends MappedObject> T map(ByteBuffer bb)
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Holds the value of sizeof of the sub-type of this MappedObject<br>
* <br>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.SIZEOF</code> will yield 8 (2 floats)<br>
* <code>Vec3.SIZEOF</code> will yield 12 (3 floats)<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
public static int SIZEOF = -1; // any method that calls these field will have its call-site modified ('final' per subtype)
@SuppressWarnings("unused")
public static <T extends MappedObject> T map(long address, int capacity)
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* The mapped object view offset, in elements. Read/write.
* This is a virtual field, used as a convenient getter/setter for {@see viewAddress}.
*/
public int view;
/**
* Creates a MappedObject instance, mapping the memory region of an allocated direct ByteBuffer with a capacity of <code>elementCount*SIZEOF</code>
* <br>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.malloc(int)</code> will return a mapped Vec2 instance.<br>
* <code>Vec3.malloc(int)</code> will return a mapped Vec3 instance.<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
/** Moves the current view to the next element. */
public final void next() {
setViewAddress(this.viewAddress + this.stride);
}
@SuppressWarnings("unused")
public static <T extends MappedObject> T malloc(int elementCount)
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
final void setViewAddress(final long address) {
if ( CHECKS )
checkAddress(address);
this.viewAddress = address;
}
/**
* Creates an identical new MappedObject instance, comparable to the
* contract of ByteBuffer.duplicate()
*/
final void checkAddress(final long address) {
if ( preventGC.capacity() < (address + stride - baseAddress) )
throw new IndexOutOfBoundsException();
}
public final <T extends MappedObject> T dup()
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Creates a MappedObject instance, mapping the memory region of the specified direct ByteBuffer.
* <p/>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.map(buffer)</code> will return a mapped Vec2 instance.<br>
* <code>Vec3.map(buffer)</code> will return a mapped Vec3 instance.<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
@SuppressWarnings("unused")
public static <T extends MappedObject> T map(ByteBuffer bb) {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Creates a new MappedObject instance, with a base offset equal to
* the offset of the current view, comparable to the contract of ByteBuffer.slice()
*/
/**
* Creates a MappedObject instance, mapping the memory region specified. This is useful for mapping
* arbitrary regions in memory, e.g. OpenCL CLMem objects, without creating a ByteBuffer first.
* <p/>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.map(buffer)</code> will return a mapped Vec2 instance.<br>
* <code>Vec3.map(buffer)</code> will return a mapped Vec3 instance.<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
@SuppressWarnings("unused")
public static <T extends MappedObject> T map(long address, int capacity) {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
public final <T extends MappedObject> T slice()
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Creates a MappedObject instance, mapping the memory region of an allocated direct ByteBuffer with a capacity of <code>elementCount*SIZEOF</code>
* <p/>
* The behavior of this (transformed) method does not follow the normal Java behavior.<br>
* <code>Vec2.malloc(int)</code> will return a mapped Vec2 instance.<br>
* <code>Vec3.malloc(int)</code> will return a mapped Vec3 instance.<br>
* This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br>
* Using Java 5.0's static-import on this method will break functionality.
*/
@SuppressWarnings("unused")
public static <T extends MappedObject> T malloc(int elementCount) {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
public final <T extends MappedObject> void runViewConstructor()
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Creates an identical new MappedObject instance, comparable to the
* contract of {@link ByteBuffer#duplicate}. This is useful when more than one
* views of the mapped object are required at the same time, e.g. in
* multithreaded access.
*/
public final <T extends MappedObject> T dup() {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Copies and amount of <code>SIZEOF</code> bytes, from the current
* mapped object, to the specified mapped object.
*/
/**
* Creates a new MappedObject instance, with a base offset equal to
* the offset of the current view, comparable to the contract of {@link ByteBuffer#slice}.
*/
public final <T extends MappedObject> T slice() {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
@SuppressWarnings("unused")
public final <T extends MappedObject> void copyTo(T target)
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Any code in the default constructor will not run automatically. This method
* can be used to run execute that code on the current view.
*/
public final <T extends MappedObject> void runViewConstructor() {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Copies and amount of <code>SIZEOF*instances<c/ode> bytes, from the
* current mapped object, to the specified mapped object.
*/
/**
* Copies and amount of <code>SIZEOF</code> bytes, from the current
* mapped object, to the specified mapped object.
*/
@SuppressWarnings("unused")
public final <T extends MappedObject> void copyTo(T target) {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
@SuppressWarnings("unused")
public final <T extends MappedObject> void copyRange(T target, int instances)
{
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Copies and amount of <code>SIZEOF*instances<c/ode> bytes, from the
* current mapped object, to the specified mapped object.
*/
@SuppressWarnings("unused")
public final <T extends MappedObject> void copyRange(T target, int instances) {
// any method that calls this method will have its call-site modified
throw new InternalError("type not registered");
}
/**
* Creates an Iterable<MappedObject> that will step through
* <code>elementCount</code> views, leaving the <code>view</code> at
* the last valid value.<br>
* <br>
* For convenience you are encouraged to static-import this specific method:
* <code>import static org.lwjgl.util.mapped.MappedObject.foreach;</code>
*/
/**
* Creates an {@link Iterable} <MappedObject> that will step through
* <code>elementCount</code> views, leaving the <code>view</code> at
* the last valid value.<br>
* <p/>
* For convenience you are encouraged to static-import this specific method:
* <code>import static org.lwjgl.util.mapped.MappedObject.foreach;</code>
*/
public static <T extends MappedObject> MappedForeach<T> foreach(T mapped, int elementCount) {
return new MappedForeach<T>(mapped, elementCount);
}
public static <T extends MappedObject> MappedForeach<T> foreach(T mapped, int elementCount)
{
return new MappedForeach<T>(mapped, elementCount);
}
/**
* Configures a newly initiated mapped object with the specified stride and offset.
*
* @throws IllegalStateException if view is not at index 0
*/
public static <T extends MappedObject> T configure(T mapped, int stride, int offset) {
if ( mapped.baseAddress != mapped.viewAddress )
throw new IllegalStateException("view must be zero");
/**
* Configures a newly initiated mapped object with the specified stride and offset.
* @throws IllegalStateException if view is not at index 0
*/
if ( offset < 0 )
throw new IllegalStateException("offset must not be negative: " + offset);
if ( offset % mapped.align != 0 )
throw new IllegalStateException("offset not a multiple of alignment: " + offset);
public static final <T extends MappedObject> T configure(T mapped, int stride, int offset)
{
if (mapped.baseAddress != mapped.viewAddress)
throw new IllegalStateException("view must be zero");
if ( stride < mapped.stride )
throw new IllegalStateException("new stride must not be smaller than current stride: " + stride);
if ( stride % mapped.align != 0 )
throw new IllegalStateException("stride not a multiple of alignment: " + stride);
if (offset < 0)
throw new IllegalStateException("offset must not be negative: " + offset);
if (offset % mapped.align != 0)
throw new IllegalStateException("offset not a multiple of alignment: " + offset);
mapped.baseAddress += offset;
mapped.viewAddress += offset;
mapped.stride = stride;
if (stride < mapped.stride)
throw new IllegalStateException("new stride must not be smaller than current stride: " + stride);
if (stride % mapped.align != 0)
throw new IllegalStateException("stride not a multiple of alignment: " + stride);
return mapped;
}
mapped.baseAddress += offset;
mapped.viewAddress += offset;
mapped.stride = stride;
ByteBuffer preventGC;
return mapped;
}
/**
* Returns the {@link ByteBuffer} that backs this mapped object.
*
* @return the backing buffer
*/
public ByteBuffer backingByteBuffer() {
return this.preventGC;
}
ByteBuffer preventGC;
public ByteBuffer backingByteBuffer()
{
return this.preventGC;
}
}
}

View File

@ -1,9 +1,38 @@
/*
* Created on Jun 24, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import org.lwjgl.LWJGLUtil;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
@ -12,125 +41,117 @@ import java.net.URLClassLoader;
import java.util.Arrays;
/**
* This classloader is responsible for applying the bytecode transformation to mapped objects.
* The transformation can either be applied using a Java agent, or with the convenient {@link #fork} method.
*
* @author Riven
*/
public class MappedObjectClassLoader extends URLClassLoader {
public class MappedObjectClassLoader extends URLClassLoader
{
static final String MAPPEDOBJECT_PACKAGE_PREFIX;
static final String MAPPEDOBJECT_PACKAGE_PREFIX;
static
{
MAPPEDOBJECT_PACKAGE_PREFIX = MappedObjectClassLoader.class.getPackage().getName() + ".";
}
static {
MAPPEDOBJECT_PACKAGE_PREFIX = MappedObjectClassLoader.class.getPackage().getName() + ".";
}
static boolean FORKED = false;
static boolean FORKED;
public static boolean fork(Class< ? > mainClass, String[] args)
{
if (FORKED)
{
return false;
}
/**
* Forks the specified class containing a main method, passing the specified arguments. See
* {@link org.lwjgl.test.mapped.TestMappedObject} for example usage.
*
* @param mainClass the class containing the main method
* @param args the arguments to pass
*
* @return true if the fork was successful.
*/
public static boolean fork(Class<?> mainClass, String[] args) {
if ( FORKED ) {
return false;
}
FORKED = true;
FORKED = true;
try
{
URLClassLoader loader = new MappedObjectClassLoader(mainClass);
Class< ? > replacedMainClass = loader.loadClass(mainClass.getName());
Method mainMethod = replacedMainClass.getMethod("main", String[].class);
mainMethod.invoke(null, new Object[] { args });
}
catch (InvocationTargetException exc)
{
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), exc.getCause());
}
catch (Throwable cause)
{
throw new Error("failed to fork", cause);
}
try {
URLClassLoader loader = new MappedObjectClassLoader(mainClass);
Class<?> replacedMainClass = loader.loadClass(mainClass.getName());
Method mainMethod = replacedMainClass.getMethod("main", String[].class);
mainMethod.invoke(null, new Object[] { args });
} catch (InvocationTargetException exc) {
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), exc.getCause());
} catch (Throwable cause) {
throw new Error("failed to fork", cause);
}
return true;
}
return true;
}
private MappedObjectClassLoader(Class< ? > mainClass)
{
super(((URLClassLoader) mainClass.getClassLoader()).getURLs());
}
private MappedObjectClassLoader(Class<?> mainClass) {
super(((URLClassLoader)mainClass.getClassLoader()).getURLs());
}
private static long total_time_transforming;
private static long total_time_transforming;
@Override
protected synchronized Class< ? > loadClass(String name, boolean resolve) throws ClassNotFoundException
{
if (name.startsWith("java."))
return super.loadClass(name, resolve);
if (name.startsWith("javax."))
return super.loadClass(name, resolve);
@Override
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if ( name.startsWith("java.") )
return super.loadClass(name, resolve);
if ( name.startsWith("javax.") )
return super.loadClass(name, resolve);
if (name.startsWith("sun."))
return super.loadClass(name, resolve);
if (name.startsWith("sunw."))
return super.loadClass(name, resolve);
if ( name.startsWith("sun.") )
return super.loadClass(name, resolve);
if ( name.startsWith("sunw.") )
return super.loadClass(name, resolve);
// never transform classes in this very package, sub-packages should be transformed
if (name.startsWith(MAPPEDOBJECT_PACKAGE_PREFIX))
if (name.substring(MAPPEDOBJECT_PACKAGE_PREFIX.length()).indexOf('.') == -1)
return super.loadClass(name, resolve);
// never transform classes in this very package, sub-packages should be transformed
if ( name.startsWith(MAPPEDOBJECT_PACKAGE_PREFIX) )
if ( name.substring(MAPPEDOBJECT_PACKAGE_PREFIX.length()).indexOf('.') == -1 )
return super.loadClass(name, resolve);
String className = name.replace('.', '/');
String className = name.replace('.', '/');
if (MappedObjectTransformer.PRINT_ACTIVITY)
System.out.println(MappedObjectClassLoader.class.getSimpleName() + ": " + className);
if ( MappedObjectTransformer.PRINT_ACTIVITY )
LWJGLUtil.log(MappedObjectClassLoader.class.getSimpleName() + ": " + className);
byte[] bytecode = readStream(this.getResourceAsStream(className.concat(".class")));
byte[] bytecode = readStream(this.getResourceAsStream(className.concat(".class")));
long t0 = System.nanoTime();
bytecode = MappedObjectTransformer.transformFieldAccess(className, bytecode);
long t1 = System.nanoTime();
total_time_transforming += (t1 - t0);
long t0 = System.nanoTime();
bytecode = MappedObjectTransformer.transformFieldAccess(className, bytecode);
long t1 = System.nanoTime();
total_time_transforming += (t1 - t0);
if (MappedObjectTransformer.PRINT_TIMING)
System.out.println("transforming " + className + " took " + (t1 - t0) / 1000 + " micros (total: " + (total_time_transforming / 1000 / 1000) + "ms)");
if ( MappedObjectTransformer.PRINT_TIMING )
LWJGLUtil.log("transforming " + className + " took " + (t1 - t0) / 1000 + " micros (total: " + (total_time_transforming / 1000 / 1000) + "ms)");
Class< ? > clazz = super.defineClass(name, bytecode, 0, bytecode.length);
if (resolve)
resolveClass(clazz);
return clazz;
}
Class<?> clazz = super.defineClass(name, bytecode, 0, bytecode.length);
if ( resolve )
resolveClass(clazz);
return clazz;
}
static byte[] readStream(InputStream in)
{
byte[] bytecode = new byte[256];
int len = 0;
try
{
while (true)
{
if (bytecode.length == len)
bytecode = Arrays.copyOf(bytecode, len * 2);
int got = in.read(bytecode, len, bytecode.length - len);
if (got == -1)
break;
len += got;
}
}
catch (IOException exc)
{
// stop!
}
finally
{
try
{
in.close();
}
catch (IOException exc)
{
// ignore...
}
}
return Arrays.copyOf(bytecode, len);
}
}
private static byte[] readStream(InputStream in) {
byte[] bytecode = new byte[256];
int len = 0;
try {
while ( true ) {
if ( bytecode.length == len )
bytecode = Arrays.copyOf(bytecode, len * 2);
int got = in.read(bytecode, len, bytecode.length - len);
if ( got == -1 )
break;
len += got;
}
} catch (IOException exc) {
// stop!
} finally {
try {
in.close();
} catch (IOException exc) {
// ignore...
}
}
return Arrays.copyOf(bytecode, len);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,34 @@
/*
* Created on Jun 24, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import java.lang.reflect.Field;
@ -11,73 +38,57 @@ import java.nio.ByteBuffer;
import sun.misc.Unsafe;
/**
* [INTERNAL USE ONLY]
*
* @author Riven
*/
public class MappedObjectUnsafe {
public class MappedObjectUnsafe
{
public static final Unsafe INSTANCE = getUnsafeInstance();
public static final int ADDRESS_SIZE = INSTANCE.addressSize();
private static final Object[] ARRAY = new Object[1];
private static final long ARRAY_ELEMENT_OFFSET = INSTANCE.arrayBaseOffset(ARRAY.getClass());
public static final Unsafe INSTANCE = getUnsafeInstance();
private static final long BUFFER_ADDRESS_OFFSET = getObjectFieldOffset(ByteBuffer.class, "address");
private static final long BUFFER_CAPACITY_OFFSET = getObjectFieldOffset(ByteBuffer.class, "capacity");
private static final long BUFFER_ADDRESS_OFFSET = getObjectFieldOffset(ByteBuffer.class, "address");
private static final long BUFFER_CAPACITY_OFFSET = getObjectFieldOffset(ByteBuffer.class, "capacity");
//
public static long getBufferBaseAddress(Buffer buffer) {
return INSTANCE.getLong(buffer, BUFFER_ADDRESS_OFFSET);
}
public static long getBufferBaseAddress(Buffer buffer)
{
return INSTANCE.getLong(buffer, BUFFER_ADDRESS_OFFSET);
}
private static final ByteBuffer global = ByteBuffer.allocateDirect(4 * 1024);
private static final ByteBuffer global = ByteBuffer.allocateDirect(4 * 1024);
static ByteBuffer newBuffer(long address, int capacity) {
if ( address <= 0L || capacity < 0 )
throw new IllegalStateException("you almost crashed the jvm");
public static ByteBuffer newBuffer(long address, int capacity)
{
if (address <= 0L || capacity < 0)
throw new IllegalStateException("you almost crashed the jvm");
ByteBuffer buffer = global.duplicate();
INSTANCE.putLong(buffer, BUFFER_ADDRESS_OFFSET, address);
INSTANCE.putInt(buffer, BUFFER_CAPACITY_OFFSET, capacity);
buffer.position(0);
buffer.limit(capacity);
return buffer;
}
ByteBuffer buffer = global.duplicate();
INSTANCE.putLong(buffer, BUFFER_ADDRESS_OFFSET, address);
INSTANCE.putInt(buffer, BUFFER_CAPACITY_OFFSET, capacity);
buffer.position(0);
buffer.limit(capacity);
return buffer;
}
private static long getObjectFieldOffset(Class<?> type, String fieldName) {
while ( type != null ) {
try {
return INSTANCE.objectFieldOffset(type.getDeclaredField(fieldName));
} catch (Throwable t) {
type = type.getSuperclass();
}
}
throw new InternalError();
}
private static Unsafe getUnsafeInstance() {
try {
ByteBuffer buffer = ByteBuffer.allocateDirect(1);
Field unsafeField = buffer.getClass().getDeclaredField("unsafe");
unsafeField.setAccessible(true);
Unsafe instance = (Unsafe)unsafeField.get(buffer);
buffer.flip(); // prevented 'buffer' from being gc'ed
return instance;
} catch (Exception exc) {
throw new InternalError();
}
}
private static long getObjectFieldOffset(Class< ? > type, String fieldName)
{
while (type != null)
{
try
{
return INSTANCE.objectFieldOffset(type.getDeclaredField(fieldName));
}
catch (Throwable t)
{
type = type.getSuperclass();
}
}
throw new InternalError();
}
private static Unsafe getUnsafeInstance()
{
try
{
ByteBuffer buffer = ByteBuffer.allocateDirect(1);
Field unsafeField = buffer.getClass().getDeclaredField("unsafe");
unsafeField.setAccessible(true);
Unsafe instance = (Unsafe) unsafeField.get(buffer);
buffer.flip(); // prevented 'buffer' from being gc'ed
return instance;
}
catch (Exception exc)
{
throw new InternalError();
}
}
}
}

View File

@ -1,23 +1,69 @@
/*
* Created on Jul 11, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
public class MappedSet
{
public static MappedSet2 create(MappedObject a, MappedObject b)
{
return new MappedSet2(a, b);
}
/**
* Factory for mapped sets. A mapped set can be used as a Structure of Arrays by
* linking together the view of two or more mapped objects. Changing the view
* of the mapped set, changes the corresponding view of all the mapped objects in
* the set.
*/
public class MappedSet {
public static MappedSet3 create(MappedObject a, MappedObject b, MappedObject c)
{
return new MappedSet3(a, b, c);
}
/**
* Creates a <code>MappedSet</code> by linking the specified <code>MappedObject</code>s.
*
* @return the mapped set.
*/
public static MappedSet2 create(MappedObject a, MappedObject b) {
return new MappedSet2(a, b);
}
public static MappedSet4 create(MappedObject a, MappedObject b, MappedObject c, MappedObject d)
{
return new MappedSet4(a, b, c, d);
}
}
/**
* Creates a <code>MappedSet</code> by linking the specified <code>MappedObject</code>s.
*
* @return the mapped set.
*/
public static MappedSet3 create(MappedObject a, MappedObject b, MappedObject c) {
return new MappedSet3(a, b, c);
}
/**
* Creates a <code>MappedSet</code> by linking the specified <code>MappedObject</code>s.
*
* @return the mapped set.
*/
public static MappedSet4 create(MappedObject a, MappedObject b, MappedObject c, MappedObject d) {
return new MappedSet4(a, b, c, d);
}
}

View File

@ -1,30 +1,56 @@
/*
* Created on Jul 11, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
public class MappedSet2
{
private final MappedObject a, b;
/** <code>MappedSet</code> implementation for two <code>MappedObject</code>s. */
public class MappedSet2 {
MappedSet2(MappedObject a, MappedObject b)
{
this.a = a;
this.b = b;
}
private final MappedObject a, b;
public int view;
MappedSet2(MappedObject a, MappedObject b) {
this.a = a;
this.b = b;
}
void view(int view)
{
this.a.viewAddress = this.a.baseAddress + this.a.stride * view;
this.b.viewAddress = this.b.baseAddress + this.b.stride * view;
}
public int view;
public void next()
{
this.a.viewAddress += this.a.stride;
this.b.viewAddress += this.b.stride;
}
}
void view(int view) {
MappedHelper.put_view(this.a, view);
MappedHelper.put_view(this.b, view);
}
public void next() {
this.a.next();
this.b.next();
}
}

View File

@ -1,33 +1,59 @@
/*
* Created on Jul 11, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
public class MappedSet3
{
private final MappedObject a, b, c;
/** <code>MappedSet</code> implementation for three <code>MappedObject</code>s. */
public class MappedSet3 {
MappedSet3(MappedObject a, MappedObject b, MappedObject c)
{
this.a = a;
this.b = b;
this.c = c;
}
private final MappedObject a, b, c;
public int view;
MappedSet3(MappedObject a, MappedObject b, MappedObject c) {
this.a = a;
this.b = b;
this.c = c;
}
void view(int view)
{
this.a.viewAddress = this.a.baseAddress + this.a.stride * view;
this.b.viewAddress = this.b.baseAddress + this.b.stride * view;
this.c.viewAddress = this.c.baseAddress + this.c.stride * view;
}
public int view;
public void next()
{
this.a.viewAddress += this.a.stride;
this.b.viewAddress += this.b.stride;
this.c.viewAddress += this.c.stride;
}
}
void view(int view) {
MappedHelper.put_view(this.a, view);
MappedHelper.put_view(this.b, view);
MappedHelper.put_view(this.c, view);
}
public void next() {
this.a.next();
this.b.next();
this.c.next();
}
}

View File

@ -1,36 +1,62 @@
/*
* Created on Jul 11, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
public class MappedSet4
{
private final MappedObject a, b, c, d;
/** <code>MappedSet</code> implementation for four <code>MappedObject</code>s. */
public class MappedSet4 {
MappedSet4(MappedObject a, MappedObject b, MappedObject c, MappedObject d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
private final MappedObject a, b, c, d;
public int view;
MappedSet4(MappedObject a, MappedObject b, MappedObject c, MappedObject d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
void view(int view)
{
this.a.viewAddress = this.a.baseAddress + this.a.stride * view;
this.b.viewAddress = this.b.baseAddress + this.b.stride * view;
this.c.viewAddress = this.c.baseAddress + this.c.stride * view;
this.d.viewAddress = this.d.baseAddress + this.d.stride * view;
}
public int view;
public void next()
{
this.a.viewAddress += this.a.stride;
this.b.viewAddress += this.b.stride;
this.c.viewAddress += this.c.stride;
this.d.viewAddress += this.d.stride;
}
}
void view(int view) {
MappedHelper.put_view(this.a, view);
MappedHelper.put_view(this.b, view);
MappedHelper.put_view(this.c, view);
MappedHelper.put_view(this.d, view);
}
public void next() {
this.a.next();
this.b.next();
this.c.next();
this.d.next();
}
}

View File

@ -1,21 +1,86 @@
/*
* Created on Jun 24, 2011
* Copyright (c) 2002-2011 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.mapped;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation marks a class as a mapped object, which will go under bytecode
* transformation at runtime. Mapped objects cannot be instantiated directly; a data
* buffer must be mapped first and the mapped object instance will then be used as a
* view on top of the buffer. Instead of a separate instance per "element" in the buffer,
* only a single instance is used to manage everything. See {@link MappedObject}
* for API details and {@link org.lwjgl.test.mapped.TestMappedObject} for examples.
* <p/>
* The instance fields of the annotated class should only be limited to primitive types or
* {@link java.nio.ByteBuffer}. Static fields are supported and they can have any type.
* <p/>
* The purpose of mapped objects is to reduce the memory requirements required for the type
* of data that are often used in OpenGL/OpenCL programming, while at the same time enabling
* clean Java code. There are also performance benefits related to not having to copy data
* between buffers and Java objects and the removal of bounds checking when accessing
* buffer data.
*
* @author Riven
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MappedType {
int sizeof();
int align() default 4;
/**
* The total size of the mapped object, in bytes.
*
* @return the byte size
*/
int sizeof();
boolean autoGenerateOffsets() default true;
}
/**
* The mapped data memory alignment, in bytes.
*
* @return the memory alignment
*/
int align() default 4;
/**
* When autoGenerateOffsets is true, byte offsets of the mapped fields will
* be generated automatically. This is convenient for packed data. For manually
* aligned data, autoGenerateOffsets must be set to false and the user needs
* to manually specify byte offsets using the {@link MappedField} annotation.
*
* @return true if automatic byte offset generation is required.
*/
boolean autoGenerateOffsets() default true;
}