FreeListManager { boolean validateAddressAndSizeWithinSlab(long addr, int size) { for (int i = 0; i < slabs.length; i++) { if (slabs[i].getMemoryAddress() <= addr && addr < (slabs[i].getMemoryAddress() + slabs[i].getSize())) { if (size != -1) { if (!(slabs[i].getMemoryAddress() <= (addr + size - 1) && (addr + size - 1) < (slabs[i].getMemoryAddress() + slabs[i].getSize()))) { throw new IllegalStateException(" address 0x" + Long.toString(addr + size - 1, 16) + " does not address the original slab memory"); } } return true; } } return false; } FreeListManager(MemoryAllocatorImpl ma, final Slab[] slabs); long getUsedMemory(); long getFreeMemory(); @SuppressWarnings("synthetic-access") OffHeapStoredObject allocate(int size); @SuppressWarnings("synthetic-access") void free(long addr); static final int TINY_MULTIPLE; static final int TINY_FREE_LIST_COUNT; static final int HUGE_MULTIPLE; static final int MAX_TINY; }
@Test public void validateAddressWithinSlab() { Slab chunk = new SlabImpl(10); long address = chunk.getMemoryAddress(); this.freeListManager = createFreeListManager(ma, new Slab[] {chunk}); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address, -1)).isTrue(); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address + 9, -1)).isTrue(); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address - 1, -1)).isFalse(); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address + 10, -1)).isFalse(); }
@Test public void validateAddressAndSizeWithinSlab() { Slab chunk = new SlabImpl(10); long address = chunk.getMemoryAddress(); this.freeListManager = createFreeListManager(ma, new Slab[] {chunk}); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address, 1)).isTrue(); assertThat(this.freeListManager.validateAddressAndSizeWithinSlab(address, 10)).isTrue(); Throwable thrown = catchThrowable(() -> this.freeListManager.validateAddressAndSizeWithinSlab(address, 0)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage(" address 0x" + Long.toString(address + 0 - 1, 16) + " does not address the original slab memory"); thrown = catchThrowable(() -> this.freeListManager.validateAddressAndSizeWithinSlab(address, 11)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage(" address 0x" + Long.toString(address + 11 - 1, 16) + " does not address the original slab memory"); }