#include <Windows.h>
#include <new>
class HeapAllocator {
static HANDLE Hh;
static bool Initinit;
public:
static void *alloc(std::size_t size) noexcept(false) {
if (!Initinit) {
Hh = ::HeapCreate(0, 0, 0); // Private, expandable heap
Initinit = true;
}
if (Hh) {
return ::HeapAlloc(Hh, 0, size);
}
throw std::bad_alloc();
}
static void dealloc(void *ptr) noexcept {
if (Hh) {
(void)::HeapFree(Hh, 0, ptr);
}
}
};
HANDLE HeapAllocator::Hh = nullptr;
bool HeapAllocator::Initinit = false;
void *operator new(std::size_t size) noexcept(false) {
return HeapAllocator::alloc(size);
}
void operator delete(void *ptr) noexcept {
return HeapAllocator::dealloc(ptr);
} |