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 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
- Author:
- Andrey Pangin, Vadim Tsesko
-
-
Constructor Summary
Constructors Constructor Description Malloc(long capacity)Malloc(long base, long capacity)Malloc(MappedFile mmap)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intallocatedSize(long address)longbase()longcalloc(int size)voidfree(long address)longgetFreeMemory()longgetTotalMemory()longgetUsedMemory()longmalloc(int size)voidverify()Verify the layout of the heap.
-
-
-
Constructor Detail
-
Malloc
public Malloc(long capacity)
-
Malloc
public Malloc(long base, long capacity)
-
Malloc
public Malloc(MappedFile mmap)
-
-
Method Detail
-
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
-
allocatedSize
public int allocatedSize(long address)
-
-