FreeListManager { int findSlab(long addr) { for (int i = 0; i < this.slabs.length; i++) { Slab slab = this.slabs[i]; long slabAddr = slab.getMemoryAddress(); if (addr >= slabAddr) { if (addr < slabAddr + slab.getSize()) { return i; } } } throw new IllegalStateException("could not find a slab for addr " + addr); } 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 findSlab() { Slab chunk = new SlabImpl(10); long address = chunk.getMemoryAddress(); this.freeListManager = createFreeListManager(ma, new Slab[] {chunk}); assertThat(this.freeListManager.findSlab(address)).isEqualTo(0); assertThat(this.freeListManager.findSlab(address + 9)).isEqualTo(0); Throwable thrown = catchThrowable(() -> this.freeListManager.findSlab(address - 1)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage("could not find a slab for addr " + (address - 1)); thrown = catchThrowable(() -> this.freeListManager.findSlab(address + 10)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage("could not find a slab for addr " + (address + 10)); }
@Test public void findSecondSlab() { Slab chunk = new SlabImpl(10); long address = chunk.getMemoryAddress(); Slab slab = new SlabImpl(DEFAULT_SLAB_SIZE); this.freeListManager = createFreeListManager(ma, new Slab[] {slab, chunk}); assertThat(this.freeListManager.findSlab(address)).isEqualTo(1); assertThat(this.freeListManager.findSlab(address + 9)).isEqualTo(1); Throwable thrown = catchThrowable(() -> this.freeListManager.findSlab(address - 1)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage("could not find a slab for addr " + (address - 1)); thrown = catchThrowable(() -> this.freeListManager.findSlab(address + 10)); assertThat(thrown).isExactlyInstanceOf(IllegalStateException.class) .hasMessage("could not find a slab for addr " + (address + 10)); }