Forráskód Böngészése

Remove unnecessary vtable for Segment in PageAllocator

Curtis Man 8 éve
szülő
commit
5b3ae8bbbc

+ 16 - 0
lib/Common/EnumHelp.h

@@ -191,6 +191,22 @@
     PREVENT_COPYCONSTRUCT(ClassName); \
     PREVENT_ASSIGN(ClassName);
 
+///----------------------------------------------------------------------------
+///----------------------------------------------------------------------------
+///
+/// macro PREVENT_STANDALONE_HEAPINSTANCE
+///
+/// PREVENT_STANDALONE_HEAPINSTANCE is used within a C++ type definition to 
+/// define and explicitly the new operator, preventing them from accidentally 
+/// being instantiated in the heap by itself.  This also ensures that the 
+/// correct destructor will always be called without using virtual destructors.
+///
+///----------------------------------------------------------------------------
+///----------------------------------------------------------------------------
+
+#define PREVENT_STANDALONE_HEAPINSTANCE() \
+    private: \
+        static void * operator new(size_t size);
 
 ///----------------------------------------------------------------------------
 ///----------------------------------------------------------------------------

+ 4 - 0
lib/Common/Memory/CustomHeap.cpp

@@ -3,6 +3,9 @@
 // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 //-------------------------------------------------------------------------------------------------------
 #include "CommonMemoryPch.h"
+
+#if ENABLE_NATIVE_CODEGEN || DYNAMIC_INTERPRETER_THUNK
+
 #include "Memory/XDataAllocator.h"
 #if defined(_M_ARM)
 #include <wchar.h>
@@ -1215,3 +1218,4 @@ CodePageAllocators<SectionAllocWrapper, PreReservedSectionAllocWrapper>::FreeLoc
 } // namespace CustomHeap
 
 } // namespace Memory
+#endif // ENABLE_NATIVE_CODEGEN || DYNAMIC_INTERPRETER_THUNK

+ 3 - 0
lib/Common/Memory/CustomHeap.h

@@ -3,6 +3,8 @@
 // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 //-------------------------------------------------------------------------------------------------------
 #pragma once
+
+#if ENABLE_NATIVE_CODEGEN || DYNAMIC_INTERPRETER_THUNK
 namespace Memory
 {
 
@@ -576,3 +578,4 @@ BucketId GetBucketForSize(DECLSPEC_GUARD_OVERFLOW size_t bytes);
 void FillDebugBreak(_Out_writes_bytes_all_(byteCount) BYTE* buffer, _In_ size_t byteCount);
 } // namespace CustomHeap
 } // namespace Memory
+#endif

+ 2 - 0
lib/Common/Memory/IdleDecommitPageAllocator.h

@@ -60,7 +60,9 @@ private:
 #endif
 
     friend class PageAllocatorBase<VirtualAllocWrapper>;
+#if ENABLE_NATIVE_CODEGEN
     friend class PageAllocatorBase<PreReservedVirtualAllocWrapper>;
+#endif
 
 #if IDLE_DECOMMIT_ENABLED && DBG
 public:

+ 25 - 5
lib/Common/Memory/PageAllocator.cpp

@@ -21,9 +21,13 @@ SegmentBaseCommon::SegmentBaseCommon(PageAllocatorBaseCommon* allocator)
 
 bool SegmentBaseCommon::IsInPreReservedHeapPageAllocator() const
 {
+#if ENABLE_NATIVE_CODEGEN
     return allocator->GetAllocatorType() == PageAllocatorBaseCommon::AllocatorType::PreReservedVirtualAlloc
 #if ENABLE_OOP_NATIVE_CODEGEN
         || allocator->GetAllocatorType() == PageAllocatorBaseCommon::AllocatorType::PreReservedSectionAlloc
+#endif
+#else
+    return false
 #endif
         ;
 }
