diff options
| author | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
| commit | 0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch) | |
| tree | 94730e7594fd09ae1fa820391ce311f6daf13905 /thirdparty/tourist/foundation/src/ref.cpp | |
| parent | Fix forward declaration order for s_GotSigWinch and SigWinchHandler (diff) | |
| parent | trace: declare Region event name fields as AnsiString (#1012) (diff) | |
| download | archived-zen-sb/zen-help.tar.xz archived-zen-sb/zen-help.zip | |
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher
- Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
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(); +} + |