1//
2// immer: immutable data structures for C++
3// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4//
5// This software is distributed under the Boost Software License, Version 1.0.
6// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7//
8
9#pragma once
10
11#include <immer/heap/unsafe_free_list_heap.hpp>
12
13namespace immer {
14namespace detail {
15
16template <typename Heap>
17struct thread_local_free_list_storage
18{
19 struct head_t
20 {
21 free_list_node* data;
22 std::size_t count;
23
24 ~head_t() { Heap::clear(); }
25 };
26
27 static head_t& head()
28 {
29 thread_local static head_t head_{nullptr, 0};
30 return head_;
31 }
32};
33
34} // namespace detail
35
36/*!
37 * Adaptor that does not release the memory to the parent heap but
38 * instead it keeps the memory in a `thread_local` global free
39 * list. Must be preceded by a `with_data<free_list_node, ...>` heap
40 * adaptor. When the current thread finishes, the memory is returned
41 * to the parent heap.
42 *
43 * @tparam Size Maximum size of the objects to be allocated.
44 * @tparam Limit Maximum number of elements to keep in the free list.
45 * @tparam Base Type of the parent heap.
46 */
47template <std::size_t Size, std::size_t Limit, typename Base>
48struct thread_local_free_list_heap : detail::unsafe_free_list_heap_impl<
49 detail::thread_local_free_list_storage,
50 Size,
51 Limit,
52 Base>
53{};
54
55} // namespace immer
56