michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * ***** BEGIN LICENSE BLOCK ***** michael@0: * Copyright (C) 2010 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: #ifndef yarr_PageAllocation_h michael@0: #define yarr_PageAllocation_h michael@0: michael@0: #include "yarr/wtfbridge.h" michael@0: #include "yarr/OSAllocator.h" michael@0: #include "yarr/PageBlock.h" michael@0: #include "assembler/wtf/VMTags.h" michael@0: michael@0: #if WTF_OS_DARWIN michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if WTF_OS_HAIKU michael@0: #include michael@0: #endif michael@0: michael@0: #if WTF_OS_WINDOWS michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if WTF_OS_SYMBIAN michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if WTF_HAVE_ERRNO_H michael@0: #include michael@0: #endif michael@0: michael@0: #if WTF_HAVE_MMAP michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: namespace WTF { michael@0: michael@0: /* michael@0: PageAllocation michael@0: michael@0: The PageAllocation class provides a cross-platform memory allocation interface michael@0: with similar capabilities to posix mmap/munmap. Memory is allocated by calling michael@0: PageAllocation::allocate, and deallocated by calling deallocate on the michael@0: PageAllocation object. The PageAllocation holds the allocation's base pointer michael@0: and size. michael@0: michael@0: The allocate method is passed the size required (which must be a multiple of michael@0: the system page size, which can be accessed using PageAllocation::pageSize). michael@0: Callers may also optinally provide a flag indicating the usage (for use by michael@0: system memory usage tracking tools, where implemented), and boolean values michael@0: specifying the required protection (defaulting to writable, non-executable). michael@0: */ michael@0: michael@0: class PageAllocation : private PageBlock { michael@0: public: michael@0: PageAllocation() michael@0: { michael@0: } michael@0: michael@0: using PageBlock::size; michael@0: using PageBlock::base; michael@0: michael@0: #ifndef __clang__ michael@0: using PageBlock::operator bool; michael@0: #else michael@0: // FIXME: This is a workaround for , wherein Clang incorrectly emits an access michael@0: // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool". michael@0: operator bool() const { return PageBlock::operator bool(); } michael@0: #endif michael@0: michael@0: static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) michael@0: { michael@0: ASSERT(isPageAligned(size)); michael@0: return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size); michael@0: } michael@0: michael@0: void deallocate() michael@0: { michael@0: // Clear base & size before calling release; if this is *inside* allocation michael@0: // then we won't be able to clear then after deallocating the memory. michael@0: PageAllocation tmp; michael@0: JSC::std::swap(tmp, *this); michael@0: michael@0: ASSERT(tmp); michael@0: ASSERT(!*this); michael@0: michael@0: OSAllocator::decommitAndRelease(tmp.base(), tmp.size()); michael@0: } michael@0: michael@0: private: michael@0: PageAllocation(void* base, size_t size) michael@0: : PageBlock(base, size) michael@0: { michael@0: } michael@0: }; michael@0: michael@0: } // namespace WTF michael@0: michael@0: using WTF::PageAllocation; michael@0: michael@0: #endif /* yarr_PageAllocation_h */