What is the purpose and usage of `memory_resource`?
Asked Answered
H

1

31

The standard C++17 include a new namespace pmr including a set of classes grouped under the name of memory_resource.

After a search on internet, I found very few vulgarized information about it, the direct consequence is this question:

What are the main ideas behind pmr and especially pmr::memory_resource?


Detailing a bit more the question, some of the question marks in my head are:

  • What does it bring new, or what were the limitations it solve?
  • What is the difference with allocator?
  • Does polymorphic mean it is possible to select runtime the allocator provided to a container constructor? (e.g. for testing purpose)
  • Does it helps for implementing memory pool, or other memory management schemes?

Context:

In the intend of creating a memory pool allocator, I found information about this namespace. Reading names like pool_options or polymorphic_allocator raised my attention.


Related questions:

polymorphic_allocator: when and why should I use it?

Handal answered 28/6, 2017 at 9:58 Comment(6)
en.cppreference.com/w/cpp/memory/memory_resource – Micropyle
Also see #38011044 – Keishakeisling
@Curious: Very interesting link, and if you agree, I will relate it as it explain a sub-set of the topic. – Handal
@AdrianMaire I don't think I follow, do you mean you want to include it in the question? If so go for it πŸ‘πŸ» – Keishakeisling
@AdrianMaire: But all of your questions are answered there. memory_resource is what you derive from to achieve the polymorphic allocation functionality, as exposed through the allocator type polymorphic_allocator. – Amylum
@NicolBolas: inclusion is mono-directional: Polymorphic_allocator is part of memory_resource, but memory_resource is not only polymorphic_allocator. I especially focus memory pools, so there is a full chapter about pool_options, synchronized_pool_resource, unsynchronized_pool_resource monotonic_buffer_resource, memory_resource class.. and only polymorphic_allocator is half answered. On the other hand, you closed because of duplicated question not duplicated answer (Remember that people do not know the answer and search by question) – Handal
H
13

A polymorphic_allocator is intended to let you have an allocator whose behavior is dynamically determined at runtime.

The only way to create a polymorphic_allocator is:

  1. Default constructed, in which case it uses std::pmr::get_default_resource() return value, which is a memory_resource*.

  2. Pass it a memory_resource*.

  3. copy from another polymorphic_allocator.

So the point of customization for a polymorphic_allocator is creating a class that inherits from memory_resource and implementing its methods, or using one of the pre-declared memory_resources that are defined in std::pmr: (un)synchronized_pool_resource and monotonic_buffer_resource as types, or std::pmr::new_delete_resource() / std::pmr::null_memory_resource().

Suppose you want a your memory to be allocated using a strategy different than the 5 provided in std::pmr. Then you make a class that inherits from std::pmr::memory_resource, and pass it to a container which uses a polymorphic_allocator.

Huba answered 29/8, 2018 at 18:17 Comment(7)
What is the difference compared to allocator? – Prelature
@matt first paragraph? – Huba
Could you give more information as to why a regular allocator can't determine its behavior at runtime? After all, it is a function that gets invoked in the same way. – Prelature
@mattf An allocator is a type that satisfies specific requirements, not a function. pmr is a type that satisfies the allocator requirements, so you are free to reproduce the implementation of pmr in your hand written allocator type. Such a hand written pmr won't bestd::pmr, but it will otherwise behave the same. – Huba
Allocators are a bit wierd. If you have questions about them in general, I'd advise asking a question rather than making a comment. – Huba
Such a hand written pmr won't be std::pmr, but it will otherwise behave the same. Might be good to add that to the answer as I felt it didn't address that part of the question as it stands. – Prelature
@markf I am answering what pmr is, not how allocators work. I get that there are people not fluent in C++, but honestly I have to assume some competence. "What is a template", "what is an allocator", "what is a class", "what is a type", "how do std containers use allocators" - I can write a book. I am answering what pmr is, assuming you understand all of that (and more). The other questions are best solved via the [ask question] button. Someone can write a better answer for each of those problems. – Huba

© 2022 - 2024 β€” McMap. All rights reserved.