upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/buffer_va_index.h
blob: 3ab8edf82c98babaa3de6d6aabb5f9cef592af75 (plain)
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
#pragma once

// Which registered buffer a console VA falls in (VulkanContext::buffer_at, for vertex and index views and
// WriteBufferImmediate). Ranges are kept ordered by their first VA, so a lookup is a binary search instead of a scan of
// every registered resource. No Vulkan or Windows dependency; not thread-safe (VulkanContext guards it with
// resource_mutex_, like the bindings themselves).
//
// D3D12 lets placed buffers alias, so ranges may overlap. A lookup names the bytes it needs, [va, va + bytes), and
// returns, among the ranges that hold va, the one that starts closest below it (the larger ID on a tie) that also
// holds all of those bytes; when none does, the closest one that holds va. So a view that starts in a small placed
// buffer and runs past its end resolves to a larger buffer that aliases the same memory and holds all of it, instead
// of being cut at the small one's end (found in review of #2823). The walk goes down from the nearest start and stops
// once no range can still reach the VA, or, with a fallback found, once no range can still hold all the bytes; both
// are bounded by `longest_`. Without overlaps the first range it looks at is the answer, or none is.
// `longest_` is the longest range indexed now, not the longest ever: a large buffer that is freed stops lengthening
// later misses. A miss still walks every start within the longest live range below the VA.

#include <cstddef>
#include <cstdint>
#include <map>
#include <set>
#include <utility>

namespace leonida {

template<typename Id>
class BufferVaIndex {
public:
    // Registers [va, va + size). A null VA or an empty range is not indexed (no lookup can land in it).
    void insert(std::uint64_t va, std::uint64_t size, Id id) {
        if (!va || !size) return;
        const auto [it, added] = ranges_.try_emplace({va, id}, size);
        try {
            sizes_.insert(size);
        } catch (...) {
            if (added) ranges_.erase(it);
            throw;
        }
        if (!added) {  // the same (va, id) again: its new size replaces the old one
            drop_size(it->second);
            it->second = size;
        }
        longest_ = *sizes_.rbegin();
    }

    // Forgets what insert(va, size, id) registered; nothing when it was not indexed.
    void erase(std::uint64_t va, Id id) {
        const auto it = ranges_.find({va, id});
        if (it == ranges_.end()) return;
        drop_size(it->second);
        ranges_.erase(it);
        longest_ = sizes_.empty() ? 0 : *sizes_.rbegin();
    }

    struct Hit {
        Id id;
        std::uint64_t start;  // the range's first VA
        std::uint64_t size;
    };

    // The range that holds `va`, preferring one that holds [va, va + bytes) (bytes 0 counts as 1); false when none
    // holds va.
    bool find(std::uint64_t va, Hit& out, std::uint64_t bytes = 1) const {
        if (!bytes) bytes = 1;
        bool found = false;  // `out` holds the closest range that holds va, though not all the bytes
        auto it = ranges_.upper_bound({va, max_id()});
        while (it != ranges_.begin()) {
            --it;
            const std::uint64_t start = it->first.first;
            const std::uint64_t offset = va - start;  // start <= va here; it only grows as the walk goes down
            if (offset >= longest_) break;            // no range that starts this low reaches va
            if (found && bytes > longest_ - offset) break;  // nor holds all the bytes
            if (offset >= it->second) continue;
            if (bytes <= it->second - offset) {
                out = {it->first.second, start, it->second};
                return true;
            }
            if (!found) {
                out = {it->first.second, start, it->second};
                found = true;
            }
        }
        return found;
    }

    // find() can return a shorter alias. Only a complete range can serve the view without new backing.
    bool covers(std::uint64_t va, std::uint64_t bytes) const {
        Hit hit{};
        return find(va, hit, bytes) && bytes <= hit.size - (va - hit.start);
    }

    std::size_t size() const { return ranges_.size(); }
    std::uint64_t longest() const { return longest_; }
    // f(first VA, bytes) for every range, in VA order.
    template <class F>
    void for_each(F&& f) const {
        for (const auto& [key, bytes] : ranges_) f(key.first, bytes);
    }

private:
    static constexpr Id max_id() { return ~Id(0); }

    void drop_size(std::uint64_t size) { sizes_.erase(sizes_.find(size)); }  // one copy, not every range of that size

    std::map<std::pair<std::uint64_t, Id>, std::uint64_t> ranges_;  // (first VA, id) -> bytes
    std::multiset<std::uint64_t> sizes_;  // the size of every indexed range, so the longest is known after an erase
    std::uint64_t longest_ = 0;  // the longest indexed range, 0 when none is
};

} // namespace leonida