This source file includes following definitions.
- call_new_handler
- malloc
- free
- realloc
- malloc_stats
- _msize
- _get_heap_handle
- get_allocator_waste_size_thunk
- get_stats_thunk
- release_free_memory_thunk
- _heap_init
- _heap_term
- _aligned_malloc
- _aligned_free
- SetupSubprocessAllocator
- TCMallocDoMallocForTest
- TCMallocDoFreeForTest
- ExcludeSpaceForMarkForTest
#include "base/allocator/allocator_shim.h"
#include <config.h>
#include "base/allocator/allocator_extension_thunks.h"
#include "base/profiler/alternate_timer.h"
#include "base/sysinfo.h"
#ifndef __THROW
# define __THROW
#endif
static int new_mode = 0;
typedef enum {
TCMALLOC,
WINHEAP,
WINLFH,
} Allocator;
#if defined(SYZYASAN)
static Allocator allocator = WINHEAP;
#else
static Allocator allocator = TCMALLOC;
#endif
static const char primary_name[] = "CHROME_ALLOCATOR";
static const char secondary_name[] = "CHROME_ALLOCATOR_2";
#include "debugallocation_shim.cc"
#include "win_allocator.cc"
inline bool call_new_handler(bool nothrow) {
std::new_handler nh;
{
SpinLockHolder h(&set_new_handler_lock);
nh = std::set_new_handler(0);
(void) std::set_new_handler(nh);
}
#if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \
(defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS)
if (!nh)
return false;
(*nh)();
return false;
#else
if (!nh) {
if (nothrow)
return false;
throw std::bad_alloc();
}
try {
(*nh)();
} catch (const std::bad_alloc&) {
if (!nothrow)
throw;
return true;
}
#endif
return false;
}
extern "C" {
void* malloc(size_t size) __THROW {
void* ptr;
for (;;) {
switch (allocator) {
case WINHEAP:
case WINLFH:
ptr = win_heap_malloc(size);
break;
case TCMALLOC:
default:
ptr = do_malloc(size);
break;
}
if (ptr)
return ptr;
if (!new_mode || !call_new_handler(true))
break;
}
return ptr;
}
void free(void* p) __THROW {
switch (allocator) {
case WINHEAP:
case WINLFH:
win_heap_free(p);
return;
case TCMALLOC:
do_free(p);
return;
}
}
void* realloc(void* ptr, size_t size) __THROW {
if (!ptr)
return malloc(size);
void* new_ptr;
for (;;) {
switch (allocator) {
case WINHEAP:
case WINLFH:
new_ptr = win_heap_realloc(ptr, size);
break;
case TCMALLOC:
default:
new_ptr = do_realloc(ptr, size);
break;
}
if (new_ptr || !size)
return new_ptr;
if (!new_mode || !call_new_handler(true))
break;
}
return new_ptr;
}
void malloc_stats(void) __THROW {
switch (allocator) {
case WINHEAP:
case WINLFH:
return;
case TCMALLOC:
tc_malloc_stats();
return;
}
}
#ifdef WIN32
extern "C" size_t _msize(void* p) {
switch (allocator) {
case WINHEAP:
case WINLFH:
return win_heap_msize(p);
}
return MallocExtension::instance()->GetAllocatedSize(p);
}
extern "C" intptr_t _get_heap_handle() {
return 0;
}
static bool get_allocator_waste_size_thunk(size_t* size) {
switch (allocator) {
case WINHEAP:
case WINLFH:
return false;
}
size_t heap_size, allocated_bytes, unmapped_bytes;
MallocExtension* ext = MallocExtension::instance();
if (ext->GetNumericProperty("generic.heap_size", &heap_size) &&
ext->GetNumericProperty("generic.current_allocated_bytes",
&allocated_bytes) &&
ext->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes",
&unmapped_bytes)) {
*size = heap_size - allocated_bytes - unmapped_bytes;
return true;
}
return false;
}
static void get_stats_thunk(char* buffer, int buffer_length) {
MallocExtension::instance()->GetStats(buffer, buffer_length);
}
static void release_free_memory_thunk() {
MallocExtension::instance()->ReleaseFreeMemory();
}
extern "C" int _heap_init() {
#if !defined(SYZYASAN)
const char* environment_value = GetenvBeforeMain(primary_name);
if (environment_value) {
if (!stricmp(environment_value, "winheap"))
allocator = WINHEAP;
else if (!stricmp(environment_value, "winlfh"))
allocator = WINLFH;
else if (!stricmp(environment_value, "tcmalloc"))
allocator = TCMALLOC;
}
#endif
switch (allocator) {
case WINHEAP:
return win_heap_init(false) ? 1 : 0;
case WINLFH:
return win_heap_init(true) ? 1 : 0;
case TCMALLOC:
default:
break;
}
new TCMallocGuard();
const char* profiling =
GetenvBeforeMain(tracked_objects::kAlternateProfilerTime);
if (profiling && *profiling == '1') {
tracked_objects::SetAlternateTimeSource(
tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread,
tracked_objects::TIME_SOURCE_TYPE_TCMALLOC);
}
base::allocator::thunks::SetGetAllocatorWasteSizeFunction(
get_allocator_waste_size_thunk);
base::allocator::thunks::SetGetStatsFunction(get_stats_thunk);
base::allocator::thunks::SetReleaseFreeMemoryFunction(
release_free_memory_thunk);
return 1;
}
extern "C" void _heap_term() {}
extern "C" void* _crtheap = reinterpret_cast<void*>(1);
void* _aligned_malloc(size_t size, size_t alignment) {
DCHECK_GT(size, 0U);
DCHECK_EQ(alignment & (alignment - 1), 0U);
DCHECK_EQ(alignment % sizeof(void*), 0U);
void* ptr;
for (;;) {
switch (allocator) {
case WINHEAP:
case WINLFH:
ptr = win_heap_memalign(alignment, size);
break;
case TCMALLOC:
default:
ptr = tc_memalign(alignment, size);
break;
}
if (ptr) {
DCHECK_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U);
return ptr;
}
if (!new_mode || !call_new_handler(true))
break;
}
return ptr;
}
void _aligned_free(void* p) {
switch (allocator) {
case WINHEAP:
case WINLFH:
win_heap_memalign_free(p);
return;
case TCMALLOC:
do_free(p);
}
}
#endif
#include "generic_allocators.cc"
}
namespace base {
namespace allocator {
void SetupSubprocessAllocator() {
size_t primary_length = 0;
getenv_s(&primary_length, NULL, 0, primary_name);
size_t secondary_length = 0;
char buffer[20];
getenv_s(&secondary_length, buffer, sizeof(buffer), secondary_name);
DCHECK_GT(sizeof(buffer), secondary_length);
buffer[sizeof(buffer) - 1] = '\0';
if (secondary_length || !primary_length) {
#if !defined(SYZYASAN)
const char* secondary_value = secondary_length ? buffer : "TCMALLOC";
#else
const char* secondary_value = "WINHEAP";
#endif
int ret_val = _putenv_s(primary_name, secondary_value);
DCHECK_EQ(0, ret_val);
}
}
void* TCMallocDoMallocForTest(size_t size) {
return do_malloc(size);
}
void TCMallocDoFreeForTest(void* ptr) {
do_free(ptr);
}
size_t ExcludeSpaceForMarkForTest(size_t size) {
return ExcludeSpaceForMark(size);
}
}
}