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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Assertions.h> |
29 | #include <wtf/OSAllocator.h> |
30 | #include <wtf/PageBlock.h> |
31 | #include <algorithm> |
32 | |
33 | #if OS(DARWIN) |
34 | #include <mach/mach_init.h> |
35 | #include <mach/vm_map.h> |
36 | #endif |
37 | |
38 | #if OS(WINDOWS) |
39 | #include <malloc.h> |
40 | #include <windows.h> |
41 | #endif |
42 | |
43 | #if HAVE(ERRNO_H) |
44 | #include <errno.h> |
45 | #endif |
46 | |
47 | #if HAVE(MMAP) |
48 | #include <sys/mman.h> |
49 | #include <unistd.h> |
50 | #endif |
51 | |
52 | namespace WTF { |
53 | |
54 | /* |
55 | PageAllocation |
56 | |
57 | The PageAllocation class provides a cross-platform memory allocation interface |
58 | with similar capabilities to posix mmap/munmap. Memory is allocated by calling |
59 | PageAllocation::allocate, and deallocated by calling deallocate on the |
60 | PageAllocation object. The PageAllocation holds the allocation's base pointer |
61 | and size. |
62 | |
63 | The allocate method is passed the size required (which must be a multiple of |
64 | the system page size, which can be accessed using PageAllocation::pageSize). |
65 | Callers may also optinally provide a flag indicating the usage (for use by |
66 | system memory usage tracking tools, where implemented), and boolean values |
67 | specifying the required protection (defaulting to writable, non-executable). |
68 | */ |
69 | |
70 | class PageAllocation : private PageBlock { |
71 | public: |
72 | PageAllocation() |
73 | { |
74 | } |
75 | |
76 | using PageBlock::size; |
77 | using PageBlock::base; |
78 | |
79 | #ifndef __clang__ |
80 | using PageBlock::operator bool; |
81 | #else |
82 | // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access |
83 | // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool". |
84 | operator bool() const { return PageBlock::operator bool(); } |
85 | #endif |
86 | |
87 | static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) |
88 | { |
89 | ASSERT(isPageAligned(size)); |
90 | return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size); |
91 | } |
92 | |
93 | void deallocate() |
94 | { |
95 | // Clear base & size before calling release; if this is *inside* allocation |
96 | // then we won't be able to clear then after deallocating the memory. |
97 | PageAllocation tmp; |
98 | std::swap(tmp, *this); |
99 | |
100 | ASSERT(tmp); |
101 | ASSERT(!*this); |
102 | |
103 | OSAllocator::decommitAndRelease(tmp.base(), tmp.size()); |
104 | } |
105 | |
106 | private: |
107 | PageAllocation(void* base, size_t size) |
108 | : PageBlock(base, size, false) |
109 | { |
110 | } |
111 | }; |
112 | |
113 | } // namespace WTF |
114 | |
115 | using WTF::PageAllocation; |
116 | |