Slab { public synchronized ByteBuffer takePageBuffer() { ByteBuffer buffer = available.isEmpty() ? takeNew() : takeUsed(); if (buffer != null) { assert buffer.position() == 0; assert buffer.remaining() == pageSize; } return buffer; } Slab(int slabId, int slabSize, int pageSize); synchronized ByteBuffer takePageBuffer(); void returnPageBuffer(ByteBuffer pageBuffer); int slabId(); byte[] byteArray(); void clear();  }
@Test public void testInitialAllocations() { Slab slab = new Slab(0, SLAB_SIZE, PAGE_SIZE); int[] arrayOffsets = new int[PAGES]; for (int p = 0; p < PAGES; p++) { ByteBuffer buffer = slab.takePageBuffer(); assertNotNull(buffer); assertEquals(0, buffer.position()); assertEquals(PAGE_SIZE, buffer.limit()); arrayOffsets[p] = buffer.arrayOffset(); } assertNull(slab.takePageBuffer()); Arrays.sort(arrayOffsets); int expectedArrayOffset = 0; for (int b = 0; b < PAGES; b++) { assertEquals(expectedArrayOffset, arrayOffsets[b]); expectedArrayOffset += PAGE_SIZE; } }