Skip to content

Commit

Permalink
Provide weak definitions of C23 free_sized/free_aligned_sized methods.
Browse files Browse the repository at this point in the history
These can be early adopted but the allocator may not provide them (for example,
sanitizers) even though TCMalloc does.

PiperOrigin-RevId: 687375825
Change-Id: I483074edf0f85a5d5b797d761e2d0f2607be7c87
  • Loading branch information
ckennelly authored and copybara-github committed Oct 18, 2024
1 parent 43726a4 commit ade6f2e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tcmalloc/libc_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ extern "C" {
void* malloc(size_t size) TCMALLOC_NOTHROW
TCMALLOC_ALIAS(TCMallocInternalMalloc);
void free(void* ptr) TCMALLOC_NOTHROW TCMALLOC_ALIAS(TCMallocInternalFree);
void free_sized(void* ptr, size_t size) TCMALLOC_NOTHROW
void free_sized(void* ptr, size_t size)
TCMALLOC_ALIAS(TCMallocInternalFreeSized);
void free_aligned_sized(void* ptr, size_t align, size_t size) TCMALLOC_NOTHROW
void free_aligned_sized(void* ptr, size_t align, size_t size)
TCMALLOC_ALIAS(TCMallocInternalFreeAlignedSized);
void sdallocx(void* ptr, size_t size, int flags) noexcept
TCMALLOC_ALIAS(TCMallocInternalSdallocx);
Expand Down
10 changes: 10 additions & 0 deletions tcmalloc/malloc_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,16 @@ ABSL_ATTRIBUTE_WEAK ABSL_ATTRIBUTE_NOINLINE void sdallocx(void* ptr, size_t,
free(ptr);
}

ABSL_ATTRIBUTE_WEAK ABSL_ATTRIBUTE_NOINLINE void free_sized(void* ptr, size_t) {
free(ptr);
}

ABSL_ATTRIBUTE_WEAK ABSL_ATTRIBUTE_NOINLINE void free_aligned_sized(void* ptr,
size_t,
size_t) {
free(ptr);
}

ABSL_ATTRIBUTE_WEAK ABSL_ATTRIBUTE_NOINLINE tcmalloc::sized_ptr_t
__size_returning_new(size_t size) {
return {::operator new(size), size};
Expand Down
9 changes: 9 additions & 0 deletions tcmalloc/malloc_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,15 @@ extern "C" size_t nallocx(size_t size, int flags) noexcept;
// uses the size to improve deallocation performance.
extern "C" void sdallocx(void* ptr, size_t size, int flags) noexcept;

#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
// Frees ptr allocated with malloc(size) introduced in C23.
extern "C" void free_sized(void* ptr, size_t size);

// Frees ptr allocated with aligned_alloc/posix_memalign with the specified size
// and alignment introduced in C23.
extern "C" void free_aligned_sized(void* ptr, size_t alignment, size_t size);
#endif

// Define __sized_ptr_t in the global namespace so that it can be named by the
// __size_returning_new implementations defined in tcmalloc.cc.
struct __sized_ptr_t {
Expand Down
6 changes: 3 additions & 3 deletions tcmalloc/tcmalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1311,13 +1311,13 @@ extern "C" ABSL_CACHELINE_ALIGNED void TCMallocInternalFree(
do_free(ptr);
}

extern "C" ABSL_CACHELINE_ALIGNED void TCMallocInternalFreeSized(
void* ptr, size_t size) noexcept {
extern "C" ABSL_CACHELINE_ALIGNED void TCMallocInternalFreeSized(void* ptr,
size_t size) {
do_free_with_size(ptr, size, MallocAlignPolicy());
}

extern "C" ABSL_CACHELINE_ALIGNED void TCMallocInternalFreeAlignedSized(
void* ptr, size_t align, size_t size) noexcept {
void* ptr, size_t align, size_t size) {
TC_ASSERT(absl::has_single_bit(align));
do_free_with_size(ptr, size, AlignAsPolicy(align));
}
Expand Down
8 changes: 4 additions & 4 deletions tcmalloc/tcmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ ABSL_ATTRIBUTE_UNUSED void* TCMallocInternalMalloc(size_t size) noexcept
ABSL_ATTRIBUTE_SECTION(google_malloc);
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalFree(void* ptr) noexcept
ABSL_ATTRIBUTE_SECTION(google_malloc);
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalFreeSized(void* ptr,
size_t size) noexcept
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalFreeSized(void* ptr, size_t size)
ABSL_ATTRIBUTE_SECTION(google_malloc);
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalFreeAlignedSized(
void* ptr, size_t align, size_t size) noexcept
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalFreeAlignedSized(void* ptr,
size_t align,
size_t size)
ABSL_ATTRIBUTE_SECTION(google_malloc);
ABSL_ATTRIBUTE_UNUSED void TCMallocInternalSdallocx(void* ptr, size_t size,
int flags) noexcept
Expand Down
8 changes: 0 additions & 8 deletions tcmalloc/testing/profile_drop_frames_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@
#include "tcmalloc/malloc_extension.h"
#include "tcmalloc/new_extension.h"

#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
// free_sized is a sized free function introduced in C23.
extern "C" void free_sized(void* ptr, size_t size) noexcept;
// free_aligned_sized is an overaligned sized free function introduced in C23.
extern "C" void free_aligned_sized(void* ptr, size_t align,
size_t size) noexcept;
#endif

namespace {

inline ABSL_ATTRIBUTE_ALWAYS_INLINE void wrap_delete(void* ptr, size_t size) {
Expand Down
8 changes: 0 additions & 8 deletions tcmalloc/testing/tcmalloc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@
// kLogMaxMemalign.
const int kLogMaxMemalign = 18;

#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
// free_sized is a sized free function introduced in C23.
extern "C" void free_sized(void* ptr, size_t size) noexcept;
// free_aligned_sized is an overaligned sized free function introduced in C23.
extern "C" void free_aligned_sized(void* ptr, size_t align,
size_t size) noexcept;
#endif

extern "C" void* reallocarray(void* ptr, size_t nmemb, size_t size);

#if !defined(__GLIBC__)
Expand Down

0 comments on commit ade6f2e

Please sign in to comment.