Added alignment checks to PointerBuffer constructor.

This commit is contained in:
Ioannis Tsakpinis 2011-05-11 00:12:52 +00:00
parent 0f274256b7
commit 3fb5f8ab8b
3 changed files with 20 additions and 2 deletions

View File

@ -192,4 +192,14 @@ public final class BufferUtils {
/** Fill buffer with zeros from position to remaining */
private static native void zeroBuffer0(Buffer b, long off, long size);
/**
* Returns the memory address of the specified buffer.
*
* @param buffer the buffer
*
* @return the memory address
*/
static native long getBufferAddress(Buffer buffer);
}

View File

@ -59,8 +59,8 @@ public class PointerBuffer implements Comparable {
protected final ByteBuffer pointers;
protected final Buffer view;
protected final IntBuffer view32;
protected final Buffer view;
protected final IntBuffer view32;
protected final LongBuffer view64;
/**
@ -83,6 +83,10 @@ public class PointerBuffer implements Comparable {
if ( !source.isDirect() )
throw new IllegalArgumentException("ByteBuffer is not direct");
final int alignment = is64Bit ? 8 : 4;
if ( (BufferUtils.getBufferAddress(source) + source.position()) % alignment != 0 || source.remaining() % alignment != 0 )
throw new IllegalArgumentException("The source buffer is not aligned to " + alignment + " bytes.");
pointers = source.slice().order(source.order());
if ( is64Bit ) {

View File

@ -2,4 +2,8 @@
JNIEXPORT void JNICALL Java_org_lwjgl_BufferUtils_zeroBuffer0(JNIEnv *env, jclass clazz, jobject buffer, jlong offset, jlong size) {
memset((char*)(*env)->GetDirectBufferAddress(env, buffer) + (size_t)offset, 0, (size_t)size);
}
JNIEXPORT jlong JNICALL Java_org_lwjgl_BufferUtils_getBufferAddress(JNIEnv *env, jclass clazz, jobject buffer) {
return (uintptr_t)(*env)->GetDirectBufferAddress(env, buffer);
}