命名空间
变体
操作

标准库头文件 <memory_resource> (C++17)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

内容

在命名空间 std::pmr 中定义
一个分配器,它支持基于其构造的 std::pmr::memory_resource 的运行时多态性
(类模板) [编辑]
封装内存资源的类的抽象接口
(类) [编辑]
一组用于池资源的构造函数选项
(类) [编辑]
一个线程安全的 std::pmr::memory_resource,用于管理不同块大小的池中的分配
(类) [编辑]
一个非线程安全的 std::pmr::memory_resource,用于管理不同块大小的池中的分配
(类) [编辑]
一个特殊用途的 std::pmr::memory_resource,它只在资源被销毁时才释放分配的内存
(类) [编辑]

函数

在命名空间 std::pmr 中定义
返回一个静态的程序范围 std::pmr::memory_resource,它使用全局 operator newoperator delete 来分配和释放内存
(函数) [编辑]
返回一个静态的 std::pmr::memory_resource,它不执行任何分配
(函数) [编辑]
获取默认的 std::pmr::memory_resource
(函数) [编辑]
设置默认的 std::pmr::memory_resource
(函数) [编辑]

[编辑] 概述

namespace std::pmr {
  // class memory_resource
  class memory_resource;
 
  bool operator==(const memory_resource& a, const memory_resource& b) noexcept;
 
  // class template polymorphic_allocator
  template<class Tp> class polymorphic_allocator;
 
  template<class T1, class T2>
    bool operator==(const polymorphic_allocator<T1>& a,
                    const polymorphic_allocator<T2>& b) noexcept;
 
  // global memory resources
  memory_resource* new_delete_resource() noexcept;
  memory_resource* null_memory_resource() noexcept;
  memory_resource* set_default_resource(memory_resource* r) noexcept;
  memory_resource* get_default_resource() noexcept;
 
  // pool resource classes
  struct pool_options;
  class synchronized_pool_resource;
  class unsynchronized_pool_resource;
  class monotonic_buffer_resource;
}

[编辑] std::pmr::memory_resource

namespace std::pmr {
  class memory_resource {
    static constexpr size_t max_align = alignof(max_align_t);   // exposition only
 
  public:
    memory_resource() = default;
    memory_resource(const memory_resource&) = default;
    virtual ~memory_resource();
 
    memory_resource& operator=(const memory_resource&) = default;
 
    void* allocate(size_t bytes, size_t alignment = max_align);
    void deallocate(void* p, size_t bytes, size_t alignment = max_align);
 
    bool is_equal(const memory_resource& other) const noexcept;
 
  private:
    virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
    virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
 
    virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
  };
}

[编辑] 类模板 std::pmr::polymorphic_allocator

namespace std::pmr {
  template<class Tp = byte> class polymorphic_allocator {
    memory_resource* memory_rsrc;       // exposition only
 
  public:
    using value_type = Tp;
 
    // constructors
    polymorphic_allocator() noexcept;
    polymorphic_allocator(memory_resource* r);
 
    polymorphic_allocator(const polymorphic_allocator& other) = default;
 
    template<class U>
      polymorphic_allocator(const polymorphic_allocator<U>& other) noexcept;
 
    polymorphic_allocator& operator=(const polymorphic_allocator&) = delete;
 
    // member functions
    Tp* allocate(size_t n);
    void deallocate(Tp* p, size_t n);
 
    void* allocate_bytes(size_t nbytes, size_t alignment = alignof(max_align_t));
    void deallocate_bytes(void* p, size_t nbytes, size_t alignment = alignof(max_align_t));
    template<class T> T* allocate_object(size_t n = 1);
    template<class T> void deallocate_object(T* p, size_t n = 1);
    template<class T, class... CtorArgs>
      T* new_object(CtorArgs&&... ctor_args);
    template<class T> void delete_object(T* p);
 
    template<class T, class... Args>
      void construct(T* p, Args&&... args);
 
    template<class T>
      void destroy(T* p); // deprecated
 
    polymorphic_allocator select_on_container_copy_construction() const;
 
    memory_resource* resource() const;
  };
}

