Skip to content

Commit e91237c

Browse files
authored
grpc: Fix 32-bit target APIs (#537)
1 parent ba8fca7 commit e91237c

File tree

9 files changed

+29
-25
lines changed

9 files changed

+29
-25
lines changed

grpc/grpc-client/src/nativeMain/kotlin/kotlinx/rpc/grpc/client/GrpcCallCredentials.native.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ private class CredentialsPluginState(
3030
val coroutineContext: CoroutineContext,
3131
)
3232

33+
@OptIn(UnsafeNumber::class)
3334
private fun getMetadataCallback(
3435
state: COpaquePointer?,
3536
context: CValue<grpc_auth_metadata_context>,
@@ -121,6 +122,7 @@ private fun destroyCallback(state: COpaquePointer?) {
121122
state?.asStableRef<CredentialsPluginState>()?.dispose()
122123
}
123124

125+
@OptIn(UnsafeNumber::class)
124126
internal fun GrpcCallCredentials.createRaw(
125127
coroutineContext: CoroutineContext,
126128
): CPointer<grpc_call_credentials>? = memScoped {

grpc/grpc-client/src/nativeMain/kotlin/kotlinx/rpc/grpc/client/internal/NativeClientCall.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.cinterop.ByteVar
1313
import kotlinx.cinterop.CPointer
1414
import kotlinx.cinterop.CPointerVar
1515
import kotlinx.cinterop.ExperimentalForeignApi
16+
import kotlinx.cinterop.UnsafeNumber
1617
import kotlinx.cinterop.alloc
1718
import kotlinx.cinterop.allocArray
1819
import kotlinx.cinterop.convert
@@ -326,6 +327,7 @@ internal class NativeClientCall<Request, Response>(
326327
}
327328
}
328329

330+
@OptIn(UnsafeNumber::class)
329331
private fun sendAndReceiveInitialMetadata(headers: GrpcMetadata) {
330332
// sending and receiving initial metadata
331333
val arena = Arena()

grpc/grpc-client/src/nativeMain/kotlin/kotlinx/rpc/grpc/client/internal/NativeManagedChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlinx.atomicfu.atomic
1212
import kotlinx.cinterop.CPointer
1313
import kotlinx.cinterop.ExperimentalForeignApi
1414
import kotlinx.cinterop.MemScope
15+
import kotlinx.cinterop.UnsafeNumber
1516
import kotlinx.cinterop.alloc
1617
import kotlinx.cinterop.allocArray
1718
import kotlinx.cinterop.convert
@@ -215,6 +216,7 @@ internal sealed class GrpcArg(val key: String) {
215216
}
216217
}
217218

219+
@OptIn(UnsafeNumber::class)
218220
private fun List<GrpcArg>.toRaw(memScope: MemScope): grpc_channel_args {
219221
with(memScope) {
220222
val arr = allocArray<grpc_arg>(size) {

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/GrpcMetadata.native.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package kotlinx.rpc.grpc
88

99
import kotlinx.cinterop.ExperimentalForeignApi
1010
import kotlinx.cinterop.NativePlacement
11+
import kotlinx.cinterop.UnsafeNumber
1112
import kotlinx.cinterop.addressOf
1213
import kotlinx.cinterop.alloc
1314
import kotlinx.cinterop.allocArray
@@ -62,6 +63,7 @@ public actual class GrpcMetadataKey<T> actual constructor(name: String, public v
6263
public actual class GrpcMetadata actual constructor() {
6364
internal val map: LinkedHashMap<String, MutableList<ByteArray>> = linkedMapOf()
6465

66+
@OptIn(UnsafeNumber::class)
6567
public constructor(raw: grpc_metadata_array) : this() {
6668
for (i in 0 until raw.count.toInt()) {
6769
val metadata = raw.metadata?.get(i)
@@ -73,6 +75,7 @@ public actual class GrpcMetadata actual constructor() {
7375
}
7476
}
7577

78+
@OptIn(UnsafeNumber::class)
7679
@InternalRpcApi
7780
public fun NativePlacement.allocRawGrpcMetadata(): grpc_metadata_array {
7881
val raw = alloc<grpc_metadata_array>()
@@ -91,7 +94,7 @@ public actual class GrpcMetadata actual constructor() {
9194
for (entry in values) {
9295
val size = entry.size.toULong()
9396
val valSlice = entry.usePinned { pinned ->
94-
grpc_slice_from_copied_buffer(pinned.addressOf(0), size)
97+
grpc_slice_from_copied_buffer(pinned.addressOf(0), size.convert())
9598
}
9699
// we create a fresh reference for each entry
97100
val keySliceRef = grpc_slice_ref(keySlice)

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/CompletionQueue.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.cinterop.CFunction
1515
import kotlinx.cinterop.CPointer
1616
import kotlinx.cinterop.ExperimentalForeignApi
1717
import kotlinx.cinterop.StableRef
18+
import kotlinx.cinterop.UnsafeNumber
1819
import kotlinx.cinterop.alloc
1920
import kotlinx.cinterop.asStableRef
2021
import kotlinx.cinterop.convert
@@ -126,6 +127,7 @@ public class CompletionQueue {
126127
* Submits a batch operation to the queue.
127128
* See [BatchResult] for possible outcomes.
128129
*/
130+
@OptIn(UnsafeNumber::class)
129131
public fun runBatch(call: CPointer<grpc_call>, ops: CPointer<grpc_op>, nOps: ULong): BatchResult {
130132
if (_shutdownDone.isCompleted) return BatchResult.CQShutdown
131133

@@ -149,7 +151,7 @@ public class CompletionQueue {
149151
return BatchResult.CQShutdown
150152
}
151153

152-
err = grpc_call_start_batch(call, ops, nOps, tag, null)
154+
err = grpc_call_start_batch(call, ops, nOps.convert(), tag, null)
153155
}
154156

155157
if (err != grpc_call_error.GRPC_CALL_OK) {
@@ -212,6 +214,7 @@ private fun shutdownCb(functor: CPointer<grpc_completion_queue_functor>?, ok: In
212214
private val OPS_COMPLETE_CB = staticCFunction(::opsCompleteCb)
213215
private val SHUTDOWN_CB = staticCFunction(::shutdownCb)
214216

217+
@OptIn(UnsafeNumber::class)
215218
private fun newCbTag(
216219
userData: Any,
217220
cb: CPointer<CFunction<(CPointer<grpc_completion_queue_functor>?, Int) -> Unit>>,

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.native.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.cinterop.ByteVar
1010
import kotlinx.cinterop.CPointer
1111
import kotlinx.cinterop.CValue
1212
import kotlinx.cinterop.ExperimentalForeignApi
13+
import kotlinx.cinterop.UnsafeNumber
1314
import kotlinx.cinterop.addressOf
1415
import kotlinx.cinterop.alloc
1516
import kotlinx.cinterop.allocArray
@@ -49,7 +50,7 @@ import platform.posix.memcpy
4950

5051

5152
@InternalRpcApi
52-
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class)
53+
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class, UnsafeNumber::class)
5354
public fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
5455
var consumed = 0L
5556
while (consumed < length) {
@@ -66,6 +67,7 @@ public fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long
6667
}
6768
}
6869

70+
@OptIn(UnsafeNumber::class)
6971
@InternalRpcApi
7072
public fun grpc_slice.toByteArray(): ByteArray = memScoped {
7173
val out = ByteArray(len().toInt())
@@ -98,6 +100,7 @@ public fun CPointer<grpc_byte_buffer>.toKotlin(): Buffer = memScoped {
98100
return out
99101
}
100102

103+
@OptIn(UnsafeNumber::class)
101104
@InternalRpcApi
102105
public fun Source.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
103106
if (this is Buffer) return toGrpcByteBuffer()
@@ -109,29 +112,30 @@ public fun Source.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
109112
val n = readAtMostTo(tmp, 0, tmp.size)
110113
if (n <= 0) break
111114
tmp.usePinned {
112-
slices += grpc_slice_from_copied_buffer(it.addressOf(0), n.toULong())
115+
slices += grpc_slice_from_copied_buffer(it.addressOf(0), n.convert())
113116
}
114117
}
115118

116119
return slices.toGrpcByteBuffer()
117120
}
118121

119122
@InternalRpcApi
120-
@OptIn(UnsafeIoApi::class)
123+
@OptIn(UnsafeIoApi::class, UnsafeNumber::class)
121124
public fun Buffer.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
122125
val slices = ArrayList<CValue<grpc_slice>>(4)
123126

124127
while (size > 0L) {
125128
UnsafeBufferOperations.readFromHead(this) { arr, start, end ->
126129
val len = end - start
127-
arr.usePinned { p -> slices += grpc_slice_from_copied_buffer(p.addressOf(start), len.toULong()) }
130+
arr.usePinned { p -> slices += grpc_slice_from_copied_buffer(p.addressOf(start), len.convert()) }
128131
len
129132
}
130133
}
131134

132135
return slices.toGrpcByteBuffer()
133136
}
134137

138+
@OptIn(UnsafeNumber::class)
135139
private fun ArrayList<CValue<grpc_slice>>.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> = memScoped {
136140
val count = if (isEmpty()) 1 else size
137141
val sliceArr = allocArray<grpc_slice>(count)
@@ -150,7 +154,7 @@ private fun ArrayList<CValue<grpc_slice>>.toGrpcByteBuffer(): CPointer<grpc_byte
150154
}
151155

152156

153-
val buf = grpc_raw_byte_buffer_create(sliceArr, count.toULong())!!
157+
val buf = grpc_raw_byte_buffer_create(sliceArr, count.convert())!!
154158
// unref each slice, as the buffer takes ownership
155159
this@toGrpcByteBuffer.forEach { grpc_slice_unref(it) }
156160

@@ -166,10 +170,11 @@ public fun grpc_slice.startPtr(): CPointer<ByteVar> {
166170
}
167171
}
168172

173+
@OptIn(UnsafeNumber::class)
169174
@InternalRpcApi
170175
public fun grpc_slice.len(): ULong {
171176
return if (this.refcount != null) {
172-
this.data.refcounted.length
177+
this.data.refcounted.length.convert()
173178
} else {
174179
this.data.inlined.length.convert()
175180
}
@@ -231,6 +236,7 @@ public fun grpc_metadata.destroy() {
231236
grpc_slice_unref(value.readValue())
232237
}
233238

239+
@OptIn(UnsafeNumber::class)
234240
@InternalRpcApi
235241
public fun grpc_metadata_array.destroyEntries() {
236242
for (i in 0 until count.convert()) {

grpc/grpc-ktor-server/gradle.properties

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,4 @@
55
kotlinx.rpc.exclude.wasmWasi=true
66
kotlinx.rpc.exclude.js=true
77
kotlinx.rpc.exclude.wasmJs=true
8-
kotlinx.rpc.exclude.iosArm64=true
9-
kotlinx.rpc.exclude.iosX64=true
10-
kotlinx.rpc.exclude.iosSimulatorArm64=true
11-
kotlinx.rpc.exclude.linuxArm64=true
12-
kotlinx.rpc.exclude.linuxX64=true
13-
kotlinx.rpc.exclude.macosX64=true
148
kotlinx.rpc.exclude.mingwX64=true
15-
kotlinx.rpc.exclude.tvosArm64=true
16-
kotlinx.rpc.exclude.tvosSimulatorArm64=true
17-
kotlinx.rpc.exclude.tvosX64=true
18-
kotlinx.rpc.exclude.watchosArm32=true
19-
kotlinx.rpc.exclude.watchosArm64=true
20-
kotlinx.rpc.exclude.watchosDeviceArm64=true
21-
kotlinx.rpc.exclude.watchosSimulatorArm64=true
22-
kotlinx.rpc.exclude.watchosX64=true

protobuf/protobuf-core/src/nativeMain/kotlin/kotlinx/rpc/protobuf/internal/WireEncoder.native.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal class WireEncoderNative(private val sink: Sink) : WireEncoder {
6565
*/
6666
private inner class Ctx {
6767
fun write(buf: CPointer<ByteVar>, size: Int): Boolean {
68-
sink.writeFully(buf, 0L, size.toLong())
68+
sink.writeFully(buf, 0L, size)
6969
return true
7070
}
7171
}

protobuf/protobuf-core/src/nativeMain/kotlin/kotlinx/rpc/protobuf/internal/bufferUnsafeExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import kotlinx.io.unsafe.UnsafeBufferOperations
1212
import platform.posix.memcpy
1313

1414

15-
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class)
16-
internal fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
15+
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class, UnsafeNumber::class)
16+
internal fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Int) {
1717
var consumed = 0L
1818
while (consumed < length) {
1919
UnsafeBufferOperations.writeToTail(this.buffer, 1) { array, start, endExclusive ->

0 commit comments

Comments
 (0)