Path: blob/main/contrib/llvm-project/libcxx/src/memory_resource.cpp
35147 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <memory>9#include <memory_resource>1011#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER12# include <atomic>13#elif !defined(_LIBCPP_HAS_NO_THREADS)14# include <mutex>15# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)16# pragma comment(lib, "pthread")17# endif18#endif1920_LIBCPP_BEGIN_NAMESPACE_STD2122namespace pmr {2324// memory_resource2526memory_resource::~memory_resource() = default;2728// new_delete_resource()2930#ifdef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION31static bool is_aligned_to(void* ptr, size_t align) {32void* p2 = ptr;33size_t space = 1;34void* result = std::align(align, 1, p2, space);35return (result == ptr);36}37#endif3839class _LIBCPP_EXPORTED_FROM_ABI __new_delete_memory_resource_imp : public memory_resource {40void* do_allocate(size_t bytes, size_t align) override {41#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION42return std::__libcpp_allocate(bytes, align);43#else44if (bytes == 0)45bytes = 1;46void* result = std::__libcpp_allocate(bytes, align);47if (!is_aligned_to(result, align)) {48std::__libcpp_deallocate(result, bytes, align);49__throw_bad_alloc();50}51return result;52#endif53}5455void do_deallocate(void* p, size_t bytes, size_t align) override { std::__libcpp_deallocate(p, bytes, align); }5657bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }58};5960// null_memory_resource()6162class _LIBCPP_EXPORTED_FROM_ABI __null_memory_resource_imp : public memory_resource {63void* do_allocate(size_t, size_t) override { __throw_bad_alloc(); }64void do_deallocate(void*, size_t, size_t) override {}65bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }66};6768namespace {6970union ResourceInitHelper {71struct {72__new_delete_memory_resource_imp new_delete_res;73__null_memory_resource_imp null_res;74} resources;75char dummy;76constexpr ResourceInitHelper() : resources() {}77~ResourceInitHelper() {}78};7980// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority81// attribute with a value that's reserved for the implementation (we're the implementation).82#include "memory_resource_init_helper.h"8384} // end namespace8586memory_resource* new_delete_resource() noexcept { return &res_init.resources.new_delete_res; }8788memory_resource* null_memory_resource() noexcept { return &res_init.resources.null_res; }8990// default_memory_resource()9192static memory_resource* __default_memory_resource(bool set = false, memory_resource* new_res = nullptr) noexcept {93#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER94static constinit atomic<memory_resource*> __res{&res_init.resources.new_delete_res};95if (set) {96new_res = new_res ? new_res : new_delete_resource();97// TODO: Can a weaker ordering be used?98return std::atomic_exchange_explicit(&__res, new_res, memory_order_acq_rel);99} else {100return std::atomic_load_explicit(&__res, memory_order_acquire);101}102#elif !defined(_LIBCPP_HAS_NO_THREADS)103static constinit memory_resource* res = &res_init.resources.new_delete_res;104static mutex res_lock;105if (set) {106new_res = new_res ? new_res : new_delete_resource();107lock_guard<mutex> guard(res_lock);108memory_resource* old_res = res;109res = new_res;110return old_res;111} else {112lock_guard<mutex> guard(res_lock);113return res;114}115#else116static constinit memory_resource* res = &res_init.resources.new_delete_res;117if (set) {118new_res = new_res ? new_res : new_delete_resource();119memory_resource* old_res = res;120res = new_res;121return old_res;122} else {123return res;124}125#endif126}127128memory_resource* get_default_resource() noexcept { return __default_memory_resource(); }129130memory_resource* set_default_resource(memory_resource* __new_res) noexcept {131return __default_memory_resource(true, __new_res);132}133134// 23.12.5, mem.res.pool135136static size_t roundup(size_t count, size_t alignment) {137size_t mask = alignment - 1;138return (count + mask) & ~mask;139}140141struct unsynchronized_pool_resource::__adhoc_pool::__chunk_footer {142__chunk_footer* __next_;143char* __start_;144size_t __align_;145size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }146};147148void unsynchronized_pool_resource::__adhoc_pool::__release_ptr(memory_resource* upstream) {149while (__first_ != nullptr) {150__chunk_footer* next = __first_->__next_;151upstream->deallocate(__first_->__start_, __first_->__allocation_size(), __first_->__align_);152__first_ = next;153}154}155156void* unsynchronized_pool_resource::__adhoc_pool::__do_allocate(memory_resource* upstream, size_t bytes, size_t align) {157const size_t footer_size = sizeof(__chunk_footer);158const size_t footer_align = alignof(__chunk_footer);159160if (align < footer_align)161align = footer_align;162163size_t aligned_capacity = roundup(bytes, footer_align) + footer_size;164165void* result = upstream->allocate(aligned_capacity, align);166167__chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);168h->__next_ = __first_;169h->__start_ = (char*)result;170h->__align_ = align;171__first_ = h;172return result;173}174175void unsynchronized_pool_resource::__adhoc_pool::__do_deallocate(176memory_resource* upstream, void* p, size_t bytes, size_t align) {177_LIBCPP_ASSERT_NON_NULL(__first_ != nullptr, "deallocating a block that was not allocated with this allocator");178if (__first_->__start_ == p) {179__chunk_footer* next = __first_->__next_;180upstream->deallocate(p, __first_->__allocation_size(), __first_->__align_);181__first_ = next;182} else {183for (__chunk_footer* h = __first_; h->__next_ != nullptr; h = h->__next_) {184if (h->__next_->__start_ == p) {185__chunk_footer* next = h->__next_->__next_;186upstream->deallocate(p, h->__next_->__allocation_size(), h->__next_->__align_);187h->__next_ = next;188return;189}190}191// The request to deallocate memory ends up being a no-op, likely resulting in a memory leak.192_LIBCPP_ASSERT_VALID_DEALLOCATION(false, "deallocating a block that was not allocated with this allocator");193}194}195196class unsynchronized_pool_resource::__fixed_pool {197struct __chunk_footer {198__chunk_footer* __next_;199char* __start_;200size_t __align_;201size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }202};203204struct __vacancy_header {205__vacancy_header* __next_vacancy_;206};207208__chunk_footer* __first_chunk_ = nullptr;209__vacancy_header* __first_vacancy_ = nullptr;210211public:212explicit __fixed_pool() = default;213214void __release_ptr(memory_resource* upstream) {215__first_vacancy_ = nullptr;216while (__first_chunk_ != nullptr) {217__chunk_footer* next = __first_chunk_->__next_;218upstream->deallocate(__first_chunk_->__start_, __first_chunk_->__allocation_size(), __first_chunk_->__align_);219__first_chunk_ = next;220}221}222223void* __try_allocate_from_vacancies() {224if (__first_vacancy_ != nullptr) {225void* result = __first_vacancy_;226__first_vacancy_ = __first_vacancy_->__next_vacancy_;227return result;228}229return nullptr;230}231232void* __allocate_in_new_chunk(memory_resource* upstream, size_t block_size, size_t chunk_size) {233_LIBCPP_ASSERT_INTERNAL(chunk_size % block_size == 0, "");234static_assert(__default_alignment >= alignof(std::max_align_t), "");235static_assert(__default_alignment >= alignof(__chunk_footer), "");236static_assert(__default_alignment >= alignof(__vacancy_header), "");237238const size_t footer_size = sizeof(__chunk_footer);239const size_t footer_align = alignof(__chunk_footer);240241size_t aligned_capacity = roundup(chunk_size, footer_align) + footer_size;242243void* result = upstream->allocate(aligned_capacity, __default_alignment);244245__chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);246h->__next_ = __first_chunk_;247h->__start_ = (char*)result;248h->__align_ = __default_alignment;249__first_chunk_ = h;250251if (chunk_size > block_size) {252__vacancy_header* last_vh = this->__first_vacancy_;253for (size_t i = block_size; i != chunk_size; i += block_size) {254__vacancy_header* vh = (__vacancy_header*)((char*)result + i);255vh->__next_vacancy_ = last_vh;256last_vh = vh;257}258this->__first_vacancy_ = last_vh;259}260return result;261}262263void __evacuate(void* p) {264__vacancy_header* vh = (__vacancy_header*)(p);265vh->__next_vacancy_ = __first_vacancy_;266__first_vacancy_ = vh;267}268269size_t __previous_chunk_size_in_bytes() const { return __first_chunk_ ? __first_chunk_->__allocation_size() : 0; }270271static const size_t __default_alignment = alignof(max_align_t);272};273274size_t unsynchronized_pool_resource::__pool_block_size(int i) const { return size_t(1) << __log2_pool_block_size(i); }275276int unsynchronized_pool_resource::__log2_pool_block_size(int i) const { return (i + __log2_smallest_block_size); }277278int unsynchronized_pool_resource::__pool_index(size_t bytes, size_t align) const {279if (align > alignof(std::max_align_t) || bytes > (size_t(1) << __num_fixed_pools_))280return __num_fixed_pools_;281else {282int i = 0;283bytes = (bytes > align) ? bytes : align;284bytes -= 1;285bytes >>= __log2_smallest_block_size;286while (bytes != 0) {287bytes >>= 1;288i += 1;289}290return i;291}292}293294unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream)295: __res_(upstream), __fixed_pools_(nullptr) {296size_t largest_block_size;297if (opts.largest_required_pool_block == 0)298largest_block_size = __default_largest_block_size;299else if (opts.largest_required_pool_block < __smallest_block_size)300largest_block_size = __smallest_block_size;301else if (opts.largest_required_pool_block > __max_largest_block_size)302largest_block_size = __max_largest_block_size;303else304largest_block_size = opts.largest_required_pool_block;305306if (opts.max_blocks_per_chunk == 0)307__options_max_blocks_per_chunk_ = __max_blocks_per_chunk;308else if (opts.max_blocks_per_chunk < __min_blocks_per_chunk)309__options_max_blocks_per_chunk_ = __min_blocks_per_chunk;310else if (opts.max_blocks_per_chunk > __max_blocks_per_chunk)311__options_max_blocks_per_chunk_ = __max_blocks_per_chunk;312else313__options_max_blocks_per_chunk_ = opts.max_blocks_per_chunk;314315__num_fixed_pools_ = 1;316size_t capacity = __smallest_block_size;317while (capacity < largest_block_size) {318capacity <<= 1;319__num_fixed_pools_ += 1;320}321}322323pool_options unsynchronized_pool_resource::options() const {324pool_options p;325p.max_blocks_per_chunk = __options_max_blocks_per_chunk_;326p.largest_required_pool_block = __pool_block_size(__num_fixed_pools_ - 1);327return p;328}329330void unsynchronized_pool_resource::release() {331__adhoc_pool_.__release_ptr(__res_);332if (__fixed_pools_ != nullptr) {333const int n = __num_fixed_pools_;334for (int i = 0; i < n; ++i)335__fixed_pools_[i].__release_ptr(__res_);336__res_->deallocate(__fixed_pools_, __num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));337__fixed_pools_ = nullptr;338}339}340341void* unsynchronized_pool_resource::do_allocate(size_t bytes, size_t align) {342// A pointer to allocated storage (6.6.4.4.1) with a size of at least bytes.343// The size and alignment of the allocated memory shall meet the requirements for344// a class derived from memory_resource (23.12).345// If the pool selected for a block of size bytes is unable to satisfy the memory request346// from its own internal data structures, it will call upstream_resource()->allocate()347// to obtain more memory. If bytes is larger than that which the largest pool can handle,348// then memory will be allocated using upstream_resource()->allocate().349350int i = __pool_index(bytes, align);351if (i == __num_fixed_pools_)352return __adhoc_pool_.__do_allocate(__res_, bytes, align);353else {354if (__fixed_pools_ == nullptr) {355__fixed_pools_ =356(__fixed_pool*)__res_->allocate(__num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));357__fixed_pool* first = __fixed_pools_;358__fixed_pool* last = __fixed_pools_ + __num_fixed_pools_;359for (__fixed_pool* pool = first; pool != last; ++pool)360::new ((void*)pool) __fixed_pool;361}362void* result = __fixed_pools_[i].__try_allocate_from_vacancies();363if (result == nullptr) {364auto min = [](size_t a, size_t b) { return a < b ? a : b; };365auto max = [](size_t a, size_t b) { return a < b ? b : a; };366367size_t prev_chunk_size_in_bytes = __fixed_pools_[i].__previous_chunk_size_in_bytes();368size_t prev_chunk_size_in_blocks = prev_chunk_size_in_bytes >> __log2_pool_block_size(i);369370size_t chunk_size_in_blocks;371372if (prev_chunk_size_in_blocks == 0) {373size_t min_blocks_per_chunk = max(__min_bytes_per_chunk >> __log2_pool_block_size(i), __min_blocks_per_chunk);374chunk_size_in_blocks = min_blocks_per_chunk;375} else {376static_assert(__max_bytes_per_chunk <= SIZE_MAX - (__max_bytes_per_chunk / 4), "unsigned overflow is possible");377chunk_size_in_blocks = prev_chunk_size_in_blocks + (prev_chunk_size_in_blocks / 4);378}379380size_t max_blocks_per_chunk =381min((__max_bytes_per_chunk >> __log2_pool_block_size(i)),382min(__max_blocks_per_chunk, __options_max_blocks_per_chunk_));383if (chunk_size_in_blocks > max_blocks_per_chunk)384chunk_size_in_blocks = max_blocks_per_chunk;385386size_t block_size = __pool_block_size(i);387388size_t chunk_size_in_bytes = (chunk_size_in_blocks << __log2_pool_block_size(i));389result = __fixed_pools_[i].__allocate_in_new_chunk(__res_, block_size, chunk_size_in_bytes);390}391return result;392}393}394395void unsynchronized_pool_resource::do_deallocate(void* p, size_t bytes, size_t align) {396// Returns the memory at p to the pool. It is unspecified if,397// or under what circumstances, this operation will result in398// a call to upstream_resource()->deallocate().399400int i = __pool_index(bytes, align);401if (i == __num_fixed_pools_)402return __adhoc_pool_.__do_deallocate(__res_, p, bytes, align);403else {404_LIBCPP_ASSERT_NON_NULL(405__fixed_pools_ != nullptr, "deallocating a block that was not allocated with this allocator");406__fixed_pools_[i].__evacuate(p);407}408}409410bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const noexcept { return &other == this; }411412// 23.12.6, mem.res.monotonic.buffer413414static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {415if (size > space)416return nullptr;417418char* p1 = static_cast<char*>(ptr);419char* new_ptr = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 - size) & ~(align - 1));420421if (new_ptr < (p1 - space))422return nullptr;423424ptr = new_ptr;425space -= p1 - new_ptr;426427return ptr;428}429430void* monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk(size_t bytes, size_t align) {431if (!__cur_)432return nullptr;433void* new_ptr = static_cast<void*>(__cur_);434size_t new_capacity = (__cur_ - __start_);435void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity);436if (aligned_ptr != nullptr)437__cur_ = static_cast<char*>(new_ptr);438return aligned_ptr;439}440441void* monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk(size_t bytes, size_t align) {442void* new_ptr = static_cast<void*>(__cur_);443size_t new_capacity = (__cur_ - __start_);444void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity);445if (aligned_ptr != nullptr)446__cur_ = static_cast<char*>(new_ptr);447return aligned_ptr;448}449450void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {451const size_t footer_size = sizeof(__chunk_footer);452const size_t footer_align = alignof(__chunk_footer);453454auto previous_allocation_size = [&]() {455if (__chunks_ != nullptr)456return __chunks_->__allocation_size();457458size_t newsize = (__initial_.__start_ != nullptr) ? (__initial_.__end_ - __initial_.__start_) : __initial_.__size_;459460return roundup(newsize, footer_align) + footer_size;461};462463if (void* result = __initial_.__try_allocate_from_chunk(bytes, align))464return result;465if (__chunks_ != nullptr) {466if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align))467return result;468}469470// Allocate a brand-new chunk.471472if (align < footer_align)473align = footer_align;474475size_t aligned_capacity = roundup(bytes, footer_align) + footer_size;476size_t previous_capacity = previous_allocation_size();477478if (aligned_capacity <= previous_capacity) {479size_t newsize = 2 * (previous_capacity - footer_size);480aligned_capacity = roundup(newsize, footer_align) + footer_size;481}482483char* start = (char*)__res_->allocate(aligned_capacity, align);484auto end = start + aligned_capacity - footer_size;485__chunk_footer* footer = (__chunk_footer*)(end);486footer->__next_ = __chunks_;487footer->__start_ = start;488footer->__cur_ = end;489footer->__align_ = align;490__chunks_ = footer;491492return __chunks_->__try_allocate_from_chunk(bytes, align);493}494495} // namespace pmr496497_LIBCPP_END_NAMESPACE_STD498499500