Index: third_party/tcmalloc/chromium/src/malloc_hook.cc |
diff --git a/third_party/tcmalloc/chromium/src/malloc_hook.cc b/third_party/tcmalloc/chromium/src/malloc_hook.cc |
index f6af7d816179e91afabcc6838ad5fbb99ec1791e..5d6b3366d66ab08e1d3abdd02427957aea40714e 100644 |
--- a/third_party/tcmalloc/chromium/src/malloc_hook.cc |
+++ b/third_party/tcmalloc/chromium/src/malloc_hook.cc |
@@ -46,12 +46,11 @@ |
#include <stdint.h> |
#endif |
#include <algorithm> |
-#include "base/basictypes.h" |
#include "base/logging.h" |
#include "base/spinlock.h" |
#include "maybe_threads.h" |
#include "malloc_hook-inl.h" |
-#include <google/malloc_hook.h> |
+#include <gperftools/malloc_hook.h> |
// This #ifdef should almost never be set. Set NO_TCMALLOC_SAMPLES if |
// you're porting to a system where you really can't get a stacktrace. |
@@ -59,7 +58,7 @@ |
// We use #define so code compiles even if you #include stacktrace.h somehow. |
# define GetStackTrace(stack, depth, skip) (0) |
#else |
-# include <google/stacktrace.h> |
+# include <gperftools/stacktrace.h> |
#endif |
// __THROW is defined in glibc systems. It means, counter-intuitively, |
@@ -204,14 +203,8 @@ static SpinLock hooklist_spinlock(base::LINKER_INITIALIZED); |
template <typename T> |
bool HookList<T>::Add(T value_as_t) { |
- // Note: we need to check this _before_ reinterpret_cast, since |
- // reinterpret_cast may include random junk from memory. |
- if (value_as_t == 0) { |
- return false; |
- } |
- AtomicWord value = reinterpret_cast<const AtomicWord&>(value_as_t); |
+ AtomicWord value = bit_cast<AtomicWord>(value_as_t); |
if (value == 0) { |
- // This should not actually happen, but just to be sure... |
return false; |
} |
SpinLockHolder l(&hooklist_spinlock); |
@@ -240,8 +233,7 @@ bool HookList<T>::Remove(T value_as_t) { |
SpinLockHolder l(&hooklist_spinlock); |
AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); |
int index = 0; |
- // Note: we need to cast back to T since T may be smaller than AtomicWord. |
- while (index < hooks_end && value_as_t != reinterpret_cast<T>( |
+ while (index < hooks_end && value_as_t != bit_cast<T>( |
base::subtle::Acquire_Load(&priv_data[index]))) { |
++index; |
} |
@@ -268,7 +260,7 @@ int HookList<T>::Traverse(T* output_array, int n) const { |
for (int i = 0; i < hooks_end && n > 0; ++i) { |
AtomicWord data = base::subtle::Acquire_Load(&priv_data[i]); |
if (data != 0) { |
- *output_array++ = reinterpret_cast<const T&>(data); |
+ *output_array++ = bit_cast<T>(data); |
++actual_hooks_end; |
--n; |
} |
@@ -283,7 +275,7 @@ int HookList<T>::Traverse(T* output_array, int n) const { |
// Explicit instantiation for malloc_hook_test.cc. This ensures all the methods |
// are instantiated. |
-template class HookList<MallocHook::NewHook>; |
+template struct HookList<MallocHook::NewHook>; |
HookList<MallocHook::NewHook> new_hooks_ = |
INIT_HOOK_LIST_WITH_VALUE(&InitialNewHook); |
@@ -698,191 +690,17 @@ extern "C" int MallocHook_GetCallerStackTrace(void** result, int max_depth, |
#endif |
} |
-// On Linux/x86, we override mmap/munmap/mremap/sbrk |
-// and provide support for calling the related hooks. |
-// |
-// We define mmap() and mmap64(), which somewhat reimplements libc's mmap |
-// syscall stubs. Unfortunately libc only exports the stubs via weak symbols |
-// (which we're overriding with our mmap64() and mmap() wrappers) so we can't |
-// just call through to them. |
- |
- |
-#if defined(__linux) && \ |
- (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
-#include <unistd.h> |
-#include <syscall.h> |
-#include <sys/mman.h> |
-#include <errno.h> |
-#include "base/linux_syscall_support.h" |
- |
-// The x86-32 case and the x86-64 case differ: |
-// 32b has a mmap2() syscall, 64b does not. |
-// 64b and 32b have different calling conventions for mmap(). |
-#if defined(__x86_64__) || defined(__PPC64__) |
- |
-static inline void* do_mmap64(void *start, size_t length, |
- int prot, int flags, |
- int fd, __off64_t offset) __THROW { |
- return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset); |
-} |
- |
-#elif defined(__i386__) || defined(__PPC__) |
- |
-static inline void* do_mmap64(void *start, size_t length, |
- int prot, int flags, |
- int fd, __off64_t offset) __THROW { |
- void *result; |
- |
- // Try mmap2() unless it's not supported |
- static bool have_mmap2 = true; |
- if (have_mmap2) { |
- static int pagesize = 0; |
- if (!pagesize) pagesize = getpagesize(); |
- |
- // Check that the offset is page aligned |
- if (offset & (pagesize - 1)) { |
- result = MAP_FAILED; |
- errno = EINVAL; |
- goto out; |
- } |
- |
- result = (void *)syscall(SYS_mmap2, |
- start, length, prot, flags, fd, |
- (off_t) (offset / pagesize)); |
- if (result != MAP_FAILED || errno != ENOSYS) goto out; |
- |
- // We don't have mmap2() after all - don't bother trying it in future |
- have_mmap2 = false; |
- } |
- |
- if (((off_t)offset) != offset) { |
- // If we're trying to map a 64-bit offset, fail now since we don't |
- // have 64-bit mmap() support. |
- result = MAP_FAILED; |
- errno = EINVAL; |
- goto out; |
- } |
- |
- { |
- // Fall back to old 32-bit offset mmap() call |
- // Old syscall interface cannot handle six args, so pass in an array |
- int32 args[6] = { (int32) start, length, prot, flags, fd, (off_t) offset }; |
- result = (void *)syscall(SYS_mmap, args); |
- } |
- out: |
- return result; |
-} |
- |
-# endif // defined(__x86_64__) |
- |
-// We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook |
-// calls right into mmap and mmap64, so that the stack frames in the caller's |
-// stack are at the same offsets for all the calls of memory allocating |
-// functions. |
- |
-// Put all callers of MallocHook::Invoke* in this module into |
-// malloc_hook section, |
-// so that MallocHook::GetCallerStackTrace can function accurately: |
- |
-// Make sure mmap doesn't get #define'd away by <sys/mman.h> |
-#undef mmap |
- |
-extern "C" { |
- void* mmap64(void *start, size_t length, int prot, int flags, |
- int fd, __off64_t offset ) __THROW |
- ATTRIBUTE_SECTION(malloc_hook); |
- void* mmap(void *start, size_t length,int prot, int flags, |
- int fd, off_t offset) __THROW |
- ATTRIBUTE_SECTION(malloc_hook); |
- int munmap(void* start, size_t length) __THROW |
- ATTRIBUTE_SECTION(malloc_hook); |
- void* mremap(void* old_addr, size_t old_size, size_t new_size, |
- int flags, ...) __THROW |
- ATTRIBUTE_SECTION(malloc_hook); |
- void* sbrk(std::ptrdiff_t increment) __THROW |
- ATTRIBUTE_SECTION(malloc_hook); |
-} |
- |
-extern "C" void* mmap64(void *start, size_t length, int prot, int flags, |
- int fd, __off64_t offset) __THROW { |
- MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
- void *result; |
- if (!MallocHook::InvokeMmapReplacement( |
- start, length, prot, flags, fd, offset, &result)) { |
- result = do_mmap64(start, length, prot, flags, fd, offset); |
- } |
- MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
- return result; |
-} |
- |
-#if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
+// On systems where we know how, we override mmap/munmap/mremap/sbrk |
+// to provide support for calling the related hooks (in addition, |
+// of course, to doing what these functions normally do). |
-extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
- int fd, off_t offset) __THROW { |
- MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
- void *result; |
- if (!MallocHook::InvokeMmapReplacement( |
- start, length, prot, flags, fd, offset, &result)) { |
- result = do_mmap64(start, length, prot, flags, fd, |
- static_cast<size_t>(offset)); // avoid sign extension |
- } |
- MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
- return result; |
-} |
+#if defined(__linux) |
+# include "malloc_hook_mmap_linux.h" |
-#endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
+#elif defined(__FreeBSD__) |
+# include "malloc_hook_mmap_freebsd.h" |
-extern "C" int munmap(void* start, size_t length) __THROW { |
- MallocHook::InvokeMunmapHook(start, length); |
- int result; |
- if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
- result = syscall(SYS_munmap, start, length); |
- } |
- return result; |
-} |
- |
-extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size, |
- int flags, ...) __THROW { |
- va_list ap; |
- va_start(ap, flags); |
- void *new_address = va_arg(ap, void *); |
- va_end(ap); |
- void* result = sys_mremap(old_addr, old_size, new_size, flags, new_address); |
- MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags, |
- new_address); |
- return result; |
-} |
- |
-// libc's version: |
-extern "C" void* __sbrk(std::ptrdiff_t increment); |
- |
-extern "C" void* sbrk(std::ptrdiff_t increment) __THROW { |
- MallocHook::InvokePreSbrkHook(increment); |
- void *result = __sbrk(increment); |
- MallocHook::InvokeSbrkHook(result, increment); |
- return result; |
-} |
- |
-/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
- int flags, int fd, off_t offset) { |
- void* result; |
- if (!MallocHook::InvokeMmapReplacement( |
- start, length, prot, flags, fd, offset, &result)) { |
- result = do_mmap64(start, length, prot, flags, fd, offset); |
- } |
- return result; |
-} |
- |
-/*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
- int result; |
- if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
- result = sys_munmap(start, length); |
- } |
- return result; |
-} |
- |
-#else // defined(__linux) && |
- // (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
+#else |
/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
int flags, int fd, off_t offset) { |
@@ -902,5 +720,4 @@ extern "C" void* sbrk(std::ptrdiff_t increment) __THROW { |
return result; |
} |
-#endif // defined(__linux) && |
- // (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
+#endif |