Uniform alloc and cValue APIs in kotlinx.cinterop
#KT-25628 Fixed
This commit is contained in:
committed by
SvyatoslavScherbina
parent
4cb83cbcc5
commit
f4cf5e842d
@@ -109,6 +109,14 @@ class Arena(parent: NativeFreeablePlacement = nativeHeap) : ArenaBase(parent) {
|
||||
inline fun <reified T : CVariable> NativePlacement.alloc(): T =
|
||||
alloc(sizeOf<T>(), alignOf<T>()).reinterpret()
|
||||
|
||||
/**
|
||||
* Allocates variable of given type and initializes it applying given block.
|
||||
*
|
||||
* @param T must not be abstract
|
||||
*/
|
||||
inline fun <reified T : CVariable> NativePlacement.alloc(initialize: T.() -> Unit): T =
|
||||
alloc<T>().also { it.initialize() }
|
||||
|
||||
/**
|
||||
* Allocates C array of given elements type and length.
|
||||
*
|
||||
@@ -225,6 +233,8 @@ fun <T : CVariable> zeroValue(size: Int, align: Int): CValue<T> = object : CValu
|
||||
inline fun <reified T : CVariable> zeroValue(): CValue<T> =
|
||||
zeroValue<T>(sizeOf<T>().toInt(), alignOf<T>())
|
||||
|
||||
inline fun <reified T : CVariable> cValue(): CValue<T> = zeroValue<T>()
|
||||
|
||||
private fun <T : CPointed> NativePlacement.placeBytes(bytes: ByteArray, align: Int): CPointer<T> {
|
||||
val result = this.alloc(size = bytes.size, align = align)
|
||||
nativeMemUtils.putByteArray(bytes, result, bytes.size)
|
||||
|
||||
Reference in New Issue
Block a user