2013-07-16 17:11:14 +00:00
|
|
|
/*
|
2013-07-19 17:29:33 +00:00
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
2013-07-16 17:11:14 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
Author: Leonardo de Moura
|
2013-07-16 17:11:14 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Goodies for reference counting
|
2013-12-09 22:56:48 +00:00
|
|
|
#include "util/thread.h"
|
|
|
|
#include "util/debug.h"
|
|
|
|
|
2013-07-16 17:11:14 +00:00
|
|
|
#define MK_LEAN_RC() \
|
2013-07-22 11:03:46 +00:00
|
|
|
private: \
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic<unsigned> m_rc; \
|
2013-07-22 11:03:46 +00:00
|
|
|
public: \
|
2013-12-09 22:56:48 +00:00
|
|
|
unsigned get_rc() const { return atomic_load(&m_rc); } \
|
|
|
|
void inc_ref() { atomic_fetch_add_explicit(&m_rc, 1u, memory_order_relaxed); } \
|
2014-07-21 15:22:58 +00:00
|
|
|
bool dec_ref_core() { \
|
|
|
|
lean_assert(get_rc() > 0); \
|
|
|
|
if (atomic_fetch_sub_explicit(&m_rc, 1u, memory_order_release) == 1u) { \
|
|
|
|
atomic_thread_fence(memory_order_acquire); \
|
|
|
|
return true; \
|
|
|
|
} else { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
void dec_ref() { if (dec_ref_core()) { dealloc(); } }
|
2013-07-29 05:34:39 +00:00
|
|
|
|
2013-12-13 23:00:30 +00:00
|
|
|
#define LEAN_COPY_REF(Arg) \
|
2013-07-29 05:34:39 +00:00
|
|
|
if (Arg.m_ptr) \
|
|
|
|
Arg.m_ptr->inc_ref(); \
|
2013-08-11 21:23:36 +00:00
|
|
|
auto new_ptr = Arg.m_ptr; \
|
2013-07-29 05:34:39 +00:00
|
|
|
if (m_ptr) \
|
|
|
|
m_ptr->dec_ref(); \
|
2013-09-13 23:14:24 +00:00
|
|
|
m_ptr = new_ptr; \
|
2013-07-29 05:34:39 +00:00
|
|
|
return *this;
|
|
|
|
|
2013-12-13 23:00:30 +00:00
|
|
|
#define LEAN_MOVE_REF(Arg) \
|
2013-07-29 05:34:39 +00:00
|
|
|
if (m_ptr) \
|
|
|
|
m_ptr->dec_ref(); \
|
|
|
|
m_ptr = Arg.m_ptr; \
|
2013-08-01 20:57:43 +00:00
|
|
|
Arg.m_ptr = nullptr; \
|
2013-07-29 05:34:39 +00:00
|
|
|
return *this;
|