kmem

Memory utilities for Kotlin

License

License

Categories

Categories

Data
GroupId

GroupId

com.soywiz.korlibs.kmem
ArtifactId

ArtifactId

kmem-metadata
Last Version

Last Version

2.0.10
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

kmem
Memory utilities for Kotlin
Project URL

Project URL

https://github.com/korlibs/kmem
Source Code Management

Source Code Management

https://github.com/korlibs/kmem

Download kmem-metadata

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Korio

Kmem

This library provides extension methods and properties useful for memory handling, and bit manipulation, as well as array and buffer similar to JS typed arrays.

Build Status Maven Version Discord

Support kmem

If you like kmem, or want your company logo here, please consider becoming a sponsor ★,
in addition to ensure the continuity of the project, you will get exclusive content.

Full Documentation: https://korlibs.soywiz.com/kmem/

Some samples

// Array copying
val array = arrayOf("a", "b", "c", null, null)
arraycopy(array, 0, array, 1, 4)

// Array filling
val array = intArrayOf(1, 1, 1, 1, 1)
array.fill(2)
assertEquals(intArrayOf(2, 2, 2, 2, 2).toList(), array.toList())

// CLZ32
assertEquals(1, (0b11111111111111111111111111111110).toInt().countTrailingZeros())

// Byte Array building
val byteArray = buildByteArray {
    append(1)
    append(2)
    append(byteArrayOf(3, 4, 5))
    s32LE(6)
}

// Byte Array reading
val byteArray = buildByteArray { f32BE(1f).f32LE(2f) }
byteArray.read {
    assertEquals(1f, f32BE())
    assertEquals(2f, f32LE())
}

// Float16
assertEquals(+1.0, Float16.fromBits(0x3c00).toDouble())

// Power of Two
assertEquals(16, 10.nextPowerOfTwo)
assertEquals(16, 17.prevPowerOfTwo)
assertEquals(true, 1024.isPowerOfTwo)

// Aligned (multiple of)
assertEquals(false, 77.isAlignedTo(10))
assertEquals(70, 77.prevAlignedTo(10))
assertEquals(80, 77.nextAlignedTo(10))

// ByteArray indexed typed reading
val v = byteArray.readS32LE(10)

// Buffers
val mem = FBuffer.alloc(10)
for (n in 0 until 8) mem[n] = n
assertEquals(0x03020100, mem.getAlignedInt32(0))
assertEquals(0x07060504, mem.getAlignedInt32(1))

Usage with gradle

def kmemVersion = "1.9.1"

repositories {
    maven { url "https://dl.bintray.com/korlibs/korlibs" }
}

dependencies {
    // For multiplatform projects
    implementation "com.soywiz.korlibs.kmem:kmem:$kmemVersion"
    
    // For JVM/Android only
    implementation "com.soywiz.korlibs.kmem:kmem-jvm:$kmemVersion"
    // For JS only
    implementation "com.soywiz.korlibs.kmem:kmem-js:$kmemVersion"
}

// settigs.gradle
enableFeaturePreview('GRADLE_METADATA')
com.soywiz.korlibs.kmem
Kotlin cORoutine Libraries for fullstack development - https://bintray.com/korlibs/korlibs - https://github.com/korlibs

Versions

Version
2.0.10