Skip to content

Instantly share code, notes, and snippets.

Created February 17, 2013 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4971308 to your computer and use it in GitHub Desktop.
Save anonymous/4971308 to your computer and use it in GitHub Desktop.
#pragma once
#include <cstddef>
#include <limits>
#include <memory>
#include <utility>
#include <Windows.h>
namespace woof {
template<class T>
class executable_memory_allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
pointer address(reference x) const /* noexcept */ {
return std:addressof(x);
}
const_pointer address(const_reference x) const /* noexcept */ {
return std::addressof(x);
}
pointer allocate(size_type n, const_pointer hint = nullptr) {
void* ptr = VirtualAlloc(hint, n, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (ptr == nullptr) {
throw std::bad_alloc{};
}
return ptr;
}
void deallocate(pointer p, size_type) {
VirtualFree(p, 0, MEM_RELEASE);
}
size_type max_size() const /* noexcept */ {
return std::numeric_limits<size_type>::max();
}
#if 0 // Fuck you Microsoft
template<class U, class... Args>
void construct(U* p, Args&&... args) {
return new (p) U(std::forward<Args>(args)...);
}
#endif
template<class U>
void construct(U* p) {
return new (p) U;
}
template<class U, class A1>
void construct(U* p, A1&& a1) {
return new (p) U(std::forward<A1>(a1));
}
template<class U, class A1, class A2>
void construct(U* p, A1&& a1, A2&& a2) {
return new (p) U(std::forward<A1>(a1), std::forward<A2>(a2));
}
template<class U, class A1, class A2, class A3>
void construct(U* p, A1&& a1, A2&& a2, A3&& a3) {
return new (p) U(std::forward<A1>(a1), std::forward<A2>(a2), std::forward<A3>(a3));
}
template<class U>
void destroy(U* p) {
p->~U();
}
};
template< class T1, class T2 >
bool operator==(executable_memory_allocator<T1> const&, executable_memory_allocator<T2> const&) {
return true;
}
template< class T1, class T2 >
bool operator!=(executable_memory_allocator<T1> const&, executable_memory_allocator<T2> const&) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment