Namespaces
Variants

std::pmr:: monotonic_buffer_resource

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더 파일에 정의됨 <memory_resource>
class monotonic_buffer_resource : public std:: pmr :: memory_resource ;
(C++17부터)

클래스 std::pmr::monotonic_buffer_resource 는 할당된 메모리를 해당 리소스가 파괴될 때만 해제하는 특수 목적의 메모리 리소스 클래스입니다. 이는 메모리를 사용하여 소수의 객체를 구축한 다음 한꺼번에 해제하는 상황에서 매우 빠른 메모리 할당을 목적으로 설계되었습니다.

monotonic_buffer_resource 는 초기 버퍼와 함께 생성될 수 있습니다. 초기 버퍼가 없거나 버퍼가 소진된 경우, 생성 시 제공된 upstream memory resource 에서 추가 버퍼를 획득합니다. 획득하는 버퍼의 크기는 기하급수적으로 증가합니다.

monotonic_buffer_resource 는 스레드 안전하지 않습니다.

목차

멤버 함수

monotonic_buffer_resource 를 생성합니다
(public member function)
[virtual]
monotonic_buffer_resource 를 파괴하고 모든 할당된 메모리를 해제합니다
(virtual public member function)
operator=
[deleted]
복사 할당 연산자가 삭제됨. monotonic_buffer_resource 는 복사 할당이 불가능합니다
(public member function)
Public member functions
모든 할당된 메모리를 해제합니다
(public member function)
상위 스트림 메모리 리소스에 대한 포인터를 반환합니다
(public member function)
Protected member functions
[virtual]
메모리를 할당합니다
(virtual protected member function)
[virtual]
아무 작업도 수행하지 않음
(virtual protected member function)
[virtual]
다른 std::pmr::memory_resource 와 동등성을 비교합니다
(virtual protected member function)

예제

이 프로그램은 다음과 같은 할당자를 사용하여 대규모 이중 연결 리스트 생성 시간을 측정합니다:

  • 기본 표준 할당자,
  • 기본 pmr 할당자,
  • pmr 단조 리소스를 사용하지만 명시적 메모리 버퍼가 없는 할당자,
  • pmr 단조 리소스와 외부 메모리 버퍼(스택 상)를 사용하는 할당자.
#include <array>
#include <chrono>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <list>
#include <memory_resource>
template<typename Func>
auto benchmark(Func test_func, int iterations)
{
    const auto start = std::chrono::system_clock::now();
    while (iterations-- > 0)
        test_func();
    const auto stop = std::chrono::system_clock::now
**변경사항 없음** - C++ 특정 용어와 HTML 태그 내의 텍스트는 번역하지 않음();
    const auto secs = std::chrono::duration
**번역 결과:**
std::chrono::duration
**번역 설명:**
- HTML 태그와 속성은 그대로 유지
- `` 태그 내의 C++ 네임스페이스(std::chrono::duration)는 번역하지 않음
- C++ 관련 용어는 원문 그대로 보존
- 태그 구조와 형식은 완전히 동일하게 유지<double>(stop - start);
    return secs.count();
}
int main()
{
    constexpr int iterations{100};
    constexpr int total_nodes{2'00'000};
    auto default_std_alloc = [total_nodes]
    {
        std::list<int> list;
        for (int i{}; i != total_nodes; ++i)
            list.push_back(i);
    };
    auto default_pmr_alloc = [total_nodes]
    {
        std::pmr::list<int> list;
        for (int i{}; i != total_nodes; ++i)
            list.push_back(i);
    };
    auto pmr_alloc_no_buf = [total_nodes]
    {
        std::pmr::monotonic_buffer_resource mbr;
        std::pmr::polymorphic_allocator
**번역 결과:**
- HTML 태그와 속성은 번역하지 않고 원본 형식을 유지했습니다.
- `` 태그 내의 텍스트는 C++ 특정 용어이므로 번역하지 않았습니다.
- `std::pmr::polymorphic_allocator`는 C++ 표준 라이브러리 용어로 그대로 보존했습니다.
**번역된 텍스트:**
웹페이지의 링크 텍스트는 C++ 용어이므로 번역 없이 원문을 유지하는 것이 적절합니다.<int> pa{&mbr};
        std::pmr::list<int> list{pa};
        for (int i{}; i != total_nodes; ++i)
            list.push_back(i);
    };
    auto pmr_alloc_and_buf = [total_nodes]
    {
        std::array<std::byte, total_nodes * 32> buffer; // 모든 노드를 수용하기에 충분한
        std::pmr::monotonic_buffer_resource mbr{buffer.data(), buffer.size()};
        std::pmr::polymorphic_allocator
(번역 없음 - C++ 특정 용어이므로 원문 유지)<int> pa{&mbr};
        std::pmr::list<int> list{pa};
        for (int i{}; i != total_nodes; ++i)
            list.push_back(i);
    };
    const double t1 = benchmark(default_std_alloc, iterations);
    const double t2 = benchmark(default_pmr_alloc, iterations);
    const double t3 = benchmark(pmr_alloc_no_buf , iterations);
    const double t4 = benchmark(pmr_alloc_and_buf, iterations);
    std::cout << std::fixed << std::setprecision(3)
              << "t1 (기본 std allocator): " << t1 << " 초; t1/t1: " << t1/t1 << '\n'
              << "t2 (기본 pmr 할당자): " << t2 << " sec; t1/t2: " << t1/t2 << '\n'
              << "t3 (pmr alloc no buf): " << t3 << " 초; t1/t3: " << t1/t3 << '\n'
              << "t4 (pmr alloc and buf): " << t4 << " sec; t1/t4: " << t1/t4 << '\n';
}

가능한 출력:

t1 (default std alloc): 0.720 sec; t1/t1: 1.000
t2 (default pmr alloc): 0.915 sec; t1/t2: 0.787
t3 (pmr alloc  no buf): 0.370 sec; t1/t3: 1.945
t4 (pmr alloc and buf): 0.247 sec; t1/t4: 2.914