Commit 0c29a7ee authored by jan.koester's avatar jan.koester
Browse files

Merge branch 'gtk-desktop': set/map node handles keep the tree node (Firefox content processes)

parents 4326986c 97856ac3
Loading
Loading
Loading
Loading
+39 −15
Changes for include/cxx/__node_handle: 39 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -30,9 +30,12 @@

/* Node handles of the ordered containers (set/map ::extract(),
 * ::insert(node_type&&); real gap: libdnf5 takes keys out of a set with
 * extract()). The tree nodes here aren't handed out: a handle owns the
 * element itself, moved out of the container, and insert() moves it
 * back in. */
 * extract()). The ordered containers hand out the tree node itself:
 * extract() unlinks it without destroying it and insert() links the same
 * node back, so pointers and iterators to the element stay valid, as the
 * standard requires (real gap: Firefox's TaskController keeps set
 * iterators in its tasks across extract()/merge()). The unordered
 * containers still move the element into a handle of its own. */

#include <new>
#include <utility>
@@ -42,30 +45,51 @@ namespace std __attribute__((__visibility__("default"))) {
template<class V>
class __node_handle_base {
public:
    __node_handle_base() noexcept : _has(false) {}
    __node_handle_base(__node_handle_base&& o) noexcept : _has(false) {
        if (o._has) { ::new (static_cast<void*>(_buf)) V(std::move(o.__v())); _has = true; o.reset(); }
    }
    __node_handle_base() noexcept : _v(nullptr), _node(nullptr), _free(nullptr) {}
    __node_handle_base(__node_handle_base&& o) noexcept
        : _v(o._v), _node(o._node), _free(o._free) { o._v = nullptr; o._node = nullptr; o._free = nullptr; }
    __node_handle_base& operator=(__node_handle_base&& o) noexcept {
        if (this != &o) {
            reset();
            if (o._has) { ::new (static_cast<void*>(_buf)) V(std::move(o.__v())); _has = true; o.reset(); }
            _v = o._v; _node = o._node; _free = o._free;
            o._v = nullptr; o._node = nullptr; o._free = nullptr;
        }
        return *this;
    }
    ~__node_handle_base() { reset(); }

    bool empty() const noexcept { return !_has; }
    explicit operator bool() const noexcept { return _has; }
    bool empty() const noexcept { return !_v; }
    explicit operator bool() const noexcept { return _v != nullptr; }

    /* a handle that owns a loose element (containers without node reuse) */
    template<class... A>
    void __emplace(A&&... a) { reset(); ::new (static_cast<void*>(_buf)) V(std::forward<A>(a)...); _has = true; }
    V& __v() const noexcept { return *reinterpret_cast<V*>(const_cast<unsigned char*>(_buf)); }
    void reset() noexcept { if (_has) { __v().~V(); _has = false; } }
    void __emplace(A&&... a) {
        reset();
        V* v = new V(std::forward<A>(a)...);
        _v = v; _node = v; _free = &__free_value;
    }
    /* a handle that owns a container node taken out with extract() */
    void __adopt(V* v, void* node, void (*free_node)(void*)) noexcept {
        reset();
        _v = v; _node = node; _free = free_node;
    }
    /* gives the node back to a container (insert(node_type&&)) */
    void* __release() noexcept {
        void* n = _node;
        _v = nullptr; _node = nullptr; _free = nullptr;
        return n;
    }
    void* __node() const noexcept { return _node; }
    V& __v() const noexcept { return *_v; }
    void reset() noexcept {
        if (_v) { _free(_node); _v = nullptr; _node = nullptr; _free = nullptr; }
    }

private:
    alignas(V) unsigned char _buf[sizeof(V)];
    bool _has;
    static void __free_value(void* p) noexcept { delete static_cast<V*>(p); }
    V* _v;
    void* _node;
    void (*_free)(void*);
};

template<class K>
+28 −0
Changes for include/cxx/__rb_tree: 28 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -584,6 +584,34 @@ public:
        return next;
    }

    /* Node handles (set/map extract(), insert(node_type&&), merge()):
     * a node leaves the tree without being destroyed and comes back as
     * the same node, so iterators and pointers to it stay valid. */
    void* __unlink(iterator pos) noexcept {
        node_base* n = __rb_detail::__rb_erase(pos._n, _header.parent, _header.left, _header.right);
        _size--;
        return static_cast<void*>(n);
    }
    static Value* __node_value(void* n) noexcept {
        return &static_cast<node*>(static_cast<node_base*>(n))->value;
    }
    /* frees a node still held by a handle (the handle has no tree) */
    static void __free_node(void* n) noexcept {
        node* p = static_cast<node*>(static_cast<node_base*>(n));
        _node_alloc_t a;
        p->~node();
        _nat::deallocate(a, p, 1);
    }
    pair<iterator, bool> __relink_unique(void* n) {
        node* nn = static_cast<node*>(static_cast<node_base*>(n));
        node_base* found = __find_insert_pos_unique(KeyOfValue()(nn->value));
        if (found) return make_pair(iterator(found), false);
        return make_pair(iterator(__link(nn)), true);
    }
    iterator __relink_equal(void* n) {
        return iterator(__link_equal(static_cast<node*>(static_cast<node_base*>(n))));
    }

    void clear() {
        __clear_subtree(_header.parent);
        __init_header();
+10 −14
Changes for include/cxx/map: 10 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -234,19 +234,13 @@ public:
    }

    /* Real merge() -- was missing (real gap: lldb's ObjectFileELF.cpp).
     * Moves every element of o whose key isn't already here; the rest
     * stay in o, as the standard specifies. Re-inserts values instead of
     * splicing nodes, which is observably the same except that
     * references into o's moved elements don't carry over. */
     * Moves the nodes of o whose keys aren't here yet (iterators and
     * references to them stay valid); the rest stay in o. */
    template<class C2>
    void merge(map<K, V, C2, Alloc>& o) {
        for (auto it = o.begin(); it != o.end();) {
            if (find(it->first) == end()) {
                _tree.emplace_unique(it->first, std::move(it->second));
                it = o.erase(it);
            } else {
                ++it;
            }
            auto cur = it++;
            if (find(cur->first) == end()) insert(o.extract(cur));
        }
    }
    template<class C2>
