• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 namespace WTF {
30 
allocate(size_t size,size_t alignment,OSAllocator::Usage usage,bool writable,bool executable)31 PageAllocationAligned PageAllocationAligned::allocate(size_t size, size_t alignment, OSAllocator::Usage usage, bool writable, bool executable)
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     if (executable)
47         protection |= PROT_EXEC;
48 
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);
52 #else
53     size_t alignmentDelta = alignment - pageSize();
54 
55     // Resererve with suffcient additional VM to correctly align.
56     size_t reservationSize = size + alignmentDelta;
57     void* reservationBase = OSAllocator::reserveUncommitted(reservationSize, usage, writable, executable);
58 
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)
62         : reservationBase;
63     OSAllocator::commit(alignedBase, size, writable, executable);
64 
65     return PageAllocationAligned(alignedBase, size, reservationBase, reservationSize);
66 #endif
67 }
68 
deallocate()69 void PageAllocationAligned::deallocate()
70 {
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);
75 
76     ASSERT(tmp);
77     ASSERT(!*this);
78 
79 #if OS(DARWIN)
80     vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(tmp.base()), tmp.size());
81 #else
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());
84 #endif
85 }
86 
87 } // namespace WTF
88