Path: blob/21.2-virgl/src/gallium/frontends/clover/core/context.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "core/context.hpp"2324using namespace clover;2526context::context(const property_list &props,27const ref_vector<device> &devs,28const notify_action ¬ify) :29notify(notify), props(props), devs(devs) {30}3132context::~context() {33while (_destroy_notify.size()) {34_destroy_notify.top()();35_destroy_notify.pop();36}37}3839bool40context::operator==(const context &ctx) const {41return this == &ctx;42}4344bool45context::operator!=(const context &ctx) const {46return this != &ctx;47}4849void50context::destroy_notify(std::function<void ()> f) {51_destroy_notify.push(f);52}5354const context::property_list &55context::properties() const {56return props;57}5859context::device_range60context::devices() const {61return map(evals(), devs);62}6364void65context::add_svm_allocation(const void *ptr, size_t size) {66svm_ptrs.emplace(ptr, size);67}6869void70context::remove_svm_allocation(const void *ptr) {71svm_ptrs.erase(ptr);72}7374context::svm_pointer_map::value_type75context::find_svm_allocation(const void *ptr) const {76// std::prev on an iterator of an empty container causes SIGSEGVs77if (svm_ptrs.empty())78return { nullptr, 0 };7980auto it = std::prev(svm_ptrs.upper_bound(ptr));81if (it == svm_ptrs.end())82return { nullptr, 0 };8384uintptr_t base = reinterpret_cast<uintptr_t>((*it).first);85uintptr_t end = (*it).second + base;86uintptr_t ptrv = reinterpret_cast<uintptr_t>(ptr);87if (ptrv >= base && ptrv < end)88return *it;8990return { nullptr, 0 };91}929394