private static IntBuffer getMemory(final int START_SIZE) { final int PAGE_SIZE = MappedObjectUnsafe.INSTANCE.pageSize(); final ByteBuffer buffer = ByteBuffer.allocateDirect((START_SIZE * 4) + PAGE_SIZE).order(ByteOrder.nativeOrder()); // Align to page and, consequently, to cache line. Otherwise results will be inconsistent. if ( MemoryUtil.getAddress(buffer) % PAGE_SIZE != 0 ) { // Round up to page boundary buffer.position(PAGE_SIZE - (int)(MemoryUtil.getAddress(buffer) & (PAGE_SIZE - 1))); } return buffer.asIntBuffer(); }
final void checkRange(final int bytes) { if ( bytes < 0 ) throw new IllegalArgumentException(); if ( preventGC.capacity() < (viewAddress - MemoryUtil.getAddress0(preventGC) + bytes) ) throw new BufferOverflowException(); }
/** * Construct a direct, native-ordered and cache-line-aligned bytebuffer with the specified size. * * @param size The size, in bytes * * @return a ByteBuffer */ public static ByteBuffer createByteBuffer(int size) { ByteBuffer buffer = ByteBuffer.allocateDirect(size + CACHE_LINE_SIZE); // Align to cache line. if ( MemoryUtil.getAddress(buffer) % CACHE_LINE_SIZE != 0 ) { // Round up to cache line boundary buffer.position(CACHE_LINE_SIZE - (int)(MemoryUtil.getAddress(buffer) & (CACHE_LINE_SIZE - 1))); } buffer.limit(buffer.position() + size); return buffer.slice().order(ByteOrder.nativeOrder()); }
final void checkAddress(final long address) { final long base = MemoryUtil.getAddress0(preventGC); final int offset = (int)(address - base); if ( address < base || preventGC.capacity() < (offset + getSizeof()) ) throw new IndexOutOfBoundsException(Integer.toString(offset / getSizeof())); }
public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof) { if ( LWJGLUtil.CHECKS && mo.baseAddress != 0L ) throw new IllegalStateException("this method should not be called by user-code"); if ( LWJGLUtil.CHECKS && !buffer.isDirect() ) throw new IllegalArgumentException("bytebuffer must be direct"); mo.preventGC = buffer; if ( LWJGLUtil.CHECKS && align <= 0 ) throw new IllegalArgumentException("invalid alignment"); if ( LWJGLUtil.CHECKS && (sizeof <= 0 || sizeof % align != 0) ) throw new IllegalStateException("sizeof not a multiple of alignment"); long addr = MemoryUtil.getAddress(buffer); if ( LWJGLUtil.CHECKS && addr % align != 0 ) throw new IllegalStateException("buffer address not aligned on " + align + " bytes"); mo.baseAddress = mo.viewAddress = addr; }
@Override public void glCompressedTexImage3D(int target, int level, int internalformat, int width, int height, int depth, int border, int imageSize, Buffer data) { GL13.glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, MemoryUtil.getAddress((ByteBuffer) data)); }
@Override public void glCompressedTexImage3D(int target, int level, int internalformat, int width, int height, int depth, int border, int imageSize, Buffer data) { GL13.glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, MemoryUtil.getAddress((ByteBuffer) data)); }
@Override public void glCompressedTexImage3D(int target, int level, int internalformat, int width, int height, int depth, int border, int imageSize, Buffer data) { GL13.glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, MemoryUtil.getAddress((ByteBuffer) data)); }
static long memoryLoop(final int index, final int repeats, final IntBuffer memory, final int padding) { final long address = MemoryUtil.getAddress(memory) + (index * padding * 4); final long time = System.nanoTime(); for ( int i = 0; i < repeats; i++ ) { // Use volatile access to avoid server VM optimizations. ivput(ivget(address) + 1, address); } return System.nanoTime() - time; }