Skip to content

Commit a68e162

Browse files
committed
Use memScope to scope String to C string copies
1 parent 6c1423a commit a68e162

14 files changed

+109
-101
lines changed

couchbase-lite-ee/src/linuxMingwMain/ee/kotbase/Collection.linuxMingw.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ internal fun Collection.createIndexImpl(name: String, config: IndexConfiguration
3131
is ValueIndexConfiguration -> CBLCollection_CreateValueIndex(
3232
actual,
3333
name.toFLString(this),
34-
config.actual,
34+
config.actual(this),
3535
error
3636
)
3737
is FullTextIndexConfiguration -> CBLCollection_CreateFullTextIndex(
3838
actual,
3939
name.toFLString(this),
40-
config.actual,
40+
config.actual(this),
4141
error
4242
)
4343
is ArrayIndexConfiguration -> CBLCollection_CreateArrayIndex(
4444
actual,
4545
name.toFLString(this),
46-
config.actual,
46+
config.actual(this),
4747
error
4848
)
4949
is VectorIndexConfiguration -> CBLCollection_CreateVectorIndex(
5050
actual,
5151
name.toFLString(this),
52-
config.actual,
52+
config.actual(this),
5353
error
5454
)
5555
}

couchbase-lite-ee/src/linuxMingwMain/ee/kotbase/VectorIndexConfiguration.linuxMingw.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package kotbase
1717

18+
import kotlinx.cinterop.AutofreeScope
1819
import kotlinx.cinterop.CValue
1920
import kotlinx.cinterop.cValue
2021
import kotlinx.cinterop.convert
22+
import kotlinx.cinterop.cstr
2123
import libcblite.*
22-
import platform.posix.strdup
2324
import platform.posix.strlen
2425

2526
public actual class VectorIndexConfiguration
@@ -103,21 +104,20 @@ public actual constructor(
103104
return this
104105
}
105106

106-
internal val actual: CValue<CBLVectorIndexConfiguration>
107-
get() {
108-
val exp = expression
109-
return cValue {
110-
centroids = this@VectorIndexConfiguration.centroids.convert()
111-
dimensions = this@VectorIndexConfiguration.dimensions.convert()
112-
encoding = this@VectorIndexConfiguration.encoding.actual
113-
expression.buf = strdup(exp)
114-
expression.size = strlen(exp)
115-
expressionLanguage = kCBLN1QLLanguage
116-
isLazy = this@VectorIndexConfiguration.isLazy
117-
maxTrainingSize = this@VectorIndexConfiguration.maxTrainingSize.convert()
118-
metric = this@VectorIndexConfiguration.metric.actual
119-
minTrainingSize = this@VectorIndexConfiguration.minTrainingSize.convert()
120-
numProbes = this@VectorIndexConfiguration.numProbes.convert()
121-
}
107+
internal fun actual(scope: AutofreeScope): CValue<CBLVectorIndexConfiguration> {
108+
val exp = expression
109+
return cValue {
110+
centroids = this@VectorIndexConfiguration.centroids.convert()
111+
dimensions = this@VectorIndexConfiguration.dimensions.convert()
112+
encoding = this@VectorIndexConfiguration.encoding.actual
113+
expression.buf = exp.cstr.getPointer(scope)
114+
expression.size = strlen(exp)
115+
expressionLanguage = kCBLN1QLLanguage
116+
isLazy = this@VectorIndexConfiguration.isLazy
117+
maxTrainingSize = this@VectorIndexConfiguration.maxTrainingSize.convert()
118+
metric = this@VectorIndexConfiguration.metric.actual
119+
minTrainingSize = this@VectorIndexConfiguration.minTrainingSize.convert()
120+
numProbes = this@VectorIndexConfiguration.numProbes.convert()
122121
}
122+
}
123123
}

couchbase-lite/src/linuxMingwMain/ce/kotbase/Collection.linuxMingw.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ internal fun Collection.createIndexImpl(name: String, config: IndexConfiguration
2929
is ValueIndexConfiguration -> CBLCollection_CreateValueIndex(
3030
actual,
3131
name.toFLString(this),
32-
config.actual,
32+
config.actual(this),
3333
error
3434
)
3535
is FullTextIndexConfiguration -> CBLCollection_CreateFullTextIndex(
3636
actual,
3737
name.toFLString(this),
38-
config.actual,
38+
config.actual(this),
3939
error
4040
)
4141
is ArrayIndexConfiguration -> CBLCollection_CreateArrayIndex(
4242
actual,
4343
name.toFLString(this),
44-
config.actual,
44+
config.actual(this),
4545
error
4646
)
4747
}

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/ArrayIndexConfiguration.linuxMingw.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package kotbase
1717

