]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/PageAllocation.h
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. ``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.
26 #ifndef PageAllocation_h
27 #define PageAllocation_h
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>
37 #include <mach/mach_init.h>
38 #include <mach/vm_map.h>
69 The PageAllocation class provides a cross-platform memory allocation interface
70 with similar capabilities to posix mmap/munmap. Memory is allocated by calling
71 PageAllocation::allocate, and deallocated by calling deallocate on the
72 PageAllocation object. The PageAllocation holds the allocation's base pointer
75 The allocate method is passed the size required (which must be a multiple of
76 the system page size, which can be accessed using PageAllocation::pageSize).
77 Callers may also optinally provide a flag indicating the usage (for use by
78 system memory usage tracking tools, where implemented), and boolean values
79 specifying the required protection (defaulting to writable, non-executable).
82 class PageAllocation
: private PageBlock
{
88 using PageBlock::size
;
89 using PageBlock::base
;
92 using PageBlock::operator bool;
94 // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access
95 // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool".
96 operator bool() const { return PageBlock::operator bool(); }
99 static PageAllocation
allocate(size_t size
, OSAllocator::Usage usage
= OSAllocator::UnknownUsage
, bool writable
= true, bool executable
= false)
101 ASSERT(isPageAligned(size
));
102 return PageAllocation(OSAllocator::reserveAndCommit(size
, usage
, writable
, executable
), size
);
107 // Clear base & size before calling release; if this is *inside* allocation
108 // then we won't be able to clear then after deallocating the memory.
110 std::swap(tmp
, *this);
115 OSAllocator::decommitAndRelease(tmp
.base(), tmp
.size());
119 PageAllocation(void* base
, size_t size
)
120 : PageBlock(base
, size
, false)
127 using WTF::PageAllocation
;
129 #endif // PageAllocation_h