aboutsummaryrefslogtreecommitdiff
path: root/libcxx/src
diff options
context:
space:
mode:
authorLouis Dionne <ldionne.2@gmail.com>2025-01-13 09:10:36 -0500
committerGitHub <noreply@github.com>2025-01-13 09:10:36 -0500
commitcedb44af53f195135e8e8de98f161048d19f8857 (patch)
treec44eb865177a07e21e81813b38bc864566e2e1ea /libcxx/src
parent162397f98d04415eebe115cdcb01558932e5c802 (diff)
downloadllvm-cedb44af53f195135e8e8de98f161048d19f8857.zip
llvm-cedb44af53f195135e8e8de98f161048d19f8857.tar.gz
llvm-cedb44af53f195135e8e8de98f161048d19f8857.tar.bz2
[libc++] Pass type information down to __libcpp_allocate (#118837)
Currently, places where we call __libcpp_allocate must drop type information on the ground even when they actually have such information available. That is unfortunate since some toolchains and system allocators are able to provide improved security when they know what type is being allocated. This is the purpose of http://wg21.link/p2719, where we introduce a new variant of `operator new` which takes a type in its interface. A different but related issue is that `std::allocator` does not honor any in-class `T::operator new` since it is specified to call the global `::operator new` instead. This patch closes the gap to make it trivial for implementations that provide typed memory allocators to actually benefit from that information in more contexts, and also makes libc++ forward-compatible with future proposals that would fix the existing defects in `std::allocator`. It also makes the internal allocation API higher level by operating on objects instead of operating on bytes of memory. Since this is a widely-used function and making this a template could have an impact on debug info sizes, I tried minimizing the number of templated layers by removing `__do_deallocate_handle_size`, which was easy to replace with a macro (and IMO this leads to cleaner code).
Diffstat (limited to 'libcxx/src')
-rw-r--r--libcxx/src/memory_resource.cpp10
1 files changed, 6 insertions, 4 deletions
diff --git a/libcxx/src/memory_resource.cpp b/libcxx/src/memory_resource.cpp
index e182e5a..e1a9e1a 100644
--- a/libcxx/src/memory_resource.cpp
+++ b/libcxx/src/memory_resource.cpp
@@ -41,20 +41,22 @@ static bool is_aligned_to(void* ptr, size_t align) {
class _LIBCPP_EXPORTED_FROM_ABI __new_delete_memory_resource_imp : public memory_resource {
void* do_allocate(size_t bytes, size_t align) override {
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
- return std::__libcpp_allocate(bytes, align);
+ return std::__libcpp_allocate<std::byte>(__element_count(bytes), align);
#else
if (bytes == 0)
bytes = 1;
- void* result = std::__libcpp_allocate(bytes, align);
+ std::byte* result = std::__libcpp_allocate<std::byte>(__element_count(bytes), align);
if (!is_aligned_to(result, align)) {
- std::__libcpp_deallocate(result, bytes, align);
+ std::__libcpp_deallocate<std::byte>(result, __element_count(bytes), align);
__throw_bad_alloc();
}
return result;
#endif
}
- void do_deallocate(void* p, size_t bytes, size_t align) override { std::__libcpp_deallocate(p, bytes, align); }
+ void do_deallocate(void* p, size_t bytes, size_t align) override {
+ std::__libcpp_deallocate<std::byte>(static_cast<std::byte*>(p), __element_count(bytes), align);
+ }
bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }
};