@@ -588,8 +582,9 @@ public:

    node_type extract(iterator pos) {
        node_type nh;
        nh.__emplace(std::move(const_cast<K&>(pos->first)), std::move(pos->second));
        erase(pos);
        void* n = _tree.__unlink(pos);
        /* the node holds pair<const K, V>; the handle exposes a mutable key */
        nh.__adopt(reinterpret_cast<pair<K, V>*>(Tree::__node_value(n)), n, &Tree::__free_node);
        return nh;
    }
    node_type extract(const K& k) {
@@ -598,10 +593,11 @@ public:
    }
    insert_return_type insert(node_type&& nh) {
        if (nh.empty()) return {end(), false, node_type()};
        pair<iterator, bool> r = insert(value_type(std::move(nh.key()), std::move(nh.mapped())));
        if (r.second) { nh.reset(); return {r.first, true, node_type()}; }
        auto r = _tree.__relink_unique(nh.__node());
        if (r.second) { nh.__release(); return {r.first, true, node_type()}; }
        return {r.first, false, std::move(nh)};
    }
    iterator insert(const_iterator /*hint*/, node_type&& nh) { return insert(std::move(nh)).position; }

    iterator erase(iterator it) { return _tree.erase(it); }

+11 −13
Changes for include/cxx/set: 11 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -290,8 +290,8 @@ public:

    node_type extract(iterator pos) {
        node_type nh;
        nh.__emplace(std::move(const_cast<K&>(*pos)));
        erase(pos);
        void* n = _tree.__unlink(typename Tree::iterator(pos._n));
        nh.__adopt(Tree::__node_value(n), n, &Tree::__free_node);
        return nh;
    }
    node_type extract(const K& k) {
@@ -300,10 +300,11 @@ public:
    }
    insert_return_type insert(node_type&& nh) {
        if (nh.empty()) return {end(), false, node_type()};
        pair<iterator, bool> r = insert(std::move(nh.value()));
        if (r.second) { nh.reset(); return {r.first, true, node_type()}; }
        return {r.first, false, std::move(nh)};
        auto r = _tree.__relink_unique(nh.__node());
        if (r.second) { nh.__release(); return {iterator(r.first), true, node_type()}; }
        return {iterator(r.first), false, std::move(nh)};
    }
    iterator insert(const_iterator /*hint*/, node_type&& nh) { return insert(std::move(nh)).position; }

    size_t erase(const K& k) {
        iterator it = find(k);
@@ -317,17 +318,14 @@ public:
     * holds, `t.gaps.erase(it)`, rather than re-searching by key).
     * Real standard contract: returns an iterator to the element that
     * followed the erased one. */
    /* merge (real gap: Firefox's TaskController.cpp): moves the elements
     * not already here, the rest stay in o */
    /* merge (real gap: Firefox's TaskController.cpp): moves the nodes
     * whose keys aren't here yet (iterators to them stay valid), the
     * rest stay in o */
    template<class C2>
    void merge(set<K, C2, Alloc>& o) {
        for (auto it = o.begin(); it != o.end();) {
            if (find(*it) == end()) {
                insert(std::move(const_cast<K&>(*it)));
                it = o.erase(it);
            } else {
                ++it;
            }
            auto cur = it++;
            if (find(*cur) == end()) insert(o.extract(cur));
        }
    }
    template<class C2>
+9 −2
Changes for include/cxx/unordered_map: 9 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -534,8 +534,15 @@ public:
    typedef __insert_return_type<iterator, node_type> insert_return_type;
    node_type extract(iterator pos) {
        node_type nh;
        nh.__emplace(std::move(const_cast<K&>(pos->first)), std::move(pos->second));
        erase(pos);
        /* unlink the node first: erasing by key after moving the key
         * into the handle looked up the moved-from key */
        node* n = pos._n;
        node** link = &_buckets[pos._bidx];
        while (*link != n) link = &(*link)->next;
        *link = n->next;
        _size--;
        nh.__emplace(std::move(const_cast<K&>(n->kv.first)), std::move(n->kv.second));
        _del_node(n);
        return nh;
    }
    node_type extract(const K& k) {
Loading