public static @Nullable byte[] getMetadata(File file) throws IOException { final long length = file.length(); final MMapBuffer buffer = new MMapBuffer(file, 0, length, FileChannel.MapMode.READ_ONLY, ByteOrder.BIG_ENDIAN); final DirectMemory memory = buffer.memory(); final int metadataLength = memory.getInt(length - 12); if (metadataLength == Integer.MAX_VALUE) return null; final byte[] metadata = new byte[metadataLength]; memory.getBytes(length-12-metadataLength, metadata); return metadata; }
@Override public Void apply(final MMapBuffer b) { //noinspection deprecation madviseDontNeed(b.memory.getAddress(), b.memory.length()); return null; } });
public void sync(long position, long length) throws IOException { final long ap = address+position; final long a = (ap)/PAGE_SIZE*PAGE_SIZE; final long l = Math.min(length+(ap-a), address+memory.length()-ap); final int err = msync(a, l); if (err != 0) { throw new IOException("msync failed with error code: "+err); } }
public NativeBuffer realloc(long newSize) { if (mmapped && newSize >= MMAP_THRESHOLD) { if (OS_TYPE_IS_MAC) { // MAC does not support mremap return createNewAndClose(newSize); } else { final long newAddress = MMapBuffer.mremap(address, memory.length(), newSize); if (newAddress == MMapBuffer.MAP_FAILED) { throw new RuntimeException("anonymous mremap failed with error " + MMapBuffer.errno()); } return new NativeBuffer(newAddress, new DirectMemory(newAddress, newSize, memory.getOrder()), true); } } else if (!mmapped && newSize < MMAP_THRESHOLD) { return realloc0(newSize); } else { return createNewAndClose(newSize); } }
public static <T> MMapGOV4Function<T> readFrom(final String path) throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException { final GOV4Function<T> gov4Function; try (final InputStream file = new FileInputStream(Files.buildPath(path, "GOV4Function.bin")); final ObjectInput input = new ObjectInputStream(file)) { gov4Function = (GOV4Function<T>)(input.readObject()); } final Field widthField = gov4Function.getClass().getDeclaredField("width"); widthField.setAccessible(true); final int width = (Integer) widthField.get(gov4Function); final MMapBuffer buffer = new MMapBuffer(new File(Files.buildPath(path, "signatures.bin")), FileChannel.MapMode.READ_ONLY, ByteOrder.LITTLE_ENDIAN); final MMapLongBigList signaturesData = new MMapLongBigList(buffer.memory().longArray(0, buffer.memory().length() / 8), width); final Field dataField = gov4Function.getClass().getDeclaredField("data"); dataField.setAccessible(true); dataField.set(gov4Function, signaturesData); return new MMapGOV4Function<>(gov4Function, buffer, width); }
@Override public void getBytes(final long l, final byte[] bytes) { getBytes(l, bytes, 0, bytes.length); }
@Override public void getBytes(long l, ByteBuffer dest) { final int length = dest.remaining(); checkBounds(l, length); if (dest.isDirect()) { NativeMemoryUtils.copyToDirectBuffer(address+l, dest, dest.position(), length); dest.position(dest.position()+length); } else if (dest.hasArray()) { final byte[] array = dest.array(); final int offset = dest.arrayOffset(); getBytes(l, array, offset+dest.position(), length); dest.position(dest.position()+length); } else { final byte[] copyBuffer = new byte[Math.min(length, 4096)]; long sourceAddr = l; while (dest.remaining() > 0) { final int copySize = Math.min(copyBuffer.length, dest.remaining()); getBytes(sourceAddr, copyBuffer, 0, copySize); dest.put(copyBuffer, 0, copySize); sourceAddr+=copySize; } } }
static void mincore(long addr, long length, DirectMemory memory) { int err = mincore(addr, length, memory.getAddress()); if (err != 0) { switch (err) { case ENOMEM: throw new IllegalArgumentException("illegal arguments: address: "+addr+" length: "+length+" vec: "+memory.getAddress()); case EAGAIN: mincore(addr, length, memory); return; case EFAULT: throw new IllegalArgumentException("memory at "+memory.getAddress()+" is not valid"); case EINVAL: throw new IllegalArgumentException("address "+addr+" is not a multiple of the page size"); default: throw new IllegalArgumentException("unknown error"); } } }
/** * st_dev: 8 * st_ino: 8 * st_mode: 4 * st_nlink: 8 * st_uid: 4 * st_gid: 4 * st_rdev: 8 * st_size: 8 * st_blksize: 8 * st_blocks: 8 * st_atime: 8 * st_mtime: 8 * st_ctime: 8 */ private Stat(DirectMemory direct) { device = direct.getLong(0); inode = direct.getLong(8); mode = direct.getInt(16); numLinks = direct.getLong(20); uid = direct.getInt(28); gid = direct.getInt(32); rdev = direct.getLong(36); size = direct.getLong(44); blockSize = direct.getLong(52); numBlocks = direct.getLong(60); aTime = direct.getLong(68); mTime = direct.getLong(76); cTime = direct.getLong(84); }