Package one.nio.mem
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
- Author:
- Andrey Pangin, Vadim Tsesko
-
Constructor Summary
ConstructorsConstructorDescriptionMalloc(long capacity) Malloc(long base, long capacity) Malloc(MappedFile mmap) -
Method Summary
-
Constructor Details
-
Malloc
public Malloc(long capacity) -
Malloc
public Malloc(long base, long capacity) -
Malloc
-
-
Method Details
-
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
public long calloc(int size) -
malloc
public long malloc(int size) -
free
public void free(long address) -
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.
-