[libc-commits] [PATCH] D157653: [libc][WIP] move realloc into alloc_checker

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Aug 11 11:47:53 PDT 2023


sivachandra added a comment.

In D157653#4580926 <https://reviews.llvm.org/D157653#4580926>, @mcgrathr wrote:

> What I meant about a custom `operator new` to do `realloc` was something like this (C++20 example and without the AllocChecker arg, which would be added here too):
>
>   #include <span>                                                                 
>   #include <cstdlib>                                                              
>                                                                                   
>   template<typename T>                                                            
>   [[nodiscard]] inline void* operator new[] (size_t size, std::span<T> old) noexcept {                                                                           
>     void* ptr = ::realloc(old.data(), old.size_bytes() + size);        
>     if (!ptr) { return nullptr; }                                                 
>     return static_cast<char*>(ptr) + old.size_bytes();                            
>   }                                                                               
>                                                                                   
>   struct S { int x = 23; };                                                       
>                                                                                   
>   S* extend(S* old, size_t count) {                                               
>     return new (std::span{old,count}) S[count];                                   
>   }                                                                               
>                                                                                   
>   int* extend(int* old, size_t count) {                                           
>     return new (std::span{old,count}) int[count];                                 
>   } 
>
> Instead of the local `span` equivalent you might want to use a custom type just so the name makes it more obvious what's being done, e.g. `new (Realloc{old, count}, ac) T[n];`.
>
> That example shows how for a type where default-initialization is not just uninitialized, it does what you want like `new` for the original allocation does, while for a type that can be uninitialized, that's still an option with no extra overhead.
> And of course you can use it with constructor arguments in a situation where that makes sense.
>
> Note that `realloc` is also sometimes used to trim an allocation to shorter than it was, and I'm not sure there's a way to do an analogous trick for that case such that destructors get run naturally.

Should we restrict this to trivially destructible types?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157653/new/

https://reviews.llvm.org/D157653



More information about the libc-commits mailing list