Class Malloc
java.lang.Object
one.nio.mem.Malloc
- All Implemented Interfaces:
Allocator, MallocMXBean
- Direct Known Subclasses:
MallocMT
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) |
+-----------------------------+
left is the offset to the beginning of a previous chunk.
Free chunks have MSB of the size unset.
Free chunks are linked by double-linked lists (using next and previous) starting from the
corresponding bin.
Occupied chunk format (8 byte header + payload):
+--------------+--------------+
| size (int32) | left (int32) |
+--------------+--------------+
| user data |
+-----------------------------+
Occupied chunks have MSB of the size set.
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
FieldsModifier and TypeFieldDescription(package private) final long(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) final long(package private) static final int(package private) static final intprivate long(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) static final int(package private) static final long(package private) static final long(package private) static final int -
Constructor Summary
ConstructorsConstructorDescriptionMalloc(long capacity) Malloc(long base, long capacity) Malloc(MappedFile mmap) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddBoundary(long address) private voidaddFreeChunk(long address, int size) intallocatedSize(long address) final 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) longlonglong(package private) voidinit()longmalloc(int size) (package private) final longmallocImpl(int bin, int size) private voidrelocate(long delta) private voidremoveFreeChunk(long address) private voidvoidverify()Verify the layout of the heap.
-
Field Details
-
SIGNATURE_V3
static final long SIGNATURE_V3- See Also:
-
SIGNATURE_V2
static final long SIGNATURE_V2- See Also:
-
SIGNATURE_OFFSET
static final int SIGNATURE_OFFSET- See Also:
-
CAPACITY_OFFSET
static final int CAPACITY_OFFSET- See Also:
-
BASE_OFFSET
static final int BASE_OFFSET- See Also:
-
HEADER_SIZE
static final int HEADER_SIZE- See Also:
-
SIZE_OFFSET
static final int SIZE_OFFSET- See Also:
-
LEFT_OFFSET
static final int LEFT_OFFSET- See Also:
-
NEXT_OFFSET
static final int NEXT_OFFSET- See Also:
-
PREV_OFFSET
static final int PREV_OFFSET- See Also:
-
BIN_COUNT
static final int BIN_COUNT- See Also:
-
BIN_SIZE
static final int BIN_SIZE- See Also:
-
BIN_SPACE
static final int BIN_SPACE- See Also:
-
MAX_CHUNK
static final int MAX_CHUNK- See Also:
-
MIN_CHUNK
static final int MIN_CHUNK- See Also:
-
OCCUPIED_MASK
static final int OCCUPIED_MASK- See Also:
-
FREE_MASK
static final int FREE_MASK- See Also:
-
base
final long base -
capacity
final long capacity -
freeMemory
private volatile long freeMemory
-
-
Constructor Details
-
Malloc
public Malloc(long capacity) -
Malloc
public Malloc(long base, long capacity) -
Malloc
-
-
Method Details
-
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
-
calloc
-
malloc
-
mallocImpl
final long mallocImpl(int bin, int size) -
free
-
allocatedSize
public int allocatedSize(long address) -
verify
-
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)
-