Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed callbacks #81

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions include/RED4ext/Callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class FlexCallback<R (*)(Args...), InlineSize>
static_assert(InlineSize >= sizeof(void*), "Buffer size can't be less than pointer size");

FlexCallback(R (*aFunc)(Args...))
: allocator(nullptr)
: allocator(0)
, extendedSize(0)
{
using TargetType = Detail::UnboundFunctionTarget<R, Args...>;
Expand All @@ -233,7 +233,7 @@ class FlexCallback<R (*)(Args...), InlineSize>

template<typename C>
FlexCallback(C* aContext, R (C::*aFunc)(Args...))
: allocator(nullptr)
: allocator(0)
, extendedSize(0)
{
using TargetType = Detail::MemberFunctionTarget<C, R, Args...>;
Expand All @@ -246,7 +246,7 @@ class FlexCallback<R (*)(Args...), InlineSize>
template<typename L>
requires Detail::IsClosure<L, R, Args...>
FlexCallback(L&& aClosure)
: allocator(nullptr)
: allocator(0)
, extendedSize(0)
{
using TargetType = Detail::ClosureTarget<L, R, Args...>;
Expand Down Expand Up @@ -311,7 +311,7 @@ class FlexCallback<R (*)(Args...), InlineSize>

uint8_t buffer[InlineSize];
HandlerPtr handler;
Memory::IAllocator* allocator;
uint64_t allocator; // This is not a pointer to allocator, this is allocator instance that takes 8 bytes
uint32_t extendedSize;

protected:
Expand Down Expand Up @@ -344,17 +344,22 @@ class FlexCallback<R (*)(Args...), InlineSize>
handler = nullptr;
}

inline Memory::IAllocator* GetAllocator()
{
return reinterpret_cast<Memory::IAllocator*>(&allocator);
}

void InitializeBuffer(uint32_t aSize)
{
if (aSize > InlineSize)
{
if (!allocator)
{
allocator = AllocatorType::Get();
std::memcpy(&allocator, AllocatorType::Get(), sizeof(uint64_t));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::memcpy(&allocator, AllocatorType::Get(), sizeof(uint64_t));
std::memcpy(&allocator, AllocatorType::Get(), sizeof(Memory::IAllocator));

This is the vtable for allocator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use uint64_t because allocator field is also uint64_t.
I guess we could use sizeof(Memory::IAllocator) to also define allocator.

}

auto bufferPtr = reinterpret_cast<void**>(buffer);
*bufferPtr = allocator->Alloc(aSize).memory;
*bufferPtr = reinterpret_cast<Memory::IAllocator*>(&allocator)->Alloc(aSize).memory;

extendedSize = aSize;
extendedSize |= ExtendedFlag;
Expand All @@ -365,7 +370,7 @@ class FlexCallback<R (*)(Args...), InlineSize>
}
}

void InitializeBuffer(uint32_t aSize, Memory::IAllocator* aAllocator)
void InitializeBuffer(uint32_t aSize, uint64_t aAllocator)
{
allocator = aAllocator;

Expand Down Expand Up @@ -406,7 +411,7 @@ class FlexCallback<R (*)(Args...), InlineSize>
{
if (IsExtendedMode())
{
allocator->Free(GetBuffer());
reinterpret_cast<Memory::IAllocator*>(&allocator)->Free(GetBuffer());
extendedSize = 0;
}
}
Expand Down
10 changes: 10 additions & 0 deletions include/RED4ext/ResourceReference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ struct ResourceAsyncReference
return *this;
}

bool operator==(const ResourceAsyncReference& aRhs) const noexcept
{
return path == aRhs.path;
}

bool operator!=(const ResourceAsyncReference& aRhs) const noexcept
{
return !(*this == aRhs);
}

[[nodiscard]] ResourceReference<T> Resolve() const noexcept
{
return {path};
Expand Down