@@ -40,6 +44,9 @@ SegmentBase<T>::SegmentBase(PageAllocatorBase<T> * allocator, size_t pageCount,
     , isWriteBarrierAllowed(false)
     , isWriteBarrierEnabled(enableWriteBarrier)
 #endif
+#if DBG
+    , isPageSegment(false)
+#endif // DBG
 {
     this->segmentPageCount = pageCount + secondaryAllocPageCount;
 }
@@ -211,6 +218,9 @@ template<typename T>
 PageSegmentBase<T>::PageSegmentBase(PageAllocatorBase<T> * allocator, bool committed, bool allocated, bool enableWriteBarrier) :
     SegmentBase<T>(allocator, allocator->maxAllocPageCount, enableWriteBarrier), decommitPageCount(0)
 {
+#if DBG
+    this->isPageSegment = true;
+#endif // DBG
     Assert(this->segmentPageCount == allocator->maxAllocPageCount + allocator->secondaryAllocPageCount);
 
     uint maxPageCount = GetMaxPageCount();
@@ -248,6 +258,9 @@ template<typename T>
 PageSegmentBase<T>::PageSegmentBase(PageAllocatorBase<T> * allocator, void* address, uint pageCount, uint committedCount, bool enableWriteBarrier) :
     SegmentBase<T>(allocator, allocator->maxAllocPageCount, enableWriteBarrier), decommitPageCount(0), freePageCount(0)
 {
+#if DBG
+    this->isPageSegment = true;
+#endif // DBG
     this->address = (char*)address;
     this->segmentPageCount = pageCount;
 }
@@ -1871,6 +1884,7 @@ PageAllocatorBase<TVirtualAlloc, TSegment, TPageSegment>::SuspendIdleDecommit()
 #endif
 }
 
+#if ENABLE_OOP_NATIVE_CODEGEN
 template<>
 void
 PageAllocatorBase<SectionAllocWrapper>::SuspendIdleDecommit()
@@ -1883,6 +1897,7 @@ PageAllocatorBase<PreReservedSectionAllocWrapper>::SuspendIdleDecommit()
 {
     Assert(!this->IsIdleDecommitPageAllocator());
 }
+#endif
 
 template<typename TVirtualAlloc, typename TSegment, typename TPageSegment>
 void
@@ -1899,6 +1914,7 @@ PageAllocatorBase<TVirtualAlloc, TSegment, TPageSegment>::ResumeIdleDecommit()
 #endif
 }
 
+#if ENABLE_OOP_NATIVE_CODEGEN
 template<>
 void
 PageAllocatorBase<SectionAllocWrapper>::ResumeIdleDecommit()
@@ -1911,7 +1927,7 @@ PageAllocatorBase<PreReservedSectionAllocWrapper>::ResumeIdleDecommit()
 {
     Assert(!this->IsIdleDecommitPageAllocator());
 }
-
+#endif
 
 template<typename TVirtualAlloc, typename TSegment, typename TPageSegment>
 void
