diff --git a/.clang-tidy b/.clang-tidy index c009861f..f601c5df 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,8 @@ --- Checks: '*include*, + bugprone-branch-clone, + cppcoreguidelines-prefer-member-initializer, + bugprone-implicit-widening-of-multiplication-result, modernize-use-nodiscard, cppcoreguidelines-init-variables, misc-const-correctness, diff --git a/src/memory/CopyingHeap.cpp b/src/memory/CopyingHeap.cpp index 243272fe..200efbee 100644 --- a/src/memory/CopyingHeap.cpp +++ b/src/memory/CopyingHeap.cpp @@ -12,22 +12,16 @@ #include "Heap.h" CopyingHeap::CopyingHeap(size_t objectSpaceSize) - : Heap(new CopyingCollector(this)) { - size_t const bufSize = objectSpaceSize; - - currentBuffer = malloc(bufSize); - oldBuffer = malloc(bufSize); - - memset(currentBuffer, 0x0, bufSize); - memset(oldBuffer, 0x0, bufSize); - - currentBufferEnd = (void*)((size_t)currentBuffer + bufSize); - collectionLimit = - (void*)((size_t)currentBuffer + ((size_t)((double)bufSize * 0.9))); - nextFreePosition = currentBuffer; - - oldBufferEnd = (void*)((size_t)oldBuffer + bufSize); - oldBufferIsValid = false; + : Heap(new CopyingCollector(this)), + currentBuffer(malloc(objectSpaceSize)), + oldBuffer(malloc(objectSpaceSize)), + currentBufferEnd((void*)((size_t)currentBuffer + objectSpaceSize)), + collectionLimit((void*)((size_t)currentBuffer + + ((size_t)((double)objectSpaceSize * 0.9)))), + oldBufferEnd((void*)((size_t)oldBuffer + objectSpaceSize)), + nextFreePosition(currentBuffer) { + memset(currentBuffer, 0x0, objectSpaceSize); + memset(oldBuffer, 0x0, objectSpaceSize); } void CopyingHeap::switchBuffers(bool increaseMemory) { diff --git a/src/memory/CopyingHeap.h b/src/memory/CopyingHeap.h index 344cd212..b0102ebb 100644 --- a/src/memory/CopyingHeap.h +++ b/src/memory/CopyingHeap.h @@ -26,5 +26,5 @@ class CopyingHeap : public Heap { void* oldBufferEnd; void* nextFreePosition; - bool oldBufferIsValid; + bool oldBufferIsValid{false}; }; diff --git a/src/memory/GenerationalCollector.cpp b/src/memory/GenerationalCollector.cpp index b172f764..7e3a4e35 100644 --- a/src/memory/GenerationalCollector.cpp +++ b/src/memory/GenerationalCollector.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "../misc/debug.h" @@ -14,13 +15,12 @@ #include "../vmobjects/VMObjectBase.h" #include "GarbageCollector.h" -#define INITIAL_MAJOR_COLLECTION_THRESHOLD (5 * 1024 * 1024) // 5 MB +#define INITIAL_MAJOR_COLLECTION_THRESHOLD \ + ((uintptr_t)5 * 1024U * 1024U) // 5 MB GenerationalCollector::GenerationalCollector(GenerationalHeap* heap) - : GarbageCollector(heap) { - majorCollectionThreshold = INITIAL_MAJOR_COLLECTION_THRESHOLD; - matureObjectsSize = 0; -} + : GarbageCollector(heap), + majorCollectionThreshold(INITIAL_MAJOR_COLLECTION_THRESHOLD) {} static gc_oop_t mark_object(gc_oop_t oop) { // don't process tagged objects @@ -89,8 +89,8 @@ void GenerationalCollector::MinorCollection() { Universe::WalkGlobals(©_if_necessary); // and also all objects that have been detected by the write barriers - for (auto objIter = heap->oldObjsWithRefToYoungObjs->begin(); - objIter != heap->oldObjsWithRefToYoungObjs->end(); + for (auto objIter = heap->oldObjsWithRefToYoungObjs.begin(); + objIter != heap->oldObjsWithRefToYoungObjs.end(); objIter++) { // content of oldObjsWithRefToYoungObjs is not altered while iteration, // because copy_if_necessary returns old objs only -> ignored by @@ -99,7 +99,7 @@ void GenerationalCollector::MinorCollection() { obj->SetGCField(MASK_OBJECT_IS_OLD); obj->WalkObjects(©_if_necessary); } - heap->oldObjsWithRefToYoungObjs->clear(); + heap->oldObjsWithRefToYoungObjs.clear(); heap->nextFreePosition = heap->nursery; } @@ -111,22 +111,21 @@ void GenerationalCollector::MajorCollection() { // now that all objects are marked we can safely delete all allocated // objects that are not marked - auto* survivors = new vector(); - for (auto objIter = heap->allocatedObjects->begin(); - objIter != heap->allocatedObjects->end(); + vector survivors; + for (auto objIter = heap->allocatedObjects.begin(); + objIter != heap->allocatedObjects.end(); objIter++) { AbstractVMObject* obj = *objIter; assert(IsValidObject(obj)); if ((obj->GetGCField() & MASK_OBJECT_IS_MARKED) != 0) { - survivors->push_back(obj); + survivors.push_back(obj); obj->SetGCField(MASK_OBJECT_IS_OLD); } else { heap->FreeObject(obj); } } - delete heap->allocatedObjects; - heap->allocatedObjects = survivors; + heap->allocatedObjects.swap(survivors); } void GenerationalCollector::Collect() { diff --git a/src/memory/GenerationalCollector.h b/src/memory/GenerationalCollector.h index 032a1f5c..7babe3c5 100644 --- a/src/memory/GenerationalCollector.h +++ b/src/memory/GenerationalCollector.h @@ -11,7 +11,7 @@ class GenerationalCollector : public GarbageCollector { private: uintptr_t majorCollectionThreshold; - size_t matureObjectsSize; + size_t matureObjectsSize{0}; void MajorCollection(); void MinorCollection(); }; diff --git a/src/memory/GenerationalHeap.cpp b/src/memory/GenerationalHeap.cpp index d07cf68f..7dadc350 100644 --- a/src/memory/GenerationalHeap.cpp +++ b/src/memory/GenerationalHeap.cpp @@ -15,21 +15,16 @@ using namespace std; GenerationalHeap::GenerationalHeap(size_t objectSpaceSize) - : Heap(new GenerationalCollector(this)) { - // our initial collection limit is 90% of objectSpaceSize - // collectionLimit = objectSpaceSize * 0.9; - - nursery = malloc(objectSpaceSize); - nurserySize = objectSpaceSize; - maxNurseryObjSize = objectSpaceSize / 2; - nursery_end = (size_t)nursery + nurserySize; - matureObjectsSize = 0; + : Heap(new GenerationalCollector(this)), + nursery(malloc(objectSpaceSize)), + nursery_end((size_t)nursery + objectSpaceSize), + nurserySize(objectSpaceSize), maxNurseryObjSize(objectSpaceSize / 2), + nextFreePosition(nursery), + + // our initial collection limit is 90% of objectSpaceSize + collectionLimit((void*)((size_t)nursery + + ((size_t)((double)objectSpaceSize * 0.9)))) { memset(nursery, 0x0, objectSpaceSize); - collectionLimit = - (void*)((size_t)nursery + ((size_t)((double)objectSpaceSize * 0.9))); - nextFreePosition = nursery; - allocatedObjects = new vector(); - oldObjsWithRefToYoungObjs = new vector(); } AbstractVMObject* GenerationalHeap::AllocateNurseryObject(size_t size) { @@ -53,7 +48,7 @@ AbstractVMObject* GenerationalHeap::AllocateMatureObject(size_t size) { ErrorPrint("\nFailed to allocate " + to_string(size) + " Bytes.\n"); Quit(-1); } - allocatedObjects->push_back(newObject); + allocatedObjects.push_back(newObject); matureObjectsSize += size; return newObject; } @@ -61,7 +56,7 @@ AbstractVMObject* GenerationalHeap::AllocateMatureObject(size_t size) { void GenerationalHeap::writeBarrier_OldHolder(VMObjectBase* holder, const vm_oop_t referencedObject) { if (isObjectInNursery(referencedObject)) { - oldObjsWithRefToYoungObjs->push_back((size_t)holder); + oldObjsWithRefToYoungObjs.push_back((size_t)holder); holder->SetGCField(holder->GetGCField() | MASK_SEEN_BY_WRITE_BARRIER); } } diff --git a/src/memory/GenerationalHeap.h b/src/memory/GenerationalHeap.h index 72e41273..37286a32 100644 --- a/src/memory/GenerationalHeap.h +++ b/src/memory/GenerationalHeap.h @@ -35,13 +35,13 @@ class GenerationalHeap : public Heap { size_t nursery_end; size_t nurserySize; size_t maxNurseryObjSize; - size_t matureObjectsSize; + size_t matureObjectsSize{0}; void* nextFreePosition; void writeBarrier_OldHolder(VMObjectBase* holder, vm_oop_t referencedObject); void* collectionLimit; - vector* oldObjsWithRefToYoungObjs; - vector* allocatedObjects; + vector oldObjsWithRefToYoungObjs; + vector allocatedObjects; }; inline bool GenerationalHeap::isObjectInNursery(vm_oop_t obj) { diff --git a/src/vmobjects/VMSymbol.cpp b/src/vmobjects/VMSymbol.cpp index 3d9b8648..eb0dcac2 100644 --- a/src/vmobjects/VMSymbol.cpp +++ b/src/vmobjects/VMSymbol.cpp @@ -45,7 +45,6 @@ VMSymbol::VMSymbol(const size_t length, const char* const str) length), numberOfArgumentsOfSignature( Signature::DetermineNumberOfArguments(str, length)) { - nextCachePos = 0; size_t i = 0; for (; i < length; ++i) { chars[i] = str[i]; diff --git a/src/vmobjects/VMSymbol.h b/src/vmobjects/VMSymbol.h index 7c0099cd..8fb1985f 100644 --- a/src/vmobjects/VMSymbol.h +++ b/src/vmobjects/VMSymbol.h @@ -48,7 +48,7 @@ class VMSymbol : public VMString { private: const uint8_t numberOfArgumentsOfSignature; GCClass* cachedClass_invokable[3]{}; - size_t nextCachePos; + size_t nextCachePos{0}; GCInvokable* cachedInvokable[3]{}; inline VMInvokable* GetCachedInvokable(const VMClass* cls) const {