@Test @DisplayName("get(long, int, byte[], int) && put(long, int, byte[], int)") void get_put() { val size = (int) createSize(0xFF); val buffer = new byte[size]; val address = assumeAllocate(size); val softly = new SoftAssertions(); mmanager.get(address, buffer.length, buffer, 0); for (var i = 0; i < buffer.length; i += 1) buffer[i] += 1; mmanager.put(address, buffer.length, buffer, 0); val copy = buffer.clone(); mmanager.get(address, buffer.length, buffer, 0); softly.assertThat(buffer).isEqualTo(copy); for (var i = 0; i < buffer.length; i += 1) buffer[i] = 0; mmanager.get(address, 0, buffer, 0); softly.assertThat(buffer).isEqualTo(new byte[buffer.length]); if (!OPTIMIZED) { mmanager.get(address, 2, buffer, buffer.length - 1); val one = new byte[buffer.length]; one[one.length - 1] = copy[0]; softly.assertThat(buffer).isEqualTo(one); } mmanager.put(address, 0, new byte[buffer.length], 0); mmanager.get(address, buffer.length, buffer, 0); softly.assertThat(buffer).isEqualTo(copy); if (!OPTIMIZED) { mmanager.put(address, 2, new byte[buffer.length], buffer.length - 1); mmanager.get(address, buffer.length, buffer, 0); buffer[0] = copy[0]; softly.assertThat(buffer).isEqualTo(copy); } mmanager.free(address, size, false, false); softly.assertAll(); }
@Override @SuppressWarnings("BooleanParameter") public void free(long address, long bytes, final boolean huge, final boolean lock) { if (!OPTIMIZED) { if (unsafe == null) throw new NullPointerException("The Unsafe object is not available."); if (address == 0) throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); if (bytes <= 0) throw new IllegalArgumentException("The parameter 'bytes' MUST be positive."); } if (huge) { if (hugepageSize <= 0) { if (DEBUG >= LOG_TRACE) log.trace("Freeing {} hugepage-based bytes @ 0x{}.", bytes, leftPad(address)); return; } if (DEBUG >= LOG_TRACE) { val xaddressCopy = leftPad(address); val bytesCopy = bytes; val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; log.trace("Freeing {} hugepage-based bytes @ 0x{} (originally {} bytes @ 0x{}).", bytes, leftPad(address), bytesCopy, xaddressCopy); } else { val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; } } else { unsafe.freeMemory(address); return; } c_free(address, bytes, huge, lock); }
SmartUnsafeMemoryManager extends UnsafeMemoryManager { @Override @SuppressWarnings("BooleanParameter") public void free(long address, long bytes, final boolean huge, final boolean lock) { if (!OPTIMIZED) { if (unsafe == null) throw new NullPointerException("The Unsafe object is not available."); if (address == 0) throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); if (bytes <= 0) throw new IllegalArgumentException("The parameter 'bytes' MUST be positive."); } if (huge) { if (hugepageSize <= 0) { if (DEBUG >= LOG_TRACE) log.trace("Freeing {} hugepage-based bytes @ 0x{}.", bytes, leftPad(address)); return; } if (DEBUG >= LOG_TRACE) { val xaddressCopy = leftPad(address); val bytesCopy = bytes; val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; log.trace("Freeing {} hugepage-based bytes @ 0x{} (originally {} bytes @ 0x{}).", bytes, leftPad(address), bytesCopy, xaddressCopy); } else { val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; } } else { unsafe.freeMemory(address); return; } c_free(address, bytes, huge, lock); } }
SmartUnsafeMemoryManager extends UnsafeMemoryManager { @Override @SuppressWarnings("BooleanParameter") public void free(long address, long bytes, final boolean huge, final boolean lock) { if (!OPTIMIZED) { if (unsafe == null) throw new NullPointerException("The Unsafe object is not available."); if (address == 0) throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); if (bytes <= 0) throw new IllegalArgumentException("The parameter 'bytes' MUST be positive."); } if (huge) { if (hugepageSize <= 0) { if (DEBUG >= LOG_TRACE) log.trace("Freeing {} hugepage-based bytes @ 0x{}.", bytes, leftPad(address)); return; } if (DEBUG >= LOG_TRACE) { val xaddressCopy = leftPad(address); val bytesCopy = bytes; val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; log.trace("Freeing {} hugepage-based bytes @ 0x{} (originally {} bytes @ 0x{}).", bytes, leftPad(address), bytesCopy, xaddressCopy); } else { val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; } } else { unsafe.freeMemory(address); return; } c_free(address, bytes, huge, lock); } private  SmartUnsafeMemoryManager(); }
SmartUnsafeMemoryManager extends UnsafeMemoryManager { @Override @SuppressWarnings("BooleanParameter") public void free(long address, long bytes, final boolean huge, final boolean lock) { if (!OPTIMIZED) { if (unsafe == null) throw new NullPointerException("The Unsafe object is not available."); if (address == 0) throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); if (bytes <= 0) throw new IllegalArgumentException("The parameter 'bytes' MUST be positive."); } if (huge) { if (hugepageSize <= 0) { if (DEBUG >= LOG_TRACE) log.trace("Freeing {} hugepage-based bytes @ 0x{}.", bytes, leftPad(address)); return; } if (DEBUG >= LOG_TRACE) { val xaddressCopy = leftPad(address); val bytesCopy = bytes; val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; log.trace("Freeing {} hugepage-based bytes @ 0x{} (originally {} bytes @ 0x{}).", bytes, leftPad(address), bytesCopy, xaddressCopy); } else { val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; } } else { unsafe.freeMemory(address); return; } c_free(address, bytes, huge, lock); } private  SmartUnsafeMemoryManager(); @Override @Contract(pure = true) boolean isValid(); @Override @Contract(pure = true) long getPageSize(); @Override @Contract(value = " -> fail", pure = true) @SuppressWarnings({"deprecation", "PMD.DataflowAnomalyAnalysis"}) long getHugepageSize(); @Override @Contract(pure = true) @SuppressWarnings("BooleanParameter") long allocate(long bytes, boolean huge, boolean lock); @Override @SuppressWarnings("BooleanParameter") void free(long address, long bytes, final boolean huge, final boolean lock); @Override @Contract(pure = true) @SuppressWarnings({"deprecation", "BooleanParameter", "IOResourceOpenedButNotSafelyClosed", "resource"}) long mmap(final @NotNull File file, final boolean huge, final boolean lock); @Override @SuppressWarnings({"deprecation", "BooleanParameter", "PMD.DataflowAnomalyAnalysis"}) void munmap(final long address, final @NotNull File file, final boolean huge, final boolean lock); @Override @Contract(pure = true) @SuppressWarnings("deprecation") @SuppressFBWarnings("") long virt2phys(final long address); @Override @Contract(value = "_, _, _ -> fail", pure = true) @SuppressWarnings({"deprecation", "BooleanParameter"}) @NotNull DmaMemory dmaAllocate(final long bytes, final boolean huge, final boolean lock); }
SmartUnsafeMemoryManager extends UnsafeMemoryManager { @Override @SuppressWarnings("BooleanParameter") public void free(long address, long bytes, final boolean huge, final boolean lock) { if (!OPTIMIZED) { if (unsafe == null) throw new NullPointerException("The Unsafe object is not available."); if (address == 0) throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); if (bytes <= 0) throw new IllegalArgumentException("The parameter 'bytes' MUST be positive."); } if (huge) { if (hugepageSize <= 0) { if (DEBUG >= LOG_TRACE) log.trace("Freeing {} hugepage-based bytes @ 0x{}.", bytes, leftPad(address)); return; } if (DEBUG >= LOG_TRACE) { val xaddressCopy = leftPad(address); val bytesCopy = bytes; val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; log.trace("Freeing {} hugepage-based bytes @ 0x{} (originally {} bytes @ 0x{}).", bytes, leftPad(address), bytesCopy, xaddressCopy); } else { val mask = (hugepageSize - 1); address = (address & mask) == 0 ? address : address & ~mask; bytes = (bytes & mask) == 0 ? bytes : (bytes + hugepageSize) & ~mask; } } else { unsafe.freeMemory(address); return; } c_free(address, bytes, huge, lock); } private  SmartUnsafeMemoryManager(); @Override @Contract(pure = true) boolean isValid(); @Override @Contract(pure = true) long getPageSize(); @Override @Contract(value = " -> fail", pure = true) @SuppressWarnings({"deprecation", "PMD.DataflowAnomalyAnalysis"}) long getHugepageSize(); @Override @Contract(pure = true) @SuppressWarnings("BooleanParameter") long allocate(long bytes, boolean huge, boolean lock); @Override @SuppressWarnings("BooleanParameter") void free(long address, long bytes, final boolean huge, final boolean lock); @Override @Contract(pure = true) @SuppressWarnings({"deprecation", "BooleanParameter", "IOResourceOpenedButNotSafelyClosed", "resource"}) long mmap(final @NotNull File file, final boolean huge, final boolean lock); @Override @SuppressWarnings({"deprecation", "BooleanParameter", "PMD.DataflowAnomalyAnalysis"}) void munmap(final long address, final @NotNull File file, final boolean huge, final boolean lock); @Override @Contract(pure = true) @SuppressWarnings("deprecation") @SuppressFBWarnings("") long virt2phys(final long address); @Override @Contract(value = "_, _, _ -> fail", pure = true) @SuppressWarnings({"deprecation", "BooleanParameter"}) @NotNull DmaMemory dmaAllocate(final long bytes, final boolean huge, final boolean lock);  }