Implement methods for conversions between Long and CPointer
This commit is contained in:
committed by
SvyatoslavScherbina
parent
11ecf0c179
commit
20d95ae4c8
+10
@@ -151,6 +151,16 @@ val intPtr: CPointer<IntVar> = bytePtr.reinterpret()
|
||||
As in C, those reinterpret casts are unsafe and could potentially lead to
|
||||
subtle memory problems in an application.
|
||||
|
||||
Also there are unsafe casts between `CPointer<T>?` and `Long` available,
|
||||
provided by `.toLong()` and `.toCPointer<T>()` extension methods:
|
||||
```
|
||||
val longValue = ptr.toLong()
|
||||
val originalPtr = longValue.toCPointer<T>()
|
||||
```
|
||||
|
||||
Note that if the type of the result is known from the context, the type argument
|
||||
can be omitted as usual due to type inference.
|
||||
|
||||
### Memory allocation ###
|
||||
|
||||
The native memory can be allocated using `NativePlacement` interface, e.g.
|
||||
|
||||
@@ -161,6 +161,10 @@ val CPointer<*>?.rawValue: NativePtr
|
||||
|
||||
fun <T : CPointed> CPointer<*>.reinterpret(): CPointer<T> = interpretCPointer(this.rawValue)!!
|
||||
|
||||
fun <T : CPointed> CPointer<T>?.toLong() = this.rawValue.toLong()
|
||||
|
||||
fun <T : CPointed> Long.toCPointer(): CPointer<T>? = interpretCPointer(nativeNullPtr + this)
|
||||
|
||||
/**
|
||||
* The [CPointed] without any specified interpretation.
|
||||
*/
|
||||
|
||||
@@ -78,7 +78,7 @@ fun callWithVarargs(codePtr: NativePtr, returnValuePtr: NativePtr, returnTypeKin
|
||||
// All supported arguments take at most 8 bytes each:
|
||||
val argumentsStorage = argumentsPlacement.allocArray<LongVar>(totalArgumentsNumber)
|
||||
val arguments = argumentsPlacement.allocArray<CPointerVar<*>>(totalArgumentsNumber)
|
||||
val types = argumentsPlacement.allocArray<CPointerVar<*>>(totalArgumentsNumber)
|
||||
val types = argumentsPlacement.allocArray<COpaquePointerVar>(totalArgumentsNumber)
|
||||
|
||||
var index = 0
|
||||
|
||||
@@ -87,7 +87,7 @@ fun callWithVarargs(codePtr: NativePtr, returnValuePtr: NativePtr, returnTypeKin
|
||||
val typeKind = convertArgument(argument, isVariadic = isVariadic,
|
||||
location = storage, additionalPlacement = argumentsPlacement)
|
||||
|
||||
types[index] = interpretCPointer<COpaque>(nativeNullPtr + typeKind.toLong())
|
||||
types[index] = typeKind.toLong().toCPointer()
|
||||
arguments[index] = storage
|
||||
|
||||
++index
|
||||
|
||||
Reference in New Issue
Block a user