public PageListTest( int pageId ) { this.pageId = pageId; this.prevPageId = pageId == 0 ? pageIds.length - 1 : (pageId - 1) % pageIds.length; this.nextPageId = (pageId + 1) % pageIds.length; pageSize = UnsafeUtil.pageSize(); }
private void clearMemory( long baseAddress, long pageCount ) { long memcpyChunkSize = UnsafeUtil.pageSize(); long metaDataEntriesPerChunk = memcpyChunkSize / META_DATA_BYTES_PER_PAGE; if ( pageCount < metaDataEntriesPerChunk || forceSlowMemoryClear ) { clearMemorySimple( baseAddress, pageCount ); } else { clearMemoryFast( baseAddress, pageCount, memcpyChunkSize, metaDataEntriesPerChunk ); } UnsafeUtil.fullFence(); // Guarantee the visibility of the cleared memory. }
@Test void mustBeAbleToAllocateSlabsLargerThanGrabSize() { MemoryAllocator mman = createAllocator( "2 MiB" ); long page1 = mman.allocateAligned( UnsafeUtil.pageSize(), 1 ); long largeBlock = mman.allocateAligned( 1024 * 1024, 1 ); // 1 MiB long page2 = mman.allocateAligned( UnsafeUtil.pageSize(), 1 ); assertThat( page1, is( not( 0L ) ) ); assertThat( largeBlock, is( not( 0L ) ) ); assertThat( page2, is( not( 0L ) ) ); }
@Test void pageSizeIsPowerOfTwo() { assertThat( pageSize(), isOneOf( 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824 ) ); }
@Test void allocatedPointersMustBeAlignedPastMemoryLimit() { MemoryAllocator mman = createAllocator( ONE_PAGE ); for ( int i = 0; i < 4100; i++ ) { assertThat( mman.allocateAligned( 1, 2 ) % 2, is( 0L ) ); } int pageSize = UnsafeUtil.pageSize(); for ( int i = 0; i < pageSize - 1; i++ ) { int alignment = pageSize - i; long address = mman.allocateAligned( PageCache.PAGE_SIZE, alignment ); assertThat( "iteration " + i + ", aligning to " + alignment, address % alignment, is( 0L ) ); } }
@Test void allocatedPointerMustBeAlignedToArbitraryByte() { int pageSize = UnsafeUtil.pageSize(); for ( int initialOffset = 0; initialOffset < 8; initialOffset++ ) { for ( int i = 0; i < pageSize - 1; i++ ) { MemoryAllocator mman = createAllocator( ONE_PAGE ); mman.allocateAligned( initialOffset, 1 ); long alignment = 1 + i; long address = mman.allocateAligned( PageCache.PAGE_SIZE, alignment ); assertThat( "With initial offset " + initialOffset + ", iteration " + i + ", aligning to " + alignment + " and got address " + address, address % alignment, is( 0L ) ); } } }
private void clearMemory( long baseAddress, long pageCount ) { long memcpyChunkSize = UnsafeUtil.pageSize(); long metaDataEntriesPerChunk = memcpyChunkSize / META_DATA_BYTES_PER_PAGE; if ( pageCount < metaDataEntriesPerChunk || forceSlowMemoryClear ) { clearMemorySimple( baseAddress, pageCount ); } else { clearMemoryFast( baseAddress, pageCount, memcpyChunkSize, metaDataEntriesPerChunk ); } UnsafeUtil.fullFence(); // Guarantee the visibility of the cleared memory. }