Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: third_party/WebKit/Source/platform/heap/Heap.h

Issue 1676973002: Introduce HeapAllocHooks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: WTF_EXPORT -> PLATFORM_EXPORT Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/heap/Heap.h
diff --git a/third_party/WebKit/Source/platform/heap/Heap.h b/third_party/WebKit/Source/platform/heap/Heap.h
index d60fef7ed4a557f697dbd83f9e767272398c8986..618a3f0f283ba4ae4770f7a68360a5ff6528fae2 100644
--- a/third_party/WebKit/Source/platform/heap/Heap.h
+++ b/third_party/WebKit/Source/platform/heap/Heap.h
@@ -45,6 +45,47 @@
namespace blink {
+class PLATFORM_EXPORT HeapAllocHooks {
+public:
+ // TODO(hajimehoshi): Pass a type name of the allocated object.
+ typedef void AllocationHook(Address, size_t);
+ typedef void FreeHook(Address);
+
+ static void setAllocationHook(AllocationHook* hook) { m_allocationHook = hook; }
+ static void setFreeHook(FreeHook* hook) { m_freeHook = hook; }
+
+ static void allocationHookIfEnabled(Address address, size_t size)
+ {
+ AllocationHook* allocationHook = m_allocationHook;
+ if (UNLIKELY(allocationHook != nullptr))
haraken 2016/02/12 10:29:19 UNLIKELY(!allocationHook)
hajimehoshi 2016/02/15 07:14:46 I guess UNLIKELY(!!allocationHook). Done. UNLIKEL
+ allocationHook(address, size);
+ }
+
+ static void freeHookIfEnabled(Address address)
+ {
+ FreeHook* freeHook = m_freeHook;
+ if (UNLIKELY(freeHook != nullptr))
haraken 2016/02/12 10:29:18 UNLIKELY(!freeHook)
hajimehoshi 2016/02/15 07:14:46 UNLIKELY(!!freeHook). Done.
+ freeHook(address);
+ }
+
+ static void reallocHookIfEnabled(Address oldAddress, Address newAddress, size_t size)
+ {
+ // Report a reallocation as a free followed by an allocation.
+ AllocationHook* allocationHook = m_allocationHook;
+ FreeHook* freeHook = m_freeHook;
+ if (UNLIKELY(allocationHook && freeHook)) {
+ freeHook(oldAddress);
+ allocationHook(newAddress, size);
+ }
+ }
+
+private:
+ // Pointers to hook functions that HeapAlloc will call on allocation and
+ // free if the pointers are non-null.
haraken 2016/02/12 10:29:19 I'd remove this comment.
hajimehoshi 2016/02/15 07:14:46 Done.
+ static AllocationHook* m_allocationHook;
+ static FreeHook* m_freeHook;
+};
+
class CrossThreadPersistentRegion;
template<typename T> class Member;
template<typename T> class WeakMember;
@@ -453,7 +494,9 @@ template<typename T>
Address Heap::allocate(size_t size, bool eagerlySweep)
{
ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state();
- return Heap::allocateOnHeapIndex(state, size, eagerlySweep ? BlinkGC::EagerSweepHeapIndex : Heap::heapIndexForObjectSize(size), GCInfoTrait<T>::index());
+ Address address = Heap::allocateOnHeapIndex(state, size, eagerlySweep ? BlinkGC::EagerSweepHeapIndex : Heap::heapIndexForObjectSize(size), GCInfoTrait<T>::index());
+ HeapAllocHooks::allocationHookIfEnabled(address, size);
+ return address;
}
template<typename T>
@@ -486,6 +529,7 @@ Address Heap::reallocate(void* previous, size_t size)
if (copySize > size)
copySize = size;
memcpy(address, previous, copySize);
+ HeapAllocHooks::reallocHookIfEnabled(static_cast<Address>(previous), address, size);
return address;
}
« no previous file with comments | « no previous file | third_party/WebKit/Source/platform/heap/Heap.cpp » ('j') | third_party/WebKit/Source/platform/heap/HeapPage.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698