diff --git a/tcmalloc/libc_override.h b/tcmalloc/libc_override.h index 567fbea26..b8c6993ae 100644 --- a/tcmalloc/libc_override.h +++ b/tcmalloc/libc_override.h @@ -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); diff --git a/tcmalloc/malloc_extension.cc b/tcmalloc/malloc_extension.cc index 91b2d9432..decedfbf4 100644 --- a/tcmalloc/malloc_extension.cc +++ b/tcmalloc/malloc_extension.cc @@ -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}; diff --git a/tcmalloc/malloc_extension.h b/tcmalloc/malloc_extension.h index 09c6f6c22..1c5377f4d 100644 --- a/tcmalloc/malloc_extension.h +++ b/tcmalloc/malloc_extension.h @@ -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 { diff --git a/tcmalloc/tcmalloc.cc b/tcmalloc/tcmalloc.cc index 5ae66db4a..35bd61085 100644 --- a/tcmalloc/tcmalloc.cc +++ b/tcmalloc/tcmalloc.cc @@ -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)); } diff --git a/tcmalloc/tcmalloc.h b/tcmalloc/tcmalloc.h index 46f032313..b908affce 100644 --- a/tcmalloc/tcmalloc.h +++ b/tcmalloc/tcmalloc.h @@ -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 diff --git a/tcmalloc/testing/profile_drop_frames_test.cc b/tcmalloc/testing/profile_drop_frames_test.cc index d7a5ae9c5..0f18d8a01 100644 --- a/tcmalloc/testing/profile_drop_frames_test.cc +++ b/tcmalloc/testing/profile_drop_frames_test.cc @@ -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) { diff --git a/tcmalloc/testing/tcmalloc_test.cc b/tcmalloc/testing/tcmalloc_test.cc index bdd030415..9d57549d4 100644 --- a/tcmalloc/testing/tcmalloc_test.cc +++ b/tcmalloc/testing/tcmalloc_test.cc @@ -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__)