20 #include <rmm/detail/error.hpp>
21 #include <rmm/mr/device/detail/free_list.hpp>
29 namespace rmm::mr::detail {
48 [[nodiscard]]
inline char*
pointer()
const {
return static_cast<char*
>(
ptr); }
55 [[nodiscard]]
inline std::size_t
size()
const {
return size_bytes; }
64 [[nodiscard]]
inline bool is_head()
const {
return head; }
102 return (
pointer() +
size() == blk.ptr) and not(blk.is_head());
111 [[nodiscard]]
inline bool fits(std::size_t bytes)
const noexcept {
return size() >= bytes; }
123 return fits(bytes) && (
size() < blk.size() || blk.size() < bytes);
126 #ifdef RMM_DEBUG_PRINT
130 inline void print()
const
132 std::cout << fmt::format(
"{} {} B", fmt::ptr(
pointer()),
size()) << std::endl;
137 std::size_t size_bytes{};
141 #ifdef RMM_DEBUG_PRINT
142 inline std::ostream& operator<<(std::ostream& out,
const block& blk)
145 out << fmt::format(
"{} {} B\n", fmt::ptr(blk.pointer()), blk.size());
157 template <
typename block_type>
160 using is_transparent = void;
162 bool operator()(block_type
const& lhs, block_type
const& rhs)
const {
return lhs < rhs; }
163 bool operator()(
char const* ptr, block_type
const& rhs)
const {
return ptr < rhs.pointer(); }
164 bool operator()(block_type
const& lhs,
char const* ptr)
const {
return lhs.pointer() < ptr; };
198 auto const previous = (next ==
cbegin()) ? next : std::prev(next);
201 bool const merge_prev = previous->is_contiguous_before(
block);
204 if (merge_prev && merge_next) {
205 *previous = previous->merge(
block).merge(*next);
207 }
else if (merge_prev) {
208 *previous = previous->merge(
block);
209 }
else if (merge_next) {
225 using std::make_move_iterator;
227 std::for_each(make_move_iterator(other.begin()), make_move_iterator(other.end()), inserter);
244 auto const iter = std::min_element(
cbegin(),
cend(), finder);
246 if (iter !=
cend() && iter->fits(
size)) {
256 #ifdef RMM_DEBUG_PRINT
262 std::cout <<
size() <<
'\n';
263 std::for_each(
cbegin(),
cend(), [](
auto const iter) { iter.print(); });
const_iterator cend() const noexcept
beginning of the free list
Definition: free_list.hpp:93
const_iterator cbegin() const noexcept
beginning of the free list
Definition: free_list.hpp:86
An ordered list of free memory blocks that coalesces contiguous blocks on insertion.
Definition: coalescing_free_list.hpp:172
bool is_contiguous_before(block const &blk) const noexcept
Verifies whether this block can be merged to the beginning of block b.
Definition: coalescing_free_list.hpp:99
iterator begin() noexcept
beginning of the free list
Definition: free_list.hpp:82
void erase(const_iterator iter)
Removes the block indicated by iter from the free list.
Definition: free_list.hpp:115
Base class defining an interface for a list of free memory blocks.
Definition: free_list.hpp:65
size_type size() const noexcept
The size of the free list in blocks.
Definition: free_list.hpp:100
A simple block structure specifying the size and location of a block of memory, with a flag indicatin...
Definition: coalescing_free_list.hpp:36
iterator end() noexcept
end of the free list
Definition: free_list.hpp:89
bool is_head() const
Returns whether this block is the start of an allocation from an upstream allocator.
Definition: coalescing_free_list.hpp:64
Comparator for block types based on pointer address.
Definition: coalescing_free_list.hpp:158
void insert(block_type const &block)
Inserts a block into the free_list in the correct order, coalescing it with the preceding and followi...
Definition: coalescing_free_list.hpp:187
void insert(free_list &&other)
Moves blocks from free_list other into this free_list in their correct order, coalescing them with th...
Definition: coalescing_free_list.hpp:223
void * ptr
Raw memory pointer.
Definition: free_list.hpp:26
bool is_empty() const noexcept
checks whether the free_list is empty.
Definition: free_list.hpp:108
std::size_t size() const
Returns the size of the memory represented by this block.
Definition: coalescing_free_list.hpp:55
char * pointer() const
Returns the pointer to the memory represented by this block.
Definition: coalescing_free_list.hpp:48
void insert(const_iterator pos, block_type const &block)
Insert a block in the free list before the specified position.
Definition: free_list.hpp:143
bool operator<(block const &rhs) const noexcept
Comparison operator to enable comparing blocks and storing in ordered containers.
Definition: coalescing_free_list.hpp:75
block merge(block const &blk) const noexcept
Coalesce two contiguous blocks into one.
Definition: coalescing_free_list.hpp:86
Definition: free_list.hpp:25
block_type get_block(std::size_t size)
Finds the smallest block in the free_list large enough to fit size bytes.
Definition: coalescing_free_list.hpp:238
bool fits(std::size_t bytes) const noexcept
Is this block large enough to fit sz bytes?
Definition: coalescing_free_list.hpp:111
bool is_better_fit(std::size_t bytes, block const &blk) const noexcept
Is this block a better fit for sz bytes than block b?
Definition: coalescing_free_list.hpp:121