[K/N] Introduce intrinsics that atomically update array elements

Supported atomic update of elements for IntArray, LongArray and Array<T>
See KT-58360

Merge-request: KT-MR-11020
Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
This commit is contained in:
mvicsokolova
2023-07-12 14:32:36 +00:00
committed by Space Team
parent 20c53fc15d
commit d9fa9c1b3b
13 changed files with 598 additions and 54 deletions
@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
@@ -114,4 +115,22 @@ void Kotlin_CPointer_CopyMemory(KNativePtr to, KNativePtr from, KInt count) {
memcpy(to, from, count);
}
RUNTIME_NOTHROW RUNTIME_PURE KRef* Kotlin_arrayGetElementAddress(KRef array, KInt index) {
ArrayHeader* arr = array->array();
RuntimeAssert(index >= 0 && static_cast<uint32_t>(index) < arr->count_, "Index %" PRId32 " must be in [0, %" PRIu32 ")", index, arr->count_);
return ArrayAddressOfElementAt(arr, index);
}
RUNTIME_NOTHROW RUNTIME_PURE KInt* Kotlin_intArrayGetElementAddress(KRef array, KInt index) {
ArrayHeader* arr = array->array();
RuntimeAssert(index >= 0 && static_cast<uint32_t>(index) < arr->count_, "Index %" PRId32 " must be in [0, %" PRIu32 ")", index, arr->count_);
return IntArrayAddressOfElementAt(arr, index);
}
RUNTIME_NOTHROW RUNTIME_PURE KLong* Kotlin_longArrayGetElementAddress(KRef array, KInt index) {
ArrayHeader* arr = array->array();
RuntimeAssert(index >= 0 && static_cast<uint32_t>(index) < arr->count_, "Index %" PRId32 " must be in [0, %" PRIu32 ")", index, arr->count_);
return LongArrayAddressOfElementAt(arr, index);
}
} // extern "C"
@@ -62,6 +62,14 @@ inline const KInt* IntArrayAddressOfElementAt(const ArrayHeader* obj, KInt index
return AddressOfElementAt<KInt>(obj, index);
}
inline KLong* LongArrayAddressOfElementAt(ArrayHeader* obj, KInt index) {
return AddressOfElementAt<KLong>(obj, index);
}
inline const KLong* LongArrayAddressOfElementAt(const ArrayHeader* obj, KInt index) {
return AddressOfElementAt<KLong>(obj, index);
}
// Consider aligning of base to sizeof(T).
template <typename T>
inline T* PrimitiveArrayAddressOfElementAt(ArrayHeader* obj, KInt index) {
@@ -90,6 +98,9 @@ void Kotlin_io_Console_println0();
void Kotlin_io_Console_println0ToStdErr();
void Kotlin_NativePtrArray_set(KRef thiz, KInt index, KNativePtr value);
KNativePtr Kotlin_NativePtrArray_get(KConstRef thiz, KInt index);
RUNTIME_NOTHROW RUNTIME_PURE KRef* Kotlin_arrayGetElementAddress(KRef array, KInt index);
RUNTIME_NOTHROW RUNTIME_PURE KInt* Kotlin_intArrayGetElementAddress(KRef array, KInt index);
RUNTIME_NOTHROW RUNTIME_PURE KLong* Kotlin_longArrayGetElementAddress(KRef array, KInt index);
#ifdef __cplusplus
}