about summary refs log tree commit diff
path: root/immer/transience/gc_transience_policy.hpp
blob: d3c30efa1cf770d84d5a216841d660e609fd4e29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include <immer/heap/tags.hpp>

#include <atomic>
#include <memory>
#include <utility>

namespace immer {

/*!
 * Provides transience ownership tracking when a *tracing garbage
 * collector* is used instead of reference counting.
 *
 * @rst
 *
 * .. warning:: Using this policy without an allocation scheme that
 *    includes automatic tracing garbage collection may cause memory
 *    leaks.
 *
 * @endrst
 */
struct gc_transience_policy
{
    template <typename HeapPolicy>
    struct apply
    {
        struct type
        {
            using heap_ = typename HeapPolicy::type;

            struct edit
            {
                void* v;
                edit() = delete;
                bool operator==(edit x) const { return v == x.v; }
                bool operator!=(edit x) const { return v != x.v; }
            };

            struct owner
            {
                void* make_token_()
                {
                    return heap_::allocate(1, norefs_tag{});
                };

                mutable std::atomic<void*> token_;

                operator edit() { return {token_}; }

                owner()
                    : token_{make_token_()}
                {}
                owner(const owner& o)
                    : token_{make_token_()}
                {
                    o.token_ = make_token_();
                }
                owner(owner&& o) noexcept
                    : token_{o.token_.load()}
                {}
                owner& operator=(const owner& o)
                {
                    o.token_ = make_token_();
                    token_   = make_token_();
                    return *this;
                }
                owner& operator=(owner&& o) noexcept
                {
                    token_ = o.token_.load();
                    return *this;
                }
            };

            struct ownee
            {
                edit token_{nullptr};

                ownee& operator=(edit e)
                {
                    assert(e != noone);
                    // This would be a nice safety plug but it sadly
                    // does not hold during transient concatenation.
                    // assert(token_ == e || token_ == edit{nullptr});
                    token_ = e;
                    return *this;
                }

                bool can_mutate(edit t) const { return token_ == t; }
                bool owned() const { return token_ != edit{nullptr}; }
            };

            static owner noone;
        };
    };
};

template <typename HP>
typename gc_transience_policy::apply<HP>::type::owner
    gc_transience_policy::apply<HP>::type::noone = {};

} // namespace immer