diff --git a/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.cpp b/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.cpp index a5dfedc0571..e6dc7c7b731 100644 --- a/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.cpp +++ b/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.cpp @@ -119,6 +119,17 @@ void CustomAllocator::PrepareForGC() noexcept { extraObjectPage_ = nullptr; } +// static +size_t CustomAllocator::GetAllocatedHeapSize(ObjHeader* object) noexcept { + RuntimeAssert(object->heap(), "Object must be a heap object"); + const auto* typeInfo = object->type_info(); + if (typeInfo->IsArray()) { + return ArrayAllocatedDataSize(typeInfo, object->array()->count_); + } else { + return ObjectAllocatedDataSize(typeInfo); + } +} + uint8_t* CustomAllocator::Allocate(uint64_t size) noexcept { gcScheduler_.OnSafePointAllocation(size); CustomAllocDebug("CustomAllocator::Allocate(%" PRIu64 ")", size); diff --git a/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.hpp b/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.hpp index 5b058ca013a..cd0a73673a4 100644 --- a/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.hpp +++ b/kotlin-native/runtime/src/custom_alloc/cpp/CustomAllocator.hpp @@ -34,6 +34,8 @@ public: void PrepareForGC() noexcept; + static size_t GetAllocatedHeapSize(ObjHeader* object) noexcept; + private: uint8_t* Allocate(uint64_t cellCount) noexcept; uint8_t* AllocateInSingleObjectPage(uint64_t cellCount) noexcept; diff --git a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.cpp b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.cpp index a51b4b2c68c..4fc03d5d0e6 100644 --- a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.cpp +++ b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.cpp @@ -105,13 +105,14 @@ NO_EXTERNAL_CALLS_CHECK void gc::ConcurrentMarkAndSweep::ThreadData::OnSuspendFo gc::Mark(handle, markQueue); } +#ifndef CUSTOM_ALLOCATOR gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep( mm::ObjectFactory& objectFactory, GCScheduler& gcScheduler) noexcept : -#ifndef CUSTOM_ALLOCATOR objectFactory_(objectFactory), gcScheduler_(gcScheduler), finalizerProcessor_(std_support::make_unique([this](int64_t epoch) { #else +gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(GCScheduler& gcScheduler) noexcept : gcScheduler_(gcScheduler), finalizerProcessor_(std_support::make_unique([this](int64_t epoch) { #endif GCHandle::getByEpoch(epoch).finalizersDone(); diff --git a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.hpp b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.hpp index e26d9b972a4..c2bc62ed568 100644 --- a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.hpp +++ b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMarkAndSweep.hpp @@ -108,7 +108,11 @@ public: using Allocator = ThreadData::Allocator; +#ifdef CUSTOM_ALLOCATOR + explicit ConcurrentMarkAndSweep(GCScheduler& scheduler) noexcept; +#else ConcurrentMarkAndSweep(mm::ObjectFactory& objectFactory, GCScheduler& scheduler) noexcept; +#endif ~ConcurrentMarkAndSweep(); void StartFinalizerThreadIfNeeded() noexcept; diff --git a/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.cpp b/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.cpp index 72e6c82715f..049e173e819 100644 --- a/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.cpp +++ b/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.cpp @@ -88,21 +88,41 @@ gc::GC::~GC() = default; // static size_t gc::GC::GetAllocatedHeapSize(ObjHeader* object) noexcept { +#ifdef CUSTOM_ALLOCATOR + return alloc::CustomAllocator::GetAllocatedHeapSize(object); +#else return mm::ObjectFactory::GetAllocatedHeapSize(object); +#endif } size_t gc::GC::GetHeapObjectsCountUnsafe() const noexcept { +#ifndef CUSTOM_ALLOCATOR return impl_->objectFactory().GetObjectsCountUnsafe(); +#else + return 0; +#endif } size_t gc::GC::GetTotalHeapObjectsSizeUnsafe() const noexcept { +#ifndef CUSTOM_ALLOCATOR return impl_->objectFactory().GetTotalObjectsSizeUnsafe(); +#else + return 0; +#endif } size_t gc::GC::GetExtraObjectsCountUnsafe() const noexcept { +#ifndef CUSTOM_ALLOCATOR return mm::GlobalData::Instance().extraObjectDataFactory().GetSizeUnsafe(); +#else + return 0; +#endif } size_t gc::GC::GetTotalExtraObjectsSizeUnsafe() const noexcept { +#ifndef CUSTOM_ALLOCATOR return mm::GlobalData::Instance().extraObjectDataFactory().GetTotalObjectsSizeUnsafe(); +#else + return 0; +#endif } size_t gc::GC::GetTotalHeapObjectsSizeBytes() const noexcept { @@ -115,7 +135,9 @@ gc::GCSchedulerConfig& gc::GC::gcSchedulerConfig() noexcept { void gc::GC::ClearForTests() noexcept { impl_->gc().StopFinalizerThreadIfRunning(); +#ifndef CUSTOM_ALLOCATOR impl_->objectFactory().ClearForTests(); +#endif GCHandle::ClearForTests(); } diff --git a/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.hpp b/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.hpp index 345dfd43a57..86499eaa0cc 100644 --- a/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.hpp +++ b/kotlin-native/runtime/src/gc/cms/cpp/GCImpl.hpp @@ -20,14 +20,22 @@ using GCImpl = ConcurrentMarkAndSweep; class GC::Impl : private Pinned { public: +#ifdef CUSTOM_ALLOCATOR + Impl() noexcept : gc_(gcScheduler_) {} +#else Impl() noexcept : gc_(objectFactory_, gcScheduler_) {} +#endif +#ifndef CUSTOM_ALLOCATOR mm::ObjectFactory& objectFactory() noexcept { return objectFactory_; } +#endif GCScheduler& gcScheduler() noexcept { return gcScheduler_; } GCImpl& gc() noexcept { return gc_; } private: +#ifndef CUSTOM_ALLOCATOR mm::ObjectFactory objectFactory_; +#endif GCScheduler gcScheduler_; GCImpl gc_; };