@Override public Bytez alloc(long len) { MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len); mallocBytez.clear(); allocated.add(mallocBytez); alloced.getAndAdd(len); return mallocBytez; }
@Override public void freeAll() { for (int i = 0; i < allocated.size(); i++) { MallocBytez mallocBytez = allocated.get(i); mallocBytez.free(); } allocated.clear(); } }
@Override public void getBooleanArr(long byteIndex, boolean[] target, int elemoff, int numElems) { for ( int i = 0; i < numElems; i++) { target[elemoff+i] = getBool(byteIndex+i); } }
/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
public MallocBytez slice(long off, int len) { if (off+len >= length) throw new RuntimeException("invalid slice "+off+":"+len+" mylen:"+length); return new MallocBytez(baseAdress+off,len); }
public MallocBytez(long adr, long len) { setBase(adr, len); }
@Override public void setBoolean(long byteIndex, boolean[] o, int elemoff, int numElems) { for ( int i = 0; i < numElems; i++) { put(byteIndex+i, (byte) (o[i+elemoff] ? 1 : 0)); } }
@Override public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes) { if ( other instanceof HeapBytez) { HeapBytez hp = (HeapBytez) other; unsafe.copyMemory(null,baseAdress+myByteIndex, hp.getBase(), hp.getOff()+otherByteIndex,lenBytes); } else { for ( long i = 0; i < lenBytes; i++ ) { other.put(otherByteIndex+i,get(myByteIndex+i)); } } }
@Override public void putBoolVolatile(long byteIndex, boolean value) { putVolatile(byteIndex, (byte) (value?1:0)); }
@Override public boolean getBoolVolatile(long byteIndex) { return getVolatile(byteIndex) != 0; }
@Override public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes) { if ( lenBytes == 0 ) return; checkIndex(myByteIndex, (int) lenBytes); if ( other instanceof HeapBytez) { HeapBytez hp = (HeapBytez) other; unsafe.copyMemory(base,off+myByteIndex,hp.base,hp.off+otherByteIndex,lenBytes); } else if (other instanceof MallocBytez ) { MallocBytez mb = (MallocBytez) other; unsafe.copyMemory(base,off+myByteIndex,null,otherByteIndex+mb.getBaseAdress(),lenBytes); } else { for ( long i = 0; i < lenBytes; i++ ) { other.put(otherByteIndex+i,get(myByteIndex+i)); } } }
public MallocBytez slice(long off, int len) { if (off+len >= length) throw new RuntimeException("invalid slice "+off+":"+len+" mylen:"+length); return new MallocBytez(baseAdress+off,len); }
public MallocBytez(long adr, long len) { setBase(adr, len); }
@Override public void putBool(long byteIndex, boolean val) { put(byteIndex, (byte) (val ? 1 : 0)); }
@Override public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes) { if ( other instanceof HeapBytez) { HeapBytez hp = (HeapBytez) other; unsafe.copyMemory(null,baseAdress+myByteIndex, hp.getBase(), hp.getOff()+otherByteIndex,lenBytes); } else { for ( long i = 0; i < lenBytes; i++ ) { other.put(otherByteIndex+i,get(myByteIndex+i)); } } }
@Override public void putBoolVolatile(long byteIndex, boolean value) { putVolatile(byteIndex, (byte) (value?1:0)); }
@Override public boolean getBoolVolatile(long byteIndex) { return getVolatile(byteIndex) != 0; }
/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
@Override public Bytez alloc(long len) { MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len); mallocBytez.clear(); allocated.add(mallocBytez); alloced.getAndAdd(len); return mallocBytez; }
@Override public BasicBytez newInstance(long size) { return new MallocBytez(unsafe.allocateMemory(size),size); }