PoolArena#size2SizeIdx(int) method.
This ensures that when we request for memory segments of size > pageSize the normalizedCapacity
equals the next nearest size in SizeClasses.
A chunk has the following layout:
/-----------------\
| run |
| |
| |
|-----------------|
| run |
| |
|-----------------|
| unalloctated |
| (freed) |
| |
|-----------------|
| subpage |
|-----------------|
| unallocated |
| (freed) |
| ... |
| ... |
| ... |
| |
| |
| |
\-----------------/
handle:
-------
a handle is a long number, the bit layout of a run looks like:
oooooooo ooooooos ssssssss ssssssue bbbbbbbb bbbbbbbb bbbbbbbb bbbbbbbb
o: runOffset (page offset in the chunk), 15bit
s: size (number of pages) of this run, 15bit
u: isUsed?, 1bit
e: isSubpage?, 1bit
b: bitmapIdx of subpage, zero if it's not subpage, 32bit
runsAvailMap:
------
a map which manages all runs (used and not in used).
For each run, the first runOffset and last runOffset are stored in runsAvailMap.
key: runOffset
value: handle
runsAvail:
----------
an array of PriorityQueue.
Each queue manages same size of runs.
Runs are sorted by offset, so that we always allocate runs with smaller offset.
Algorithm:
----------
As we allocate runs, we update values stored in runsAvailMap and runsAvail so that the property is maintained.
Initialization -
In the beginning we store the initial run which is the whole chunk.
The initial run:
runOffset = 0
size = chunkSize
isUsed = no
isSubpage = no
bitmapIdx = 0
Algorithm: [allocateRun(size)]
----------
1) find the first avail run using in runsAvails according to size
2) if pages of run is larger than request pages then split it, and save the tailing run
for later using
Algorithm: [allocateSubpage(size)]
----------
1) find a not full subpage according to size.
if it already exists just return, otherwise allocate a new PoolSubpage and call init()
note that this subpage object is added to subpagesPool in the PoolArena when we init() it
2) call subpage.allocate()
Algorithm: [free(handle, length, nioBuffer)]
----------
1) if it is a subpage, return the slab back into this subpage
2) if the subpage is not used, or it is a run, then start free this run
3) merge continuous avail runs
4) save the merged run
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final PoolArena | |
| pack-priv final Buffer | |
| pack-priv final Drop | |
| private static final int | |
| private final int | |
| private static final AllocatorControl | |
| pack-priv int | |
| private static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| private static final InternalLogger | |
| pack-priv final Object | |
| pack-priv PoolChunk | |
| private final int | |
| private final int | |
| pack-priv PoolChunkList | |
| pack-priv int | |
| pack-priv PoolChunk | |
| pack-priv static final int | |
| private final LongPriorityQueue[] | runsAvail
manage all avail runs |
| private final ReentrantLock | |
| private final LongLongHashMap | runsAvailMap
store the first page and last page of each avail run |
| private static final int | |
| pack-priv static final int | |
| private static final int | |
| private final PoolSubpage[] | subpages
manage all subpages in this chunk |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv UntetheredMemory | |
| pack-priv UntetheredMemory | |
| pack-priv UntetheredMemory | |
| private long | |
| private long | Returns: index in memoryMapsizeIdx of normalized size sizeIdx)Create / initialize a new PoolSubpage of normCapacity. |
| pack-priv static int | |
| private int | |
| public int | chunkSize()
Implements io. |
| private long | |
| private long | |
| private long | |
| pack-priv void | |
| pack-priv void | free(long
handle to free handle, int normCapacity)Free a subpage, or a run of pages When a subpage is freed from PoolSubpage, it might be added back to subpage pool of the owning PoolArena. |
| public int | freeBytes()
Implements io. |
| private long | |
| private void | |
| private void | |
| pack-priv static boolean | |
| pack-priv static boolean | |
| private static int | |
| private static LongPriorityQueue[] | |
| public int | pinnedBytes()
Implements io. |
| private void | |
| private void | |
| private int | |
| pack-priv static int | |
| pack-priv static int | |
| pack-priv static int | |
| private long | |
| private static long | |
| public String | |
| public int | usage()
Implements io. |
| private int |
| arena | back to summary |
|---|---|
| pack-priv final PoolArena arena | |
| base | back to summary |
|---|---|
| pack-priv final Buffer base | |
| baseDrop | back to summary |
|---|---|
| pack-priv final Drop<Buffer> baseDrop | |
| BITMAP_IDX_BIT_LENGTH | back to summary |
|---|---|
| private static final int BITMAP_IDX_BIT_LENGTH | |
| chunkSize | back to summary |
|---|---|
| private final int chunkSize | |
| CONTROL | back to summary |
|---|---|
| private static final AllocatorControl CONTROL | |
| freeBytes | back to summary |
|---|---|
| pack-priv int freeBytes | |
| INUSED_BIT_LENGTH | back to summary |
|---|---|
| private static final int INUSED_BIT_LENGTH | |
| IS_SUBPAGE_SHIFT | back to summary |
|---|---|
| pack-priv static final int IS_SUBPAGE_SHIFT | |
| IS_USED_SHIFT | back to summary |
|---|---|
| pack-priv static final int IS_USED_SHIFT | |
| LOGGER | back to summary |
|---|---|
| private static final InternalLogger LOGGER | |
| memory | back to summary |
|---|---|
| pack-priv final Object memory | |
| next | back to summary |
|---|---|
| pack-priv PoolChunk next | |
| pageShifts | back to summary |
|---|---|
| private final int pageShifts | |
| pageSize | back to summary |
|---|---|
| private final int pageSize | |
| parent | back to summary |
|---|---|
| pack-priv PoolChunkList parent | |
| pinnedBytes | back to summary |
|---|---|
| pack-priv int pinnedBytes | |
| prev | back to summary |
|---|---|
| pack-priv PoolChunk prev | |
| RUN_OFFSET_SHIFT | back to summary |
|---|---|
| pack-priv static final int RUN_OFFSET_SHIFT | |
| runsAvail | back to summary |
|---|---|
| private final LongPriorityQueue[] runsAvail manage all avail runs | |
| runsAvailLock | back to summary |
|---|---|
| private final ReentrantLock runsAvailLock | |
| runsAvailMap | back to summary |
|---|---|
| private final LongLongHashMap runsAvailMap store the first page and last page of each avail run | |
| SIZE_BIT_LENGTH | back to summary |
|---|---|
| private static final int SIZE_BIT_LENGTH | |
| SIZE_SHIFT | back to summary |
|---|---|
| pack-priv static final int SIZE_SHIFT | |
| SUBPAGE_BIT_LENGTH | back to summary |
|---|---|
| private static final int SUBPAGE_BIT_LENGTH | |
| subpages | back to summary |
|---|---|
| private final PoolSubpage[] subpages manage all subpages in this chunk | |
| PoolChunk | back to summary |
|---|---|
| pack-priv PoolChunk(PoolArena arena, int pageSize, int pageShifts, int chunkSize, int maxPageIdx) | |
| allocate | back to summary |
|---|---|
| pack-priv UntetheredMemory allocate(int size, int sizeIdx, PoolThreadCache cache) | |
| allocateBuffer | back to summary |
|---|---|
| pack-priv UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache) | |
| allocateBufferWithSubpage | back to summary |
|---|---|
| pack-priv UntetheredMemory allocateBufferWithSubpage(long handle, int size, PoolThreadCache threadCache) | |
| allocateRun | back to summary |
|---|---|
| private long allocateRun(int runSize) | |
| allocateSubpage | back to summary |
|---|---|
| private long allocateSubpage(int sizeIdx) Create / initialize a new PoolSubpage of normCapacity. Any PoolSubpage created / initialized here is added to subpage pool in the PoolArena that owns this PoolChunk
| |
| bitmapIdx | back to summary |
|---|---|
| pack-priv static int bitmapIdx(long handle) | |
| calculateRunSize | back to summary |
|---|---|
| private int calculateRunSize(int sizeIdx) | |
| chunkSize | back to summary |
|---|---|
| public int chunkSize() Implements io. Doc from io. Return the size of the chunk in bytes, this is the maximum of bytes that can be served out of the chunk.
| |
| collapseNext | back to summary |
|---|---|
| private long collapseNext(long handle) | |
| collapsePast | back to summary |
|---|---|
| private long collapsePast(long handle) | |
| collapseRuns | back to summary |
|---|---|
| private long collapseRuns(long handle) | |
| destroy | back to summary |
|---|---|
| pack-priv void destroy() | |
| free | back to summary |
|---|---|
| pack-priv void free(long handle, int normCapacity) Free a subpage, or a run of pages When a subpage is freed from PoolSubpage, it might be added back to subpage pool of the owning PoolArena. If the subpage pool in PoolArena has at least one other PoolSubpage of given elemSize, we can completely free the owning Page, so it is available for subsequent allocations.
| |
| freeBytes | back to summary |
|---|---|
| public int freeBytes() Implements io. Doc from io. Return the number of free bytes in the chunk.
| |
| getAvailRunByOffset | back to summary |
|---|---|
| private long getAvailRunByOffset(int runOffset) | |
| insertAvailRun | back to summary |
|---|---|
| private void insertAvailRun(int runOffset, int pages, long handle) | |
| insertAvailRun0 | back to summary |
|---|---|
| private void insertAvailRun0(int runOffset, long handle) | |
| isSubpage | back to summary |
|---|---|
| pack-priv static boolean isSubpage(long handle) | |
| isUsed | back to summary |
|---|---|
| pack-priv static boolean isUsed(long handle) | |
| lastPage | back to summary |
|---|---|
| private static int lastPage(int runOffset, int pages) | |
| newRunsAvailqueueArray | back to summary |
|---|---|
| private static LongPriorityQueue[] newRunsAvailqueueArray(int size) | |
| pinnedBytes | back to summary |
|---|---|
| public int pinnedBytes() Implements io. Doc from io. Return the number of pinned bytes in the chunk.
| |
| removeAvailRun | back to summary |
|---|---|
| private void removeAvailRun(long handle) | |
| removeAvailRun | back to summary |
|---|---|
| private void removeAvailRun(LongPriorityQueue queue, long handle) | |
| runFirstBestFit | back to summary |
|---|---|
| private int runFirstBestFit(int pageIdx) | |
| runOffset | back to summary |
|---|---|
| pack-priv static int runOffset(long handle) | |
| runPages | back to summary |
|---|---|
| pack-priv static int runPages(long handle) | |
| runSize | back to summary |
|---|---|
| pack-priv static int runSize(int pageShifts, long handle) | |
| splitLargeRun | back to summary |
|---|---|
| private long splitLargeRun(long handle, int needPages) | |
| toRunHandle | back to summary |
|---|---|
| private static long toRunHandle(int runOffset, int runPages, int inUsed) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| usage | back to summary |
|---|---|
| public int usage() Implements io. Doc from io. Return the percentage of the current usage of the chunk.
| |
| usage | back to summary |
|---|---|
| private int usage(int freeBytes) | |
| Modifier and Type | Field and Description |
|---|---|
| private final PoolChunk | |
| private final long | |
| private final int | |
| private final Object | |
| private final PoolThreadCache |
| Access | Constructor and Description |
|---|---|
| private | UntetheredChunkAllocation(Object memory, PoolChunk chunk, PoolThreadCache threadCache, long handle, int maxLength, int offset, int size)
|
| Modifier and Type | Method and Description |
|---|---|
| public <BufferType extends Buffer> Drop | drop()
Implements io. |
| public <Memory> Memory | memory()
Implements io. |
| chunk | back to summary |
|---|---|
| private final PoolChunk chunk | |
| handle | back to summary |
|---|---|
| private final long handle | |
| maxLength | back to summary |
|---|---|
| private final int maxLength | |
| memory | back to summary |
|---|---|
| private final Object memory | |
| threadCache | back to summary |
|---|---|
| private final PoolThreadCache threadCache | |
| UntetheredChunkAllocation | back to summary |
|---|---|
| private UntetheredChunkAllocation(Object memory, PoolChunk chunk, PoolThreadCache threadCache, long handle, int maxLength, int offset, int size) | |
| drop | back to summary |
|---|---|
| public <BufferType extends Buffer> Drop Implements io. Doc from io. Produces the drop instance associated with this piece of untethered memory.
| |
| memory | back to summary |
|---|---|
| public <Memory> Memory memory() Implements io. Doc from io. Produces the recoverable memory object associated with this piece of untethered memory.
| |