76 #if defined(__linux__) && __has_include(<memory_resource>)
77 #include <memory_resource>
78 #if !defined(VLINK_ENABLE_BASE_MEMORY_RESOURCE) && defined(__cpp_lib_memory_resource)
79 #define VLINK_ENABLE_BASE_MEMORY_RESOURCE
83 #ifdef VLINK_ENABLE_BASE_MEMORY_RESOURCE
101 class VLINK_EXPORT MemoryResource :
public std::pmr::memory_resource {
109 template <
typename T>
110 struct Deleter final {
111 std::pmr::polymorphic_allocator<T> alloc;
113 void operator()(T* p)
const noexcept {
115 auto allocator = alloc;
116 std::allocator_traits<decltype(allocator)>::destroy(allocator, p);
117 allocator.deallocate(p, 1);
140 explicit MemoryResource(
int level,
bool prealloc =
false);
147 explicit MemoryResource(
const MemoryPool::Config& config);
156 ~MemoryResource()
override;
163 [[nodiscard]] MemoryPool& get_memory_pool() noexcept;
171 void trim() noexcept;
184 static MemoryResource& global_instance(
bool use_env_level = true);
194 template <typename T, typename... Args>
195 [[maybe_unused]] static std::shared_ptr<T>
make_shared(Args&&... args);
209 template <typename T, typename... Args>
210 [[maybe_unused]] static std::unique_ptr<T, MemoryResource::Deleter<T>>
make_unique(Args&&... args);
213 void* do_allocate(
size_t bytes,
size_t alignment) override;
215 void do_deallocate(
void* p,
size_t bytes,
size_t alignment) override;
217 bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override;
220 explicit MemoryResource(MemoryPool& global_pool) noexcept;
222 MemoryPool* pool_{
nullptr};
223 bool owns_pool_{
false};
232 template <
typename T,
typename... Args>
234 std::pmr::polymorphic_allocator<T> alloc(&MemoryResource::global_instance());
236 return std::allocate_shared<T>(alloc, std::forward<Args>(args)...);
239 template <
typename T,
typename... Args>
241 std::pmr::polymorphic_allocator<T> alloc(&MemoryResource::global_instance());
243 T* p = alloc.allocate(1);
246 std::allocator_traits<decltype(alloc)>::construct(alloc, p, std::forward<Args>(args)...);
248 alloc.deallocate(p, 1);
252 return std::unique_ptr<T, MemoryResource::Deleter<T>>{p, MemoryResource::Deleter<T>{alloc}};
270 namespace MemoryResource {
275 template <
typename T,
typename... Args>
277 return std::make_shared<T>(std::forward<Args>(args)...);
283 template <
typename T,
typename... Args>
285 return std::make_unique<T>(std::forward<Args>(args)...);
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VLINK_EXPORT
Definition: macros.h:81
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Size-class tiered memory pool with per-tier free lists and runtime statistics.
std::unique_ptr< T > make_unique(Args &&... args)
Fallback make_unique forwarding to std::make_unique.
Definition: memory_resource.h:284
std::shared_ptr< T > make_shared(Args &&... args)
Fallback make_shared forwarding to std::make_shared.
Definition: memory_resource.h:276