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

Unified Diff: third_party/WebKit/Source/wtf/PartitionAlloc.h

Issue 1391933004: [Tracing] Add hook to PartitionAlloc for heap profiling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do check both flags on realloc Created 5 years, 2 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/wtf/PartitionAlloc.h
diff --git a/third_party/WebKit/Source/wtf/PartitionAlloc.h b/third_party/WebKit/Source/wtf/PartitionAlloc.h
index b0e233f4e2d24a442be2936fc08401c1fddc2866..166f06ea8739bf317422667ae941151f9f095cde 100644
--- a/third_party/WebKit/Source/wtf/PartitionAlloc.h
+++ b/third_party/WebKit/Source/wtf/PartitionAlloc.h
@@ -411,6 +411,46 @@ WTF_EXPORT NEVER_INLINE void* partitionReallocGeneric(PartitionRootGeneric*, voi
WTF_EXPORT void partitionDumpStats(PartitionRoot*, const char* partitionName, bool isLightDump, PartitionStatsDumper*);
WTF_EXPORT void partitionDumpStatsGeneric(PartitionRootGeneric*, const char* partitionName, bool isLightDump, PartitionStatsDumper*);
+class WTF_EXPORT PartitionAllocHooks {
+public:
+ typedef void AllocationHook(void* address, size_t);
+ typedef void FreeHook(void* address);
+
+ static void setAllocationHook(AllocationHook* hook) { m_allocationHook = hook; }
+ static void setFreeHook(FreeHook* hook) { m_freeHook = hook; }
+
+ static void allocationHookIfEnabled(void* address, size_t size)
+ {
+ AllocationHook* allocationHook = m_allocationHook;
+ if (UNLIKELY(allocationHook != nullptr))
+ allocationHook(address, size);
+ }
+
+ static void freeHookIfEnabled(void* address)
+ {
+ FreeHook* freeHook = m_freeHook;
+ if (UNLIKELY(freeHook != nullptr))
+ freeHook(address);
+ }
+
+ static void reallocHookIfEnabled(void* oldAddress, void* 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 PartitionAlloc will call on allocation and
+ // free if the pointers are non-null.
+ static AllocationHook* m_allocationHook;
+ static FreeHook* m_freeHook;
+};
+
ALWAYS_INLINE PartitionFreelistEntry* partitionFreelistMask(PartitionFreelistEntry* ptr)
{
// We use bswap on little endian as a fast mask for two reasons:
@@ -629,7 +669,9 @@ ALWAYS_INLINE void* partitionAlloc(PartitionRoot* root, size_t size)
ASSERT(index < root->numBuckets);
ASSERT(size == index << kBucketShift);
PartitionBucket* bucket = &root->buckets()[index];
- return partitionBucketAlloc(root, 0, size, bucket);
+ void* result = partitionBucketAlloc(root, 0, size, bucket);
+ PartitionAllocHooks::allocationHookIfEnabled(result, size);
+ return result;
#endif // defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
}
@@ -672,6 +714,7 @@ ALWAYS_INLINE void partitionFree(void* ptr)
ASSERT(partitionPointerIsValid(ptr));
PartitionPage* page = partitionPointerToPage(ptr);
partitionFreeWithPage(ptr, page);
+ PartitionAllocHooks::freeHookIfEnabled(ptr);
Jens Widell 2015/10/21 13:56:51 In debug, |ptr| here doesn't match what we called
Ruud van Asseldonk 2015/10/21 15:00:27 The pointers in the allocation hook and free hook
#endif
}
@@ -701,6 +744,7 @@ ALWAYS_INLINE void* partitionAllocGenericFlags(PartitionRootGeneric* root, int f
spinLockLock(&root->lock);
void* ret = partitionBucketAlloc(root, flags, size, bucket);
spinLockUnlock(&root->lock);
+ PartitionAllocHooks::allocationHookIfEnabled(ret, size);
Jens Widell 2015/10/21 13:56:51 Note that |size| here isn't necessarily the size o
Ruud van Asseldonk 2015/10/21 15:00:27 Good point. I want to record the requested size. I
return ret;
#endif
}
@@ -726,6 +770,7 @@ ALWAYS_INLINE void partitionFreeGeneric(PartitionRootGeneric* root, void* ptr)
spinLockLock(&root->lock);
partitionFreeWithPage(ptr, page);
spinLockUnlock(&root->lock);
+ PartitionAllocHooks::freeHookIfEnabled(ptr);
Jens Widell 2015/10/21 13:56:51 Same kCookieSize adjustment issue here.
Ruud van Asseldonk 2015/10/21 15:00:27 Fixed.
#endif
}

Powered by Google App Engine
This is Rietveld 408576698