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

source code of qtdeclarative/src/3rdparty/masm/wtf/PageAllocation.h