FreeListManager { @SuppressWarnings("synthetic-access") public OffHeapStoredObject allocate(int size) { assert size > 0; OffHeapStoredObject result = basicAllocate(size, true); result.setDataSize(size); this.allocatedSize.addAndGet(result.getSize()); result.initializeUseCount(); return result; } 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 allocateTinyChunkHasCorrectSize() { setUpSingleSlabManager(); int tinySize = 10; OffHeapStoredObject c = this.freeListManager.allocate(tinySize); validateChunkSizes(c, tinySize); }
@Test public void allocateTinyChunkFromFreeListHasCorrectSize() { setUpSingleSlabManager(); int tinySize = 10; OffHeapStoredObject c = this.freeListManager.allocate(tinySize); OffHeapStoredObject.release(c.getAddress(), this.freeListManager); c = this.freeListManager.allocate(tinySize); validateChunkSizes(c, tinySize); }
@Test public void allocateTinyChunkFromEmptyFreeListHasCorrectSize() { setUpSingleSlabManager(); int dataSize = 10; OffHeapStoredObject c = this.freeListManager.allocate(dataSize); OffHeapStoredObject.release(c.getAddress(), this.freeListManager); this.freeListManager.allocate(dataSize); c = this.freeListManager.allocate(dataSize); validateChunkSizes(c, dataSize); }
@Test public void allocateHugeChunkHasCorrectSize() { setUpSingleSlabManager(); int hugeSize = FreeListManager.MAX_TINY + 1; OffHeapStoredObject c = this.freeListManager.allocate(hugeSize); validateChunkSizes(c, hugeSize); }
@Test public void allocateHugeChunkFromEmptyFreeListHasCorrectSize() { setUpSingleSlabManager(); int dataSize = FreeListManager.MAX_TINY + 1; OffHeapStoredObject c = this.freeListManager.allocate(dataSize); OffHeapStoredObject.release(c.getAddress(), this.freeListManager); this.freeListManager.allocate(dataSize); c = this.freeListManager.allocate(dataSize); validateChunkSizes(c, dataSize); }
@Test public void allocateHugeChunkFromFragmentWithItemInFreeListHasCorrectSize() { setUpSingleSlabManager(); int dataSize = FreeListManager.MAX_TINY + 1 + 1024; OffHeapStoredObject c = this.freeListManager.allocate(dataSize); OffHeapStoredObject.release(c.getAddress(), this.freeListManager); dataSize = FreeListManager.MAX_TINY + 1; c = this.freeListManager.allocate(dataSize); validateChunkSizes(c, dataSize); }
@Test(expected = AssertionError.class) public void allocateZeroThrowsAssertion() { setUpSingleSlabManager(); this.freeListManager.allocate(0); }
@Test public void overMaxAllocationFails() { setUpSingleSlabManager(); OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class); when(this.ma.getOutOfOffHeapMemoryListener()).thenReturn(ooohml); Throwable thrown = catchThrowable(() -> this.freeListManager.allocate(DEFAULT_SLAB_SIZE - 7)); verify(ooohml).outOfOffHeapMemory((OutOfOffHeapMemoryException) thrown); }
@Test(expected = AssertionError.class) public void allocateNegativeThrowsAssertion() { setUpSingleSlabManager(); this.freeListManager.allocate(-123); }