Skip to content

Commit

Permalink
api: fix reference counting for self-assigned expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Oct 2, 2024
1 parent 33c6945 commit 452130c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
7 changes: 6 additions & 1 deletion api/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ namespace jule
template <typename T>
Any(const jule::Ptr<T> &ref, jule::Any::Type *type) noexcept
{
if (ref == nullptr) {
if (ref == nullptr)
{
// Pointer is nil, so can't able to use as base allocation.
// Make a nil any.
return;
Expand Down Expand Up @@ -177,6 +178,10 @@ namespace jule

inline jule::Any &operator=(const jule::Any &src) noexcept
{
// Assignment to itself.
if (this->data == src.data)
return *this;

this->dealloc();
this->data = src.data;
this->type = src.type;
Expand Down
5 changes: 3 additions & 2 deletions api/ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace jule
buffer.alloc = new (std::nothrow) T;
if (!buffer.alloc)
__jule_panic_s(__JULE_ERROR__MEMORY_ALLOCATION_FAILED
"\nruntime: memory allocation failed for heap of smart pointer");
"\nruntime: memory allocation failed for heap of smart pointer");

*buffer.alloc = instance;
buffer.ref = ref;
Expand Down Expand Up @@ -262,8 +262,9 @@ namespace jule
jule::Ptr<T> &operator=(const jule::Ptr<T> &src) noexcept
{
// Assignment to itself.
if (this->alloc != nullptr && this->alloc == src.alloc)
if (this->alloc == src.alloc)
return *this;

this->dealloc();
this->__get_copy(src);
return *this;
Expand Down
10 changes: 9 additions & 1 deletion api/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace jule
Item *alloc = new (std::nothrow) Item[cap];
if (!alloc)
__jule_panic_s(__JULE_ERROR__MEMORY_ALLOCATION_FAILED
"\nruntime: heap allocation failed of slice");
"\nruntime: heap allocation failed of slice");

#ifdef __JULE_DISABLE__REFERENCE_COUNTING
this->data = jule::Ptr<Item>::make(alloc, nullptr);
Expand Down Expand Up @@ -438,6 +438,14 @@ namespace jule

jule::Slice<Item> &operator=(const jule::Slice<Item> &src) noexcept
{
// Assignment to itself.
if (this->data.alloc == src.data.alloc)
{
this->_len = src._len;
this->_cap = src._cap;
this->_slice = src._slice;
return *this;
}
this->dealloc();
this->__get_copy(src);
return *this;
Expand Down
19 changes: 14 additions & 5 deletions api/str.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace jule
auto buf = new (std::nothrow) jule::U8[len];
if (!buf)
__jule_panic_s(__JULE_ERROR__MEMORY_ALLOCATION_FAILED
"\nruntime: memory allocation failed for string");
"\nruntime: memory allocation failed for string");
std::memset(buf, 0, len);
return buf;
}
Expand Down Expand Up @@ -395,6 +395,14 @@ namespace jule

jule::Str &operator=(const jule::Str &str)
{
// Assignment to itself.
if (this->buffer.alloc == str.buffer.alloc)
{
this->_len = str._len;
this->_slice = str._slice;
return *this;
}
this->dealloc();
this->buffer = str.buffer;
this->_slice = str._slice;
this->_len = str._len;
Expand All @@ -403,6 +411,7 @@ namespace jule

jule::Str &operator=(jule::Str &&str)
{
this->dealloc();
this->buffer = std::move(str.buffer);
this->_slice = str._slice;
this->_len = str._len;
Expand All @@ -425,22 +434,22 @@ namespace jule

jule::Bool operator<(const jule::Str &str) const noexcept
{
return __jule_compareStr((jule::Str*)this, (jule::Str*)&str) == -1;
return __jule_compareStr((jule::Str *)this, (jule::Str *)&str) == -1;
}

inline jule::Bool operator<=(const jule::Str &str) const noexcept
{
return __jule_compareStr((jule::Str*)this, (jule::Str*)&str) <= 0;
return __jule_compareStr((jule::Str *)this, (jule::Str *)&str) <= 0;
}

jule::Bool operator>(const jule::Str &str) const noexcept
{
return __jule_compareStr((jule::Str*)this, (jule::Str*)&str) == +1;
return __jule_compareStr((jule::Str *)this, (jule::Str *)&str) == +1;
}

inline jule::Bool operator>=(const jule::Str &str) const noexcept
{
return __jule_compareStr((jule::Str*)this, (jule::Str*)&str) >= 0;
return __jule_compareStr((jule::Str *)this, (jule::Str *)&str) >= 0;
}
};
} // namespace jule
Expand Down
7 changes: 6 additions & 1 deletion api/trait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ namespace jule
template <typename T>
Trait(const jule::Ptr<T> &ref, jule::Trait::Type *type) noexcept
{
if (ref == nullptr) {
if (ref == nullptr)
{
// Pointer is nil, so can't able to use as base allocation.
// Make a nil trait.
return;
Expand Down Expand Up @@ -227,6 +228,10 @@ namespace jule

inline jule::Trait &operator=(const jule::Trait &src) noexcept
{
// Assignment to itself.
if (this->data.alloc == src.data.alloc)
return *this;

this->dealloc();
this->__get_copy(src);
return *this;
Expand Down

0 comments on commit 452130c

Please sign in to comment.