Skip to content

Commit 5757c98

Browse files
committed
Add WithCrt
1 parent e4f58d6 commit 5757c98

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.crt
6+
7+
public open class WithCrt() {
8+
init {
9+
CRT.initRuntime { }
10+
}
11+
}

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/CrcNative.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
package aws.sdk.kotlin.crt.util.hashing
66

7-
import aws.sdk.kotlin.crt.CRT
7+
import aws.sdk.kotlin.crt.WithCrt
88
import kotlinx.cinterop.*
99
import libcrt.aws_checksums_crc32
1010
import libcrt.aws_checksums_crc32c
@@ -21,10 +21,6 @@ internal typealias AwsChecksumsCrcFunction = (
2121
) -> uint32_t
2222

2323
internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
24-
init {
25-
CRT.initRuntime { }
26-
}
27-
2824
private var crc = 0U
2925

3026
override fun update(input: ByteArray, offset: Int, length: Int) {
@@ -53,7 +49,7 @@ internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
5349
/**
5450
* A CRC32 [HashFunction] implemented using bindings to CRT.
5551
*/
56-
public class Crc32 : HashFunction {
52+
public class Crc32 : HashFunction, WithCrt() {
5753
private val crc32 = Crc(::aws_checksums_crc32)
5854
override fun update(input: ByteArray, offset: Int, length: Int) {
5955
crc32.update(input, offset, length)
@@ -67,7 +63,7 @@ public class Crc32 : HashFunction {
6763
/**
6864
* A CRC32C [HashFunction] implemented using bindings to CRT.
6965
*/
70-
public class Crc32c : HashFunction {
66+
public class Crc32c : HashFunction, WithCrt() {
7167
private val crc32c = Crc(::aws_checksums_crc32c)
7268
override fun update(input: ByteArray, offset: Int, length: Int) {
7369
crc32c.update(input, offset, length)

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/Md5Native.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package aws.sdk.kotlin.crt.util.hashing
66

77
import aws.sdk.kotlin.crt.Allocator
88
import aws.sdk.kotlin.crt.CRT
9+
import aws.sdk.kotlin.crt.WithCrt
910
import aws.sdk.kotlin.crt.awsAssertOpSuccess
1011
import kotlinx.cinterop.addressOf
1112
import kotlinx.cinterop.cValue
@@ -23,11 +24,7 @@ import libcrt.aws_md5_new
2324
/**
2425
* MD5 hash function implemented using bindings to CRT
2526
*/
26-
public class Md5 : HashFunction {
27-
init {
28-
CRT.initRuntime { }
29-
}
30-
27+
public class Md5 : HashFunction, WithCrt() {
3128
private var md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
3229

3330
override fun update(input: ByteArray, offset: Int, length: Int) {

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/ShaNative.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package aws.sdk.kotlin.crt.util.hashing
66

77
import aws.sdk.kotlin.crt.Allocator
88
import aws.sdk.kotlin.crt.CRT
9+
import aws.sdk.kotlin.crt.WithCrt
910
import aws.sdk.kotlin.crt.awsAssertOpSuccess
1011
import kotlinx.cinterop.*
1112
import libcrt.*
@@ -20,7 +21,7 @@ internal typealias InitializeHashFn = (
2021
/**
2122
* SHA-1 hash function implemented using bindings to CRT
2223
*/
23-
public class Sha1 : HashFunction {
24+
public class Sha1 : HashFunction, WithCrt() {
2425
private val sha1 = Sha(::aws_sha1_new)
2526
override fun update(input: ByteArray, offset: Int, length: Int) {
2627
sha1.update(input, offset, length)
@@ -34,7 +35,7 @@ public class Sha1 : HashFunction {
3435
/**
3536
* SHA-256 hash function implemented using bindings to CRT
3637
*/
37-
public class Sha256 : HashFunction {
38+
public class Sha256 : HashFunction, WithCrt() {
3839
private val sha256 = Sha(::aws_sha256_new)
3940
override fun update(input: ByteArray, offset: Int, length: Int) {
4041
sha256.update(input, offset, length)
@@ -49,7 +50,6 @@ internal class Sha(val initializeFn: InitializeHashFn) : HashFunction {
4950
private var hash: CPointer<aws_hash>
5051

5152
init {
52-
CRT.initRuntime { }
5353
hash = initializeHash()
5454
}
5555

0 commit comments

Comments
 (0)