@@ -2932,14 +2948,18 @@ uint PageSegmentBase<T>::GetMaxPageCount()
 namespace Memory
 {
     //Instantiate all the Templates in this class below.
-    template class PageAllocatorBase < PreReservedVirtualAllocWrapper >;
+    
     template class PageAllocatorBase < VirtualAllocWrapper >;
-    template class HeapPageAllocator < PreReservedVirtualAllocWrapper >;
     template class HeapPageAllocator < VirtualAllocWrapper >;
-    template class SegmentBase       < VirtualAllocWrapper > ;
-    template class SegmentBase       < PreReservedVirtualAllocWrapper >;
+    template class SegmentBase       < VirtualAllocWrapper >;
     template class PageSegmentBase   < VirtualAllocWrapper >;
+#if ENABLE_NATIVE_CODEGEN
+    template class PageAllocatorBase < PreReservedVirtualAllocWrapper >;
+    template class HeapPageAllocator < PreReservedVirtualAllocWrapper >;
+    template class SegmentBase       < PreReservedVirtualAllocWrapper >;
     template class PageSegmentBase   < PreReservedVirtualAllocWrapper >;
+#endif
+    
 #if ENABLE_OOP_NATIVE_CODEGEN
     template class PageAllocatorBase < SectionAllocWrapper >;
     template class PageAllocatorBase < PreReservedSectionAllocWrapper >;

+ 30 - 21
lib/Common/Memory/PageAllocator.h

@@ -118,12 +118,15 @@ class PageAllocatorBaseCommon;
 
 class SegmentBaseCommon
 {
+    // Disable create instance of PageAllocatorBaseCommon directly
+protected:
+    SegmentBaseCommon(PageAllocatorBaseCommon* allocator);
+    ~SegmentBaseCommon() {}
+
 protected:
     PageAllocatorBaseCommon* allocator;
 
 public:
-    SegmentBaseCommon(PageAllocatorBaseCommon* allocator);
-    virtual ~SegmentBaseCommon() {}
     bool IsInPreReservedHeapPageAllocator() const;
 };
 
@@ -136,9 +139,10 @@ public:
 template<typename TVirtualAlloc>
 class SegmentBase: public SegmentBaseCommon
 {
+    PREVENT_STANDALONE_HEAPINSTANCE();
 public:
     SegmentBase(PageAllocatorBase<TVirtualAlloc> * allocator, DECLSPEC_GUARD_OVERFLOW size_t pageCount, bool enableWriteBarrier);
-    virtual ~SegmentBase();
+    ~SegmentBase();
 
     size_t GetPageCount() const { return segmentPageCount; }
 
@@ -163,9 +167,9 @@ public:
     bool Initialize(DWORD allocFlags, bool excludeGuardPages);
 
 #if DBG
-    virtual bool IsPageSegment() const
+    bool IsPageSegment() const
     {
-        return false;
+        return isPageSegment;
     }
 #endif
 
@@ -212,6 +216,9 @@ protected:
 
     bool   isWriteBarrierAllowed : 1;
     bool   isWriteBarrierEnabled : 1;
+#if DBG
+    bool   isPageSegment : 1;
+#endif
 };
 
 /*
@@ -228,6 +235,7 @@ protected:
 template<typename TVirtualAlloc>
 class PageSegmentBase : public SegmentBase<TVirtualAlloc>
 {
+    PREVENT_STANDALONE_HEAPINSTANCE();
     typedef SegmentBase<TVirtualAlloc> Base;
 public:
     PageSegmentBase(PageAllocatorBase<TVirtualAlloc> * allocator, bool committed, bool allocated, bool enableWriteBarrier);
@@ -337,13 +345,6 @@ public:
 
     void ChangeSegmentProtection(DWORD protectFlags, DWORD expectedOldProtectFlags);
 
-#if DBG
-    bool IsPageSegment() const override
-    {
-        return true;
-    }
-#endif
-
 //---------- Private members ---------------/
 private:
     void DecommitFreePagesInternal(uint index, uint pageCount);
@@ -382,7 +383,9 @@ private:
     void * segment;
 
     friend class PageAllocatorBase<VirtualAllocWrapper>;
+#if ENABLE_NATIVE_CODEGEN
     friend class PageAllocatorBase<PreReservedVirtualAllocWrapper>;
+#endif
 #if ENABLE_OOP_NATIVE_CODEGEN
     friend class PageAllocatorBase<SectionAllocWrapper>;
     friend class PageAllocatorBase<PreReservedSectionAllocWrapper>;
@@ -435,11 +438,20 @@ private:
 
 class PageAllocatorBaseCommon
 {
+protected:
+    // Disable create instance of PageAllocatorBaseCommon directly
+    PageAllocatorBaseCommon() :
+        virtualAllocator(nullptr),
+        allocatorType(AllocatorType::VirtualAlloc)
+    {}
+    ~PageAllocatorBaseCommon() {}
 public:
     enum class AllocatorType
     {
         VirtualAlloc,
+#if ENABLE_NATIVE_CODEGEN
         PreReservedVirtualAlloc,
+#endif
 #if ENABLE_OOP_NATIVE_CODEGEN
         SectionAlloc,
         PreReservedSectionAlloc
@@ -454,16 +466,12 @@ public:
 protected:
     void* virtualAllocator;
     AllocatorType allocatorType;
-public:
-
-    PageAllocatorBaseCommon() :
-        virtualAllocator(nullptr),
-        allocatorType(AllocatorType::VirtualAlloc)
-    {}
-    virtual ~PageAllocatorBaseCommon() {}
 };
+
 template<> inline PageAllocatorBaseCommon::AllocatorType PageAllocatorBaseCommon::GetAllocatorType<VirtualAllocWrapper>() { return AllocatorType::VirtualAlloc; };
+#if ENABLE_NATIVE_CODEGEN
 template<> inline PageAllocatorBaseCommon::AllocatorType PageAllocatorBaseCommon::GetAllocatorType<PreReservedVirtualAllocWrapper>() { return AllocatorType::PreReservedVirtualAlloc; };
+#endif
 #if ENABLE_OOP_NATIVE_CODEGEN
 template<> inline PageAllocatorBaseCommon::AllocatorType PageAllocatorBaseCommon::GetAllocatorType<SectionAllocWrapper>() { return AllocatorType::SectionAlloc; };
 template<> inline PageAllocatorBaseCommon::AllocatorType PageAllocatorBaseCommon::GetAllocatorType<PreReservedSectionAllocWrapper>() { return AllocatorType::PreReservedSectionAlloc; };
@@ -644,7 +652,7 @@ public:
         bool enableWriteBarrier = false
         );
 
-    virtual ~PageAllocatorBase();
+    ~PageAllocatorBase();
 
     bool IsClosed() const { return isClosed; }
     void Close() { Assert(!isClosed); isClosed = true; }
@@ -998,6 +1006,8 @@ template<typename TVirtualAlloc>
 class HeapPageAllocator : public PageAllocatorBase<TVirtualAlloc>
 {
     typedef PageAllocatorBase<TVirtualAlloc> Base;
+
+    PREVENT_STANDALONE_HEAPINSTANCE();
 public:
     HeapPageAllocator(AllocationPolicyManager * policyManager, bool allocXdata, bool excludeGuardPages, TVirtualAlloc * virtualAllocator, HANDLE processHandle = nullptr);
 
@@ -1018,7 +1028,6 @@ private:
 #if PDATA_ENABLED
     virtual bool CreateSecondaryAllocator(SegmentBase<TVirtualAlloc>* segment, bool committed, SecondaryAllocator** allocator) override;
 #endif
-
 };
 
 }

+ 2 - 1
lib/Common/Memory/SectionAllocWrapper.cpp

@@ -5,6 +5,7 @@
 #include "CommonMemoryPch.h"
 
 #if _WIN32
+#if ENABLE_OOP_NATIVE_CODEGEN
 #include "../Core/DelayLoadLibrary.h"
 
 #ifdef NTDDI_WIN10_RS2
@@ -1005,4 +1006,4 @@ PreReservedSectionAllocWrapper::FreeLocal(LPVOID lpAddress)
 
 } // namespace Memory
 #endif
-
+#endif

+ 2 - 0
lib/Common/Memory/SectionAllocWrapper.h

@@ -5,6 +5,7 @@
 
 #pragma once
 #if _WIN32
+#if ENABLE_OOP_NATIVE_CODEGEN
 namespace Memory
 {
 
@@ -199,3 +200,4 @@ private:
 
 } // namespace Memory
 #endif
+#endif

+ 3 - 0
lib/Common/Memory/VirtualAllocWrapper.cpp

@@ -90,6 +90,7 @@ BOOL VirtualAllocWrapper::Free(LPVOID lpAddress, size_t dwSize, DWORD dwFreeType
     return ret;
 }
 
+#if ENABLE_NATIVE_CODEGEN
 /*
 * class PreReservedVirtualAllocWrapper
 */
@@ -475,6 +476,8 @@ PreReservedVirtualAllocWrapper::Free(LPVOID lpAddress, size_t dwSize, DWORD dwFr
     }
 }
 
