Path: blob/21.2-virgl/src/gallium/frontends/clover/util/pointer.hpp
4572 views
//1// Copyright 2013 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#ifndef CLOVER_UTIL_POINTER_HPP23#define CLOVER_UTIL_POINTER_HPP2425#include <atomic>2627namespace clover {28///29/// Some helper functions for raw pointer operations30///31template <class T>32static bool33ptr_is_aligned(const T *ptr, uintptr_t a) noexcept {34assert(a == (a & -a));35uintptr_t ptr_value = reinterpret_cast<uintptr_t>(ptr);36return (ptr_value & (a - 1)) == 0;37}3839///40/// Base class for objects that support reference counting.41///42class ref_counter {43public:44ref_counter(unsigned value = 1) : _ref_count(value) {}4546unsigned47ref_count() const {48return _ref_count;49}5051void52retain() {53_ref_count++;54}5556bool57release() {58return (--_ref_count) == 0;59}6061private:62std::atomic<unsigned> _ref_count;63};6465///66/// Simple reference to a clover::ref_counter object. Unlike67/// clover::intrusive_ptr and clover::intrusive_ref, it does nothing68/// special when the reference count drops to zero.69///70class ref_holder {71public:72ref_holder(ref_counter &o) : p(&o) {73p->retain();74}7576ref_holder(const ref_holder &ref) :77ref_holder(*ref.p) {78}7980ref_holder(ref_holder &&ref) :81p(ref.p) {82ref.p = NULL;83}8485~ref_holder() {86if (p)87p->release();88}8990ref_holder &91operator=(ref_holder ref) {92std::swap(ref.p, p);93return *this;94}9596bool97operator==(const ref_holder &ref) const {98return p == ref.p;99}100101bool102operator!=(const ref_holder &ref) const {103return p != ref.p;104}105106private:107ref_counter *p;108};109110///111/// Intrusive smart pointer for objects that implement the112/// clover::ref_counter interface.113///114template<typename T>115class intrusive_ptr {116public:117intrusive_ptr(T *q = NULL) : p(q) {118if (p)119p->retain();120}121122intrusive_ptr(const intrusive_ptr &ptr) :123intrusive_ptr(ptr.p) {124}125126intrusive_ptr(intrusive_ptr &&ptr) :127p(ptr.p) {128ptr.p = NULL;129}130131~intrusive_ptr() {132if (p && p->release())133delete p;134}135136intrusive_ptr &137operator=(intrusive_ptr ptr) {138std::swap(ptr.p, p);139return *this;140}141142bool143operator==(const intrusive_ptr &ref) const {144return p == ref.p;145}146147bool148operator!=(const intrusive_ptr &ref) const {149return p != ref.p;150}151152T &153operator*() const {154return *p;155}156157T *158operator->() const {159return p;160}161162T *163operator()() const {164return p;165}166167explicit operator bool() const {168return p;169}170171explicit operator T *() const {172return p;173}174175private:176T *p;177};178179///180/// Intrusive smart reference for objects that implement the181/// clover::ref_counter interface.182///183template<typename T>184class intrusive_ref {185public:186intrusive_ref(T &o) : p(&o) {187p->retain();188}189190intrusive_ref(const intrusive_ref &ref) :191intrusive_ref(*ref.p) {192}193194intrusive_ref(intrusive_ref &&ref) :195p(ref.p) {196ref.p = NULL;197}198199~intrusive_ref() {200if (p && p->release())201delete p;202}203204intrusive_ref &205operator=(intrusive_ref ref) {206std::swap(ref.p, p);207return *this;208}209210bool211operator==(const intrusive_ref &ref) const {212return p == ref.p;213}214215bool216operator!=(const intrusive_ref &ref) const {217return p != ref.p;218}219220T &221operator()() const {222return *p;223}224225operator T &() const {226return *p;227}228229private:230T *p;231};232233///234/// Initialize a clover::intrusive_ref from a newly created object235/// using the specified constructor arguments.236///237template<typename T, typename... As>238intrusive_ref<T>239create(As &&... as) {240intrusive_ref<T> ref { *new T(std::forward<As>(as)...) };241ref().release();242return ref;243}244245///246/// Class that implements the usual pointer interface but in fact247/// contains the object it seems to be pointing to.248///249template<typename T>250class pseudo_ptr {251public:252pseudo_ptr(T x) : x(x) {253}254255pseudo_ptr(const pseudo_ptr &p) : x(p.x) {256}257258pseudo_ptr &259operator=(const pseudo_ptr &p) {260x = p.x;261return *this;262}263264T &265operator*() {266return x;267}268269T *270operator->() {271return &x;272}273274explicit operator bool() const {275return true;276}277278private:279T x;280};281}282283#endif284285286