2 // Copyright 2013 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef CLOVER_UTIL_POINTER_HPP
24 #define CLOVER_UTIL_POINTER_HPP
30 /// Some helper functions for raw pointer operations
34 ptr_is_aligned(const T *ptr, uintptr_t a) noexcept {
35 assert(a == (a & -a));
36 uintptr_t ptr_value = reinterpret_cast<uintptr_t>(ptr);
37 return (ptr_value & (a - 1)) == 0;
41 /// Base class for objects that support reference counting.
45 ref_counter(unsigned value = 1) : _ref_count(value) {}
59 return (--_ref_count) == 0;
63 std::atomic<unsigned> _ref_count;
67 /// Simple reference to a clover::ref_counter object. Unlike
68 /// clover::intrusive_ptr and clover::intrusive_ref, it does nothing
69 /// special when the reference count drops to zero.
73 ref_holder(ref_counter &o) : p(&o) {
77 ref_holder(const ref_holder &ref) :
81 ref_holder(ref_holder &&ref) :
92 operator=(ref_holder ref) {
98 operator==(const ref_holder &ref) const {
103 operator!=(const ref_holder &ref) const {
112 /// Intrusive smart pointer for objects that implement the
113 /// clover::ref_counter interface.
116 class intrusive_ptr {
118 intrusive_ptr(T *q = NULL) : p(q) {
123 intrusive_ptr(const intrusive_ptr &ptr) :
124 intrusive_ptr(ptr.p) {
127 intrusive_ptr(intrusive_ptr &&ptr) :
133 if (p && p->release())
138 operator=(intrusive_ptr ptr) {
144 operator==(const intrusive_ptr &ref) const {
149 operator!=(const intrusive_ptr &ref) const {
168 explicit operator bool() const {
172 explicit operator T *() const {
181 /// Intrusive smart reference for objects that implement the
182 /// clover::ref_counter interface.
185 class intrusive_ref {
187 intrusive_ref(T &o) : p(&o) {
191 intrusive_ref(const intrusive_ref &ref) :
192 intrusive_ref(*ref.p) {
195 intrusive_ref(intrusive_ref &&ref) :
201 if (p && p->release())
206 operator=(intrusive_ref ref) {
212 operator==(const intrusive_ref &ref) const {
217 operator!=(const intrusive_ref &ref) const {
226 operator T &() const {
235 /// Initialize a clover::intrusive_ref from a newly created object
236 /// using the specified constructor arguments.
238 template<typename T, typename... As>
240 create(As &&... as) {
241 intrusive_ref<T> ref { *new T(std::forward<As>(as)...) };
247 /// Class that implements the usual pointer interface but in fact
248 /// contains the object it seems to be pointing to.
253 pseudo_ptr(T x) : x(x) {
256 pseudo_ptr(const pseudo_ptr &p) : x(p.x) {
260 operator=(const pseudo_ptr &p) {
275 explicit operator bool() const {