diff options
Diffstat (limited to 'thirdparty/tourist/foundation/src/ref.cpp')
| -rw-r--r-- | thirdparty/tourist/foundation/src/ref.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/thirdparty/tourist/foundation/src/ref.cpp b/thirdparty/tourist/foundation/src/ref.cpp new file mode 100644 index 000000000..feda5b0dd --- /dev/null +++ b/thirdparty/tourist/foundation/src/ref.cpp @@ -0,0 +1,69 @@ +#include <foundation/buffer.h> + +#include "slab.h" + +//------------------------------------------------------------------------------ +Pointer::Pointer(Slab* slab, const uint8* ptr) +: _ptr(uintptr(ptr)) +, _pinned(0) +{ + uintptr bias = (uintptr(ptr) - uintptr(slab)) / alignof(Slab); + _slab_offset = bias; +} + +//------------------------------------------------------------------------------ +Pointer::Pointer(Pointer&& rhs) +{ + *this = std::move(rhs); +} + +//------------------------------------------------------------------------------ +Pointer::~Pointer() +{ + if (_pinned) + get_slab()->dec_ref(); +} + +//------------------------------------------------------------------------------ +void Pointer::operator = (Pointer&& rhs) +{ + std::swap(_value, rhs._value); +} + +//------------------------------------------------------------------------------ +bool Pointer::is_valid() const +{ + return _ptr != 0; +} + +//------------------------------------------------------------------------------ +void Pointer::pin() +{ + if (!_pinned) + get_slab()->inc_ref(); + _pinned = 1; +} + +//------------------------------------------------------------------------------ +const uint8* Pointer::get() const +{ + return (uint8*)_ptr; +} + +//------------------------------------------------------------------------------ +Slab* Pointer::get_slab() const +{ + uintptr bias = _slab_offset * alignof(Slab); + bias += uintptr(_ptr) & (alignof(Slab) - 1); + return (Slab*)(_ptr - bias); +} + + + +//------------------------------------------------------------------------------ +BufferRef::BufferRef(Slab* slab, const uint8* ptr) +: Pointer(slab, ptr) +{ + pin(); +} + |