Skip to content

Commit

Permalink
feat: add brstd::function_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 1, 2025
1 parent ae6a683 commit 9e9f235
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 670 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ jobs:
mode:
- debug
- release
use_mimalloc:
- true
- false
tests:
- true
- false
Expand Down Expand Up @@ -58,13 +55,13 @@ jobs:
xmake repo -u
- run: |
xmake f -a x64 -m ${{ matrix.mode }} -p windows -v -y --use_mimalloc=${{ matrix.use_mimalloc }} --target_type=${{ matrix.target_type }} --tests=${{ matrix.tests }}
xmake f -a x64 -m ${{ matrix.mode }} -p windows -v -y --target_type=${{ matrix.target_type }} --tests=${{ matrix.tests }}
- run: |
xmake -v -w -y
- uses: actions/upload-artifact@v4
with:
name: levilamina-${{ matrix.target_type }}-${{ matrix.mode }}${{ matrix.use_mimalloc == false && '-nonmimalloc' || '' }}${{ matrix.tests == true && '-tests' || '' }}-windows-x64-${{ github.sha }}
name: levilamina-${{ matrix.target_type }}-${{ matrix.mode }}${{ matrix.tests == true && '-tests' || '' }}-windows-x64-${{ github.sha }}
path: |
bin/
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
xmake repo -u
- run: |
xmake f -a x64 -m ${{ matrix.mode }} -p windows -v -y --use_mimalloc=y
xmake f -a x64 -m ${{ matrix.mode }} -p windows -v -y
- run: |
xmake -v -w -y
Expand Down
557 changes: 0 additions & 557 deletions src-test/server/Packet_test.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion src/ll/api/base/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ struct function_traits : function_traits<decltype(&F::operator())> {};
#define LL_BUILD_FUNCTION_SIGNATURE(CVREF, NOEXCEPT) \
template <class Ret, class... Args> \
struct function_traits<Ret(Args...) CVREF noexcept(NOEXCEPT)> { \
using function_type = Ret(Args...); \
using function_type = Ret(Args...); \
using function_type_noexcept = Ret(Args...) noexcept(NOEXCEPT); \
template <class T> \
using cvref = T CVREF; \
static constexpr bool is_noexcept = NOEXCEPT; \
Expand Down
17 changes: 9 additions & 8 deletions src/ll/api/memory/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ll/api/base/Macro.h"
#include "ll/api/memory/Signature.h"
#include "ll/api/memory/Symbol.h"
#include "mc/platform/brstd/function_ref.h"

namespace Bedrock::Memory {
class IMemoryAllocator;
Expand Down Expand Up @@ -61,21 +62,21 @@ inline void memcpy_t(void* dst, void const* src) {
* @param len Length of the region
* @param callback Callback
*/
LLAPI void modify(void* ptr, size_t len, const std::function<void()>& callback);
LLAPI void modify(void* ptr, size_t len, brstd::function_ref<void()> callback);

template <class T>
inline void modify(T& ref, std::function<void(std::remove_cv_t<T>&)> const& f) {
inline void modify(T& ref, brstd::function_ref<void(std::remove_cv_t<T>&)> f) {
modify((void*)std::addressof(ref), sizeof(T), [&] { f((std::remove_cv_t<T>&)(ref)); });
}

template <class RTN = void, class... Args>
constexpr auto addressCall(void const* address, auto&&... args) -> RTN {
return ((RTN(*)(Args...))address)(std::forward<decltype((args))>(args)...);
template <class R = void, class... Args>
constexpr auto addressCall(void const* address, auto&&... args) -> R {
return ((R(*)(Args...))address)(std::forward<decltype((args))>(args)...);
}

template <class RTN = void, class... Args>
constexpr auto virtualCall(void const* self, ptrdiff_t vIndex, auto&&... args) -> RTN {
return (*(RTN(**)(void const*, Args...))(*(uintptr_t**)self + vIndex))(
template <class R = void, class... Args>
constexpr auto virtualCall(void const* self, ptrdiff_t vIndex, auto&&... args) -> R {
return (*(R(**)(void const*, Args...))(*(uintptr_t**)self + vIndex))(
self,
std::forward<decltype((args))>(args)...
);
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/memory/linux/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static int readMemProtection(void* addr) {
}
return 0;
}
void modify(void* ptr, size_t len, std::function<void()> const& callback) {
void modify(void* ptr, size_t len, brstd::function_ref<void()> callback) {
std::optional<thread::GlobalThreadPauser> pauser;
if (getGamingStatus() != GamingStatus::Default) {
pauser.emplace();
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/memory/win/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace ll::memory {

void modify(void* ptr, size_t len, std::function<void()> const& callback) {
void modify(void* ptr, size_t len, brstd::function_ref<void()> callback) {
std::optional<thread::GlobalThreadPauser> pauser;
if (getGamingStatus() != GamingStatus::Default) {
pauser.emplace();
Expand Down
78 changes: 3 additions & 75 deletions src/ll/core/tweak/MemoryOperators_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@

#include "Psapi.h"

#ifdef LL_USE_MIMALLOC
#define LL_DEFALUT_MEMORY_ALLOCATOR MimallocMemoryAllocator
#else
#define LL_DEFALUT_MEMORY_ALLOCATOR StdMemoryAllocator
#endif

// #define LL_MEMORY_DEBUG

namespace ll::memory {

static std::string memStr(size_t mem) {
[[maybe_unused]] static std::string memStr(size_t mem) {
double r = (double)mem;
r /= 1024;
if (r < 1024) {
Expand Down Expand Up @@ -73,72 +67,6 @@ class MimallocMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
virtual void* _realloc(gsl::not_null<void*> ptr, uint64 newSize) { return mi_realloc(ptr, newSize); }
};

class StdMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
public:
virtual void* allocate(uint64 size) { return malloc(size != 0 ? size : 1); }

virtual void release(void* ptr) { free(ptr); }

virtual void* alignedAllocate(uint64 size, uint64 alignment) {
return _aligned_malloc(size != 0 ? size : 1, alignment);
}

virtual void alignedRelease(void* ptr) { _aligned_free(ptr); }

virtual uint64 getUsableSize(void* ptr) { return ptr ? _msize(ptr) : 0ull; }

virtual void logCurrentState() {
HEAP_SUMMARY summary{.cb = sizeof(HEAP_SUMMARY)};
HeapSummary(GetProcessHeap(), 0, &summary);
PROCESS_MEMORY_COUNTERS_EX info{.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX)};
GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&info, info.cb);
// clang-format off
auto& logger= getLogger();
logger.info("heap stats: {:>12} {:>12}", "peak", "current");
logger.info(" reserved: {:>12} {:>12}", memStr(summary.cbMaxReserve), memStr(summary.cbReserved));
logger.info(" committed: {:>12} {:>12}", "", memStr(summary.cbCommitted));
logger.info(" allocated: {:>12} {:>12}", "", memStr(summary.cbAllocated));
logger.info(" pagefault: {:>12} {:>8}", "", info.PageFaultCount);
logger.info("workingset: {:>12} {:>12}", memStr(info.PeakWorkingSetSize), memStr(info.WorkingSetSize));
logger.info(" pagedpool: {:>12} {:>12}", memStr(info.QuotaPeakPagedPoolUsage), memStr(info.QuotaPagedPoolUsage));
logger.info(" nonpaged: {:>12} {:>12}", memStr(info.QuotaPeakNonPagedPoolUsage), memStr(info.QuotaNonPagedPoolUsage));
logger.info(" pagefile: {:>12} {:>12}", memStr(info.PeakPagefileUsage), memStr(info.PagefileUsage));
logger.info(" private: {:>12} {:>12}", "", memStr(info.PrivateUsage));
// clang-format on
}

virtual void* _realloc(gsl::not_null<void*> ptr, uint64 newSize) { return realloc(ptr, newSize); }
};

class MimallocMemoryAllocatorWithCheck : public MimallocMemoryAllocator {
public:
virtual void release(void* ptr) {
if (mi_is_in_heap_region(ptr)) [[likely]] {
mi_free(ptr);
} else if (ptr) [[unlikely]] {
free(ptr);
}
}

virtual void alignedRelease(void* ptr) { release(ptr); }

virtual uint64 getUsableSize(void* ptr) {
if (mi_is_in_heap_region(ptr)) [[likely]] {
return mi_usable_size(ptr);
} else {
return ptr ? _msize(ptr) : 0ull;
}
}

virtual void* _realloc(gsl::not_null<void*> ptr, uint64 newSize) {
if (mi_is_in_heap_region(ptr)) [[likely]] {
return mi_realloc(ptr, newSize);
} else {
return realloc(ptr, newSize);
}
}
};

#ifdef LL_MEMORY_DEBUG
struct MemSize {
size_t a{0};
Expand Down Expand Up @@ -229,9 +157,9 @@ class DebugAllocator : public T {

::Bedrock::Memory::IMemoryAllocator& getDefaultAllocator() {
#ifdef LL_MEMORY_DEBUG
static DebugAllocator<LL_DEFALUT_MEMORY_ALLOCATOR> ins;
static DebugAllocator<MimallocMemoryAllocator> ins;
#else
static LL_DEFALUT_MEMORY_ALLOCATOR ins;
static MimallocMemoryAllocator ins;
#endif
return ins;
}
Expand Down
6 changes: 1 addition & 5 deletions src/mc/_HeaderOutputPredefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct sockaddr_in;
struct sockaddr_in6;

#include "mc/deps/core/utility/optional_ref.h" // replace optional<reference_wrapper<>>
#include "mc/platform/brstd/function_ref.h" // function reference
#include "mc/math/vector/Vecs.h" // for vector types

template <typename T0, typename T1>
Expand Down Expand Up @@ -222,11 +223,6 @@ class ThreadSafe {
};
}

namespace brstd {
template <typename T, typename U = T>
class function_ref;
}

namespace rtc {
template<class T, size_t N = size_t(-4711ll)>
class ArrayView;
Expand Down
Loading

0 comments on commit 9e9f235

Please sign in to comment.