| Home | Libraries | People | FAQ | More

## Class memory_resource

boost::container::pmr::memory_resource

## Synopsis

    // In header: <boost/container/pmr/memory_resource.hpp>

    class memory_resource {
    public:
      // construct/copy/destruct
      ~memory_resource();

      // public member functions
      void * allocate(std::size_t, std::size_t = max_align);
      void deallocate(void *, std::size_t, std::size_t = max_align);
      bool is_equal(const memory_resource &) const noexcept;

      // friend functions
      friend bool operator==(const memory_resource &, const memory_resource &) noexcept;
      friend bool operator!=(const memory_resource &, const memory_resource &) noexcept;

      // protected member functions
      virtual void * do_allocate(std::size_t, std::size_t) = 0;
      virtual void do_deallocate(void *, std::size_t, std::size_t) = 0;
      virtual bool do_is_equal(const memory_resource &) const = 0;

      // public data members
      static constexpr std::size_t max_align;
    };

## Description

The memory_resource class is an abstract interface to an unbounded set of classes encapsulating memory resources.

### `memory_resource` public construct/copy/destruct

1.     ~memory_resource();

   Effects: Destroys this `memory_resource`.

### `memory_resource` public member functions

1.     void * allocate(std::size_t bytes, std::size_t alignment = max_align);

   Effects: Equivalent to `return do_allocate(bytes, alignment);`

2.     void deallocate(void * p, std::size_t bytes,
                       std::size_t alignment = max_align);

   Effects: Equivalent to `return do_deallocate(bytes, alignment);`

3.     bool is_equal(const memory_resource & other) const noexcept;

   Effects: Equivalent to `return return do_is_equal(other);`

### `memory_resource` friend functions

1.     friend bool operator==(const memory_resource & a, const memory_resource & b) noexcept;

   Returns: `&a == &b || a.is_equal(b)`.

2.     friend bool operator!=(const memory_resource & a, const memory_resource & b) noexcept;

   Returns: !(a == b).

### `memory_resource` protected member functions

1.     virtual void * do_allocate(std::size_t bytes, std::size_t alignment) = 0;

   Requires: Alignment shall be a power of two.

   Returns: A derived class shall implement this function to return a pointer to allocated storage with a size of at least bytes. The returned storage is aligned to the specified alignment, if such alignment is supported; otherwise it is aligned to max_align.

   Throws: A derived class implementation shall throw an appropriate exception if it is unable to allocate memory with the requested size and alignment.

2.     virtual void
       do_deallocate(void * p, std::size_t bytes, std::size_t alignment) = 0;

   Requires: p shall have been returned from a prior call to `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage at p shall not yet have been deallocated.

   Effects: A derived class shall implement this function to dispose of allocated storage.

   Throws: Nothing.

3.     virtual bool do_is_equal(const memory_resource & other) const = 0;

   Returns: A derived class shall implement this function to return true if memory allocated from this can be deallocated from other and vice-versa; otherwise it shall return false. [Note: The most-derived type of other might not match the type of this. For a derived class, D, a typical implementation of this function will compute `dynamic_cast<const D*>(&other)` and go no further (i.e., return false) if it returns nullptr. - end note].