1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "PageAllocationAligned.h" 28 29namespace WTF { 30 31PageAllocationAligned PageAllocationAligned::allocate(size_t size, size_t alignment, OSAllocator::Usage usage, bool writable) 32{ 33 ASSERT(isPageAligned(size)); 34 ASSERT(isPageAligned(alignment)); 35 ASSERT(isPowerOfTwo(alignment)); 36 ASSERT(size >= alignment); 37 size_t alignmentMask = alignment - 1; 38 39#if OS(DARWIN) 40 int flags = VM_FLAGS_ANYWHERE; 41 if (usage != OSAllocator::UnknownUsage) 42 flags |= usage; 43 int protection = PROT_READ; 44 if (writable) 45 protection |= PROT_WRITE; 46 47 vm_address_t address = 0; 48 vm_map(current_task(), &address, size, alignmentMask, flags, MEMORY_OBJECT_NULL, 0, FALSE, protection, PROT_READ | PROT_WRITE, VM_INHERIT_DEFAULT); 49 return PageAllocationAligned(reinterpret_cast<void*>(address), size); 50#else 51 size_t alignmentDelta = alignment - pageSize(); 52 53 // Resererve with suffcient additional VM to correctly align. 54 size_t reservationSize = size + alignmentDelta; 55 void* reservationBase = OSAllocator::reserveUncommitted(reservationSize, usage, writable, false); 56 57 // Select an aligned region within the reservation and commit. 58 void* alignedBase = reinterpret_cast<uintptr_t>(reservationBase) & alignmentMask 59 ? reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(reservationBase) & ~alignmentMask) + alignment) 60 : reservationBase; 61 OSAllocator::commit(alignedBase, size, writable, false); 62 63 return PageAllocationAligned(alignedBase, size, reservationBase, reservationSize); 64#endif 65} 66 67void PageAllocationAligned::deallocate() 68{ 69 // Clear base & size before calling release; if this is *inside* allocation 70 // then we won't be able to clear then after deallocating the memory. 71 PageAllocationAligned tmp; 72 std::swap(tmp, *this); 73 74 ASSERT(tmp); 75 ASSERT(!*this); 76 77#if OS(DARWIN) 78 vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(tmp.base()), tmp.size()); 79#else 80 ASSERT(tmp.m_reservation.contains(tmp.base(), tmp.size())); 81 OSAllocator::decommitAndRelease(tmp.m_reservation.base(), tmp.m_reservation.size(), tmp.base(), tmp.size()); 82#endif 83} 84 85} // namespace WTF 86