PageAllocationAligned.cpp [plain text]
#include "config.h"
#include "PageAllocationAligned.h"
namespace WTF {
PageAllocationAligned PageAllocationAligned::allocate(size_t size, size_t alignment, OSAllocator::Usage usage, bool writable)
{
ASSERT(isPageAligned(size));
ASSERT(isPageAligned(alignment));
ASSERT(isPowerOfTwo(alignment));
ASSERT(size >= alignment);
size_t alignmentMask = alignment - 1;
#if OS(DARWIN)
int flags = VM_FLAGS_ANYWHERE;
if (usage != OSAllocator::UnknownUsage)
flags |= usage;
int protection = PROT_READ;
if (writable)
protection |= PROT_WRITE;
vm_address_t address = 0;
vm_map(current_task(), &address, size, alignmentMask, flags, MEMORY_OBJECT_NULL, 0, FALSE, protection, PROT_READ | PROT_WRITE, VM_INHERIT_DEFAULT);
return PageAllocationAligned(reinterpret_cast<void*>(address), size);
#else
size_t alignmentDelta = alignment - pageSize();
size_t reservationSize = size + alignmentDelta;
void* reservationBase = OSAllocator::reserveUncommitted(reservationSize, usage, writable, false);
void* alignedBase = reinterpret_cast<uintptr_t>(reservationBase) & alignmentMask
? reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(reservationBase) & ~alignmentMask) + alignment)
: reservationBase;
OSAllocator::commit(alignedBase, size, writable, false);
return PageAllocationAligned(alignedBase, size, reservationBase, reservationSize);
#endif
}
void PageAllocationAligned::deallocate()
{
PageAllocationAligned tmp;
std::swap(tmp, *this);
ASSERT(tmp);
ASSERT(!*this);
#if OS(DARWIN)
vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(tmp.base()), tmp.size());
#else
ASSERT(tmp.m_reservation.contains(tmp.base(), tmp.size()));
OSAllocator::decommitAndRelease(tmp.m_reservation.base(), tmp.m_reservation.size(), tmp.base(), tmp.size());
#endif
}
}