18+
import kotlinx.cinterop.AutofreeScope
1819
import kotlinx.cinterop.CValue
1920
import kotlinx.cinterop.cValue
21+
import kotlinx.cinterop.cstr
2022
import libcblite.CBLArrayIndexConfiguration
2123
import libcblite.kCBLN1QLLanguage
22-
import platform.posix.strdup
2324
import platform.posix.strlen
2425

2526
public actual class ArrayIndexConfiguration
@@ -41,16 +42,15 @@ public actual constructor(
4142
}
4243
}
4344

44-
internal val actual: CValue<CBLArrayIndexConfiguration>
45-
get() {
46-
val exp = expressions.joinToString(separator = ",")
47-
val path = path
48-
return cValue {
49-
expressionLanguage = kCBLN1QLLanguage
50-
expressions.buf = strdup(exp)
51-
expressions.size = strlen(exp)
52-
this.path.buf = strdup(path)
53-
this.path.size = strlen(path)
54-
}
45+
internal fun actual(scope: AutofreeScope): CValue<CBLArrayIndexConfiguration> {
46+
val exp = expressions.joinToString(separator = ",")
47+
val path = path
48+
return cValue {
49+
expressionLanguage = kCBLN1QLLanguage
50+
expressions.buf = exp.cstr.getPointer(scope)
51+
expressions.size = strlen(exp)
52+
this.path.buf = path.cstr.getPointer(scope)
53+
this.path.size = strlen(path)
5554
}
55+
}
5656
}

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/Collection.linuxMingw.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,13 @@ internal constructor(
357357
is ValueIndex -> CBLCollection_CreateValueIndex(
358358
actual,
359359
name.toFLString(this),
360-
index.actual,
360+
index.actual(this),
361361
error
362362
)
363363
is FullTextIndex -> CBLCollection_CreateFullTextIndex(
364364
actual,
365365
name.toFLString(this),
366-
index.actual,
366+
index.actual(this),
367367
error
368368
)
369369
else -> error("Unhandled Index type ${index::class}")

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/Database.linuxMingw.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,13 @@ internal constructor(
723723
is ValueIndex -> CBLDatabase_CreateValueIndex(
724724
actual,
725725
name.toFLString(this),
726-
index.actual,
726+
index.actual(this),
727727
error
728728
)
729729
is FullTextIndex -> CBLDatabase_CreateFullTextIndex(
730730
actual,
731731
name.toFLString(this),
732-
index.actual,
732+
index.actual(this),
733733
error
734734
)
735735
else -> error("Unhandled Index type ${index::class}")

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/FileLogger.linuxMingw.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public actual class FileLogger internal constructor() : Logger {
3333
if (value != null) {
3434
SystemFileSystem.createDirectories(Path(value.directory), false)
3535
}
36-
val actual = value?.getActual(level) ?: LogFileConfiguration.getNullActual()
37-
wrapCBLError { error ->
38-
CBLLog_SetFileConfig(actual, error)
36+
memScoped {
37+
val actual = value?.getActual(level, this) ?: LogFileConfiguration.getNullActual()
38+
wrapCBLError { error ->
39+
CBLLog_SetFileConfig(actual, error)
40+
}
3941
}
4042
}
4143

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/FullTextIndex.linuxMingw.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
package kotbase
1717

1818
import kotbase.internal.JsonUtils
19+
import kotlinx.cinterop.AutofreeScope
1920
import kotlinx.cinterop.CValue
2021
import kotlinx.cinterop.cValue
22+
import kotlinx.cinterop.cstr
2123
import libcblite.CBLFullTextIndexConfiguration
2224
import libcblite.kCBLJSONLanguage
23-
import platform.posix.strdup
2425
import platform.posix.strlen
2526

2627
public actual class FullTextIndex
@@ -50,17 +51,16 @@ internal constructor(private val items: List<FullTextIndexItem>) : Index() {
5051
return JsonUtils.toJson(data)
5152
}
5253

53-
internal val actual: CValue<CBLFullTextIndexConfiguration>
54-
get() {
55-
val json = getJson()
56-
val lang = language
57-
return cValue {
58-
expressionLanguage = kCBLJSONLanguage
59-
expressions.buf = strdup(json)
60-
expressions.size = strlen(json)
61-
language.buf = lang?.let { strdup(it) }
62-
language.size = lang?.let { strlen(it) } ?: 0U
63-
ignoreAccents = isIgnoringAccents
64-
}
54+
internal fun actual(scope: AutofreeScope): CValue<CBLFullTextIndexConfiguration> {
55+
val json = getJson()
56+
val lang = language
57+
return cValue {
58+
expressionLanguage = kCBLJSONLanguage
59+
expressions.buf = json.cstr.getPointer(scope)
60+
expressions.size = strlen(json)
61+
language.buf = lang?.cstr?.getPointer(scope)
62+
language.size = lang?.let { strlen(it) } ?: 0U
63+
ignoreAccents = isIgnoringAccents
6564
}
65+
}
6666
}

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/FullTextIndexConfiguration.linuxMingw.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package kotbase
1717

18+
import kotlinx.cinterop.AutofreeScope
1819
import kotlinx.cinterop.CValue
1920
import kotlinx.cinterop.cValue
21+
import kotlinx.cinterop.cstr
2022
import libcblite.CBLFullTextIndexConfiguration
2123
import libcblite.kCBLN1QLLanguage
22-
import platform.posix.strdup
2324
import platform.posix.strlen
2425

2526
public actual class FullTextIndexConfiguration
@@ -65,22 +66,21 @@ public actual constructor(
6566

6667
public actual var where: String? = where
6768

68-
internal val actual: CValue<CBLFullTextIndexConfiguration>
69-
get() {
70-
val exp = expressions.joinToString(separator = ",")
71-
val lang = language
72-
val whr = where
73-
return cValue {
74-
expressionLanguage = kCBLN1QLLanguage
75-
expressions.buf = strdup(exp)
76-
expressions.size = strlen(exp)
77-
language.buf = lang?.let { strdup(it) }
78-
language.size = lang?.let { strlen(it) } ?: 0U
79-
where.buf = whr?.let { strdup(it) }
80-
where.size = whr?.let { strlen(it) } ?: 0U
81-
ignoreAccents = isIgnoringAccents
82-
}
69+
internal fun actual(scope: AutofreeScope): CValue<CBLFullTextIndexConfiguration> {
70+
val exp = expressions.joinToString(separator = ",")
71+
val lang = language
72+
val whr = where
73+
return cValue {
74+
expressionLanguage = kCBLN1QLLanguage
75+
expressions.buf = exp.cstr.getPointer(scope)
76+
expressions.size = strlen(exp)
77+
language.buf = lang?.cstr?.getPointer(scope)
78+
language.size = lang?.let { strlen(it) } ?: 0U
79+
where.buf = whr?.cstr?.getPointer(scope)
80+
where.size = whr?.let { strlen(it) } ?: 0U
81+
ignoreAccents = isIgnoringAccents
8382
}
83+
}
8484

8585
internal actual companion object
8686
}

couchbase-lite/src/linuxMingwMain/kotlin/kotbase/LogFileConfiguration.linuxMingw.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package kotbase
1717

18+
import kotlinx.cinterop.AutofreeScope
1819
import kotlinx.cinterop.CValue
1920
import kotlinx.cinterop.cValue
2021
import kotlinx.cinterop.convert
22+
import kotlinx.cinterop.cstr
2123
import libcblite.CBLLogFileConfiguration
22-
import platform.posix.strdup
2324
import platform.posix.strlen
2425

2526
public actual class LogFileConfiguration
@@ -84,11 +85,11 @@ public actual constructor(public actual val directory: String) {
8485

8586
override fun hashCode(): Int = directory.hashCode()
8687

87-
internal fun getActual(level: LogLevel): CValue<CBLLogFileConfiguration> {
88+
internal fun getActual(level: LogLevel, scope: AutofreeScope): CValue<CBLLogFileConfiguration> {
8889
readonly = true
8990
return cValue {
9091
with(directory) {
91-
buf = strdup(this@LogFileConfiguration.directory)
92+
buf = this@LogFileConfiguration.directory.cstr.getPointer(scope)
9293
size = strlen(this@LogFileConfiguration.directory)
9394
}
9495
this.level = level.actual

0 commit comments

Comments
 (0)