- All Superinterfaces:
Addressable
,AutoCloseable
- Enclosing interface:
- CLinker
va_list
.
A va list is a stateful cursor used to iterate over a set of variadic arguments.
Per the C specification (see C standard 6.5.2.2 Function calls - item 6),
arguments to variadic calls are erased by way of 'default argument promotions',
which erases integral types by way of integer promotion (see C standard 6.3.1.1 - item 2),
and which erases all float
arguments to double
.
As such, this interface only supports reading int
, double
,
and any other type that fits into a long
.
Unless otherwise specified, passing a null
argument, or an array argument containing one or more null
elements to a method in this class causes a NullPointerException
to be thrown.
- API Note:
- In the future, if the Java language permits,
CLinker.VaList
may become asealed
interface, which would prohibit subclassing except by explicitly permitted types.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A builder interface used to construct a Cva_list
. -
Method Summary
Modifier and TypeMethodDescriptionaddress()
Returns the memory address of the Cva_list
associated with this instance.void
close()
Releases the underlying Cva_list
modelled by this instance, and any native memory that is attached to this va list that holds its elements (seemake(Consumer)
).copy()
Copies this Cva_list
at its current position.copy(NativeScope scope)
Copies this Cva_list
at its current position.static CLinker.VaList
empty()
Returns an empty Cva_list
constant.boolean
isAlive()
A predicate used to check if the memory associated with the Cva_list
modelled by this instance is still valid to use.static CLinker.VaList
make(Consumer<CLinker.VaList.Builder> actions)
Constructs a newVaList
using a builder (seeCLinker.VaList.Builder
).static CLinker.VaList
make(Consumer<CLinker.VaList.Builder> actions, NativeScope scope)
Constructs a newVaList
using a builder (seeCLinker.VaList.Builder
).static CLinker.VaList
ofAddressRestricted(MemoryAddress address)
Constructs a newVaList
instance out of a memory address pointing to an existing Cva_list
.void
skip(MemoryLayout... layouts)
Skips a number of elements with the given memory layouts, and advances this va list's position.vargAsAddress(MemoryLayout layout)
Reads the next value as aMemoryAddress
and advances this va list's position.double
vargAsDouble(MemoryLayout layout)
Reads the next value as adouble
and advances this va list's position.int
vargAsInt(MemoryLayout layout)
Reads the next value as anint
and advances this va list's position.long
vargAsLong(MemoryLayout layout)
Reads the next value as along
and advances this va list's position.vargAsSegment(MemoryLayout layout)
Reads the next value as aMemorySegment
, and advances this va list's position.vargAsSegment(MemoryLayout layout, NativeScope scope)
Reads the next value as aMemorySegment
, and advances this va list's position.
-
Method Details
-
vargAsInt
Reads the next value as anint
and advances this va list's position.- Parameters:
layout
- the layout of the value- Returns:
- the value read as an
int
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withint
-
vargAsLong
Reads the next value as along
and advances this va list's position.- Parameters:
layout
- the layout of the value- Returns:
- the value read as an
long
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withlong
-
vargAsDouble
Reads the next value as adouble
and advances this va list's position.- Parameters:
layout
- the layout of the value- Returns:
- the value read as an
double
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withdouble
-
vargAsAddress
Reads the next value as aMemoryAddress
and advances this va list's position.- Parameters:
layout
- the layout of the value- Returns:
- the value read as an
MemoryAddress
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withMemoryAddress
-
vargAsSegment
Reads the next value as aMemorySegment
, and advances this va list's position.The memory segment returned by this method will be allocated using
MemorySegment.allocateNative(long, long)
, and will have to be closed separately.- Parameters:
layout
- the layout of the value- Returns:
- the value read as an
MemorySegment
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withMemorySegment
-
vargAsSegment
Reads the next value as aMemorySegment
, and advances this va list's position.The memory segment returned by this method will be allocated using the given
NativeScope
.- Parameters:
layout
- the layout of the valuescope
- the scope to allocate the segment in- Returns:
- the value read as an
MemorySegment
- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).IllegalArgumentException
- if the given memory layout is not compatible withMemorySegment
-
skip
Skips a number of elements with the given memory layouts, and advances this va list's position.- Parameters:
layouts
- the layout of the value- Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).
-
isAlive
boolean isAlive()A predicate used to check if the memory associated with the Cva_list
modelled by this instance is still valid to use.- Returns:
- true, if the memory associated with the C
va_list
modelled by this instance is still valid - See Also:
close()
-
close
void close()Releases the underlying Cva_list
modelled by this instance, and any native memory that is attached to this va list that holds its elements (seemake(Consumer)
).After calling this method,
isAlive()
will returnfalse
and further attempts to read values from this va list will result in an exception.- Specified by:
close
in interfaceAutoCloseable
- See Also:
isAlive()
-
copy
CLinker.VaList copy()Copies this Cva_list
at its current position. Copying is useful to traverse the va list's elements starting from the current position, without affecting the state of the original va list, essentially allowing the elements to be traversed multiple times.If this method needs to allocate native memory for the copy, it will use
MemorySegment.allocateNative(long, long)
to do so.close()
will have to be called on the returned va list instance to release the allocated memory.This method only copies the va list cursor itself and not the memory that may be attached to the va list which holds its elements. That means that if this va list was created with the
make(Consumer)
method, closing this va list will also release the native memory that holds its elements, making the copy unusable.- Returns:
- a copy of this C
va_list
. - Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).
-
copy
Copies this Cva_list
at its current position. Copying is useful to traverse the va list's elements starting from the current position, without affecting the state of the original va list, essentially allowing the elements to be traversed multiple times.If this method needs to allocate native memory for the copy, it will use the given
NativeScope
to do so.This method only copies the va list cursor itself and not the memory that may be attached to the va list which holds its elements. That means that if this va list was created with the
make(Consumer)
method, closing this va list will also release the native memory that holds its elements, making the copy unusable.- Parameters:
scope
- the scope to allocate the copy in- Returns:
- a copy of this C
va_list
. - Throws:
IllegalStateException
- if the Cva_list
associated with this instance is no longer valid (seeclose()
).
-
address
MemoryAddress address()Returns the memory address of the Cva_list
associated with this instance.- Specified by:
address
in interfaceAddressable
- Returns:
- the memory address of the C
va_list
associated with this instance.
-
ofAddressRestricted
Constructs a newVaList
instance out of a memory address pointing to an existing Cva_list
.This method is restricted. Restricted method are unsafe, and, if used incorrectly, their use might crash the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on restricted methods, and use safe and supported functionalities, where possible.
- Parameters:
address
- a memory address pointing to an existing Cva_list
.- Returns:
- a new
VaList
instance backed by the Cva_list
ataddress
.
-
make
Constructs a newVaList
using a builder (seeCLinker.VaList.Builder
).If this method needs to allocate native memory for the va list, it will use
MemorySegment.allocateNative(long, long)
to do so.This method will allocate native memory to hold the elements in the va list. This memory will be 'attached' to the returned va list instance, and will be released when
close()
is called.Note that when there are no elements added to the created va list, this method will return the same as
empty()
.- Parameters:
actions
- a consumer for a builder (seeCLinker.VaList.Builder
) which can be used to specify the elements of the underlying Cva_list
.- Returns:
- a new
VaList
instance backed by a fresh Cva_list
.
-
make
Constructs a newVaList
using a builder (seeCLinker.VaList.Builder
).If this method needs to allocate native memory for the va list, it will use the given
NativeScope
to do so.This method will allocate native memory to hold the elements in the va list. This memory will be managed by the given
NativeScope
, and will be released when the scope is closed.Note that when there are no elements added to the created va list, this method will return the same as
empty()
.- Parameters:
actions
- a consumer for a builder (seeCLinker.VaList.Builder
) which can be used to specify the elements of the underlying Cva_list
.scope
- the scope to be used for the valist allocation.- Returns:
- a new
VaList
instance backed by a fresh Cva_list
.
-
empty
Returns an empty Cva_list
constant.The returned
VaList
can not be closed.- Returns:
- a
VaList
modelling an empty Cva_list
.
-