Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Rename ByteBuffer to DirectMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominaezzz committed Jun 28, 2020
1 parent cae39cb commit 9ec0882
Show file tree
Hide file tree
Showing 49 changed files with 295 additions and 303 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/codegen/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.squareup.kotlinpoet.MemberName


internal val VIRTUAL_STACK = ClassName("com.kgl.core", "VirtualStack")
internal val BYTE_BUFFER = ClassName("com.kgl.core", "ByteBuffer")
internal val DIRECT_BUFFER = ClassName("com.kgl.core", "DirectMemory")
internal val C_OPAQUE_POINTER = ClassName("kotlinx.cinterop", "COpaquePointer")
internal val BYTE_VAR = ClassName("kotlinx.cinterop", "ByteVar")

Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/codegen/vulkan/GenerateVulkan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ open class GenerateVulkan : DefaultTask() {
1 -> {
when(typeName) {
"char" -> STRING
"void" -> if (len.isNotEmpty()) BYTE_BUFFER else LONG
"void" -> if (len.isNotEmpty()) DIRECT_BUFFER else LONG
else -> {
val mainType = kglClassMap[typeName] ?: TODO("$type has no KGL representation.")
arrayClassesMap[typeName] ?: collectionType.parameterizedBy(mainType)
Expand Down Expand Up @@ -1458,7 +1458,7 @@ open class GenerateVulkan : DefaultTask() {
}
val isOptional = member.optional || lengthParam?.optional == true
if (lengthParam != null) {
addParameter(memberNameKt, BYTE_BUFFER.copy(nullable = isOptional))
addParameter(memberNameKt, DIRECT_BUFFER.copy(nullable = isOptional))

val assert = if (isOptional) "?" else ""
if (platform == Platform.JVM) {
Expand Down
5 changes: 0 additions & 5 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/Buffer.kt

This file was deleted.

4 changes: 0 additions & 4 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/BufferUtils.kt

This file was deleted.

21 changes: 0 additions & 21 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/ByteBuffer.kt

This file was deleted.

20 changes: 20 additions & 0 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/DirectMemory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.kgl.core

expect class DirectMemory {
val size: Long

operator fun get(offset: Long): Byte
operator fun set(offset: Long, value: Byte)

fun get(dst: ByteArray, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = dst.size.toLong())
fun set(src: ByteArray, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = src.size.toLong())

fun copyTo(dst: DirectMemory, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = dst.size - dstOffset)
fun slice(offset: Long, length: Long): DirectMemory
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun DirectMemory.checkBounds(offset: Long, length: Long) {
require(offset in 0 until this.size)
require(length in 0 until (this.size - offset))
}
4 changes: 4 additions & 0 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/MemUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kgl.core


expect inline fun <T> withMemory(length: Long, block: (DirectMemory) -> T): T
8 changes: 0 additions & 8 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/BufferUtils.kt

This file was deleted.

45 changes: 0 additions & 45 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/ByteBuffer.kt

This file was deleted.

42 changes: 42 additions & 0 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/DirectMemory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.kgl.core

import org.khronos.webgl.DataView

actual class DirectMemory(private val ptr: DataView) {
actual val size: Long get() = ptr.byteLength.toLong()

actual operator fun get(offset: Long): Byte {
return ptr.getInt8(offset.toInt())
}

actual operator fun set(offset: Long, value: Byte) {
ptr.setInt8(offset.toInt(), value)
}

actual fun get(dst: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(srcOffset, length)
for (i in 0 until length) {
dst[(dstOffset + i).toInt()] = get(srcOffset + i)
}
}

actual fun set(src: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(dstOffset, length)
for (i in 0 until length) {
this[dstOffset + i] = src[(srcOffset + i).toInt()]
}
}

actual fun copyTo(dst: DirectMemory, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(srcOffset, length)
dst.checkBounds(dstOffset, length)
for (i in 0 until length) {
dst[dstOffset + i] = get(srcOffset + i)
}
}

actual fun slice(offset: Long, length: Long): DirectMemory {
checkBounds(offset, length)
return DirectMemory(DataView(ptr.buffer, ptr.byteOffset + offset.toInt(), length.toInt()))
}
}
11 changes: 11 additions & 0 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/MemUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kgl.core

import org.khronos.webgl.DataView
import org.khronos.webgl.Int8Array

actual inline fun <T> withMemory(length: Long, block: (DirectMemory) -> T): T {
val array = ByteArray(length.toInt())
val jsArray = array.unsafeCast<Int8Array>()
val view = DataView(jsArray.buffer, jsArray.byteOffset, jsArray.byteLength)
return block(DirectMemory(view))
}
51 changes: 0 additions & 51 deletions kgl-core/src/jvmMain/kotlin/com/kgl/core/ByteBuffer.kt

This file was deleted.

50 changes: 50 additions & 0 deletions kgl-core/src/jvmMain/kotlin/com/kgl/core/DirectMemory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.kgl.core

import org.lwjgl.system.MemoryUtil
import org.lwjgl.system.jni.JNINativeInterface
import java.nio.ByteBuffer as JvmByteBuffer

actual class DirectMemory(private val ptr: Long, actual val size: Long) {

constructor(buffer: JvmByteBuffer) : this(MemoryUtil.memAddress(buffer), buffer.capacity().toLong())

actual operator fun get(offset: Long): Byte {
return MemoryUtil.memGetByte(ptr + (offset * Byte.SIZE_BYTES))
}

actual operator fun set(offset: Long, value: Byte) {
MemoryUtil.memPutByte(ptr + (offset * Byte.SIZE_BYTES), value)
}

actual fun get(dst: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(srcOffset, length)

JNINativeInterface.nSetByteArrayRegion(dst, dstOffset.toInt(), length.toInt(), ptr + (srcOffset * Byte.SIZE_BYTES))
}

actual fun set(src: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(dstOffset, length)
JNINativeInterface.nGetByteArrayRegion(src, srcOffset.toInt(), length.toInt(), ptr + (dstOffset * Byte.SIZE_BYTES))
}

actual fun copyTo(dst: DirectMemory, srcOffset: Long, dstOffset: Long, length: Long) {
checkBounds(srcOffset, length)
dst.checkBounds(dstOffset, length)

MemoryUtil.memCopy(
ptr + (srcOffset * Byte.SIZE_BYTES),
dst.ptr + (dstOffset * Byte.SIZE_BYTES),
length
)
}

actual fun slice(offset: Long, length: Long): DirectMemory {
checkBounds(offset, length)

return DirectMemory(ptr + (offset * Byte.SIZE_BYTES), length)
}

fun asJvmByteBuffer(): JvmByteBuffer {
return MemoryUtil.memByteBuffer(ptr, size.toInt())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.kgl.core
import org.lwjgl.system.MemoryUtil


actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
actual inline fun <T> withMemory(length: Long, block: (DirectMemory) -> T): T {
val jvmBuffer = MemoryUtil.memAlloc(length.toInt())
try {
return block(ByteBuffer(jvmBuffer))
return block(DirectMemory(jvmBuffer))
} finally {
MemoryUtil.memFree(jvmBuffer)
}
Expand Down
49 changes: 0 additions & 49 deletions kgl-core/src/nativeMain/kotlin/com/kgl/core/ByteBuffer.kt

This file was deleted.

Loading

0 comments on commit 9ec0882

Please sign in to comment.