[编辑] std::pmr::pool_options

namespace std::pmr {
  struct pool_options {
    size_t max_blocks_per_chunk = 0;
    size_t largest_required_pool_block = 0;
  };
}

[编辑] std::pmr::synchronized_pool_resource

namespace std::pmr {
  class synchronized_pool_resource : public memory_resource {
  public:
    synchronized_pool_resource(const pool_options& opts, memory_resource* upstream);
 
    synchronized_pool_resource()
        : synchronized_pool_resource(pool_options(), get_default_resource()) {}
    explicit synchronized_pool_resource(memory_resource* upstream)
        : synchronized_pool_resource(pool_options(), upstream) {}
    explicit synchronized_pool_resource(const pool_options& opts)
        : synchronized_pool_resource(opts, get_default_resource()) {}
 
    synchronized_pool_resource(const synchronized_pool_resource&) = delete;
    virtual ~synchronized_pool_resource();
 
    synchronized_pool_resource& operator=(const synchronized_pool_resource&) = delete;
 
    void release();
    memory_resource* upstream_resource() const;
    pool_options options() const;
 
  protected:
    void* do_allocate(size_t bytes, size_t alignment) override;
    void do_deallocate(void* p, size_t bytes, size_t alignment) override;
 
    bool do_is_equal(const memory_resource& other) const noexcept override;
  };
}

[编辑] std::pmr::unsynchronized_pool_resource

namespace std::pmr {
  class unsynchronized_pool_resource : public memory_resource {
  public:
    unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream);
 
    unsynchronized_pool_resource()
        : unsynchronized_pool_resource(pool_options(), get_default_resource()) {}
    explicit unsynchronized_pool_resource(memory_resource* upstream)
        : unsynchronized_pool_resource(pool_options(), upstream) {}
    explicit unsynchronized_pool_resource(const pool_options& opts)
        : unsynchronized_pool_resource(opts, get_default_resource()) {}
 
    unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
    virtual ~unsynchronized_pool_resource();
 
    unsynchronized_pool_resource& operator=(const unsynchronized_pool_resource&) = delete;
 
    void release();
    memory_resource* upstream_resource() const;
    pool_options options() const;
 
  protected:
    void* do_allocate(size_t bytes, size_t alignment) override;
    void do_deallocate(void* p, size_t bytes, size_t alignment) override;
 
    bool do_is_equal(const memory_resource& other) const noexcept override;
  };
}

[编辑] std::pmr::monotonic_buffer_resource

namespace std::pmr {
  class monotonic_buffer_resource : public memory_resource {
    memory_resource* upstream_rsrc;     // exposition only
    void* current_buffer;               // exposition only
    size_t next_buffer_size;            // exposition only
 
  public:
    explicit monotonic_buffer_resource(memory_resource* upstream);
    monotonic_buffer_resource(size_t initial_size, memory_resource* upstream);
    monotonic_buffer_resource(void* buffer, size_t buffer_size, memory_resource* upstream);
 
    monotonic_buffer_resource()
      : monotonic_buffer_resource(get_default_resource()) {}
    explicit monotonic_buffer_resource(size_t initial_size)
      : monotonic_buffer_resource(initial_size, get_default_resource()) {}
    monotonic_buffer_resource(void* buffer, size_t buffer_size)
      : monotonic_buffer_resource(buffer, buffer_size, get_default_resource()) {}
 
    monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
 
    virtual ~monotonic_buffer_resource();
 
    monotonic_buffer_resource& operator=(const monotonic_buffer_resource&) = delete;
 
    void release();
    memory_resource* upstream_resource() const;
 
  protected:
    void* do_allocate(size_t bytes, size_t alignment) override;
    void do_deallocate(void* p, size_t bytes, size_t alignment) override;
 
    bool do_is_equal(const memory_resource& other) const noexcept override;
  };
}