Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2026 by Thun Lu. All rights reserved. 3 : : * Author: Thun Lu <thun.lu@zohomail.cn> 4 : : * Repo: https://github.com/thun-res/vlink 5 : : * _ __ __ _ __ 6 : : * | | / / / / (_) ____ / /__ 7 : : * | | / / / / / / / __ \ / //_/ 8 : : * | |/ / / /___ / / / / / / / ,< 9 : : * |___/ /_____/ /_/ /_/ /_/ /_/|_| 10 : : * 11 : : * Licensed under the Apache License, Version 2.0 (the "License"); 12 : : * you may not use this file except in compliance with the License. 13 : : * You may obtain a copy of the License at 14 : : * 15 : : * http://www.apache.org/licenses/LICENSE-2.0 16 : : * 17 : : * Unless required by applicable law or agreed to in writing, software 18 : : * distributed under the License is distributed on an "AS IS" BASIS, 19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 : : * See the License for the specific language governing permissions and 21 : : * limitations under the License. 22 : : */ 23 : : 24 : : #include "./base/memory_resource.h" 25 : : 26 : : #ifdef VLINK_ENABLE_BASE_MEMORY_RESOURCE 27 : : 28 : : #define MEMORY_RESOURCE_NERVER_DELETE 0 29 : : 30 : : #include <new> 31 : : 32 : : #include "./base/macros.h" 33 : : 34 : : namespace vlink { 35 : : 36 [ + - + - ]: 2 : MemoryResource::MemoryResource() : pool_(new MemoryPool()), owns_pool_(true) {} 37 : : 38 [ + - + - ]: 9 : MemoryResource::MemoryResource(int level, bool prealloc) : pool_(new MemoryPool(level, prealloc)), owns_pool_(true) {} 39 : : 40 [ + - + - ]: 1 : MemoryResource::MemoryResource(const MemoryPool::Config& config) : pool_(new MemoryPool(config)), owns_pool_(true) {} 41 : : 42 : 71 : MemoryResource::~MemoryResource() { 43 [ + + ]: 71 : if (owns_pool_) { 44 [ + - ]: 12 : delete pool_; 45 : : } 46 : 71 : } 47 : : 48 : 7 : MemoryPool& MemoryResource::get_memory_pool() noexcept { return *pool_; } 49 : : 50 : 1 : void MemoryResource::trim() noexcept { pool_->trim(); } 51 : : 52 : 8909 : MemoryResource& MemoryResource::global_instance(bool use_env_level) { 53 : : #if MEMORY_RESOURCE_NERVER_DELETE 54 : : alignas(MemoryResource) static char buf[sizeof(MemoryResource)]; 55 : : 56 : : static auto* instance = new (buf) MemoryResource(MemoryPool::global_instance(use_env_level)); 57 : : 58 : : return *instance; 59 : : #else 60 [ + + + - : 8909 : static MemoryResource instance(MemoryPool::global_instance(use_env_level)); + - - - ] 61 : : 62 : 8909 : return instance; 63 : : #endif 64 : : } 65 : : 66 : 11765 : void* MemoryResource::do_allocate(size_t bytes, size_t alignment) { 67 : 11765 : void* p = pool_->allocate(bytes, alignment); 68 : : 69 [ - + ]: 11765 : if VUNLIKELY (p == nullptr) { 70 : 0 : throw std::bad_alloc(); 71 : : } 72 : : 73 : 11765 : return p; 74 : : } 75 : : 76 : 11763 : void MemoryResource::do_deallocate(void* p, size_t bytes, size_t alignment) { pool_->deallocate(p, bytes, alignment); } 77 : : 78 : 6 : bool MemoryResource::do_is_equal(const std::pmr::memory_resource& other) const noexcept { 79 [ + + ]: 6 : if (this == &other) { 80 : 2 : return true; 81 : : } 82 : : 83 : : #if defined(NDEBUG) || defined(__ANDROID__) 84 : : const auto* rhs = static_cast<const MemoryResource*>(&other); 85 : : #else 86 [ + - ]: 4 : const auto* rhs = dynamic_cast<const MemoryResource*>(&other); 87 : : #endif 88 : : 89 [ + + - + ]: 4 : return rhs != nullptr && pool_ == rhs->pool_; 90 : : } 91 : : 92 : : // NOLINTNEXTLINE(modernize-use-default-member-init) 93 : 59 : MemoryResource::MemoryResource(MemoryPool& global_pool) noexcept : pool_(&global_pool), owns_pool_(false) {} 94 : : 95 : : } // namespace vlink 96 : : 97 : : #endif