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. AND ITS CONTRIBUTORS ``AS IS'' michael@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS michael@0: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: #ifndef yarr_PageBlock_h michael@0: #define yarr_PageBlock_h michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace WTF { michael@0: michael@0: size_t pageSize(); michael@0: inline bool isPageAligned(void* address) { return !(reinterpret_cast(address) & (pageSize() - 1)); } michael@0: inline bool isPageAligned(size_t size) { return !(size & (pageSize() - 1)); } michael@0: inline bool isPowerOfTwo(size_t size) { return !(size & (size - 1)); } michael@0: michael@0: class PageBlock { michael@0: public: michael@0: PageBlock(); michael@0: PageBlock(const PageBlock&); michael@0: PageBlock(void*, size_t); michael@0: michael@0: void* base() const { return m_base; } michael@0: size_t size() const { return m_size; } michael@0: michael@0: operator bool() const { return !!m_base; } michael@0: michael@0: bool contains(void* containedBase, size_t containedSize) michael@0: { michael@0: return containedBase >= m_base michael@0: && (static_cast(containedBase) + containedSize) <= (static_cast(m_base) + m_size); michael@0: } michael@0: michael@0: private: michael@0: void* m_base; michael@0: size_t m_size; michael@0: }; michael@0: michael@0: inline PageBlock::PageBlock() michael@0: : m_base(0) michael@0: , m_size(0) michael@0: { michael@0: } michael@0: michael@0: inline PageBlock::PageBlock(const PageBlock& other) michael@0: : m_base(other.m_base) michael@0: , m_size(other.m_size) michael@0: { michael@0: } michael@0: michael@0: inline PageBlock::PageBlock(void* base, size_t size) michael@0: : m_base(base) michael@0: , m_size(size) michael@0: { michael@0: } michael@0: michael@0: } // namespace WTF michael@0: michael@0: using WTF::pageSize; michael@0: using WTF::isPageAligned; michael@0: using WTF::isPageAligned; michael@0: using WTF::isPowerOfTwo; michael@0: michael@0: #endif /* yarr_PageBlock_h */