命名空间
变体
操作

std::pmr::set_default_resource

来自 cppreference.com
< cpp‎ | memory
 
 
工具库
语言支持
类型支持 (基本类型, RTTI)
库功能测试宏 (C++20)
动态内存管理
程序工具
协程支持 (C++20)
可变参数函数
调试支持
(C++26)
三方比较
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (在 C++20 中已弃用)
整数比较函数
(C++20)(C++20)(C++20)   
(C++20)
交换类型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
通用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
基本字符串转换
(C++17)
(C++17)

 
动态内存管理
未初始化内存算法
受限未初始化内存算法
分配器
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)



 
定义在头文件 <memory_resource>
std::pmr::memory_resource* set_default_resource( std::pmr::memory_resource* r ) noexcept;
(自 C++17 起)

如果 r 不是空指针,则将默认内存资源指针设置为 r;否则,将默认内存资源指针设置为 std::pmr::new_delete_resource().

默认内存资源指针在没有提供显式内存资源时被某些设施使用。初始默认内存资源指针是 std::pmr::new_delete_resource 的返回值。

此函数是线程安全的。每次对 std::pmr::set_default_resource 的调用都(参见 std::memory_order)随后的 std::pmr::set_default_resourcestd::pmr::get_default_resource 调用同步。

[编辑] 返回值

返回默认内存资源指针的先前值。

[编辑] 示例

#include <array>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <memory_resource>
#include <vector>
 
class noisy_allocator : public std::pmr::memory_resource
{
    void* do_allocate(std::size_t bytes, std::size_t alignment) override
    {
        std::cout << "+ Allocating " << bytes << " bytes @ ";
        void* p = std::pmr::new_delete_resource()->allocate(bytes, alignment);
        std::cout << p << '\n';
        return p;
    }
 
    void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
    {
        std::cout << "- Deallocating " << bytes << " bytes @ " << p << '\n';
        return std::pmr::new_delete_resource()->deallocate(p, bytes, alignment);
    }
 
    bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override
    {
        return std::pmr::new_delete_resource()->is_equal(other);
    }
};
 
int main()
{
    constexpr int push_back_limit{16};
    noisy_allocator mem;
    std::pmr::set_default_resource(&mem);
 
    {
        std::cout << "Entering scope #1 (without buffer on stack)...\n";
        std::cout << "Creating vector v...\n";
        std::pmr::vector<std::uint16_t> v{1, 2, 3, 4};
        std::cout << "v.data() @ " << v.data() << '\n';
 
        std::cout << "Requesting more...\n";
        for (int i{0}; i != push_back_limit; ++i)
        {
            v.push_back(i);
            std::cout << "v.size(): " << v.size() << '\n';
        }
        std::cout << "Exiting scope #1...\n";
    }
 
    std::cout << '\n';
 
    {
        std::cout << "Entering scope #2 (with buffer on stack)...\n";
 
        std::uint8_t buffer[16];
        std::cout << "Allocating buffer on stack: " << sizeof buffer << " bytes @ "
                  << static_cast<void*>(buffer) << '\n';
        std::pmr::monotonic_buffer_resource mem_res{std::data(buffer), std::size(buffer)};
 
        std::cout << "Creating vector v...\n";
        std::pmr::vector<std::uint16_t> v{{1, 2, 3, 4}, &mem_res};
        std::cout << "v.data() @ " << v.data() << '\n'; // equals to `buffer` address
 
        std::cout << "Requesting more...\n";
        for (int i{0}; i != push_back_limit; ++i)
        {
            v.push_back(i);
            std::cout << "v.size(): " << v.size() << '\n';
        }
        std::cout << "Exiting scope #2...\n";
    }
}

可能的输出

Entering scope #1 (without buffer on stack)...
Creating vector v...
+ Allocating 8 bytes @ 0x1f75c30
v.data() @ 0x1f75c30
Requesting more...
+ Allocating 16 bytes @ 0x1f75c50
- Deallocating 8 bytes @ 0x1f75c30
v.size(): 5
v.size(): 6
v.size(): 7
v.size(): 8
+ Allocating 32 bytes @ 0x1f75c70
- Deallocating 16 bytes @ 0x1f75c50
v.size(): 9
v.size(): 10
v.size(): 11
v.size(): 12
v.size(): 13
v.size(): 14
v.size(): 15
v.size(): 16
+ Allocating 64 bytes @ 0x1f75ca0
- Deallocating 32 bytes @ 0x1f75c70
v.size(): 17
v.size(): 18
v.size(): 19
v.size(): 20
Exiting scope #1...
- Deallocating 64 bytes @ 0x1f75ca0
 
Entering scope #2 (with buffer on stack)...
Allocating buffer on stack: 16 bytes @ 0x7fffbe9f8240
Creating vector v...
v.data() @ 0x7fffbe9f8240
Requesting more...
+ Allocating 64 bytes @ 0x1f75ca0
v.size(): 5
v.size(): 6
v.size(): 7
v.size(): 8
v.size(): 9
v.size(): 10
v.size(): 11
v.size(): 12
v.size(): 13
v.size(): 14
v.size(): 15
v.size(): 16
+ Allocating 128 bytes @ 0x1f75cf0
v.size(): 17
v.size(): 18
v.size(): 19
v.size(): 20
Exiting scope #2...
- Deallocating 128 bytes @ 0x1f75cf0
- Deallocating 64 bytes @ 0x1f75ca0

[编辑] 另请参阅

获取默认的 std::pmr::memory_resource
(函数) [编辑]
返回一个使用全局 operator newoperator delete 来分配和释放内存的静态程序级 std::pmr::memory_resource
(函数) [编辑]