Create AllocRoutines.h

This commit is contained in:
John Platts
2022-09-19 13:26:33 -05:00
committed by GitHub
parent b43278439a
commit 01125e030e

View File

@@ -0,0 +1,44 @@
#ifndef FLATLAF_WIN32_ALLOCROUTINES_H
#define FLATLAF_WIN32_ALLOCROUTINES_H
#include <stddef.h>
#include <windows.h>
struct NoThrowingWin32HeapAllocT {
};
constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{};
#if defined(_MSC_VER)
#define FLATLAF_WIN32_ALLOC_INLINE __forceinline
#elif (defined(__GNUC__) || defined(__clang__))
#define FLATLAF_WIN32_ALLOC_INLINE inline __attribute__((__always_inline__))
#else
#define FLATLAF_WIN32_ALLOC_INLINE inline
#endif
FLATLAF_WIN32_ALLOC_INLINE void* AllocateUsingProcessHeap(size_t cb) noexcept {
return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb);
}
FLATLAF_WIN32_ALLOC_INLINE void DeleteFromProcessHeap(void* pv) noexcept {
if(pv)
::HeapFree(::GetProcessHeap(), 0, pv);
}
FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept {
return AllocateUsingProcessHeap(cb);
}
inline void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept {
return AllocateUsingProcessHeap(cb);
}
inline void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept {
DeleteFromProcessHeap(pv);
}
inline void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept {
DeleteFromProcessHeap(pv);
}
#endif