+#endif // ENABLE_NATIVE_CODEGEN
+
 #if defined(ENABLE_JIT_CLAMP)
 /*
 * class AutoEnableDynamicCodeGen

+ 3 - 0
lib/Common/Memory/VirtualAllocWrapper.h

@@ -32,6 +32,7 @@ private:
     VirtualAllocWrapper() {}
 };
 
+#if ENABLE_NATIVE_CODEGEN
 /*
 * PreReservedVirtualAllocWrapper class takes care of Reserving a large memory region initially
 * and then committing mem regions for the size requested.
@@ -88,6 +89,8 @@ private:
 #endif
 };
 
+#endif
+
 #if defined(ENABLE_JIT_CLAMP)
 
 class AutoEnableDynamicCodeGen

+ 1 - 1
lib/Runtime/Base/ThreadContext.cpp

@@ -144,7 +144,6 @@ ThreadContext::ThreadContext(AllocationPolicyManager * allocationPolicyManager,
     inlineCacheThreadInfoAllocator(_u("TC-InlineCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
     isInstInlineCacheThreadInfoAllocator(_u("TC-IsInstInlineCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
     equivalentTypeCacheInfoAllocator(_u("TC-EquivalentTypeCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
-    preReservedVirtualAllocator(),
     protoInlineCacheByPropId(&inlineCacheThreadInfoAllocator, 521),
     storeFieldInlineCacheByPropId(&inlineCacheThreadInfoAllocator, 293),
     isInstInlineCacheByFunction(&isInstInlineCacheThreadInfoAllocator, 131),
@@ -160,6 +159,7 @@ ThreadContext::ThreadContext(AllocationPolicyManager * allocationPolicyManager,
     caseInvariantPropertySet(nullptr),
     entryPointToBuiltInOperationIdCache(&threadAlloc, 0),
 #if ENABLE_NATIVE_CODEGEN
+    preReservedVirtualAllocator(),
 #if !FLOATVAR
     codeGenNumberThreadAllocator(nullptr),
     xProcNumberPageSegmentManager(nullptr),

+ 2 - 0
lib/Runtime/Base/ThreadContext.h

@@ -611,7 +611,9 @@ private:
     AllocationPolicyManager * allocationPolicyManager;
 
     JsUtil::ThreadService threadService;
+#if ENABLE_NATIVE_CODEGEN
     PreReservedVirtualAllocWrapper preReservedVirtualAllocator;
+#endif
 
     uint callRootLevel;