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?

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • http://en.cppreference.com/w/cpp/memory/memory_resource – πάντα ῥεῖ Jun 28 '17 at 10:03
  • Also see https://stackoverflow.com/questions/38010544/polymorphic-allocator-when-and-why-should-i-use-it – Curious Jun 28 '17 at 10:18
  • @Curious: Very interesting link, and if you agree, I will relate it as it explain a sub-set of the topic. – Adrian Maire Jun 28 '17 at 10:54
  • @AdrianMaire I don't think I follow, do you mean you want to include it in the question? If so go for it – Curious Jun 28 '17 at 10:56
  • 1
    @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`. – Nicol Bolas Jun 28 '17 at 16:31
  • @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**) – Adrian Maire Jun 28 '17 at 17:46

1 Answers1

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • What is the difference compared to `allocator`? –  Oct 24 '21 at 03:13
  • @matt first paragraph? – Yakk - Adam Nevraumont Oct 24 '21 at 09:56
  • 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. –  Oct 24 '21 at 15:42
  • @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 be`std::pmr`, but it will otherwise behave the same. – Yakk - Adam Nevraumont Oct 24 '21 at 20:14
  • Allocators are a bit wierd. If you have questions about them in general, I'd advise asking a question rather than making a comment. – Yakk - Adam Nevraumont Oct 24 '21 at 20:21
  • `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. –  Oct 24 '21 at 21:09
  • @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. – Yakk - Adam Nevraumont Oct 24 '21 at 21:35