From adf80418af10f34276abe33baa4e92ef59b112c6 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Tue, 12 Jan 2021 12:47:00 +0300 Subject: [PATCH] Use common pattern for function-like macro (#4625) --- .../src/legacymm/cpp/CyclicCollector.cpp | 18 +++++----- .../runtime/src/legacymm/cpp/Memory.cpp | 4 +-- kotlin-native/runtime/src/main/cpp/KAssert.h | 34 +++++++++++++------ .../runtime/src/main/cpp/MemorySharedRefs.cpp | 2 +- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/kotlin-native/runtime/src/legacymm/cpp/CyclicCollector.cpp b/kotlin-native/runtime/src/legacymm/cpp/CyclicCollector.cpp index 623abb0a216..754ae3e5df6 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/CyclicCollector.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/CyclicCollector.cpp @@ -129,10 +129,10 @@ class CyclicCollector { public: CyclicCollector() { - CHECK_CALL(pthread_mutex_init(&lock_, nullptr), "Cannot init collector mutex") - CHECK_CALL(pthread_mutex_init(×tampLock_, nullptr), "Cannot init collector timestamp mutex") - CHECK_CALL(pthread_cond_init(&cond_, nullptr), "Cannot init collector condition") - CHECK_CALL(pthread_create(&gcThread_, nullptr, gcWorkerRoutine, this), "Cannot start collector thread") + CHECK_CALL(pthread_mutex_init(&lock_, nullptr), "Cannot init collector mutex"); + CHECK_CALL(pthread_mutex_init(×tampLock_, nullptr), "Cannot init collector timestamp mutex"); + CHECK_CALL(pthread_cond_init(&cond_, nullptr), "Cannot init collector condition"); + CHECK_CALL(pthread_create(&gcThread_, nullptr, gcWorkerRoutine, this), "Cannot start collector thread"); } void clear() { @@ -146,7 +146,7 @@ class CyclicCollector { Locker locker(&lock_); terminateCollector_ = true; if (enabled) shallRunCollector_ = true; - CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") + CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector"); } // TODO: improve waiting for collector termination. while (atomicGet(&terminateCollector_)) {} @@ -173,7 +173,7 @@ class CyclicCollector { KStdUnorderedMap sideRefCounts; int restartCount = 0; while (!terminateCollector_) { - CHECK_CALL(pthread_cond_wait(&cond_, &lock_), "Cannot wait collector condition") + CHECK_CALL(pthread_cond_wait(&cond_, &lock_), "Cannot wait collector condition"); if (!shallRunCollector_) continue; atomicSet(&gcRunning_, 1); restartCount = 0; @@ -325,7 +325,7 @@ class CyclicCollector { // When exiting the worker - we shall collect the cyclic garbage here. if (enabled) { shallRunCollector_ = true; - CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") + CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector"); } currentAliveWorkers_--; } @@ -416,7 +416,7 @@ class CyclicCollector { if (checkIfShallCollect()) { Locker locker(&lock_); shallRunCollector_ = true; - CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") + CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector"); } } @@ -424,7 +424,7 @@ class CyclicCollector { if (atomicGet(&gcRunning_) != 0) return; Locker lock(&lock_); shallRunCollector_ = true; - CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") + CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector"); } void localGC() { diff --git a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp index 512fbf84dce..4a1f63ec95a 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp @@ -1164,7 +1164,7 @@ void freeAggregatingFrozenContainer(ContainerHeader* container) { auto* state = memoryState; RuntimeAssert(isAggregatingFrozenContainer(container), "expected fictitious frozen container"); MEMORY_LOG("%p is fictitious frozen container\n", container); - RuntimeAssert(!container->buffered(), "frozen objects must not participate in GC") + RuntimeAssert(!container->buffered(), "frozen objects must not participate in GC"); #if USE_GC // Forbid finalizerQueue handling. ++state->finalizerQueueSuspendCount; @@ -2056,7 +2056,7 @@ MemoryState* initMemory(bool firstRuntime) { == offsetof(MetaObjHeader, typeInfo_), "Layout mismatch"); - RuntimeAssert(sizeof(FrameOverlay) % sizeof(ObjHeader**) == 0, "Frame overlay should contain only pointers") + RuntimeAssert(sizeof(FrameOverlay) % sizeof(ObjHeader**) == 0, "Frame overlay should contain only pointers"); RuntimeAssert(memoryState == nullptr, "memory state must be clear"); memoryState = konanConstructInstance(); INIT_EVENT(memoryState) diff --git a/kotlin-native/runtime/src/main/cpp/KAssert.h b/kotlin-native/runtime/src/main/cpp/KAssert.h index 0f43d854513..a7c9e789892 100644 --- a/kotlin-native/runtime/src/main/cpp/KAssert.h +++ b/kotlin-native/runtime/src/main/cpp/KAssert.h @@ -25,6 +25,13 @@ #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) +#if KONAN_ENABLE_ASSERT +#define CURRENT_SOURCE_LOCATION __FILE__ ":" TOSTRING(__LINE__) +#else +// Do not generate location strings, when asserts are disabled to reduce code size. +#define CURRENT_SOURCE_LOCATION nullptr +#endif + RUNTIME_NORETURN void RuntimeAssertFailed(const char* location, const char* message, ...); namespace internal { @@ -46,24 +53,31 @@ extern "C" const int KonanNeedDebugInfo; #if KONAN_ENABLE_ASSERT // Use RuntimeAssert() in internal state checks, which could be ignored in production. -#define RuntimeAssert(condition, format, ...) \ -if (KonanNeedDebugInfo && (!(condition))) { \ - RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), format, ##__VA_ARGS__); \ -} +#define RuntimeAssert(condition, format, ...) \ + do { \ + if (KonanNeedDebugInfo && (!(condition))) { \ + RuntimeAssertFailed(CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \ + } \ + } while (false) #else -#define RuntimeAssert(condition, message) +#define RuntimeAssert(condition, format, ...) \ + do { \ + } while (false) #endif // Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead // to program termination. Never compiled out. -#define RuntimeCheck(condition, format, ...) \ - if (!(condition)) { \ - RuntimeAssertFailed(nullptr, format, ##__VA_ARGS__); \ - } +// TODO: Consider using `CURRENT_SOURCE_LOCATION` when `KonanNeedDebugInfo` is `true`. +#define RuntimeCheck(condition, format, ...) \ + do { \ + if (!(condition)) { \ + RuntimeAssertFailed(nullptr, format, ##__VA_ARGS__); \ + } \ + } while (false) #define TODO(...) \ do { \ - ::internal::TODOImpl(__FILE__ ":" TOSTRING(__LINE__), ##__VA_ARGS__); \ + ::internal::TODOImpl(CURRENT_SOURCE_LOCATION, ##__VA_ARGS__); \ } while (false) #endif // RUNTIME_ASSERT_H diff --git a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.cpp b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.cpp index c4a1336e42b..6e046a7323d 100644 --- a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.cpp +++ b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.cpp @@ -177,7 +177,7 @@ void BackRefFromAssociatedObject::releaseRef() { } void BackRefFromAssociatedObject::detach() { - RuntimeAssert(atomicGet(&refCount) == 0, "unexpected refCount") + RuntimeAssert(atomicGet(&refCount) == 0, "unexpected refCount"); obj_ = nullptr; // Handled in addRef/tryAddRef/releaseRef/ref. }