Skip to content

Commit ae7e71c

Browse files
compnerdcopybara-github
authored andcommitted
tcmalloc: generalise the malloc hooks for a standards conforming libc
This creates a path for non-glibc libc implementations to add the proper hooks. Not all libc implementations provide the same set of APIs, so reduce some of the interfaces to the right set. PiperOrigin-RevId: 391150233 Change-Id: I3a8c22bc222442f23840235b627116d05e54723b
1 parent 910da91 commit ae7e71c

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

tcmalloc/libc_override.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "tcmalloc/libc_override_glibc.h"
3333

3434
#else
35-
#error Need to add support for your libc/OS here
35+
#include "tcmalloc/libc_override_redefine.h"
3636

3737
#endif
3838

tcmalloc/libc_override_redefine.h

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,57 @@ void operator delete(void* ptr, const std::nothrow_t& nt) noexcept {
4444
void operator delete[](void* ptr, const std::nothrow_t& nt) noexcept {
4545
return TCMallocInternalDeleteArrayNothrow(ptr, nt);
4646
}
47+
4748
extern "C" {
48-
void* malloc(size_t s) noexcept { return TCMallocInternalMalloc(s); }
49-
void free(void* p) noexcept { TCMallocInternalFree(p); }
50-
void sdallocx(void* p, size_t s, int flags) {
51-
TCMallocInternalSdallocx(p, s, flags);
52-
}
53-
void* realloc(void* p, size_t s) noexcept {
54-
return TCMallocInternalRealloc(p, s);
55-
}
56-
void* calloc(size_t n, size_t s) noexcept {
57-
return TCMallocInternalCalloc(n, s);
58-
}
59-
void cfree(void* p) noexcept { TCMallocInternalCfree(p); }
60-
void* memalign(size_t a, size_t s) noexcept {
61-
return TCMallocInternalMemalign(a, s);
62-
}
63-
void* valloc(size_t s) noexcept { return TCMallocInternalValloc(s); }
64-
void* pvalloc(size_t s) noexcept { return TCMallocInternalPvalloc(s); }
65-
int posix_memalign(void** r, size_t a, size_t s) noexcept {
49+
void* malloc(size_t s) { return TCMallocInternalMalloc(s); }
50+
void* calloc(size_t n, size_t s) { return TCMallocInternalCalloc(n, s); }
51+
void* realloc(void* p, size_t s) { return TCMallocInternalRealloc(p, s); }
52+
void free(void* p) { TCMallocInternalFree(p); }
53+
void* memalign(size_t a, size_t s) { return TCMallocInternalMemalign(a, s); }
54+
int posix_memalign(void** r, size_t a, size_t s) {
6655
return TCMallocInternalPosixMemalign(r, a, s);
6756
}
68-
void malloc_stats(void) noexcept { TCMallocInternalMallocStats(); }
69-
int mallopt(int cmd, int v) noexcept { return TCMallocInternalMallOpt(cmd, v); }
57+
size_t malloc_usable_size(void* p) { return TCMallocInternalMallocSize(p); }
58+
59+
// tcmalloc extension
60+
void sdallocx(void* p, size_t s, int flags) noexcept {
61+
TCMallocInternalSdallocx(p, s, flags);
62+
}
63+
64+
#if defined(__GLIBC__) || defined(__NEWLIB__)
65+
// SunOS extension
66+
void cfree(void* p) { TCMallocInternalCfree(p); }
67+
#endif
68+
69+
#if defined(OS_MACOSX) || defined(__BIONIC__) || defined(__GLIBC__) || \
70+
defined(__NEWLIB__) || defined(__UCLIBC__)
71+
// Obsolete memalign
72+
void* valloc(size_t s) { return TCMallocInternalValloc(s); }
73+
#endif
74+
75+
#if defined(__BIONIC__) || defined(__GLIBC__) || defined(__NEWLIB__)
76+
// Obsolete memalign
77+
void* pvalloc(size_t s) { return TCMallocInternalPvalloc(s); }
78+
#endif
79+
80+
#if defined(__GLIBC__) || defined(__NEWLIB__) || defined(__UCLIBC__)
81+
void malloc_stats(void) { TCMallocInternalMallocStats(); }
82+
#endif
83+
84+
#if defined(__BIONIC__) || defined(__GLIBC__) || defined(__NEWLIB__) || \
85+
defined(__UCLIBC__)
86+
int mallopt(int cmd, int v) { return TCMallocInternalMallOpt(cmd, v); }
87+
#endif
88+
7089
#ifdef TCMALLOC_HAVE_STRUCT_MALLINFO
71-
struct mallinfo mallinfo(void) noexcept {
90+
struct mallinfo mallinfo(void) {
7291
return TCMallocInternalMallocInfo();
7392
}
7493
#endif
75-
size_t malloc_size(void* p) noexcept { return TCMallocInternalMallocSize(p); }
76-
size_t malloc_usable_size(void* p) noexcept {
77-
return TCMallocInternalMallocSize(p);
78-
}
94+
95+
#if defined(__GLIBC__)
96+
size_t malloc_size(void* p) { return TCMallocInternalMallocSize(p); }
97+
#endif
7998
} // extern "C"
8099

81100
#endif // TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_

0 commit comments

Comments
 (0)