Index: third_party/tcmalloc/chromium/src/memfs_malloc.cc |
diff --git a/third_party/tcmalloc/chromium/src/memfs_malloc.cc b/third_party/tcmalloc/chromium/src/memfs_malloc.cc |
index 3fb55a4e6de361dd9244ad7649469d29757af012..b59f6d9e4e36f2fa6604183a95ca1e9dd624c9e5 100644 |
--- a/third_party/tcmalloc/chromium/src/memfs_malloc.cc |
+++ b/third_party/tcmalloc/chromium/src/memfs_malloc.cc |
@@ -54,13 +54,16 @@ |
#include <new> // for operator new |
#include <string> |
-#include <google/malloc_extension.h> |
+#include <gperftools/malloc_extension.h> |
#include "base/basictypes.h" |
#include "base/googleinit.h" |
#include "base/sysinfo.h" |
-#include "system-alloc.h" |
#include "internal_logging.h" |
+// TODO(sanjay): Move the code below into the tcmalloc namespace |
+using tcmalloc::kLog; |
+using tcmalloc::kCrash; |
+using tcmalloc::Log; |
using std::string; |
DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""), |
@@ -86,7 +89,7 @@ DEFINE_bool(memfs_malloc_map_private, |
class HugetlbSysAllocator: public SysAllocator { |
public: |
explicit HugetlbSysAllocator(SysAllocator* fallback) |
- : failed_(true), // Unusable until FlagsInitialized() is called |
+ : failed_(true), // To disable allocator until Initialize() is called. |
big_page_size_(0), |
hugetlb_fd_(-1), |
hugetlb_base_(0), |
@@ -94,10 +97,10 @@ public: |
} |
void* Alloc(size_t size, size_t *actual_size, size_t alignment); |
- |
- void FlagsInitialized(); |
+ bool Initialize(); |
bool failed_; // Whether failed to allocate memory. |
+ |
private: |
void* AllocInternal(size_t size, size_t *actual_size, size_t alignment); |
@@ -136,11 +139,11 @@ void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size, |
if (result != NULL) { |
return result; |
} |
- TCMalloc_MESSAGE(__FILE__, __LINE__, |
- "HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n", |
- failed_, static_cast<int64_t>(hugetlb_base_)); |
+ Log(kLog, __FILE__, __LINE__, |
+ "HugetlbSysAllocator: (failed, allocated)", failed_, hugetlb_base_); |
if (FLAGS_memfs_malloc_abort_on_fail) { |
- CRASH("memfs_malloc_abort_on_fail is set\n"); |
+ Log(kCrash, __FILE__, __LINE__, |
+ "memfs_malloc_abort_on_fail is set"); |
} |
return fallback_->Alloc(size, actual_size, alignment); |
} |
@@ -158,13 +161,12 @@ void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, |
if (limit > 0 && hugetlb_base_ + size + extra > limit) { |
// Disable the allocator when there's less than one page left. |
if (limit - hugetlb_base_ < big_page_size_) { |
- TCMalloc_MESSAGE(__FILE__, __LINE__, "reached memfs_malloc_limit_mb\n"); |
+ Log(kLog, __FILE__, __LINE__, "reached memfs_malloc_limit_mb"); |
failed_ = true; |
} |
else { |
- TCMalloc_MESSAGE(__FILE__, __LINE__, "alloc size=%"PRIuS |
- " too large while %"PRId64" bytes remain\n", |
- size, static_cast<int64_t>(limit - hugetlb_base_)); |
+ Log(kLog, __FILE__, __LINE__, |
+ "alloc too large (size, bytes left)", size, limit-hugetlb_base_); |
} |
return NULL; |
} |
@@ -173,8 +175,8 @@ void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, |
// hugetlbfs returns EINVAL for ftruncate. |
int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra); |
if (ret != 0 && errno != EINVAL) { |
- TCMalloc_MESSAGE(__FILE__, __LINE__, "ftruncate failed: %s\n", |
- strerror(errno)); |
+ Log(kLog, __FILE__, __LINE__, |
+ "ftruncate failed", strerror(errno)); |
failed_ = true; |
return NULL; |
} |
@@ -189,8 +191,8 @@ void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, |
hugetlb_fd_, hugetlb_base_); |
if (result == reinterpret_cast<void*>(MAP_FAILED)) { |
if (!FLAGS_memfs_malloc_ignore_mmap_fail) { |
- TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap of size %"PRIuS" failed: %s\n", |
- size + extra, strerror(errno)); |
+ Log(kLog, __FILE__, __LINE__, |
+ "mmap failed (size, error)", size + extra, strerror(errno)); |
failed_ = true; |
} |
return NULL; |
@@ -212,49 +214,54 @@ void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, |
return reinterpret_cast<void*>(ptr); |
} |
-void HugetlbSysAllocator::FlagsInitialized() { |
- if (FLAGS_memfs_malloc_path.length()) { |
- char path[PATH_MAX]; |
- int rc = snprintf(path, sizeof(path), "%s.XXXXXX", |
- FLAGS_memfs_malloc_path.c_str()); |
- if (rc < 0 || rc >= sizeof(path)) { |
- CRASH("XX fatal: memfs_malloc_path too long\n"); |
- } |
- |
- int hugetlb_fd = mkstemp(path); |
- if (hugetlb_fd == -1) { |
- TCMalloc_MESSAGE(__FILE__, __LINE__, |
- "warning: unable to create memfs_malloc_path %s: %s\n", |
- path, strerror(errno)); |
- return; |
- } |
+bool HugetlbSysAllocator::Initialize() { |
+ char path[PATH_MAX]; |
+ const int pathlen = FLAGS_memfs_malloc_path.size(); |
+ if (pathlen + 8 > sizeof(path)) { |
+ Log(kCrash, __FILE__, __LINE__, "XX fatal: memfs_malloc_path too long"); |
+ return false; |
+ } |
+ memcpy(path, FLAGS_memfs_malloc_path.data(), pathlen); |
+ memcpy(path + pathlen, ".XXXXXX", 8); // Also copies terminating \0 |
- // Cleanup memory on process exit |
- if (unlink(path) == -1) { |
- CRASH("fatal: error unlinking memfs_malloc_path %s: %s\n", |
- path, strerror(errno)); |
- } |
+ int hugetlb_fd = mkstemp(path); |
+ if (hugetlb_fd == -1) { |
+ Log(kLog, __FILE__, __LINE__, |
+ "warning: unable to create memfs_malloc_path", |
+ path, strerror(errno)); |
+ return false; |
+ } |
- // Use fstatfs to figure out the default page size for memfs |
- struct statfs sfs; |
- if (fstatfs(hugetlb_fd, &sfs) == -1) { |
- CRASH("fatal: error fstatfs of memfs_malloc_path: %s\n", |
- strerror(errno)); |
- } |
- int64 page_size = sfs.f_bsize; |
+ // Cleanup memory on process exit |
+ if (unlink(path) == -1) { |
+ Log(kCrash, __FILE__, __LINE__, |
+ "fatal: error unlinking memfs_malloc_path", path, strerror(errno)); |
+ return false; |
+ } |
- hugetlb_fd_ = hugetlb_fd; |
- big_page_size_ = page_size; |
- failed_ = false; |
+ // Use fstatfs to figure out the default page size for memfs |
+ struct statfs sfs; |
+ if (fstatfs(hugetlb_fd, &sfs) == -1) { |
+ Log(kCrash, __FILE__, __LINE__, |
+ "fatal: error fstatfs of memfs_malloc_path", strerror(errno)); |
+ return false; |
} |
-} |
+ int64 page_size = sfs.f_bsize; |
-static void InitSystemAllocator() { |
- SysAllocator *alloc = MallocExtension::instance()->GetSystemAllocator(); |
- HugetlbSysAllocator *hugetlb = new (hugetlb_space) HugetlbSysAllocator(alloc); |
- MallocExtension::instance()->SetSystemAllocator(hugetlb); |
+ hugetlb_fd_ = hugetlb_fd; |
+ big_page_size_ = page_size; |
+ failed_ = false; |
+ return true; |
} |
-REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); |
+REGISTER_MODULE_INITIALIZER(memfs_malloc, { |
+ if (FLAGS_memfs_malloc_path.length()) { |
+ SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator(); |
+ HugetlbSysAllocator* hp = new (hugetlb_space) HugetlbSysAllocator(alloc); |
+ if (hp->Initialize()) { |
+ MallocExtension::instance()->SetSystemAllocator(hp); |
+ } |
+ } |
+}); |
#endif /* ifdef __linux */ |