fixed_size_free_list.hpp
1 /*
2  * Copyright (c) 2020-2021, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rmm/mr/device/detail/free_list.hpp>
20 
21 #include <cstddef>
22 #include <iostream>
23 
24 namespace rmm::mr::detail {
25 
26 struct fixed_size_free_list : free_list<block_base> {
27  fixed_size_free_list() = default;
28  ~fixed_size_free_list() override = default;
29 
31  fixed_size_free_list& operator=(fixed_size_free_list const&) = delete;
33  fixed_size_free_list& operator=(fixed_size_free_list&&) = delete;
34 
42  template <class InputIt>
43  fixed_size_free_list(InputIt first, InputIt last)
44  {
45  std::for_each(first, last, [this](block_type const& block) { insert(block); });
46  }
47 
54  void insert(block_type const& block) { push_back(block); }
55 
61  void insert(free_list&& other) { splice(cend(), std::move(other)); }
62 
69  block_type get_block(std::size_t size)
70  {
71  if (is_empty()) { return block_type{}; }
72  block_type block = *begin();
73  pop_front();
74  return block;
75  }
76 };
77 
78 } // namespace rmm::mr::detail
rmm::mr::detail::fixed_size_free_list::insert
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: fixed_size_free_list.hpp:54
rmm::mr::detail::free_list< block_base >::cend
const_iterator cend() const noexcept
beginning of the free list
Definition: free_list.hpp:93
rmm::mr::detail::free_list< block_base >::begin
iterator begin() noexcept
beginning of the free list
Definition: free_list.hpp:82
rmm::mr::detail::free_list
Base class defining an interface for a list of free memory blocks.
Definition: free_list.hpp:65
rmm::mr::detail::free_list< block_base >::size
size_type size() const noexcept
The size of the free list in blocks.
Definition: free_list.hpp:100
rmm::mr::detail::fixed_size_free_list::fixed_size_free_list
fixed_size_free_list(InputIt first, InputIt last)
Construct a new free_list from range defined by input iterators.
Definition: fixed_size_free_list.hpp:43
rmm::mr::detail::block
A simple block structure specifying the size and location of a block of memory, with a flag indicatin...
Definition: coalescing_free_list.hpp:36
rmm::mr::detail::free_list< block_base >::push_back
void push_back(const block_type &block)
Appends the given block to the end of the free list.
Definition: free_list.hpp:161
rmm::mr::detail::fixed_size_free_list::get_block
block_type get_block(std::size_t size)
Returns the first block in the free list.
Definition: fixed_size_free_list.hpp:69
rmm::mr::detail::free_list< block_base >::is_empty
bool is_empty() const noexcept
checks whether the free_list is empty.
Definition: free_list.hpp:108
rmm::mr::detail::fixed_size_free_list
Definition: fixed_size_free_list.hpp:26
rmm::mr::detail::free_list< block_base >::pop_front
void pop_front()
Removes the first element of the free list. If there are no elements in the free list,...
Definition: free_list.hpp:176
rmm::mr::detail::fixed_size_free_list::insert
void insert(free_list &&other)
Inserts blocks from another free list into this free_list.
Definition: fixed_size_free_list.hpp:61
rmm::mr::detail::block_base
Definition: free_list.hpp:25
rmm::mr::detail::free_list< block_base >::splice
void splice(const_iterator pos, free_list &&other)
Inserts a list of blocks in the free list before the specified position.
Definition: free_list.hpp:151