MemoryUtil relies on preview features of the Java platform:
MemoryUtilrefers to one or more preview APIs:AddressLayout,MemoryLayout,MemorySegment.
- Since:
 - 0.1.0
 - Author:
 - squid233
 
- 
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AddressLayoutPREVIEWAn unbounded address layout.static final longThe address ofNULL. - 
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegmentPREVIEWcalloc(long number, long size) Allocates an array in memory with elements initialized to 0.static MemorySegmentPREVIEWcalloc(long number, MemoryLayoutPREVIEW layout) Allocates an array in memory with elements initialized to 0.static voidcheckAlignment(long alignment) Checks whetheralignmentis greater than 0 and is a power-of-two value.static voidcheckByteSize(long byteSize) Checks whetherbyteSizeis greater than 0 or equals to 0.static voidfree(@Nullable MemorySegmentPREVIEW memblock) Deallocates or frees a memory block.static booleanisNullptr(long address) static booleanisNullptr(@Nullable MemorySegmentPREVIEW segment) Returnstrueifsegmentis a null pointer.static MemorySegmentPREVIEWmalloc(long size) Allocates memory blocks.static MemorySegmentPREVIEWmalloc(MemoryLayoutPREVIEW layout) The layout version ofmalloc(long)PREVIEW.static MemorySegmentPREVIEWmemcpy(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Copies bytes between buffers.static MemorySegmentPREVIEWmemmove(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Moves one buffer to another.static MemorySegmentPREVIEWmemset(MemorySegmentPREVIEW dest, int c, long count) Sets a buffer to a specified character.static MemorySegmentPREVIEWrealloc(@Nullable MemorySegmentPREVIEW memblock, long size) Reallocate memory blocks. 
- 
Field Details
- 
NULL
public static final long NULLThe address ofNULL.- See Also:
 
 - 
ADDRESS_UNBOUNDED
An unbounded address layout. 
 - 
 - 
Method Details
- 
isNullptr
Returnstrueifsegmentis a null pointer.- Parameters:
 segment- the segment.- Returns:
 trueifsegmentis a null pointer
 - 
isNullptr
public static boolean isNullptr(long address) - Parameters:
 address- the address.- Returns:
 trueifaddressis 0L
 - 
checkByteSize
Checks whetherbyteSizeis greater than 0 or equals to 0.- Parameters:
 byteSize- the size, in bytes.- Throws:
 IllegalArgumentException- ifbyteSize< 0.
 - 
checkAlignment
Checks whetheralignmentis greater than 0 and is a power-of-two value.- Parameters:
 alignment- the alignment, in bytes.- Throws:
 IllegalArgumentException- ifalignment<= 0, or ifalignmentis not a power of 2.
 - 
malloc
Allocates memory blocks.The
mallocfunction allocates a memory block of at leastsizebytes. The block may be larger thansizebytes because of the space that's required for alignment and maintenance information.- Parameters:
 size- Bytes to allocate.- Returns:
 mallocreturns a void pointer to the allocated space, orNULLPREVIEW if there is insufficient memory available. The storage space pointed to by the return value is suitably aligned for storage of any type of object that has an alignment requirement less than or equal to that of the fundamental alignment.- See Also:
 
 - 
malloc
The layout version ofmalloc(long)PREVIEW.- Parameters:
 layout- the memory layout.- Returns:
 - the allocated space.
 - See Also:
 
 - 
calloc
Allocates an array in memory with elements initialized to 0.The
callocfunction allocates storage space for an array ofnumberelements, each of lengthsizebytes. Each element is initialized to 0.- Parameters:
 number- Number of elements.size- Length in bytes of each element.- Returns:
 callocreturns a pointer to the allocated space. The storage space pointed to by the return value is suitably aligned for storage of any type of object.- See Also:
 
 - 
calloc
Allocates an array in memory with elements initialized to 0.The
callocfunction allocates storage space for an array ofnumberelements, each of lengthsizebytes. Each element is initialized to 0.- Parameters:
 number- Number of elements.layout- Length in bytes of each element.- Returns:
 callocreturns a pointer to the allocated space. The storage space pointed to by the return value is suitably aligned for storage of any type of object.- See Also:
 
 - 
realloc
public static MemorySegmentPREVIEW realloc(@Nullable @Nullable MemorySegmentPREVIEW memblock, long size) Reallocate memory blocks.The
reallocfunction changes the size of an allocated memory block. Thememblockargument points to the beginning of the memory block. IfmemblockisNULL,reallocbehaves the same way asmallocand allocates a new block ofsizebytes. Ifmemblockis notNULL, it should be a pointer returned by a previous call tocalloc,malloc, orrealloc.The
sizeargument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned byreallocis not guaranteed to be the pointer passed through thememblockargument.reallocdoes not zero newly allocated memory in the case of buffer growth.- Parameters:
 memblock- Pointer to previously allocated memory block.size- New size in bytes.- Returns:
 reallocreturns aMemorySegmentPREVIEW to the reallocated (and possibly moved) memory block.If there is not enough available memory to expand the block to the given size, the original block is left unchanged, and
NULLis returned.If
sizeis zero, then the block pointed to bymemblockis freed; the return value isNULL, andmemblockis left pointing at a freed block.The return value points to a storage space that is suitably aligned for storage of any type of object.
 - 
free
Deallocates or frees a memory block.The
freefunction deallocates a memory block (memblock) that was previously allocated by a call tocalloc,malloc, orrealloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case ofrealloc). IfmemblockisNULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that wasn't allocated bycalloc,malloc, orrealloc) may affect subsequent allocation requests and cause errors.- Parameters:
 memblock- Previously allocated memory block to be freed.
 - 
memcpy
public static MemorySegmentPREVIEW memcpy(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Copies bytes between buffers.memcpycopiescountbytes fromsrctodest. If the source and destination overlap, the behavior ofmemcpyis undefined. Usememmoveto handle overlapping regions.Make sure that the destination buffer is the same size or larger than the source buffer.
- Parameters:
 dest- New buffer.src- Buffer to copy from.count- Number of characters to copy.- Returns:
 - The value of 
dest. 
 - 
memmove
public static MemorySegmentPREVIEW memmove(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Moves one buffer to another.Copies
countbytes fromsrctodest. If some regions of the source area and the destination overlap, both functions ensure that the original source bytes in the overlapping region are copied before being overwritten.Security Note Make sure that the destination buffer is the same size or larger than the source buffer.
- Parameters:
 dest- Destination object.src- Source object.count- Number of bytes to copy.- Returns:
 - The value of 
dest. 
 - 
memset
Sets a buffer to a specified character.Sets the first
countcharacters ofdestto the characterc.Security Note Make sure that the destination buffer has enough room for at least
countcharacters.- Parameters:
 dest- Pointer to destination.c- Character to set.count- Number of characters.- Returns:
 - The value of 
dest. 
 
 - 
 
MemoryUtilwhen preview features are enabled.