Created
November 19, 2009 01:02
-
-
Save ice799/238438 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| commit 2ec0bd00031a777237a24b7cd6a06b0f7f007430 | |
| Author: Joe Damato <[email protected]> | |
| Date: Wed Nov 18 16:22:06 2009 -0800 | |
| add malloc_usable_size | |
| diff --git a/configure.ac b/configure.ac | |
| index 9db399a..12cc585 100644 | |
| --- a/configure.ac | |
| +++ b/configure.ac | |
| @@ -109,6 +109,7 @@ AC_HEADER_STDC | |
| # TODO(csilvers): we could remove a lot when WITH_CPU_PROFILER etc is "no". | |
| AC_CHECK_TYPES([__int64]) # defined in some windows platforms | |
| AC_CHECK_TYPES([struct mallinfo],,, [#include <malloc.h>]) | |
| +AC_CHECK_FUNCS(malloc_usable_size) # for getting the number of bytes allocated | |
| AC_CHECK_TYPES([Elf32_Versym],,, [#include <elf.h>]) # for vdso_support.h | |
| AC_CHECK_FUNCS(sbrk) # for tcmalloc to get memory | |
| AC_CHECK_FUNCS(geteuid) # for turning off services when run as root | |
| @@ -143,6 +144,12 @@ AC_CHECK_DECLS([cfree, | |
| #include <stdlib.h> | |
| #include <malloc.h>]) | |
| +if test "$ac_cv_have_func_malloc_usable_size" = yes; then | |
| + AC_SUBST(ac_cv_have_func_malloc_usable_size, 1) | |
| +else | |
| + AC_SUBST(ac_cv_have_func_malloc_usable_size, 0) | |
| +fi | |
| + | |
| if test "$ac_cv_type_struct_mallinfo" = yes; then | |
| AC_SUBST(ac_cv_have_struct_mallinfo, 1) # google/tcmalloc.h needs this | |
| else | |
| diff --git a/src/config.h.in b/src/config.h.in | |
| index 18be9f3..861790a 100644 | |
| --- a/src/config.h.in | |
| +++ b/src/config.h.in | |
| @@ -114,6 +114,9 @@ | |
| /* Define to 1 if the system has the type `struct mallinfo'. */ | |
| #undef HAVE_STRUCT_MALLINFO | |
| +/* Define to 1 if the system has the function malloc_usable_size.*/ | |
| +#undef HAVE_MALLOC_USABLE_SIZE | |
| + | |
| /* Define to 1 if you have the <sys/prctl.h> header file. */ | |
| #undef HAVE_SYS_PRCTL_H | |
| diff --git a/src/google/tcmalloc.h.in b/src/google/tcmalloc.h.in | |
| index 4937c62..0731f14 100644 | |
| --- a/src/google/tcmalloc.h.in | |
| +++ b/src/google/tcmalloc.h.in | |
| @@ -84,6 +84,7 @@ extern "C" { | |
| PERFTOOLS_DLL_DECL void tc_malloc_stats(void) __THROW; | |
| PERFTOOLS_DLL_DECL int tc_mallopt(int cmd, int value) __THROW; | |
| + PERFTOOLS_DLL_DECL size_t tc_malloc_usable_size(void *ptr) __THROW; | |
| #if @ac_cv_have_struct_mallinfo@ | |
| PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW; | |
| #endif | |
| diff --git a/src/tcmalloc.cc b/src/tcmalloc.cc | |
| index 5d634fa..a390dad 100644 | |
| --- a/src/tcmalloc.cc | |
| +++ b/src/tcmalloc.cc | |
| @@ -97,7 +97,7 @@ | |
| #else | |
| #include <sys/types.h> | |
| #endif | |
| -#if defined(HAVE_MALLOC_H) && defined(HAVE_STRUCT_MALLINFO) | |
| +#if defined(HAVE_MALLOC_H) && (defined(HAVE_STRUCT_MALLINFO) || defined(HAVE_MALLOC_USABLE_SIZE)) | |
| #include <malloc.h> // for struct mallinfo | |
| #endif | |
| #include <string.h> | |
| @@ -211,6 +211,10 @@ extern "C" { | |
| ATTRIBUTE_SECTION(google_malloc); | |
| int tc_mallopt(int cmd, int value) __THROW | |
| ATTRIBUTE_SECTION(google_malloc); | |
| +#ifdef HAVE_MALLOC_USABLE_SIZE | |
| + size_t tc_malloc_usable_size(void *ptr) __THROW | |
| + ATTRIBUTE_SECTION(google_malloc); | |
| +#endif | |
| #ifdef HAVE_STRUCT_MALLINFO // struct mallinfo isn't defined on freebsd | |
| struct mallinfo tc_mallinfo(void) __THROW | |
| ATTRIBUTE_SECTION(google_malloc); | |
| @@ -266,6 +270,9 @@ extern "C" { | |
| int posix_memalign(void** r, size_t a, size_t s) __THROW | |
| ALIAS("tc_posix_memalign"); | |
| void malloc_stats(void) __THROW ALIAS("tc_malloc_stats"); | |
| +#ifdef HAVE_MALLOC_USABLE_SIZE | |
| + size_t malloc_usable_size(void *ptr) __THROW ALIAS("tc_malloc_usable_size"); | |
| +#endif | |
| int mallopt(int cmd, int value) __THROW ALIAS("tc_mallopt"); | |
| #ifdef HAVE_STRUCT_MALLINFO | |
| struct mallinfo mallinfo(void) __THROW ALIAS("tc_mallinfo"); | |
| @@ -297,6 +304,9 @@ extern "C" { | |
| } | |
| void malloc_stats(void) __THROW { tc_malloc_stats(); } | |
| int mallopt(int cmd, int v) __THROW { return tc_mallopt(cmd, v); } | |
| +#ifdef HAVE_MALLOC_USABLE_SIZE | |
| + size_t malloc_usable_size(void *ptr) __THROW { return tc_malloc_usable_size(ptr); } | |
| +#endif | |
| #ifdef HAVE_STRUCT_MALLINFO | |
| struct mallinfo mallinfo(void) __THROW { return tc_mallinfo(); } | |
| #endif | |
| @@ -1062,6 +1072,30 @@ inline struct mallinfo do_mallinfo() { | |
| } | |
| #endif // #ifndef HAVE_STRUCT_MALLINFO | |
| +#ifdef HAVE_MALLOC_USABLE_SIZE | |
| +inline size_t do_malloc_usable_size(void *ptr) { | |
| + if (ptr == NULL) return 0; | |
| + Span *span = NULL; | |
| + const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; | |
| + size_t cl = Static::pageheap()->GetSizeClassIfCached(p); | |
| + size_t alloc_size = Static::sizemap()->ByteSizeForClass(cl); | |
| + | |
| + if (cl == 0) { | |
| + span = Static::pageheap()->GetDescriptor(p); | |
| + if (!span) { | |
| + return 0; | |
| + } | |
| + alloc_size = Static::sizemap()->ByteSizeForClass(span->sizeclass); | |
| + } | |
| + | |
| + if (alloc_size == 0) { | |
| + return span->length << kPageShift; | |
| + } | |
| + | |
| + return alloc_size; | |
| +} | |
| +#endif | |
| + | |
| static SpinLock set_new_handler_lock(SpinLock::LINKER_INITIALIZED); | |
| inline void* cpp_alloc(size_t size, bool nothrow) { | |
| @@ -1278,6 +1312,12 @@ extern "C" PERFTOOLS_DLL_DECL void tc_malloc_stats(void) __THROW { | |
| do_malloc_stats(); | |
| } | |
| +#ifdef HAVE_MALLOC_USABLE_SIZE | |
| +extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_usable_size(void *ptr) __THROW { | |
| + return do_malloc_usable_size(ptr); | |
| +} | |
| +#endif | |
| + | |
| extern "C" PERFTOOLS_DLL_DECL int tc_mallopt(int cmd, int value) __THROW { | |
| return do_mallopt(cmd, value); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment