Skip to content

Commit

Permalink
karm: Update from standalone.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 9, 2024
1 parent b4d34d2 commit da1a367
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 86 deletions.
5 changes: 5 additions & 0 deletions src/libs/karm-base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Karm {
#define throw_unless _Pragma("GCC error \"throw_unless is not allowed\"") throw_unless
#define dynamic_cast _Pragma("GCC error \"dynamic_cast is not allowed\"") dynamic_cast

// Safety

// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
#define lifetimebound [[clang::lifetimebound]]

// Utilities

#define arrayLen$(ARR) (sizeof(ARR) / sizeof(ARR[0]))
Expand Down
12 changes: 6 additions & 6 deletions src/libs/karm-base/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ struct Box {
return unwrap() <=> other.unwrap();
}

constexpr T *operator->() {
constexpr T *operator->() lifetimebound {
return &unwrap();
}

constexpr T &operator*() {
constexpr T &operator*() lifetimebound {
return unwrap();
}

constexpr T const *operator->() const {
constexpr T const *operator->() const lifetimebound {
return &unwrap();
}

constexpr T const &operator*() const {
constexpr T const &operator*() const lifetimebound {
return unwrap();
}

constexpr T const &unwrap() const {
constexpr T const &unwrap() const lifetimebound {
if (not _ptr) [[unlikely]]
panic("deferencing moved from Box<T>");
return *_ptr;
}

constexpr T &unwrap() {
constexpr T &unwrap() lifetimebound {
if (not _ptr) [[unlikely]]
panic("deferencing moved from Box<T>");
return *_ptr;
Expand Down
20 changes: 12 additions & 8 deletions src/libs/karm-base/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Karm {

#pragma clang unsafe_buffer_usage begin

/// A dynamically sized array of elements.
/// Often used as a backing store for other data structures. (e.g. `Vec`)
template <typename T>
Expand Down Expand Up @@ -86,11 +88,11 @@ struct Buf {
return *this;
}

constexpr T &operator[](usize i) {
constexpr T &operator[](usize i) lifetimebound {
return _buf[i].unwrap();
}

constexpr T const &operator[](usize i) const {
constexpr T const &operator[](usize i) const lifetimebound {
return _buf[i].unwrap();
}

Expand Down Expand Up @@ -254,13 +256,13 @@ struct Buf {
return ret;
}

T *buf() {
T *buf() lifetimebound {
if (_buf == nullptr)
return nullptr;
return &_buf->unwrap();
}

T const *buf() const {
T const *buf() const lifetimebound {
if (_buf == nullptr)
return nullptr;

Expand Down Expand Up @@ -356,11 +358,11 @@ struct InlineBuf {
return *this;
}

constexpr T &operator[](usize i) {
constexpr T &operator[](usize i) lifetimebound {
return _buf[i].unwrap();
}

constexpr T const &operator[](usize i) const {
constexpr T const &operator[](usize i) const lifetimebound {
return _buf[i].unwrap();
}

Expand Down Expand Up @@ -462,11 +464,11 @@ struct InlineBuf {
_len = newLen;
}

T *buf() {
T *buf() lifetimebound {
return &_buf[0].unwrap();
}

T const *buf() const {
T const *buf() const lifetimebound {
return &_buf[0].unwrap();
}

Expand Down Expand Up @@ -682,4 +684,6 @@ struct ViewBuf {
}
};

#pragma clang unsafe_buffer_usage end

} // namespace Karm
4 changes: 4 additions & 0 deletions src/libs/karm-base/cstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Karm {

#pragma clang unsafe_buffer_usage begin

using CStr = char const *;

always_inline constexpr usize cstrLen(CStr s) {
Expand All @@ -22,4 +24,6 @@ always_inline constexpr bool cstrEq(char const *str1, char const *str2) {
return *str1 == *str2;
}

#pragma clang unsafe_buffer_usage end

} // namespace Karm
10 changes: 5 additions & 5 deletions src/libs/karm-base/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Karm {

#pragma clang unsafe_buffer_usage begin

template <typename T>
struct Cursor {
using Inner = T;
Expand Down Expand Up @@ -115,10 +117,6 @@ struct Cursor {
return Bytes{_begin, _end};
}

always_inline constexpr MutBytes bytes() {
return MutBytes{_begin, _end};
}

/// Creates a rollback point for the cursor. If not manually disarmed,
/// the cursor's state will be restored to its position at the time of
/// this rollback point's creation when it goes out of scope.
Expand Down Expand Up @@ -256,7 +254,7 @@ struct MutCursor {
return Bytes{_begin, _end};
}

always_inline constexpr MutBytes bytes() {
always_inline constexpr MutBytes mutBytes() {
return MutBytes{_begin, _end};
}

Expand All @@ -270,4 +268,6 @@ struct MutCursor {
}
};

#pragma clang unsafe_buffer_usage end

} // namespace Karm
14 changes: 7 additions & 7 deletions src/libs/karm-base/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,19 @@ struct List {

// MARK: Random Access

T &peek(usize i) {
T &peek(usize i) lifetimebound {
return _ll.peek(i)->value;
}

T const &peek(usize i) const {
T const &peek(usize i) const lifetimebound {
return _ll.peek(i)->value;
}

T &operator[](usize i) {
T &operator[](usize i) lifetimebound {
return peek(i);
}

T const &operator[](usize i) const {
T const &operator[](usize i) const lifetimebound {
return peek(i);
}

Expand All @@ -295,11 +295,11 @@ struct List {
return buf;
}

T &peekFront() {
T &peekFront() lifetimebound {
return _ll.head()->value;
}

T const &peekFront() const {
T const &peekFront() const lifetimebound {
return _ll.head()->value;
}

Expand All @@ -325,7 +325,7 @@ struct List {
return value;
}

T &peekBack() {
T &peekBack() lifetimebound {
return _ll.tail()->value;
}

Expand Down
26 changes: 13 additions & 13 deletions src/libs/karm-base/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct _Cell {

virtual ~_Cell() = default;

virtual void *_unwrap() = 0;
virtual void *_unwrap() lifetimebound = 0;

virtual void clear() = 0;

Expand All @@ -40,7 +40,7 @@ struct _Cell {
}
}

_Cell *refStrong() {
_Cell *refStrong() lifetimebound {
LockScope scope(_lock);

if (_clear) [[unlikely]]
Expand All @@ -63,7 +63,7 @@ struct _Cell {
collectAndRelease();
}

_Cell *refWeak() {
_Cell *refWeak() lifetimebound {
LockScope scope(_lock);

_weak++;
Expand All @@ -84,7 +84,7 @@ struct _Cell {
}

template <typename T>
T &unwrap() {
T &unwrap() lifetimebound {
if (_clear) [[unlikely]]
panic("unwrap() called on cleared cell");

Expand All @@ -101,7 +101,7 @@ struct Cell : public _Cell {
_buf.ctor(std::forward<Args>(args)...);
}

void *_unwrap() override {
void *_unwrap() lifetimebound override {
return &_buf.unwrap();
}

Expand Down Expand Up @@ -169,19 +169,19 @@ struct Strong {

// MARK: Operators ---------------------------------------------------------

constexpr T const *operator->() const {
constexpr T const *operator->() const lifetimebound {
return &unwrap();
}

constexpr T *operator->() {
constexpr T *operator->() lifetimebound {
return &unwrap();
}

constexpr T const &operator*() const {
constexpr T const &operator*() const lifetimebound {
return unwrap();
}

constexpr T &operator*() {
constexpr T &operator*() lifetimebound {
return unwrap();
}

Expand All @@ -207,18 +207,18 @@ struct Strong {
panic("null dereference");
}

constexpr T const &unwrap() const {
constexpr T const &unwrap() const lifetimebound {
ensure();
return _cell->unwrap<T>();
}

constexpr T &unwrap() {
constexpr T &unwrap() lifetimebound {
ensure();
return _cell->unwrap<T>();
}

template <Meta::Derive<T> U>
constexpr U const &unwrap() const {
constexpr U const &unwrap() const lifetimebound {
ensure();
if (not is<U>()) [[unlikely]]
panic("unwrapping T as U");
Expand All @@ -227,7 +227,7 @@ struct Strong {
}

template <Meta::Derive<T> U>
constexpr U &unwrap() {
constexpr U &unwrap() lifetimebound {
ensure();
if (not is<U>()) [[unlikely]]
panic("unwrapping T as U");
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-base/res.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct [[nodiscard]] Res {
requires Meta::Comparable<Inner>
= default;

always_inline auto operator==(bool b) const {
always_inline bool operator==(bool b) const {
return has() == b;
}
};
Expand Down
10 changes: 7 additions & 3 deletions src/libs/karm-base/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Karm {

#pragma clang unsafe_buffer_usage begin

using Byte = u8;

inline constexpr Byte operator""_byte(unsigned long long arg) noexcept {
Expand Down Expand Up @@ -479,20 +481,20 @@ always_inline constexpr void stableSort(MutSliceable auto &slice) {
}

template <Sliceable T, typename U = T::Inner>
always_inline constexpr Opt<usize> indexOf(T const &slice, U const &needle) {
always_inline constexpr Opt<usize> indexOf(T const &slice, Meta::Equatable<U> auto const &needle) {
for (usize i = 0; i < slice.len(); i++)
if (slice[i] == needle)
return i;
return NONE;
}

template <Sliceable T, typename U = T::Inner>
always_inline constexpr bool contains(T const &slice, U const &needle) {
always_inline constexpr bool contains(T const &slice, Meta::Equatable<U> auto const &needle) {
return indexOf(slice, needle).has();
}

template <Sliceable T, typename U = T::Inner>
always_inline constexpr Opt<usize> lastIndexOf(T const &slice, U const &needle) {
always_inline constexpr Opt<usize> lastIndexOf(T const &slice, Meta::Equatable<U> auto const &needle) {
for (usize i = slice.len(); i > 0; i--)
if (slice[i - 1] == needle)
return i - 1;
Expand Down Expand Up @@ -627,4 +629,6 @@ always_inline Match endWith(Sliceable auto const &slice, Sliceable auto const &s
return Match::YES;
}

#pragma clang unsafe_buffer_usage end

} // namespace Karm
Loading

0 comments on commit da1a367

Please sign in to comment.