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#ifndef PageAllocation_h
27#define PageAllocation_h
28
29#include <wtf/Assertions.h>
30#include <wtf/OSAllocator.h>
31#include <wtf/PageBlock.h>
32#include <wtf/VMTags.h>
33#include <algorithm>
34
35#if OS(DARWIN)
36#include <mach/mach_init.h>
37#include <mach/vm_map.h>
38#endif
39
40#if OS(WINDOWS)
41#include <malloc.h>
42#include <windows.h>
43#endif
44
45#if HAVE(ERRNO_H)
46#include <errno.h>
47#endif
48
49#if HAVE(MMAP)
50#include <sys/mman.h>
51#include <unistd.h>
52#endif
53
54namespace WTF {
55
56/*
57 PageAllocation
58
59 The PageAllocation class provides a cross-platform memory allocation interface
60 with similar capabilities to posix mmap/munmap. Memory is allocated by calling
61 PageAllocation::allocate, and deallocated by calling deallocate on the
62 PageAllocation object. The PageAllocation holds the allocation's base pointer
63 and size.
64
65 The allocate method is passed the size required (which must be a multiple of
66 the system page size, which can be accessed using PageAllocation::pageSize).
67 Callers may also optinally provide a flag indicating the usage (for use by
68 system memory usage tracking tools, where implemented), and boolean values
69 specifying the required protection (defaulting to writable, non-executable).
70*/
71
72class PageAllocation : private PageBlock {
73public:
74 PageAllocation()
75 {
76 }
77
78 using PageBlock::size;
79 using PageBlock::base;
80
81#ifndef __clang__
82 using PageBlock::operator bool;
83#else
84 // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access
85 // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool".
86 operator bool() const { return PageBlock::operator bool(); }
87#endif
88
89 static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false)
90 {
91 ASSERT(isPageAligned(size));
92 return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size);
93 }
94
95 void deallocate()
96 {
97 // Clear base & size before calling release; if this is *inside* allocation
98 // then we won't be able to clear then after deallocating the memory.
99 PageAllocation tmp;
100 std::swap(tmp, *this);
101
102 ASSERT(tmp);
103 ASSERT(!*this);
104
105 OSAllocator::decommitAndRelease(tmp.base(), tmp.size());
106 }
107
108private:
109 PageAllocation(void* base, size_t size)
110 : PageBlock(base, size, false)
111 {
112 }
113};
114
115} // namespace WTF
116
117using WTF::PageAllocation;
118
119#endif // PageAllocation_h
120