Abstracting aligned alloc/free.
This commit is contained in:
parent
a79ef87889
commit
ab04175aad
|
@ -63,6 +63,31 @@ bool DeallocFixed(void* base_address, size_t length,
|
||||||
bool Protect(void* base_address, size_t length, PageAccess access,
|
bool Protect(void* base_address, size_t length, PageAccess access,
|
||||||
PageAccess* out_old_access);
|
PageAccess* out_old_access);
|
||||||
|
|
||||||
|
// Allocates a block of memory for a type with the given alignment.
|
||||||
|
// The memory must be freed with AlignedFree.
|
||||||
|
template <typename T>
|
||||||
|
inline T* AlignedAlloc(size_t alignment) {
|
||||||
|
#if __STDC_VERSION__ >= 201112L
|
||||||
|
return reinterpret_cast<T*>(aligned_alloc(alignment, sizeof(T)));
|
||||||
|
#elif XE_COMPILER_MSVC
|
||||||
|
return reinterpret_cast<T*>(_aligned_malloc(sizeof(T), alignment));
|
||||||
|
#else
|
||||||
|
#error No aligned alloc.
|
||||||
|
#endif // __STDC_VERSION__ >= 201112L
|
||||||
|
}
|
||||||
|
|
||||||
|
// Frees memory previously allocated with AlignedAlloc.
|
||||||
|
template <typename T>
|
||||||
|
void AlignedFree(T* ptr) {
|
||||||
|
#if __STDC_VERSION__ >= 201112L
|
||||||
|
free(ptr);
|
||||||
|
#elif XE_COMPILER_MSVC
|
||||||
|
_aligned_free(ptr);
|
||||||
|
#else
|
||||||
|
#error No aligned alloc.
|
||||||
|
#endif // __STDC_VERSION__ >= 201112L
|
||||||
|
}
|
||||||
|
|
||||||
typedef void* FileMappingHandle;
|
typedef void* FileMappingHandle;
|
||||||
|
|
||||||
FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length,
|
FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length,
|
||||||
|
|
|
@ -84,15 +84,8 @@ ThreadState::ThreadState(Processor* processor, uint32_t thread_id,
|
||||||
}
|
}
|
||||||
assert_not_zero(stack_address_);
|
assert_not_zero(stack_address_);
|
||||||
|
|
||||||
// Allocate with 64b alignment.
|
// Allocate with 64b alignment.
|
||||||
#if __STDC_VERSION__ >= 201112L
|
context_ = memory::AlignedAlloc<PPCContext>(64);
|
||||||
void* context_ptr = aligned_alloc(64, sizeof(PPCContext));
|
|
||||||
#elif XE_COMPILER_MSVC
|
|
||||||
void* context_ptr = _aligned_malloc(sizeof(PPCContext), 64);
|
|
||||||
#else
|
|
||||||
#error No aligned alloc.
|
|
||||||
#endif
|
|
||||||
context_ = reinterpret_cast<PPCContext*>(context_ptr);
|
|
||||||
assert_true(((uint64_t)context_ & 0x3F) == 0);
|
assert_true(((uint64_t)context_ & 0x3F) == 0);
|
||||||
std::memset(context_, 0, sizeof(PPCContext));
|
std::memset(context_, 0, sizeof(PPCContext));
|
||||||
|
|
||||||
|
@ -117,13 +110,7 @@ ThreadState::~ThreadState() {
|
||||||
thread_state_ = nullptr;
|
thread_state_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if __STDC_VERSION__ >= 201112L
|
memory::AlignedFree(context_);
|
||||||
free(context_);
|
|
||||||
#elif XE_COMPILER_MSVC
|
|
||||||
_aligned_free(context_);
|
|
||||||
#else
|
|
||||||
#error No aligned alloc.
|
|
||||||
#endif
|
|
||||||
if (stack_allocated_) {
|
if (stack_allocated_) {
|
||||||
memory()->LookupHeap(stack_address_)->Decommit(stack_address_, stack_size_);
|
memory()->LookupHeap(stack_address_)->Decommit(stack_address_, stack_size_);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue