|
|
@@ -49,8 +49,11 @@ |
|
|
#ifndef _MSC_VER |
|
|
#include <stdint.h> |
|
|
#endif |
|
|
#include <string> |
|
|
#include <vector> |
|
|
#define NO_STL |
|
|
#ifndef NO_STL |
|
|
#include <string> |
|
|
#include <vector> |
|
|
#endif |
|
|
|
|
|
// Annoying stuff for windows -- makes sure clients can import these functions |
|
|
#ifndef PERFTOOLS_DLL_DECL |
|
|
@@ -63,26 +66,22 @@ |
|
|
|
|
|
static const int kMallocHistogramSize = 64; |
|
|
|
|
|
// One day, we could support other types of writers (perhaps for C?) |
|
|
typedef std::string MallocExtensionWriter; |
|
|
#ifndef NO_STL |
|
|
typedef std::string MallocExtensionWriter; |
|
|
#else |
|
|
// One day, we could support other types of writers (perhaps for C?) |
|
|
class DummyString |
|
|
{ |
|
|
public: |
|
|
void append(const char *c, size_t size){} |
|
|
}; |
|
|
typedef DummyString MallocExtensionWriter; |
|
|
#endif |
|
|
|
|
|
namespace base { |
|
|
struct MallocRange; |
|
|
} |
|
|
|
|
|
// Interface to a pluggable system allocator. |
|
|
class SysAllocator { |
|
|
public: |
|
|
SysAllocator() { |
|
|
} |
|
|
virtual ~SysAllocator(); |
|
|
|
|
|
// Allocates "size"-byte of memory from system aligned with "alignment". |
|
|
// Returns NULL if failed. Otherwise, the returned pointer p up to and |
|
|
// including (p + actual_size -1) have been allocated. |
|
|
virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0; |
|
|
}; |
|
|
|
|
|
// The default implementations of the following routines do nothing. |
|
|
// All implementations should be thread-safe; the current one |
|
|
// (TCMallocImplementation) is. |
|
|
@@ -100,7 +99,6 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
|
|
|
// See "verify_memory.h" to see what these routines do |
|
|
virtual bool VerifyAllMemory(); |
|
|
// TODO(csilvers): change these to const void*. |
|
|
virtual bool VerifyNewMemory(void* p); |
|
|
virtual bool VerifyArrayNewMemory(void* p); |
|
|
virtual bool VerifyMallocMemory(void* p); |
|
|
@@ -116,9 +114,7 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
// Outputs to "writer" a sample of live objects and the stack traces |
|
|
// that allocated these objects. The format of the returned output |
|
|
// is equivalent to the output of the heap profiler and can |
|
|
// therefore be passed to "pprof". This function is equivalent to |
|
|
// ReadStackTraces. The main difference is that this function returns |
|
|
// serialized data appropriately formatted for use by the pprof tool. |
|
|
// therefore be passed to "pprof". |
|
|
// NOTE: by default, tcmalloc does not do any heap sampling, and this |
|
|
// function will always return an empty sample. To get useful |
|
|
// data from GetHeapSample, you must also set the environment |
|
|
@@ -128,10 +124,7 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
// Outputs to "writer" the stack traces that caused growth in the |
|
|
// address space size. The format of the returned output is |
|
|
// equivalent to the output of the heap profiler and can therefore |
|
|
// be passed to "pprof". This function is equivalent to |
|
|
// ReadHeapGrowthStackTraces. The main difference is that this function |
|
|
// returns serialized data appropriately formatted for use by the |
|
|
// pprof tool. (This does not depend on, or require, |
|
|
// be passed to "pprof". (This does not depend on, or require, |
|
|
// TCMALLOC_SAMPLE_PARAMETER.) |
|
|
virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); |
|
|
|
|
|
@@ -220,27 +213,6 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
// Most malloc implementations ignore this routine. |
|
|
virtual void MarkThreadBusy(); |
|
|
|
|
|
// Gets the system allocator used by the malloc extension instance. Returns |
|
|
// NULL for malloc implementations that do not support pluggable system |
|
|
// allocators. |
|
|
virtual SysAllocator* GetSystemAllocator(); |
|
|
|
|
|
// Sets the system allocator to the specified. |
|
|
// |
|
|
// Users could register their own system allocators for malloc implementation |
|
|
// that supports pluggable system allocators, such as TCMalloc, by doing: |
|
|
// alloc = new MyOwnSysAllocator(); |
|
|
// MallocExtension::instance()->SetSystemAllocator(alloc); |
|
|
// It's up to users whether to fall back (recommended) to the default |
|
|
// system allocator (use GetSystemAllocator() above) or not. The caller is |
|
|
// responsible to any necessary locking. |
|
|
// See tcmalloc/system-alloc.h for the interface and |
|
|
// tcmalloc/memfs_malloc.cc for the examples. |
|
|
// |
|
|
// It's a no-op for malloc implementations that do not support pluggable |
|
|
// system allocators. |
|
|
virtual void SetSystemAllocator(SysAllocator *a); |
|
|
|
|
|
// Try to release num_bytes of free memory back to the operating |
|
|
// system for reuse. Use this extension with caution -- to get this |
|
|
// memory back may require faulting pages back in by the OS, and |
|
|
@@ -281,27 +253,8 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
// will return 0.) |
|
|
// This is equivalent to malloc_size() in OS X, malloc_usable_size() |
|
|
// in glibc, and _msize() for windows. |
|
|
// TODO(csilvers): change to const void*. |
|
|
virtual size_t GetAllocatedSize(void* p); |
|
|
|
|
|
// Returns kOwned if this malloc implementation allocated the memory |
|
|
// pointed to by p, or kNotOwned if some other malloc implementation |
|
|
// allocated it or p is NULL. May also return kUnknownOwnership if |
|
|
// the malloc implementation does not keep track of ownership. |
|
|
// REQUIRES: p must be a value returned from a previous call to |
|
|
// malloc(), calloc(), realloc(), memalign(), posix_memalign(), |
|
|
// valloc(), pvalloc(), new, or new[], and must refer to memory that |
|
|
// is currently allocated (so, for instance, you should not pass in |
|
|
// a pointer after having called free() on it). |
|
|
enum Ownership { |
|
|
// NOTE: Enum values MUST be kept in sync with the version in |
|
|
// malloc_extension_c.h |
|
|
kUnknownOwnership = 0, |
|
|
kOwned, |
|
|
kNotOwned |
|
|
}; |
|
|
virtual Ownership GetOwnership(const void* p); |
|
|
|
|
|
// The current malloc implementation. Always non-NULL. |
|
|
static MallocExtension* instance(); |
|
|
|
|
|
@@ -346,8 +299,11 @@ class PERFTOOLS_DLL_DECL MallocExtension { |
|
|
// and not returned to tcmalloc. |
|
|
// |
|
|
// "tcmalloc.thread" - tcmalloc's per-thread caches. Never unmapped. |
|
|
#ifndef NO_STL |
|
|
virtual void GetFreeListSizes(std::vector<FreeListInfo>* v); |
|
|
#endif |
|
|
|
|
|
protected: |
|
|
// Get a list of stack traces of sampled allocation points. Returns |
|
|
// a pointer to a "new[]-ed" result array, and stores the sample |
|
|
// period in "sample_period". |
|
|
|