Package one.nio.mem
Class Malloc
- java.lang.Object
-
- one.nio.mem.Malloc
-
- All Implemented Interfaces:
Allocator,MallocMXBean
- Direct Known Subclasses:
MallocMT
public class Malloc extends java.lang.Object implements Allocator, MallocMXBean
A simplified implementation of Doug Lea's Memory Allocator. Allocates up to 25% larger memory chunks rounded to the bin size.Memory format:
SIGNATUREint64format version magic bytesCAPACITYint64size of the allocated memoryBASEint64base address of the memory- padding up to 64 bytes
- Bins:
BIN_0_CHUNKint64BIN_1_CHUNKint64- ...
BIN_N_CHUNKint64
There are 2 types of chunks: free and occupied. Chunks are aligned to 8 byte boundary.
Free chunk format (8 byte header + 2 x 64-bit references = 24 bytes min chunk):
+--------------+--------------+ | size (int32) | left (int32) | +--------------+--------------+ | next (int64) | +-----------------------------+ | previous (int64) | +-----------------------------+leftis the offset to the beginning of a previous chunk. Free chunks have MSB of thesizeunset. Free chunks are linked by double-linked lists (usingnextandprevious) starting from the corresponding bin.Occupied chunk format (8 byte header + payload):
Occupied chunks have MSB of the+--------------+--------------+ | size (int32) | left (int32) | +--------------+--------------+ | user data | +-----------------------------+sizeset.Invariants:
- Each chunk is linked to the bin according to chunk size
- Two free chunks are coalesced if they are physical neighbours
- Free chunks are always interleaved with occupied chunks
Bins contain chunks with sizes from the bin size inclusive up to the next bin size exclusive (see
chooseBin(int)).- Round the user requested size up to the nearest bin size
- Start looking for the first chunk starting from the corresponding bin up to the last bin:
- If there is no chunk in the current bin, go to the next bin
- If the first chunk is appropriately sized, remove it from the list of chunks and return it to the user
- If the first chunk is too large, split it, insert the tail into the list of chunks in the corresponding bin and return the head to the user
- If nothing is found, throw
OutOfMemoryException
- Try to coalesce with left and right neighbours if they are free
- Insert the resulting chunk into the corresponding bin
-
-
Field Summary
Fields Modifier and Type Field Description (package private) longbase(package private) static intBASE_OFFSET(package private) static intBIN_COUNT(package private) static intBIN_SIZE(package private) static intBIN_SPACE(package private) longcapacity(package private) static intCAPACITY_OFFSET(package private) static intFREE_MASKprivate longfreeMemory(package private) static intHEADER_SIZE(package private) static intLEFT_OFFSET(package private) static intMAX_CHUNK(package private) static intMIN_CHUNK(package private) static intNEXT_OFFSET(package private) static intOCCUPIED_MASK(package private) static intPREV_OFFSET(package private) static intSIGNATURE_OFFSET(package private) static longSIGNATURE_V2(package private) static longSIGNATURE_V3(package private) static intSIZE_OFFSET
-
Constructor Summary
Constructors Constructor Description Malloc(long capacity)Malloc(long base, long capacity)Malloc(MappedFile mmap)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddBoundary(long address)private voidaddFreeChunk(long address, int size)intallocatedSize(long address)longbase()(package private) static intbinSize(int bin)longcalloc(int size)(package private) static intchooseBin(int size)voidfree(long address)(package private) static intgetBin(int size)private longgetChunk(int bin, int size)longgetFreeMemory()longgetTotalMemory()longgetUsedMemory()(package private) voidinit()longmalloc(int size)(package private) longmallocImpl(int bin, int size)private voidrelocate(long delta)private voidremoveFreeChunk(long address)private voidupgradeBinFormat()voidverify()Verify the layout of the heap.
-
-
-
Field Detail
-
SIGNATURE_V3
static final long SIGNATURE_V3
- See Also:
- Constant Field Values
-
SIGNATURE_V2
static final long SIGNATURE_V2
- See Also:
- Constant Field Values
-
SIGNATURE_OFFSET
static final int SIGNATURE_OFFSET
- See Also:
- Constant Field Values
-
CAPACITY_OFFSET
static final int CAPACITY_OFFSET
- See Also:
- Constant Field Values
-
BASE_OFFSET
static final int BASE_OFFSET
- See Also:
- Constant Field Values
-
HEADER_SIZE
static final int HEADER_SIZE
- See Also:
- Constant Field Values
-
SIZE_OFFSET
static final int SIZE_OFFSET
- See Also:
- Constant Field Values
-
LEFT_OFFSET
static final int LEFT_OFFSET
- See Also:
- Constant Field Values
-
NEXT_OFFSET
static final int NEXT_OFFSET
- See Also:
- Constant Field Values
-
PREV_OFFSET
static final int PREV_OFFSET
- See Also:
- Constant Field Values
-
BIN_COUNT
static final int BIN_COUNT
- See Also:
- Constant Field Values
-
BIN_SIZE
static final int BIN_SIZE
- See Also:
- Constant Field Values
-
BIN_SPACE
static final int BIN_SPACE
- See Also:
- Constant Field Values
-
MAX_CHUNK
static final int MAX_CHUNK
- See Also:
- Constant Field Values
-
MIN_CHUNK
static final int MIN_CHUNK
- See Also:
- Constant Field Values
-
OCCUPIED_MASK
static final int OCCUPIED_MASK
- See Also:
- Constant Field Values
-
FREE_MASK
static final int FREE_MASK
- See Also:
- Constant Field Values
-
base
final long base
-
capacity
final long capacity
-
freeMemory
private volatile long freeMemory
-
-
Constructor Detail
-
Malloc
public Malloc(long capacity)
-
Malloc
public Malloc(long base, long capacity)
-
Malloc
public Malloc(MappedFile mmap)
-
-
Method Detail
-
getBin
static int getBin(int size)
-
binSize
static int binSize(int bin)
-
chooseBin
static int chooseBin(int size)
-
base
public final long base()
-
getTotalMemory
public long getTotalMemory()
- Specified by:
getTotalMemoryin interfaceMallocMXBean
-
getFreeMemory
public long getFreeMemory()
- Specified by:
getFreeMemoryin interfaceMallocMXBean
-
getUsedMemory
public long getUsedMemory()
- Specified by:
getUsedMemoryin interfaceMallocMXBean
-
mallocImpl
final long mallocImpl(int bin, int size)
-
allocatedSize
public int allocatedSize(long address)
-
verify
public void verify()
Description copied from interface:AllocatorVerify the layout of the heap. Expensive operation, used only for debugging purposes.
-
init
void init()
-
relocate
private void relocate(long delta)
-
upgradeBinFormat
private void upgradeBinFormat()
-
addBoundary
private void addBoundary(long address)
-
getChunk
private long getChunk(int bin, int size)
-
addFreeChunk
private void addFreeChunk(long address, int size)
-
removeFreeChunk
private void removeFreeChunk(long address)
-
-