(package private) class |
PoolChunk<T>
Description of algorithm for PageRun/PoolSubpage allocation from PoolChunk
Notation: The following terms are important to understand the code
> page - a page is the smallest unit of memory chunk that can be allocated
> chunk - a chunk is a collection of pages
> in this code chunkSize = 2^{maxOrder} * pageSize
To begin we allocate a byte array of size = chunkSize
Whenever a ByteBuf of given size needs to be created we search for the first position
in the byte array that has enough empty space to accommodate the requested size and
return a (long) handle that encodes this offset information, (this memory segment is then
marked as reserved so it is always used by exactly one ByteBuf and no more)
For simplicity all sizes are normalized according to PoolArena#normalizeCapacity method
This ensures that when we request for memory segments of size >= pageSize the normalizedCapacity
equals the next nearest power of 2
To search for the first offset in chunk that has at least requested size available we construct a
complete balanced binary tree and store it in an array (just like heaps) - memoryMap
The tree looks like this (the size of each node being mentioned in the parenthesis)
depth=0 1 node (chunkSize)
depth=1 2 nodes (chunkSize/2)
..
|