MemoryUtil
relies on preview features of the Java platform:
MemoryUtil
refers to one or more preview APIs:AddressLayout
,MemoryLayout
,MemorySegment
.
- Since:
- 0.1.0
- Author:
- squid233
-
Field Summary
Modifier and TypeFieldDescriptionstatic final AddressLayoutPREVIEW
An unbounded address layout.static final long
The address ofNULL
. -
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegmentPREVIEW
calloc
(long number, long size) Allocates an array in memory with elements initialized to 0.static MemorySegmentPREVIEW
calloc
(long number, MemoryLayoutPREVIEW layout) Allocates an array in memory with elements initialized to 0.static void
checkAlignment
(long alignment) Checks whetheralignment
is greater than 0 and is a power-of-two value.static void
checkByteSize
(long byteSize) Checks whetherbyteSize
is greater than 0 or equals to 0.static void
free
(@Nullable MemorySegmentPREVIEW memblock) Deallocates or frees a memory block.static boolean
isNullptr
(long address) static boolean
isNullptr
(@Nullable MemorySegmentPREVIEW segment) Returnstrue
ifsegment
is a null pointer.static MemorySegmentPREVIEW
malloc
(long size) Allocates memory blocks.static MemorySegmentPREVIEW
malloc
(MemoryLayoutPREVIEW layout) The layout version ofmalloc(long)
PREVIEW.static MemorySegmentPREVIEW
memcpy
(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Copies bytes between buffers.static MemorySegmentPREVIEW
memmove
(MemorySegmentPREVIEW dest, MemorySegmentPREVIEW src, long count) Moves one buffer to another.static MemorySegmentPREVIEW
memset
(MemorySegmentPREVIEW dest, int c, long count) Sets a buffer to a specified character.static MemorySegmentPREVIEW
realloc
(@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
Returnstrue
ifsegment
is a null pointer.- Parameters:
segment
- the segment.- Returns:
true
ifsegment
is a null pointer
-
isNullptr
public static boolean isNullptr(long address) - Parameters:
address
- the address.- Returns:
true
ifaddress
is 0L
-
checkByteSize
Checks whetherbyteSize
is greater than 0 or equals to 0.- Parameters:
byteSize
- the size, in bytes.- Throws:
IllegalArgumentException
- ifbyteSize
< 0
.
-
checkAlignment
Checks whetheralignment
is greater than 0 and is a power-of-two value.- Parameters:
alignment
- the alignment, in bytes.- Throws:
IllegalArgumentException
- ifalignment
<= 0
, or ifalignment
is not a power of 2.
-
malloc
Allocates memory blocks.The
malloc
function allocates a memory block of at leastsize
bytes. The block may be larger thansize
bytes because of the space that's required for alignment and maintenance information.- Parameters:
size
- Bytes to allocate.- Returns:
malloc
returns a void pointer to the allocated space, orNULL
PREVIEW 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
calloc
function allocates storage space for an array ofnumber
elements, each of lengthsize
bytes. Each element is initialized to 0.- Parameters:
number
- Number of elements.size
- Length in bytes of each element.- Returns:
calloc
returns 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
calloc
function allocates storage space for an array ofnumber
elements, each of lengthsize
bytes. Each element is initialized to 0.- Parameters:
number
- Number of elements.layout
- Length in bytes of each element.- Returns:
calloc
returns 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
realloc
function changes the size of an allocated memory block. Thememblock
argument points to the beginning of the memory block. Ifmemblock
isNULL
,realloc
behaves the same way asmalloc
and allocates a new block ofsize
bytes. Ifmemblock
is notNULL
, it should be a pointer returned by a previous call tocalloc
,malloc
, orrealloc
.The
size
argument 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 byrealloc
is not guaranteed to be the pointer passed through thememblock
argument.realloc
does 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:
realloc
returns aMemorySegment
PREVIEW 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
NULL
is returned.If
size
is zero, then the block pointed to bymemblock
is freed; the return value isNULL
, andmemblock
is 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
free
function 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
). Ifmemblock
isNULL
, 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.memcpy
copiescount
bytes fromsrc
todest
. If the source and destination overlap, the behavior ofmemcpy
is undefined. Usememmove
to 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
count
bytes fromsrc
todest
. 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
count
characters ofdest
to the characterc
.Security Note Make sure that the destination buffer has enough room for at least
count
characters.- Parameters:
dest
- Pointer to destination.c
- Character to set.count
- Number of characters.- Returns:
- The value of
dest
.
-
MemoryUtil
when preview features are enabled.