2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "PageAllocationAligned.h"
31 PageAllocationAligned PageAllocationAligned::allocate(size_t size, size_t alignment, OSAllocator::Usage usage, bool writable, bool executable)
33 ASSERT(isPageAligned(size));
34 ASSERT(isPageAligned(alignment));
35 ASSERT(isPowerOfTwo(alignment));
36 ASSERT(size >= alignment);
37 size_t alignmentMask = alignment - 1;
40 int flags = VM_FLAGS_ANYWHERE;
41 if (usage != OSAllocator::UnknownUsage)
43 int protection = PROT_READ;
45 protection |= PROT_WRITE;
47 protection |= PROT_EXEC;
49 vm_address_t address = 0;
50 vm_map(current_task(), &address, size, alignmentMask, flags, MEMORY_OBJECT_NULL, 0, FALSE, protection, PROT_READ | PROT_WRITE | PROT_EXEC, VM_INHERIT_DEFAULT);
51 return PageAllocationAligned(reinterpret_cast<void*>(address), size);
53 size_t alignmentDelta = alignment - pageSize();
55 // Resererve with suffcient additional VM to correctly align.
56 size_t reservationSize = size + alignmentDelta;
57 void* reservationBase = OSAllocator::reserveUncommitted(reservationSize, usage, writable, executable);
59 // Select an aligned region within the reservation and commit.
60 void* alignedBase = reinterpret_cast<uintptr_t>(reservationBase) & alignmentMask
61 ? reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(reservationBase) & ~alignmentMask) + alignment)
63 OSAllocator::commit(alignedBase, size, writable, executable);
65 return PageAllocationAligned(alignedBase, size, reservationBase, reservationSize);
69 void PageAllocationAligned::deallocate()
71 // Clear base & size before calling release; if this is *inside* allocation
72 // then we won't be able to clear then after deallocating the memory.
73 PageAllocationAligned tmp;
74 std::swap(tmp, *this);
80 vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(tmp.base()), tmp.size());
82 ASSERT(tmp.m_reservation.contains(tmp.base(), tmp.size()));
83 OSAllocator::decommitAndRelease(tmp.m_reservation.base(), tmp.m_reservation.size(), tmp.base(), tmp.size());